home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hall of Fame
/
HallofFameCDROM.cdr
/
proglc
/
zoo141_c.lzh
/
ZOOADD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1987-02-07
|
22KB
|
588 lines
/* zooadd.c */
/*
Copyright (C) 1986 Rahul Dhesi -- All rights reserved
*/
#include "options.h"
/* Adds files specified in parameter-list to archive zoo_path. */
#include "zoomem.h" /* to define MAXADD */
#include "zoo.h"
#include <stdio.h>
#include "various.h"
#include "parse.h"
#include "debug.h"
#include "portable.h"
/* for low-level I/O */
#ifdef NOFCNTL
#include <file.h>
#else
#include <fcntl.h>
#endif
#ifdef FLAT
#include <types.h>
#include <stat.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include "zoofns.h"
#include "errors.i"
extern int break_hit;
extern int quiet;
#ifdef LINT_ARGS
void opts_add (char *, int *, int *, int *, int *, int *, int *,
int *, int *, int *, int *);
int ver_too_high (struct zoo_header *);
get_comment (struct direntry *, FILE *, char *);
void copyfields (struct direntry *, struct tiny_header *);
void storefname (struct direntry *, char *, int);
char *choosefname (struct direntry *);
#else
void opts_add();
int ver_too_high ();
get_comment ();
void copyfields ();
void storefname ();
char *choosefname ();
#endif
extern struct zoo_header zoo_header;
extern char file_leader[];
extern unsigned int crccode;
void zooadd(zoo_path, argc, argv, option)
char *zoo_path; /* pathname of zoo archive to add to */
int argc; /* how many filespecs supplied */
char **argv; /* array of pointers to filespecs */
char *option; /* option string */
{
char *whichname; /* which name to show user */
char *flist[MAXADD]; /* list of ptrs to input fnames */
int fptr; /* will point to within flist */
int zoo_han; /* handle for open archive */
int this_han; /* handle of file to add */
char zoo_fname[LFNAMESIZE]; /* basename of archive itself */
char zoo_bak[LFNAMESIZE]; /* name of archive's backup */
char this_fname[LFNAMESIZE]; /* just filename of file to add */
char latest_name[LFNAMESIZE]; /* latest name in archive */
long last_old = 0L; /* last direntry in old chain */
FILE *zoo_file; /* stream for open archive */
char *this_path; /* pathname of file to add */
#ifdef NOENUM
#define NEW_ZOO 1
#define OLD_ZOO 2
int zoo_status;
#else
enum {NEW_ZOO, OLD_ZOO} zoo_status; /* newly created or not */
#endif
long this_dir_offset; /* pointers to within archive */
long save_position; /* pointer to within archive */
long prev_pos; /* posn of prev file of same name */
struct direntry direntry; /* directory entry */
struct direntry dir2entry; /* spare */
struct tiny_header tiny_header; /* for Z format archives */
int status; /* error status */
int success; /* successful addition of file? */
int addcount = 0; /* number added */
int update=0; /* add only files already in archive */
int suppress=0; /* suppress compression */
int new=0; /* add only files not in archive */
int zootime = 0; /* just set archive time */
int add_comment = 0; /* add comment */
int pack = 0; /* pack after adding */
int need_dir = 1; /* store directories too */
int delcount = 0; /* count of deleted entries */
int exit_status = 0; /* exit status to set */
unsigned int latest_date = 0; /* to set time on archive itself */
unsigned int latest_time = 0; /* .. same */
int move = 0; /* delete after adding to archive */
int longest; /* length of longest pathname added */
int firstfile = 1; /* first file being added? */
int z_fmt = 0; /* look for Z format files? */
/* on entry option points to first letter */
opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack,
&update, &add_comment, &z_fmt, &need_dir);
if (exists (zoo_path)) {
zoo_han = OPEN(zoo_path, F_RDWR);
zoo_status = OLD_ZOO;
} else {
if (!zootime)
zoo_han = CREATE(zoo_path, F_RDWR);
else
zoo_han = -1; /* don't create if just setting time */
zoo_status = NEW_ZOO;
}
if (zoo_han == -1)
prterror ('f', could_not_open, zoo_path);
basename(zoo_path, zoo_fname); /* get basename of archive */
rootname (zoo_path, zoo_bak); /* name without extension */
strcat (zoo_bak, BACKUP_EXT); /* name of backup of this archive */
zoo_file = fdopen (zoo_han, "r+"); /* associate a stream with the file. */
/* "r+" means read & write access */
if (zoo_file == NULL) {
prterror ('f', could_not_open, zoo_path);
}
/*
From now on, both zoo_file and zoo_han can be used (one for stream I/O and
the other for low-level I/O). But before doing any low-level I/O, or
before switching between reading and writing on the stream, a seek
must be done on the stream to flush buffered data and synchronize file
pointers.
*/
/* Now we prepare the archive for adding one or more files. If the archive
has just been created, we write the archive header */
addfname ("",0L,0,0); /* initialize table of files already in archive */
if (zoo_status == NEW_ZOO) { /* newly-created archive */
fwr_zooh (&zoo_header, zoo_file);
/* fwrite ((char *) &zoo_header, sizeof(zoo_header), 1, zoo_file); */
fseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
} else {
/* read header and rewrite with updated version numbers */
rwheader (&zoo_header, zoo_file);
/* initialize latest_name to null string */
*latest_name = '\0';
/* Skip existing files but add them to a list. The variable last_old
gets the tail of the old chain of directory entries */
skip_files (zoo_file, &latest_date, &latest_time, &delcount,
latest_name, &last_old);
}
/* The file pointer is now positioned correctly to add a file to archive,
unless the null directory entry is too short. This will be fixed below. */
/* If we are just setting time, do it and run. */
if (zootime) {
#ifdef NIXTIME
close (zoo_han);
setutime (zoo_path, latest_date, latest_time);
#else
settime (zoo_han, latest_date, latest_time);
close (zoo_han);
#endif
prterror ('m', "Archive time adjusted.\n");
exit (0);
}
if (argc < 1) /* must have at least one file to add */
prterror ('f', internal_error);
/* make list of files, excluding archive and its backup */
makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest);
/* ^^ ^^ ^^ exclude */
for (fptr = 0; (this_path = flist[fptr]) != NULL; fptr++) {
unsigned int this_date, this_time;
int INLIST; /* boolean */
int RECENT; /* boolean */
int danger; /* if update requested and disk copy is out of date */
basename (this_path, this_fname); /* get just filename for later */
this_han = OPEN(this_path, F_READ);
if (this_han == -1) {
prterror ('e', could_not_open, this_path);
exit_status++;
continue;
}
#ifndef PORTABLE
/* Test to see if this is a Z format file. We assume the file is Z format
if (a) tag is correct and (b) type is 1 and (c) embedded filename
is not longer than FNAMESIZE.
*/
if (z_fmt) {
read (this_han, (char *) &tiny_header, sizeof(tiny_header));
if (tiny_header.tinytag == TINYTAG && tiny_header.type == 1 &&
strlen (tiny_header.fname) <= FNAMESIZE)
/* ok */ ;
else {
close (this_han);
prterror ('e', "File %s does not have Z format.\n", this_fname);
exit_status++;
continue;
}
}
#endif
/* get file time; also fix name */
#ifndef PORTABLE
if (z_fmt) {
direntry.date = tiny_header.date;
direntry.time = tiny_header.time;
strcpy (direntry.fname, tiny_header.fname);
direntry.dirlen = direntry.namlen = 0;
} else {
#endif
/* Get timstamp of file being added */
#ifdef GETUTIME
getutime (this_path, &direntry.date, &direntry.time);
#else
gettime (this_han, &direntry.date, &direntry.time);
#endif
#ifdef FOLD
strlwr(this_fname);
#endif
dosname (this_fname, direntry.fname); /* MSDOS filename */
/*
Store long filename into direntry.lfname iff it is different from MSDOS
filename. Also store directory name if need_dir is true. Moved out of
zooadd() so zooadd() doesn't get too big for optimization.
*/
storefname (&direntry, this_path, need_dir);
#ifndef PORTABLE
}
#endif
#ifdef DEBUG
printf ("zooadd: direntry.lfname = [%s] direntry.dirname = [%s]\n",
direntry.lfname, direntry.dirname);
#endif
/* if update option, then we add file if it is already in the archive
AND the archived file is older */
/* The following logic was derived from a Karnaugh map so it may
be hard to understand. Essentially, if U=update requested,
N=new files requested, I=file is already in archive, and
R=file being archived is more recent than file already in
archive, then the boolean equation is:
add = U' (N' + I') + U (IR + I'N)
*/
/* Get the filename to use for this addition. */
whichname = choosefname(&direntry);
/* get position in archive of any old file of same name */
prev_pos = inlist (whichname, &this_date, &this_time);
INLIST = prev_pos > 0; /* already in archive if positive value */
if (INLIST) {
int result;
result = cmpnum (direntry.date, direntry.time, this_date, this_time);
RECENT = result > 0;
danger = result < 0;
} else
danger = 0; /* And RECENT is undefined and should not be used */
if (
!update && (!new || !INLIST) ||
update && (INLIST && RECENT || !INLIST && new)
)
; /* then continue and add file */
else {
if (update && danger)
prterror ('w', "Archived copy of %s is newer.\n", whichname);
close (this_han);
continue; /* cycle back, skip this file */
}
#ifdef CHEKDIR
/* Don't add if this is a directory */
if (isadir (this_han)) {
close (this_han);
continue;
}
#endif
#ifdef CHEKUDIR
/* Don't add if this is a directory */
if (isuadir (this_path)) {
close (this_han);
continue;
}
#endif
/* Create directory entry for new file (but don't add just yet) */
/* NOTE: we already got file date and time above for update option */
direntry.zoo_tag = ZOO_TAG;
direntry.type = 2; /* type is now 2 */
direntry.tz = 127; /* timezone unknown */
direntry.struc = 0; /* unstructured file */
direntry.system_id = 0; /* identify UNIX filesystem */
direntry.var_dir_len =
(direntry.dirlen > 0 || direntry.namlen > 0 ? 2 : 0) +
direntry.dirlen + direntry.namlen;
/*
Write a null direntry entry. Thus, if an error occurs or the program
is interrupted, the end of the archive will still be meaningful.
Special check needed for first one written.
*/
direntry.next = direntry.offset = 0L; /* trailing null entry */
this_dir_offset = ftell (zoo_file);
if (!firstfile) {
writedir (&direntry, zoo_file);
} else {
/*
Before adding the first file to the archive, we must make sure that
the previous directory chain (if any) is properly terminated with a
null entry of the right size. If this is a new archive, we simply
write a new null entry of the right size. If this is an existing
archive, we must check the size of the previous trailing null entry.
If it is too small, we will back up to the most recent real directory
entry and change its .next field to point to end of file.
*/
if (zoo_status == NEW_ZOO) {
writedir (&direntry, zoo_file); /* write null dir entry */
} else {
#define DIRLEN(x) ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len))
struct direntry tmpentry;
long tmppos;
int oldlen, newlen;
tmppos = ftell(zoo_file);
frd_dir (&tmpentry, zoo_file);
oldlen = DIRLEN(tmpentry); /* get length of direntry */
newlen = DIRLEN(direntry); /* ditto */
if (newlen > oldlen) { /* trouble */
fseek (zoo_file, last_old, 0); /* back to previous entry */
frd_dir (&tmpentry, zoo_file);
fseek (zoo_file, 0L, 2); /* get EOF position */
tmpentry.next = ftell(zoo_file); /* point to EOF */
fseek (zoo_file, last_old, 0); /* back to previous entry */
writedir (&tmpentry, zoo_file); /* update it */
fseek (zoo_file, 0L, 2); /* to EOF ... */
this_dir_offset = ftell (zoo_file);
writedir (&direntry, zoo_file); /* ...write null dir entry */
} else
fseek (zoo_file, tmppos, 0); /* long enough -- let it be */
} /* if (zoo_status == NEW_ZOO) ... */
} /* if (!firstfile) ... */
/* Now `this_dir_offset' is where the next directory entry will go */
/* first file added goes at EOF to avoid overwriting comments */
if (firstfile) {
fseek (zoo_file, 0L, 2); /* EOF */
direntry.offset = ftell (zoo_file) + SIZ_FLDR;
} else {
direntry.offset = this_dir_offset + SIZ_DIRL +
direntry.var_dir_len + SIZ_FLDR;
}
direntry.major_ver = MAJOR_EXT_VER; /* minimum version number needed */
direntry.minor_ver = MINOR_EXT_VER; /* .. to extract */
direntry.deleted = 0; /* not deleted, naturally */
direntry.comment = 0L; /* no comment (yet) */
direntry.cmt_size = 0; /* .. so no size either */
save_position = direntry.offset; /* save position in case of error */
fseek (zoo_file, direntry.offset - SIZ_FLDR, 0);
fwrite (file_leader, SIZ_FLDR, 1, zoo_file);
fseek (zoo_file, ftell (zoo_file), 0); /* for low-level I/O */
#ifdef PORTABLE
prterror ('m', "%-*s -- ", longest, this_path);
#else
if (z_fmt)
prterror ('m', "%-12s <== %-*s -- ",
direntry.fname, longest, this_path);
else
prterror ('m', "%-*s -- ", longest, this_path);
#ifdef COMMENT
prterror ('m', z_fmt ? "%-12s <== %-12s -- " : "%-12s -- ",
direntry.fname, this_fname);
prterror ('m', z_fmt ? "%-12s <== %-12s -- " : "%-12s -- ",
direntry.fname, this_fname);
#endif /* COMMENT */
#endif /* PORTABLE */
crccode = 0;
if (z_fmt) {
direntry.packing_method = tiny_header.packing_method;
lseek (this_han, (long) (sizeof(tiny_header) + tiny_header.cmt_size), 0);
status = getfile (this_han, zoo_han, tiny_header.size_now, 1);
} else if (suppress) { /* suppress compression */
direntry.packing_method = 0; /* no compression */
status = getfile (this_han, zoo_han, -1L, 1);
/* status = copyfile (this_han, zoo_han); */
} else {
direntry.packing_method = 1; /* compressed */
status = lzc(this_han, zoo_han); /* add with compression */
}
if (status != 0) { /* if I */
++exit_status; /* remember error */
if (status == 1)
prterror ('F', no_memory);
else if (status == 2)
prterror ('F', disk_full);
else if (status == 3)
prterror ('F', "Read error.\n");
else
prterror ('F', internal_error);
success = 0;
} else {
direntry.next = ftell(zoo_file);
direntry.size_now = direntry.next - direntry.offset;
/* find and store original size of file just compressed */
direntry.org_size = tell (this_han); /* should be EOF already */
/* If the compressed one is bigger, just copy */
if (direntry.size_now >= direntry.org_size && /* if II */
direntry.packing_method > 0) {
lseek (zoo_han, save_position, 0); /* ..restore file pointer */
trunc (zoo_han); /* ..truncate file */
direntry.packing_method = 0; /* ..and just copy */
lseek (this_han, 0L, 0); /* (but rewind first!) */
crccode = 0; /* re-start crc from 0 */
status = getfile (this_han, zoo_han, -1L, 1);
/* status = copyfile (this_han, zoo_han); */
if (status != 0) { /* if III */
success = 0;
printf (disk_full);
exit_status++;
} else {
success = 1;
direntry.next = ftell(zoo_file);
direntry.size_now = direntry.next - direntry.offset;
} /* end if III */
} else {
success = 1;
} /* end if II */
} /* end if I */
if (success) { /* file successfully added */
addcount++; /* how many added */
direntry.file_crc = crccode;
/* remember most recent date and time */
if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) {
latest_date = direntry.date;
latest_time = direntry.time;
}
/* mark any previous version of this file in archive as deleted */
dir2entry.comment = 0L; /* for later use assigning to direntry */
dir2entry.cmt_size = 0;
if (!z_fmt)
prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now));
if (prev_pos > 0) {
long save_pos = ftell(zoo_file); /*DEBUG*/
++delcount; /* remember to pack */
prterror ('M', "replaced\n");
fseek (zoo_file, prev_pos, 0);
readdir (&dir2entry, zoo_file, 1);
dir2entry.deleted = 1; /* mark as deleted */
fseek (zoo_file, prev_pos, 0);
writedir (&dir2entry, zoo_file);
fseek (zoo_file, save_pos, 0); /*DEBUG*/
} else prterror ('M', "added\n");
/* Preserve any old comment if we replaced the file */
direntry.comment = dir2entry.comment;
direntry.cmt_size = dir2entry.cmt_size;
#ifndef PORTABLE
/* Copy comment if any from Z format file */
if (z_fmt && tiny_header.cmt_size != 0) {
lseek (this_han, (long) sizeof(tiny_header), 0); /* get to comment */
#ifdef COMMENT
fseek (zoo_file, 0L, 2); /* append comment to end */
#endif
direntry.comment = ftell (zoo_file);
direntry.cmt_size = tiny_header.cmt_size;
/* 4th param is 0 for no CRC */
getfile (this_han, zoo_han, (long) tiny_header.cmt_size, 0);
direntry.next = ftell(zoo_file);
}
#endif
/* if user requested comments, any previous comment in a Z format
file may now be manually overwritten */
if (add_comment && !feof (stdin)) {
show_comment (&direntry, zoo_file, 1, whichname);
get_comment (&direntry, zoo_file, this_path);
direntry.next = ftell(zoo_file); /* update .next ptr */
} /* end if */
#ifndef PORTABLE
/* if adding Z format archive, copy relevant fields from its header */
if (z_fmt) { /* moved out to shorten code & allow optimizer to work */
copyfields (&direntry, &tiny_header);
}
#endif
debug((printf ("zooadd: our new .next = [%lx].\n", direntry.next)))
{
long savepos = ftell(zoo_file); /* save position */
fseek (zoo_file, this_dir_offset, 0);
writedir (&direntry, zoo_file);
fseek (zoo_file, savepos, 0); /* restore position */
}
} else { /* file was not properly added */
lseek (zoo_han, save_position, 0); /* ..restore file pointer */
trunc (zoo_han); /* ..truncate file */
} /* end if */
close (this_han);
if (!success)
break;
firstfile = 0;
} /* end for */
save_position = ftell (zoo_file);
/* Write a null direntry entry */
fseek (zoo_file, save_position, 0);
writenull (zoo_han, MAXDIRSIZE);
trunc (zoo_han); /* truncate */
#ifdef NIXTIME
fclose (zoo_file);
setutime (zoo_path, latest_date, latest_time);
#else
settime (zoo_han, latest_date, latest_time);
fclose (zoo_file);
#endif
if (!addcount) { /* no files added */
prterror ('m', "No files added.\n");
if (zoo_status == NEW_ZOO)
unlink (zoo_path);
} else {
if (delcount && pack) { /* pack if user asked and found deleted entries */
prterror ('M', "-----\nNow packing...\n");
zoopack (zoo_path, "PP");
}
/* If files to move and we added some and no error so far, delete originals */
if (move && !exit_status)
if (kill_files (flist, longest) != 0)
exit_status++;
}
if (exit_status)
exit (1);
} /* end zoo_add */