From: dhesi@bsu-cs.bsu.edu (Rahul Dhesi) Newsgroups: alt.sources Subject: zoo 2.1 source part 13/15 Message-ID: <12780@bsu-cs.bsu.edu> Date: 10 Jul 91 11:35:14 GMT Checksum: 2821476663 (verify with "brik -cv") Submitted-by: dhesi@bsu-cs.bsu.edu Archive-name: zoo210/part13 ---- Cut Here and feed the following to sh ---- #!/bin/sh # This is part 13 of zoo210 # ============= zooadd.c ============== if test -f 'zooadd.c' -a X"$1" != X"-c"; then echo 'x - skipping zooadd.c (File already exists)' else echo 'x - extracting zooadd.c (Text)' sed 's/^X//' << 'SHAR_EOF' > 'zooadd.c' && #ifndef LINT /* derived from: zooadd.c 2.34 88/08/15 10:53:11 */ static char sccsid[]="$Source: /usr/home/dhesi/zoo/RCS/zooadd.c,v $\n\ $Id: zooadd.c,v 1.10 91/07/08 23:48:39 dhesi Exp $"; #endif /* LINT */ X /* Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved (C) Copyright 1988 Rahul Dhesi -- All rights reserved (C) Copyright 1991 Rahul Dhesi -- All rights reserved */ #include "options.h" /* Adds files specified in parameter-list to archive zoo_path. */ X #define LONGEST 20 /* assumed length of longest filename */ #include "zoomem.h" /* to define MAXADD */ #include "zoo.h" #include "zooio.h" #include "various.h" #include "parse.h" #include "debug.h" X #include "portable.h" X #include "zoofns.h" #include "errors.i" extern int break_hit; extern int quiet; X void show_comment PARMS ((struct direntry *, ZOOFILE, int, char *)); void dosname PARMS ((char *, char *)); void modpath PARMS ((char *)); void opts_add PARMS ((char *, int *, int *, int *, int *, int *, int *, X int *, int *, int *, int *, int *, int *, int *, int *)); int ver_too_high PARMS ((struct zoo_header *)); void get_comment PARMS ((struct direntry *, ZOOFILE, char *)); void copyfields PARMS ((struct direntry *, struct tiny_header *)); void storefname PARMS ((struct direntry *, char *, int)); char *choosefname PARMS ((struct direntry *)); X extern struct zoo_header zoo_header; X extern char file_leader[]; extern unsigned int crccode; X 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; /* list of ptrs to input fnames */ int fptr; /* will point to within flist */ ZOOFILE this_file; /* 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 */ ZOOFILE zoo_file; /* stream for open archive */ char *this_path; /* pathname of file to add */ X #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 X 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 */ int status; /* error status */ int success; /* successful addition of file? */ int addcount = 0; /* number added */ int update=0; /* 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 add_global_comment = 0; /* archive 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 */ X 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? */ int inargs = 0; /* read filenames from stdin? */ X #ifndef PORTABLE struct tiny_header tiny_header; /* for Z format archives */ #endif X unsigned this_version_no; /* version no. of old file */ unsigned high_vflag; /* version flag of old file */ unsigned high_version_no; /* highest version no of this file */ long high_pos; /* offset of file w/highest ver no */ unsigned int fgens; /* gens. to preserve -- file */ unsigned int zgens; /* gens. to preserve -- archive */ long oldcmtpos; /* to save old comment */ unsigned int oldcmtsiz; /* to save old comment */ int genson = 0; /* whether to turn generations on */ X int use_lzh = 0; /* whether to use lzh compression */ X /* on entry option points to first letter */ X opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack, X &update, &add_comment, &z_fmt, &need_dir, &inargs, &genson, X &use_lzh, &add_global_comment); X /* POSSIBLE RACE CONDITION BETWEEN TESTING EXISTENCE AND CREATING FILE */ if (exists (zoo_path)) { X zoo_file = zooopen (zoo_path, Z_RDWR); X zoo_status = OLD_ZOO; } else { X if (!zootime) X zoo_file = zoocreate (zoo_path); X else X zoo_file = NOFILE; /* don't create if just setting time */ X zoo_status = NEW_ZOO; } X if (zoo_file == NOFILE) X 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 */ X /* Now we prepare the archive for adding one or more files. If the archive has just been created, we write the archive header */ X addfname ("",0L,0,0,0,0); /* initialize table of files already in archive */ if (zoo_status == NEW_ZOO) { /* newly-created archive */ X if (genson) /* if no generations needed */ X zoo_header.vdata = (VFL_ON|GEN_DEFAULT); /* generations on */ X fwr_zooh (&zoo_header, zoo_file); X zgens = GEN_DEFAULT; X zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */ } else { X /* read header and rewrite with updated version numbers, preserving X header type */ X rwheader (&zoo_header, zoo_file, 1); X zgens = zoo_header.vdata & VFL_GEN; /* get archive generations */ X /* initialize latest_name to null string */ X /* NOTE: latest_name is not currently used for anything, but X may be used in the future for inserting files into the X archive in alphabetic order. */ X *latest_name = '\0'; X X /* Skip existing files but add them to a list. The variable last_old X gets the tail of the old chain of directory entries */ X skip_files (zoo_file, &latest_date, &latest_time, &delcount, X 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. */ X /* If we are just setting time, do it and run. */ if (zootime) { #ifdef NIXTIME X zooclose (zoo_file); X setutime (zoo_path, latest_date, latest_time); #else X settime (zoo_file, latest_date, latest_time); X zooclose (zoo_file); #endif X prterror ('m', "Archive time adjusted.\n"); X zooexit (0); } X /* make list of files, excluding archive and its backup */ longest = LONGEST; flist = (char **) ealloc(MAXADD); if (!inargs) { X makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest); X /* ^^ ^^ ^^ exclude */ } X fptr = 0; /* ready to get filename (if makelist() was called) or to X begin adding filenames (if reading them from stdin) */ X while (1) { X unsigned int this_date, this_time; X int INLIST; /* boolean */ X int RECENT; /* boolean */ X int danger; /* if update requested and disk copy is out of date */ X if (inargs) { X again: /* loop back if filename was same as archive name or its backup */ X this_path = getstdin(); /* pathname from stdin, in static area */ X if (this_path != NULL) { X if (samefile (nameptr(zoo_fname),nameptr(this_path)) || X samefile (nameptr(zoo_bak),nameptr(this_path))) X goto again; /* don't add archive to itself */ X modpath (this_path); X /* if moving files, add to list for later deletion; if list overflows, X terminate addition loop and give warning message */ X if (move) { X if (fptr >= MAXADD-2) { X prterror ('w', too_many_files, MAXADD-2); X this_path = NULL; X } else X flist[fptr++] = str_dup (this_path); X } X } X } else { X this_path = flist[fptr++]; X } X /* exit the addition loop when no more pathnames are left */ X if (this_path == NULL) { X /* in case stdin was being read, make sure flist is NULL-terminated */ X flist[fptr] = NULL; X break; X } X X basename (this_path, this_fname); /* get just filename for later */ X X this_file = zooopen(this_path, Z_READ); X if (this_file == NOFILE) { X prterror ('e', could_not_open, this_path); X exit_status++; X continue; X } X #ifndef PORTABLE X /* Test to see if this is a Z format file. We assume the file is Z format X if (a) tag is correct and (b) type is 1 and (c) embedded filename X is not longer than FNAMESIZE. X */ X if (z_fmt) { X zooread (this_file, (char *) &tiny_header, sizeof(tiny_header)); X if (tiny_header.tinytag == TINYTAG && tiny_header.type == 1 && X strlen (tiny_header.fname) <= FNAMESIZE) X /* ok */ ; X else { X zooclose (this_file); X prterror ('e', "File %s does not have Z format.\n", this_fname); X exit_status++; X continue; X } X } #endif X X /* get file time; also fix name */ #ifndef PORTABLE X if (z_fmt) { X direntry.date = tiny_header.date; X direntry.time = tiny_header.time; X strcpy (direntry.fname, tiny_header.fname); X direntry.dirlen = direntry.namlen = 0; X } else { #endif X X /* Get timstamp of file being added */ #ifdef GETUTIME X getutime (this_path, &direntry.date, &direntry.time); #else X gettime (this_file, &direntry.date, &direntry.time); #endif X X /* save file attributes */ #ifdef FATTR X /* we expect getfattr() to set all attr. bits; currently X only the portable format is recognized */ X { # ifdef FATTR_FNAME X unsigned long getfattr PARMS ((char *); X direntry.fattr = getfattr (this_path); # else X unsigned long getfattr PARMS ((ZOOFILE)); X direntry.fattr = getfattr (this_file); # endif /* FATTR_FNAME */ X } #else X direntry.fattr = NO_FATTR; /* none */ #endif /* FATTR */ X #ifdef FOLD X str_lwr(this_fname); #endif X dosname (this_fname, direntry.fname); /* MSDOS filename */ X X /* X Store long filename into direntry.lfname iff it is different from MSDOS X filename. Also store directory name if need_dir is true. Moved out of X zooadd() so zooadd() doesn't get too big for optimization. X */ X storefname (&direntry, this_path, need_dir); X #ifndef PORTABLE X } #endif X #ifdef DEBUG printf ("zooadd: direntry.lfname = [%s] direntry.dirname = [%s]\n", X direntry.lfname, direntry.dirname); #endif X X /* if update option, then we add file if it is already in the archive X AND the archived file is older */ X X /* The following logic was derived from a Karnaugh map so it may X be hard to understand. Essentially, if U=update requested, X N=new files requested, I=file is already in archive, and X R=file being archived is more recent than file already in X archive, then the boolean equation is: X X add = U' (N' + I') + U (IR + I'N) X */ X X /* Get the filename to use for this addition. */ X whichname = choosefname(&direntry); X X /* Get position in archive of any old file of same name, ignoring X any directory prefix if need_dir is not true. Also get its X date, time, version flag, and version number. */ X prev_pos = inlist (fullpath (&direntry), &this_date, &this_time, X &this_version_no, &high_vflag, &high_version_no, X &high_pos, !need_dir); X /* define DBG_INLIST for debugging by printing values returned by inlist() */ #ifdef DBG_INLIST X printf ("FROM inlist(): prev_pos=%ld, high_pos=%ld\n", prev_pos, high_pos); X printf ("this_version_no=%u, high_vflag=%4x, high_version_no=%u\n", X this_version_no, high_vflag, high_version_no); #endif X X INLIST = prev_pos > 0; /* already in archive if positive value */ X if (INLIST) { X int result; X result = cmpnum (direntry.date, direntry.time, this_date, this_time); X RECENT = result > 0; X danger = result < 0; X } else X danger = 0; /* And RECENT is undefined and should not be used */ X X if ( X !update && (!new || !INLIST) || X update && (INLIST && RECENT || !INLIST && new) X ) X ; /* then continue and add file */ X else { X if (update && danger) X prterror ('w', "Archived copy of %s is newer.\n", whichname); X zooclose (this_file); X continue; /* cycle back, skip this file */ X } X #ifdef CHEKDIR X /* Don't add if this is a directory */ X if (isadir (this_file)) { X zooclose (this_file); X continue; X } #else # ifdef CHEKUDIR X /* Don't add if this is a directory */ X if (isuadir (this_path)) { X zooclose (this_file); X continue; X } # endif /* CHEKUDIR */ #endif /* CHEKDIR */ X X /* Create directory entry for new file (but don't add just yet) */ X /* NOTE: we already got file date and time above for update option */ X /* add tag, type, timezone, struc, system_id, and var_dir_len */ X newdir (&direntry); X X if (!genson && zoo_status == NEW_ZOO || X (zoo_header.vdata & VFL_ON) == 0) { X direntry.vflag = 0; X direntry.version_no = 0; X } X X /* X Write a null direntry entry. Thus, if an error occurs or the program X is interrupted, the end of the archive will still be meaningful. X Special check needed for first one written. X */ X X direntry.next = direntry.offset = 0L; /* trailing null entry */ X this_dir_offset = zootell (zoo_file); X if (!firstfile) { X writedir (&direntry, zoo_file); X } else { X /* X Before adding the first file to the archive, we must make sure that X the previous directory chain (if any) is properly terminated with a X null entry of the right size. If this is a new archive, we simply X write a new null entry of the right size. If this is an existing X archive, we must check the size of the previous trailing null entry. X If it is too small, we will back up to the most recent real directory X entry and change its .next field to point to end of file. X */ X X if (zoo_status == NEW_ZOO) { X writedir (&direntry, zoo_file); /* write null dir entry */ X } else { X struct direntry tmpentry; X long tmppos; X int oldlen, newlen; X tmppos = zootell (zoo_file); X frd_dir (&tmpentry, zoo_file); #define DIRLEN(x) ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len)) X oldlen = DIRLEN(tmpentry); /* get length of direntry */ X newlen = DIRLEN(direntry); /* ditto */ X X if (newlen > oldlen) { /* trouble */ X zooseek (zoo_file, last_old, 0); /* back to previous entry */ X frd_dir (&tmpentry, zoo_file); X zooseek (zoo_file, 0L, 2); /* get EOF position */ X tmpentry.next = zootell (zoo_file); /* point to EOF */ X zooseek (zoo_file, last_old, 0); /* back to previous entry */ X writedir (&tmpentry, zoo_file); /* update it */ X zooseek (zoo_file, 0L, 2); /* to EOF ... */ X this_dir_offset = zootell (zoo_file); X writedir (&direntry, zoo_file); /* ...write null dir entry */ X } else X zooseek (zoo_file, tmppos, 0); /* long enough -- let it be */ X } /* if (zoo_status == NEW_ZOO) ... */ X } /* if (!firstfile) ... */ X X /* Now `this_dir_offset' is where the next directory entry will go */ X X /* first file added goes at EOF to avoid overwriting comments */ X if (firstfile) { X zooseek (zoo_file, 0L, 2); /* EOF */ X direntry.offset = zootell (zoo_file) + SIZ_FLDR; X } else { X direntry.offset = this_dir_offset + SIZ_DIRL + X direntry.var_dir_len + SIZ_FLDR; X } X X if (use_lzh) { X direntry.major_ver = MAJOR_LZH_VER; /* minimum version number needed */ X direntry.minor_ver = MINOR_LZH_VER; /* .. to extract */ X } else { X direntry.major_ver = MAJOR_EXT_VER; /* minimum version number needed */ X direntry.minor_ver = MINOR_EXT_VER; /* .. to extract */ X } X direntry.deleted = 0; /* not deleted, naturally */ X direntry.comment = 0L; /* no comment (yet) */ X direntry.cmt_size = 0; /* .. so no size either */ X X save_position = direntry.offset; /* save position in case of error */ X X (void) zooseek (zoo_file, direntry.offset - SIZ_FLDR, 0); X (void) zoowrite (zoo_file, file_leader, SIZ_FLDR); X #ifdef PORTABLE X prterror ('m', "%-*s -- ", longest, this_path); #else X if (z_fmt) X prterror ('m', "%-12s <== %-*s -- ", X direntry.fname, longest, this_path); X else X prterror ('m', "%-*s -- ", longest, this_path); X #endif /* PORTABLE */ X X crccode = 0; #ifndef PORTABLE X if (z_fmt) X { X direntry.packing_method = tiny_header.packing_method; X zooseek (this_file, (long) (sizeof(tiny_header)+tiny_header.cmt_size), 0); X status = getfile (this_file, zoo_file, tiny_header.size_now, 1); X } else #endif X if (suppress) { /* suppress compression */ X direntry.packing_method = 0; /* no compression */ X status = getfile (this_file, zoo_file, -1L, 1); X } else { #ifdef UNBUF_IO /* unbuffered I/O */ X long lseek PARMS ((int, long, int)); X long tell PARMS ((int)); X int this_fd, zoo_fd; #endif X if (use_lzh) X direntry.packing_method = 2; X else X direntry.packing_method = 1; #ifdef UNBUF_IO #include "UNBUF_IO not currently supported" X this_fd = fileno (this_file); /* get .. */ X zoo_fd = fileno (zoo_file); /* ... file descriptors */ X zooseek (zoo_file, zootell (zoo_file), 0); /* synch */ X zooseek (this_file, zootell (this_file), 0); /* synch */ X status = lzc(this_fd, zoo_fd); /* add with compression */ X zooseek (zoo_file, tell (zoo_fd), 0); /* resynch */ X zooseek (this_file, tell (this_fd), 0); /* resynch */ #else X if (use_lzh) X status = lzh_encode(this_file, zoo_file); X else X status = lzc(this_file, zoo_file); #endif /* UNBUF_IO */ X X } X if (status != 0) { /* if I */ X ++exit_status; /* remember error */ X if (status == 1) X prterror ('F', no_memory); X else if (status == 2) X prterror ('F', disk_full); X else if (status == 3) X prterror ('F', "Read error.\n"); X else X prterror ('F', internal_error); X success = 0; X } else { X direntry.next = zootell (zoo_file); X direntry.size_now = direntry.next - direntry.offset; X X /* find and store original size of file just compressed */ /*DEBUG VMS*/ zooseek (this_file, 0L, 2); /* seek to EOF */ X X direntry.org_size = zootell (this_file); /* should be EOF already */ X X /* If the compressed one is bigger, just copy */ X X if (direntry.size_now >= direntry.org_size && /* if II */ X direntry.packing_method != 0) { X zooseek (zoo_file, save_position, 0); /* ..restore file pointer */ X zootrunc (zoo_file); /* ..truncate file */ X direntry.packing_method = 0; /* ..and just copy */ X zooseek (this_file, 0L, 0); /* (but rewind first!) */ X crccode = 0; /* re-start crc from 0 */ X status = getfile (this_file, zoo_file, -1L, 1); X if (status != 0) { /* if III */ X success = 0; X printf (disk_full); X exit_status++; X } else { X success = 1; X direntry.next = zootell (zoo_file); X direntry.size_now = direntry.next - direntry.offset; X } /* end if III */ X } else { X success = 1; X } /* end if II */ X X } /* end if I */ X X if (success) { /* file successfully added */ X addcount++; /* how many added */ X direntry.file_crc = crccode; X X /* remember most recent date and time */ X if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) { X latest_date = direntry.date; X latest_time = direntry.time; X } X #if 0 X /* mark any previous version of this file in archive as deleted */ X dir2entry.comment = 0L; /* for later use assigning to direntry */ X dir2entry.cmt_size = 0; #endif X X if (!z_fmt) X prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now)); X X oldcmtsiz = 0; /* assume no old comment */ X oldcmtpos = 0L; X X if (prev_pos > 0) { /* in archive */ X int delold = 0; /* delete old? */ X /* if versions active both archive-wide and for file */ X if ((zoo_header.vdata & VFL_ON) && (high_vflag & VFL_ON)) { X /* next test is optimization, to avoid redundant I/O */ X if (high_pos != prev_pos || this_version_no == 1) { X /* prev highest is no longer highest so adjust vflag */ X long save_pos = zootell (zoo_file); /*DEBUG*/ X zooseek (zoo_file, high_pos, 0); X readdir (&dir2entry, zoo_file, 1); X oldcmtpos = dir2entry.comment; X oldcmtsiz = dir2entry.cmt_size; X dir2entry.vflag &= (~VFL_LAST); /* no longer highest */ X zooseek (zoo_file, high_pos, 0); X writedir (&dir2entry, zoo_file); X zooseek (zoo_file, save_pos, 0); /*DEBUG*/ X } X X direntry.version_no = high_version_no + 1; /* ..one higher */ X direntry.vflag = high_vflag; X /* now see if we need to delete older version */ X fgens = high_vflag & VFL_GEN; X if (fgens == 0) X fgens = zgens; X if (zgens != 0 && zgens < fgens) X fgens = zgens; X if (fgens != 0 && direntry.version_no - this_version_no >= fgens) { X delold = 1; X prterror ('M', "replaced+\n"); X } else X prterror ('M', "added+\n"); X } else { X prterror ('M', "replaced\n"); X delold = 1; X } X X if (delold) { /* deleting old file */ X long save_pos = zootell (zoo_file); /*DEBUG*/ X ++delcount; /* remember to pack */ X zooseek (zoo_file, prev_pos, 0); X readdir (&dir2entry, zoo_file, 1); X if (dir2entry.cmt_size != 0) { /* propagate latest comment */ X oldcmtpos = dir2entry.comment; X oldcmtsiz = dir2entry.cmt_size; X } X dir2entry.deleted = 1; /* mark as deleted */ X /* following line is optimization if only 1 generation */ X dir2entry.vflag &= (~VFL_LAST); /* no longer highest */ X zooseek (zoo_file, prev_pos, 0); X writedir (&dir2entry, zoo_file); X zooseek (zoo_file, save_pos, 0); /*DEBUG*/ X } X } else /* not in archive */ X prterror ('M', "added\n"); X X /* Preserve any old comment if we replaced or superseded the file */ X direntry.comment = oldcmtpos; X direntry.cmt_size = oldcmtsiz; X #ifndef PORTABLE X /* Copy comment if any from Z format file */ X if (z_fmt && tiny_header.cmt_size != 0) { X zooseek (this_file, (long) sizeof(tiny_header), 0); /* to comment */ X direntry.comment = zootell (zoo_file); X direntry.cmt_size = tiny_header.cmt_size; X /* 4th param is 0 for no CRC */ X getfile (this_file, zoo_file, (long) tiny_header.cmt_size, 0); X direntry.next = zootell (zoo_file); X } #endif X X /* if user requested comments, any previous comment in a Z format X file may now be manually overwritten */ X if (add_comment && !feof (stdin)) { X show_comment (&direntry, zoo_file, 1, whichname); X get_comment (&direntry, zoo_file, this_path); X direntry.next = zootell (zoo_file); /* update .next ptr */ X } /* end if */ X #ifndef PORTABLE X /* if adding Z format archive, copy relevant fields from its header */ X if (z_fmt) { /* moved out to shorten code & allow optimizer to work */ X copyfields (&direntry, &tiny_header); X } #endif X X debug((printf ("zooadd: our new .next = [%lx].\n", direntry.next))) X X { X long savepos = zootell (zoo_file); /* save position */ X zooseek (zoo_file, this_dir_offset, 0); X writedir (&direntry, zoo_file); X zooseek (zoo_file, savepos, 0); /* restore position */ X } X X } else { /* file was not properly added */ X zooseek (zoo_file, save_position, 0); /* ..restore file pointer */ X zootrunc (zoo_file); /* ..truncate file */ X } /* end if */ X zooclose (this_file); if (!success) X break; firstfile = 0; } /* end for */ X save_position = zootell (zoo_file); X /* Write a null direntry entry */ zooseek (zoo_file, save_position, 0); writenull (zoo_file, MAXDIRSIZE); zootrunc (zoo_file); /* truncate */ X #ifdef NIXTIME zooclose (zoo_file); setutime (zoo_path, latest_date, latest_time); #else settime (zoo_file, latest_date, latest_time); zooclose (zoo_file); #endif X if (!addcount) { /* no files added */ X prterror ('m', "No files added.\n"); X if (zoo_status == NEW_ZOO) X unlink (zoo_path); } else { X if (delcount && pack) { /* pack if user asked and found deleted entries */ X prterror ('M', "-----\nPacking..."); X zoopack (zoo_path, "PP"); X prterror ('M', "done\n"); X } X X /* If files to move & we added some and no error so far, delete originals */ X if (move && !exit_status) X if (kill_files (flist, longest) != 0) X exit_status++; } X /* right here we handle archive comment */ if (add_global_comment) { X comment(zoo_path, "_A"); X add_global_comment = 0; } X if (exit_status) X zooexit (1); } /* end zoo_add */ SHAR_EOF chmod 0644 zooadd.c || echo 'restore of zooadd.c failed' Wc_c="`wc -c < 'zooadd.c'`" test 26320 -eq "$Wc_c" || echo 'zooadd.c: original size 26320, current size' "$Wc_c" fi # ============= zooadd2.c ============== if test -f 'zooadd2.c' -a X"$1" != X"-c"; then echo 'x - skipping zooadd2.c (File already exists)' else echo 'x - extracting zooadd2.c (Text)' sed 's/^X//' << 'SHAR_EOF' > 'zooadd2.c' && #ifndef LINT /* derived from: zooadd2.c 2.14 88/01/27 10:40:32 */ static char sccsid[]="$Id: zooadd2.c,v 1.5 91/07/04 13:33:55 dhesi Exp $"; #endif /* LINT */ X /* Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved (C) Copyright 1988 Rahul Dhesi -- All rights reserved */ #include "options.h" #include "zoo.h" #ifndef OK_STDIO #include #define OK_STDIO #endif #include "various.h" #include "zooio.h" #include "zoofns.h" #include "errors.i" #include "assert.h" #include "debug.h" #include "parse.h" X /* Miscellaneous routines to support zooadd(). */ X /**************** This function is called with zoo_file positioned to the first directory entry in an archive. It skips past all existing files, counts the number of deleted files, saves the latest data and time encountered, and adds all filenames encountered to a global list. The long filename is added if available, else the MSDOS filename is added. */ X void skip_files (zoo_file, latest_date, latest_time, delcount, X latest_name, latest_pos) ZOOFILE zoo_file; unsigned int *latest_date, *latest_time; int *delcount; char latest_name[]; long *latest_pos; { X long save_offset, next_ptr; X struct direntry direntry; X struct direntry *drp = &direntry; X X *latest_pos = 0L; X do { X /* read a directory entry */ X save_offset = zootell (zoo_file); /* save pos'n of this dir entry */ X readdir (&direntry, zoo_file, 1); /* read directory entry */ X if (drp->next == 0L) { /* END OF CHAIN */ X zooseek (zoo_file, save_offset, 0); /* back up */ X break; /* EXIT on end of chain */ X } else X *latest_pos = save_offset; X /* remember most recent date and time, for files not marked deleted */ X if (!drp->deleted) X if (drp->date > *latest_date || X (drp->date == *latest_date && drp->time > *latest_time)) { X *latest_date = drp->date; X *latest_time = drp->time; X } X next_ptr = drp->next; /* ptr to next dir entry */ X if (drp->deleted) X ++(*delcount); /* count deleted entries */ X /* add name of file and position of direntry into global list */ X /* but only if the entry is not deleted */ X if (!drp->deleted) { #ifdef FOLD X /* IS THIS REALLY NEEDED? IF SO, WHAT ABOUT drp->lfname? */ X str_lwr(drp->fname); #endif X /* add full pathname to global list */ X strcpy (latest_name, fullpath (drp)); X addfname (latest_name, save_offset, drp->date, drp->time, X drp->vflag, drp->version_no); X } X zooseek (zoo_file, next_ptr, 0); /* ..seek to next dir entry */ X } while (next_ptr != 0L); /* loop terminates on null ptr */ } X /*******************/ /* kill_files() deletes all files in the supplied list of pointers to filenames */ X int kill_files (flist, pathlength) char *flist[]; /* list of ptrs to input fnames */ int pathlength; /* length of longest pathname */ { X int status = 0; X int fptr; X prterror ('M', "-----\nErasing added files...\n"); X for (fptr = 0; flist[fptr] != NULL; fptr++) { #ifdef CHEKUDIR X if (isuadir(flist[fptr])) X continue; #else /* CHEKUDIR */ # ifdef CHEKDIR X if (isfdir(flist[fptr])) X continue; # endif /* CHEKDIR */ #endif /* CHEKUDIR */ X prterror ('m', "%-*s -- ", pathlength, flist[fptr]); X if (unlink (flist[fptr]) == 0) { X prterror ('M', "erased\n"); X } else { X prterror ('w', "Could not erase %s.\n", flist[fptr]); X status = 1; X } X } X return (status); } X #ifndef PORTABLE /*******************/ void copyfields (drp, thp) struct direntry *drp; struct tiny_header *thp; { X drp->org_size = thp->org_size; X drp->file_crc = thp->file_crc; X drp->size_now = thp->size_now; X drp->major_ver = thp->major_ver; X drp->minor_ver = thp->minor_ver; } #endif X /*******************/ /* processes option switches for zooadd() */ void opts_add (option, zootime, quiet, suppress, move, new, pack, X update, add_comment, z_fmt, need_dir, inargs, genson, X use_lzh, arch_cmnt) char *option; int *zootime, *quiet, *suppress, *move, *new, *pack, X *update, *add_comment, *z_fmt, *need_dir, *inargs, X *genson, *use_lzh, *arch_cmnt; X { X if (*option == 'T') { X (*zootime)++; X option++; X while (*option) { X switch (*option) { X case 'q': (*quiet)++; break; X default: X prterror ('f', inv_option, *option); X } X option++; X } X } X X while (*option) { X switch (*option) { X case 'a': break; X case 'h': (*use_lzh)++; break; /* use lzh compression */ X case 'f': (*suppress)++; break; /* suppress compression */ X case 'M': (*move)++; break; /* delete files after adding them */ X case 'n': (*new)++; break; /* add only files not in archive */ X case 'P': (*pack)++; break; /* pack after adding */ X case 'u': (*update)++; break; /* add only files already in archive */ X case 'q': (*quiet)++; break; /* be quiet */ X case 'c': (*add_comment)++; break; /* add comment */ X case ':': *need_dir = 0; break; /* don't store directories */ X case 'I': (*inargs)++; break; /* get filenames from stdin */ X case 'C': (*arch_cmnt)++; break; /* do an archive comment */ /* #ifdef PORTABLE */ /* avoid Turbo C warning about unused param */ X case 'z': (*z_fmt)++; break; /* look for Z format files */ /* #endif */ X case '+': X *genson = 1; break; X case '-': X *genson = 0; break; X default: X prterror ('f', inv_option, *option); X } X option++; X } /* end while */ X if (*suppress && *use_lzh) X prterror ('f', "\"f\" and \"h\" can't both be used\n"); } X /* Stores long filename into direntry.lfname iff it is different from MSDOS filename. Also stores directory name if need_dir is true. Moved out of zooadd() so zooadd() doesn't get too big for optimization. */ void storefname (direntry, this_path, need_dir) struct direntry *direntry; char *this_path; int need_dir; { X struct path_st path_st; X parse (&path_st, this_path); X direntry->lfname[0] = '\0'; X direntry->namlen = 0; #ifdef SPECMOD X specfname (path_st.lfname); X specdir (path_st.dir); #endif X if (strcmp(path_st.lfname,direntry->fname) != 0) { X strcpy (direntry->lfname, path_st.lfname); /* full filename */ X direntry->namlen = strlen(direntry->lfname) + 1; X } X if (need_dir) { X strcpy (direntry->dirname, path_st.dir); /* directory name */ X direntry->dirlen = strlen(direntry->dirname) + 1; X if (direntry->dirlen == 1) /* don't store trailing null alone */ X direntry->dirlen = 0; X } else { X direntry->dirname[0] = '\0'; X direntry->dirlen = 0; X } } X /* Function getsdtin() gets a pathname from standard input, cleans it if necessary by removing any following blanks/tabs and other junk, and returns it in a static area that is overwritten by each call. */ char *getstdin() { X char *chptr; /* temp pointer */ X static char tempname[PATHSIZE]; X do { X if (fgets (tempname, PATHSIZE, stdin) == NULL) X return (NULL); X /* remove trailing blank, tab, newline */ X for (chptr = tempname; *chptr != '\0'; chptr++) { X if ( X /* PURIFY means remove trailing blanks/tabs and all subsequent chars */ #ifdef PURIFY X *chptr == '\t' || *chptr == ' ' || #endif X *chptr == '\n' /* always remove trailing \n */ X ) X { X X *chptr = '\0'; X break; X } X } X } while (*tempname == '\0'); /* get a nonempty line */ #ifdef FOLD X str_lwr (tempname); #endif X return (tempname); } X /* Function newdir() adds some default information to a directory entry. This will be a new directory entry added to an archive. */ void newdir (direntry) register struct direntry *direntry; { #ifdef GETTZ X long gettz(); #endif X direntry->zoo_tag = ZOO_TAG; X direntry->type = 2; /* type is now 2 */ #ifdef GETTZ X direntry->tz = (uchar) (gettz() / (15 * 60)); /* seconds => 15-min units */ #else X direntry->tz = NO_TZ; /* timezone unknown */ #endif X direntry->struc = 0; /* unstructured file */ X direntry->system_id = SYSID_NIX; /* identify **IX filesystem */ X direntry->vflag = VFL_ON|VFL_LAST; /* latest version */ X direntry->version_no = 1; /* begin with version 1 */ X /* 1 for namlen, 1 for dirlen, 2 for system id, 3 for attributes, X 1 for version flag and 2 for version number */ X direntry->var_dir_len = direntry->dirlen + direntry->namlen + 10; } SHAR_EOF chmod 0644 zooadd2.c || echo 'restore of zooadd2.c failed' Wc_c="`wc -c < 'zooadd2.c'`" test 8758 -eq "$Wc_c" || echo 'zooadd2.c: original size 8758, current size' "$Wc_c" fi # ============= zoodel.c ============== if test -f 'zoodel.c' -a X"$1" != X"-c"; then echo 'x - skipping zoodel.c (File already exists)' else echo 'x - extracting zoodel.c (Text)' sed 's/^X//' << 'SHAR_EOF' > 'zoodel.c' && #ifndef LINT /* @(#) zoodel.c 2.19 88/02/06 21:23:36 */ /*$Source: /usr/home/dhesi/zoo/RCS/zoodel.c,v $*/ /*$Id: zoodel.c,v 1.4 91/07/09 01:54:11 dhesi Exp $*/ static char sccsid[]="$Source: /usr/home/dhesi/zoo/RCS/zoodel.c,v $\n\ $Id: zoodel.c,v 1.4 91/07/09 01:54:11 dhesi Exp $"; #endif /* LINT */ X /* Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved (C) Copyright 1988 Rahul Dhesi -- All rights reserved */ #include "options.h" /* Deletes or undeletes entries from an archive. choice=1 requests X deletion and choice=0 requests undeletion. */ #include "zoo.h" #include "portable.h" #ifndef OK_STDIO #include #define OK_STDIO #endif #include "various.h" /* may not be needed */ #include "zooio.h" #include "zoofns.h" #include "errors.i" X #ifndef NOSIGNAL #include #endif X int needed PARMS((char *, struct direntry *, struct zoo_header *)); int ver_too_high PARMS((struct zoo_header *)); X extern int quiet; X void zoodel (zoo_path, option, choice) char *zoo_path; char *option; int choice; { #ifndef NOSIGNAL X T_SIGNAL (*oldsignal)(); /* to save previous SIGINT handler */ #endif X int delcount = 0; /* how many entries we [un]deleted */ X char matchname[PATHSIZE]; /* will hold full pathname */ X register ZOOFILE zoo_file; X struct zoo_header zoo_header; X struct direntry direntry; X unsigned int latest_date = 0; /* so we can set time of archive later */ X unsigned int latest_time = 0; X int pack = 0; /* pack after deletion? */ X int file_deleted = 0; /* any files deleted? */ X int one = 0; /* del/undel one file only */ X int done; /* loop control */ X int action; /* delete/undelete or adjust generation */ X int subopt; /* sub option to action */ X long gencount; /* generation count */ X int doarchive = 0; /* whether to adjust archive gen count */ X unsigned valtoshow; /* value to show in informative message */ X int dodel = 0; /* selection of deleted files */ X int selected; /* if current direntry selected */ X /* values for action */ #define NO_ACTION 0 /* nothing */ #define DEL_UNDEL 1 /* delete or undelete file */ #define ADJ_LIM 2 /* adjust generation limit */ #define ADJ_GCNT 3 /* adjust generation count */ #define GEN_ON 4 /* turn on generations */ #define GEN_OFF 5 /* turn off generations */ X /* values for subopt */ #define SET 0 #define INC 1 X action = NO_ACTION; if (*option == 'g') { X while (*(++option)) { X switch (*option) { X case 'A': doarchive = 1; break; X case 'q': quiet++; break; X case 'l': action = ADJ_LIM; break; X case 'c': action = ADJ_GCNT; break; X case '=': X subopt = SET; gencount = calc_ofs (++option); X if (action == ADJ_GCNT && gencount == 0) X prterror ('f', "Generation count must be nonzero.\n"); X goto opts_done; X case '+': X if (action == NO_ACTION) { X if (option[1] =='\0') { X action = GEN_ON; X goto opts_done; X } else X prterror ('f', garbled); X } else { X subopt = INC; gencount = calc_ofs (++option); X goto opts_done; X } X case '-': X if (action == NO_ACTION) { X if (option[1] =='\0') { X action = GEN_OFF; X goto opts_done; X } else X prterror ('f', garbled); X } else { X subopt = INC; gencount = - calc_ofs (++option); X goto opts_done; X } X case 'd': X dodel++; break; X default: X prterror ('f', garbled); X } /* end switch */ X } /* end while */ X /* if normal exit from while loop, it means bad command string */ X prterror ('f', garbled); X opts_done: /* jump here from exit in while loop above */ X if (action == NO_ACTION) X prterror ('f', garbled); } else { X action = DEL_UNDEL; X while (*(++option)) { X switch (*option) { X case 'P': pack++; break; /* pack after adding */ X case 'q': quiet++; break; /* be quiet */ X case '1': one++; break; /* del or undel only one file */ X default: X prterror ('f', inv_option, *option); X } X } /* end while */ } X X /* Open archive for read/write/binary access. It must already exist */ X if ((zoo_file = zooopen (zoo_path, Z_RDWR)) == NOFILE) { X prterror ('f', could_not_open, zoo_path); X } X X /* read archive header */ X frd_zooh (&zoo_header, zoo_file); X if ((zoo_header.zoo_start + zoo_header.zoo_minus) != 0L) X prterror ('f', failed_consistency); X if (ver_too_high (&zoo_header)) X prterror ('f', wrong_version, zoo_header.major_ver, zoo_header.minor_ver); X X if (doarchive) { /* manipulate archive gen val */ X unsigned zoo_date, zoo_time; #ifdef GETUTIME X getutime (zoo_path, &zoo_date, &zoo_time); /* save archive timestamp */ #else X gettime (zoo_file, &zoo_date, &zoo_time); #endif X if (zoo_header.type == 0) X prterror ('f', packfirst); X if (action == ADJ_LIM) { X unsigned newgencount; X if (subopt == SET) X newgencount = (unsigned) gencount; X else /* INC */ X newgencount = (zoo_header.vdata & VFL_GEN) + (unsigned) gencount; X newgencount &= VFL_GEN; /* reduce to allowed bits */ X zoo_header.vdata &= (~VFL_GEN); X zoo_header.vdata |= newgencount; X prterror ('M', "Archive generation limit is now %u\n", newgencount); X } else if (action == GEN_ON) { X zoo_header.vdata |= VFL_ON; X prterror ('M', "Archive generations on\n"); X } else if (action == GEN_OFF) { X zoo_header.vdata &= (~VFL_ON); X prterror ('M', "Archive generations off\n"); X } else X prterror ('f', garbled); X zooseek (zoo_file, 0L, 0); /* back to begining of file */ X fwr_zooh (&zoo_header, zoo_file); #ifdef NIXTIME X zooclose (zoo_file); X setutime (zoo_path, zoo_date, zoo_time); /* restore archive timestamp */ #else X settime (zoo_file, zoo_date, zoo_time); X zooclose (zoo_file); #endif X return; X } X X zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */ X X done = 0; /* loop not done yet */ X while (1) { X long this_dir_offset; X this_dir_offset = zootell (zoo_file); /* save pos'n of this dir entry */ X frd_dir (&direntry, zoo_file); X if (direntry.zoo_tag != ZOO_TAG) { X prterror ('f', bad_directory); X } X if (direntry.next == 0L) { /* END OF CHAIN */ X break; /* EXIT on end of chain */ X } X X /* select directory entry if it matches criteria */ X selected = ( X (action == DEL_UNDEL && direntry.deleted != choice) X || X (action != DEL_UNDEL && X (dodel && direntry.deleted || X (dodel < 2 && !direntry.deleted)) X ) X ); X X /* WARNING: convention of choice=1 for deleted entry must be same as X in direntry definition in zoo.h */ X X /* Test for "done" so if "one" option requested, [un]del only 1 file */ X /* But we go through the whole archive to adjust archive time */ X X strcpy (matchname, fullpath (&direntry)); /* get full pathname */ X if (zoo_header.vdata & VFL_ON) X add_version (matchname, &direntry); /* add version suffix */ X X if (!done && selected && needed(matchname, &direntry, &zoo_header)) { X prterror ('m', "%-14s -- ", matchname); X delcount++; X if (action == DEL_UNDEL) { X direntry.deleted = choice; X if (choice) X file_deleted++; /* remember if any files actually deleted */ X } else { /* ADJ_LIM or ADJ_GENCNT */ X if (direntry.vflag & VFL_ON) { /* skip if no versions */ X if (action == ADJ_LIM) { X unsigned newgencount; X if (subopt == SET) X newgencount = (unsigned) gencount; X else /* INC */ X newgencount = X (int) (direntry.vflag & VFL_GEN) + (int) gencount; X newgencount &= VFL_GEN; X direntry.vflag &= (~VFL_GEN); X direntry.vflag |= newgencount; X valtoshow = newgencount; X } else { /* ADJ_GCNT */ X if (subopt == SET) X direntry.version_no = (unsigned) gencount; X else /* INC */ X direntry.version_no += (int) gencount; X direntry.version_no &= VER_MASK; /* avoid extra bits */ X valtoshow = direntry.version_no; X } X } X } X X zooseek (zoo_file, this_dir_offset, 0); X #ifndef NOSIGNAL X oldsignal = signal (SIGINT, SIG_IGN); /* disable ^C for write */ #endif X if (fwr_dir (&direntry, zoo_file) == -1) X prterror ('f', "Could not write to archive\n"); #ifndef NOSIGNAL X signal (SIGINT, oldsignal); #endif X if (action == DEL_UNDEL) X prterror ('M', choice ? "deleted\n" : "undeleted\n"); X else { X if (direntry.vflag & VFL_ON) X prterror ('M', "adjusted to %u\n", valtoshow); X else X prterror ('M', "no generations\n"); X } X if (one) X done = 1; /* if 1 option, done after 1 file */ X } X X /* remember most recent date and time if entry is not deleted */ X if (!direntry.deleted) X if (direntry.date > latest_date || X (direntry.date == latest_date && direntry.time > latest_time)) { X latest_date = direntry.date; X latest_time = direntry.time; X } X zooseek (zoo_file, direntry.next, 0); /* ..seek to next dir entry */ X } /* endwhile */ X X if (!delcount) X printf ("Zoo: No files matched.\n"); X else { #ifdef NIXTIME X zooclose (zoo_file); X setutime (zoo_path, latest_date, latest_time); #else #if 0 X fflush (zoo_file); /* superstition: might help time stamp */ #endif X settime (zoo_file, latest_date, latest_time); #endif X } X #ifndef NIXTIME zooclose (zoo_file); #endif X if (file_deleted && pack) { /* pack if files were deleted and user asked */ X prterror ('M', "-----\nPacking..."); X zoopack (zoo_path, "PP"); X prterror ('M', "done\n"); } X } SHAR_EOF chmod 0644 zoodel.c || echo 'restore of zoodel.c failed' Wc_c="`wc -c < 'zoodel.c'`" test 9676 -eq "$Wc_c" || echo 'zoodel.c: original size 9676, current size' "$Wc_c" fi true || echo 'restore of zooext.c failed' echo End of part 13, continue with part 14 exit 0