home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3618 < prev    next >
Encoding:
Internet Message Format  |  1991-07-11  |  46.2 KB

  1. From: dhesi@bsu-cs.bsu.edu (Rahul Dhesi)
  2. Newsgroups: alt.sources
  3. Subject: zoo 2.1 source part 13/15
  4. Message-ID: <12780@bsu-cs.bsu.edu>
  5. Date: 10 Jul 91 11:35:14 GMT
  6.  
  7. Checksum: 2821476663 (verify with "brik -cv")
  8. Submitted-by: dhesi@bsu-cs.bsu.edu
  9. Archive-name: zoo210/part13
  10.  
  11. ---- Cut Here and feed the following to sh ----
  12. #!/bin/sh
  13. # This is part 13 of zoo210
  14. # ============= zooadd.c ==============
  15. if test -f 'zooadd.c' -a X"$1" != X"-c"; then
  16.     echo 'x - skipping zooadd.c (File already exists)'
  17. else
  18. echo 'x - extracting zooadd.c (Text)'
  19. sed 's/^X//' << 'SHAR_EOF' > 'zooadd.c' &&
  20. #ifndef LINT
  21. /* derived from: zooadd.c 2.34 88/08/15 10:53:11 */
  22. static char sccsid[]="$Source: /usr/home/dhesi/zoo/RCS/zooadd.c,v $\n\
  23. $Id: zooadd.c,v 1.10 91/07/08 23:48:39 dhesi Exp $";
  24. #endif /* LINT */
  25. X
  26. /*
  27. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  28. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  29. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  30. */
  31. #include "options.h"
  32. /* Adds files specified in parameter-list to archive zoo_path. */
  33. X
  34. #define LONGEST    20                    /* assumed length of longest filename */
  35. #include "zoomem.h"             /* to define MAXADD */
  36. #include "zoo.h"
  37. #include "zooio.h"
  38. #include "various.h"
  39. #include "parse.h"
  40. #include "debug.h"
  41. X
  42. #include "portable.h"
  43. X
  44. #include "zoofns.h"
  45. #include "errors.i"
  46. extern int break_hit;
  47. extern int quiet;
  48. X
  49. void show_comment PARMS ((struct direntry *, ZOOFILE, int, char *));
  50. void dosname PARMS ((char *, char *));
  51. void modpath PARMS ((char *));
  52. void opts_add PARMS ((char *, int *, int *, int *, int *, int *, int *,
  53. X               int *, int *, int *, int *, int *, int *, int *, int *));
  54. int ver_too_high PARMS ((struct zoo_header *));
  55. void get_comment PARMS ((struct direntry *, ZOOFILE, char *));
  56. void copyfields PARMS ((struct direntry *, struct tiny_header *));
  57. void storefname PARMS ((struct direntry *, char *, int));
  58. char *choosefname PARMS ((struct direntry *));
  59. X
  60. extern struct zoo_header zoo_header;
  61. X
  62. extern char file_leader[];
  63. extern unsigned int crccode;
  64. X
  65. void zooadd(zoo_path, argc, argv, option)
  66. char *zoo_path;      /* pathname of zoo archive to add to */
  67. int argc;            /* how many filespecs supplied */
  68. char **argv;         /* array of pointers to filespecs */
  69. char *option;        /* option string */
  70. {
  71. char *whichname;                          /* which name to show user */
  72. char **flist;                                  /* list of ptrs to input fnames */
  73. int fptr;                                 /* will point to within flist */
  74. ZOOFILE this_file;                        /* file to add */
  75. char zoo_fname[LFNAMESIZE];               /* basename of archive itself */
  76. char zoo_bak[LFNAMESIZE];                 /* name of archive's backup */
  77. char this_fname[LFNAMESIZE];              /* just filename of file to add */
  78. char latest_name[LFNAMESIZE];             /* latest name in archive */
  79. long last_old = 0L;                       /* last direntry in old chain */
  80. ZOOFILE zoo_file;                         /* stream for open archive */
  81. char *this_path;                          /* pathname of file to add */
  82. X
  83. #ifdef NOENUM
  84. #define NEW_ZOO 1
  85. #define OLD_ZOO 2
  86. int zoo_status;
  87. #else
  88. enum {NEW_ZOO, OLD_ZOO} zoo_status;       /* newly created or not */
  89. #endif
  90. X
  91. long this_dir_offset;                     /* pointers to within archive */
  92. long save_position;                       /* pointer to within archive */
  93. long prev_pos;                            /* posn of prev file of same name */
  94. struct direntry direntry;                 /* directory entry */
  95. struct direntry dir2entry;                /* spare */
  96. int status;                               /* error status */
  97. int success;                              /* successful addition of file? */
  98. int addcount = 0;                         /* number added */
  99. int update=0;                             /* only files already in archive */
  100. int suppress=0;                           /* suppress compression */
  101. int new=0;                                /* add only files not in archive */
  102. int zootime = 0;                          /* just set archive time */
  103. int add_comment = 0;                      /* add comment */
  104. int add_global_comment = 0;                    /* archive comment */
  105. int pack = 0;                             /* pack after adding */
  106. int need_dir = 1;                         /* store directories too */
  107. int delcount = 0;                         /* count of deleted entries */
  108. int exit_status = 0;                      /* exit status to set */
  109. X
  110. unsigned int latest_date = 0;             /* to set time on archive itself */
  111. unsigned int latest_time = 0;             /* .. same */
  112. int move = 0;                             /* delete after adding to archive */
  113. int longest;                              /* length of longest pathname added */
  114. int firstfile = 1;                        /* first file being added? */
  115. int z_fmt = 0;                            /* look for Z format files? */
  116. int inargs = 0;                                    /* read filenames from stdin? */
  117. X
  118. #ifndef PORTABLE
  119. struct tiny_header tiny_header;           /* for Z format archives */
  120. #endif
  121. X
  122. unsigned this_version_no;                            /* version no. of old file */
  123. unsigned  high_vflag;                                /* version flag of old file */
  124. unsigned high_version_no;                            /* highest version no of this file */
  125. long high_pos;                                        /* offset of file w/highest ver no */
  126. unsigned int fgens;                                /* gens. to preserve -- file */
  127. unsigned int zgens;                                /* gens. to preserve -- archive */
  128. long oldcmtpos;                                    /* to save old comment */
  129. unsigned int oldcmtsiz;                            /* to save old comment */
  130. int genson = 0;                                    /* whether to turn generations on */
  131. X
  132. int use_lzh = 0;                                    /* whether to use lzh compression */
  133. X
  134. /* on entry option points to first letter */
  135. X
  136. opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack,
  137. X          &update, &add_comment, &z_fmt, &need_dir, &inargs, &genson,
  138. X             &use_lzh, &add_global_comment);
  139. X
  140. /* POSSIBLE RACE CONDITION BETWEEN TESTING EXISTENCE AND CREATING FILE */
  141. if (exists (zoo_path)) {
  142. X    zoo_file = zooopen (zoo_path, Z_RDWR);
  143. X   zoo_status = OLD_ZOO;
  144. } else {
  145. X   if (!zootime)
  146. X        zoo_file = zoocreate (zoo_path);
  147. X   else
  148. X      zoo_file = NOFILE;     /* don't create if just setting time */
  149. X   zoo_status = NEW_ZOO;
  150. }
  151. X
  152. if (zoo_file == NOFILE)
  153. X   prterror ('f', could_not_open, zoo_path);
  154. basename(zoo_path, zoo_fname);      /* get basename of archive */
  155. rootname (zoo_path, zoo_bak);       /* name without extension */
  156. strcat (zoo_bak, BACKUP_EXT);       /* name of backup of this archive */
  157. X
  158. /* Now we prepare the archive for adding one or more files.  If the archive
  159. has just been created, we write the archive header */
  160. X
  161. addfname ("",0L,0,0,0,0); /* initialize table of files already in archive */
  162. if (zoo_status == NEW_ZOO) {                 /* newly-created archive */
  163. X    if (genson)                                            /* if no generations needed */
  164. X        zoo_header.vdata = (VFL_ON|GEN_DEFAULT); /* generations on */
  165. X   fwr_zooh (&zoo_header, zoo_file);
  166. X    zgens = GEN_DEFAULT;
  167. X   zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  168. } else {
  169. X   /* read header and rewrite with updated version numbers, preserving
  170. X       header type */
  171. X   rwheader (&zoo_header, zoo_file, 1);
  172. X    zgens = zoo_header.vdata & VFL_GEN;            /* get archive generations */
  173. X   /* initialize latest_name to null string */
  174. X    /* NOTE:  latest_name is not currently used for anything, but
  175. X        may be used in the future for inserting files into the
  176. X        archive in alphabetic order. */
  177. X   *latest_name = '\0';
  178. X
  179. X   /* Skip existing files but add them to a list.  The variable last_old 
  180. X   gets the tail of the old chain of directory entries */
  181. X   skip_files (zoo_file, &latest_date, &latest_time, &delcount, 
  182. X               latest_name, &last_old);
  183. }
  184. /* The file pointer is now positioned correctly to add a file to archive, 
  185. unless the null directory entry is too short.  This will be fixed below. */
  186. X
  187. /* If we are just setting time, do it and run. */
  188. if (zootime) {
  189. #ifdef NIXTIME
  190. X   zooclose (zoo_file);
  191. X   setutime (zoo_path, latest_date, latest_time);
  192. #else
  193. X   settime (zoo_file, latest_date, latest_time);
  194. X   zooclose (zoo_file);
  195. #endif
  196. X   prterror ('m', "Archive time adjusted.\n");
  197. X   zooexit (0);
  198. }
  199. X
  200. /* make list of files, excluding archive and its backup */
  201. longest = LONGEST;
  202. flist = (char **) ealloc(MAXADD);
  203. if (!inargs) {
  204. X   makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest);
  205. X   /*                                    ^^         ^^       ^^ exclude */
  206. }
  207. X
  208. fptr = 0;    /* ready to get filename (if makelist() was called) or to
  209. X                    begin adding filenames (if reading them from stdin) */
  210. X
  211. while (1) {
  212. X   unsigned int this_date, this_time;
  213. X   int INLIST; /* boolean */
  214. X   int RECENT; /* boolean */
  215. X   int danger; /* if update requested and disk copy is out of date */
  216. X    if (inargs) {
  217. X    again: /* loop back if filename was same as archive name or its backup */
  218. X        this_path = getstdin();            /* pathname from stdin, in static area */
  219. X        if (this_path != NULL) {
  220. X            if (samefile (nameptr(zoo_fname),nameptr(this_path)) ||
  221. X                        samefile (nameptr(zoo_bak),nameptr(this_path)))
  222. X                goto again;                 /* don't add archive to itself */
  223. X            modpath (this_path);
  224. X        /* if moving files, add to list for later deletion;  if list overflows,
  225. X            terminate addition loop and give warning message */
  226. X            if (move) {
  227. X                if (fptr >= MAXADD-2) {
  228. X                    prterror ('w', too_many_files, MAXADD-2);
  229. X                    this_path = NULL;
  230. X                } else
  231. X                    flist[fptr++] = str_dup (this_path);
  232. X            }
  233. X        }
  234. X    } else  {
  235. X        this_path = flist[fptr++];
  236. X    }
  237. X    /* exit the addition loop when no more pathnames are left */
  238. X    if (this_path == NULL) {
  239. X        /* in case stdin was being read, make sure flist is NULL-terminated */
  240. X        flist[fptr] = NULL;
  241. X        break;
  242. X    }
  243. X
  244. X   basename (this_path, this_fname);   /* get just filename for later */
  245. X
  246. X   this_file = zooopen(this_path, Z_READ);
  247. X   if (this_file == NOFILE) {
  248. X      prterror ('e', could_not_open, this_path);
  249. X      exit_status++;
  250. X      continue;
  251. X   }
  252. X
  253. #ifndef PORTABLE
  254. X   /* Test to see if this is a Z format file.  We assume the file is Z format
  255. X      if (a) tag is correct and (b) type is 1 and (c) embedded filename
  256. X      is not longer than FNAMESIZE.  
  257. X   */
  258. X   if (z_fmt) {
  259. X      zooread (this_file, (char *) &tiny_header, sizeof(tiny_header));
  260. X      if (tiny_header.tinytag == TINYTAG && tiny_header.type == 1 &&
  261. X                        strlen (tiny_header.fname) <= FNAMESIZE)
  262. X          /* ok */ ;
  263. X      else {
  264. X         zooclose (this_file);
  265. X         prterror ('e', "File %s does not have Z format.\n", this_fname);
  266. X         exit_status++;
  267. X         continue;
  268. X      }
  269. X   }
  270. #endif
  271. X
  272. X   /* get file time;  also fix name */
  273. #ifndef PORTABLE
  274. X   if (z_fmt) {
  275. X      direntry.date = tiny_header.date;
  276. X      direntry.time = tiny_header.time;
  277. X      strcpy (direntry.fname, tiny_header.fname);
  278. X      direntry.dirlen = direntry.namlen = 0;
  279. X   } else {
  280. #endif
  281. X
  282. X      /* Get timstamp of file being added */
  283. #ifdef GETUTIME
  284. X      getutime (this_path, &direntry.date, &direntry.time);
  285. #else
  286. X      gettime (this_file, &direntry.date, &direntry.time);
  287. #endif
  288. X
  289. X        /* save file attributes */
  290. #ifdef FATTR
  291. X        /* we expect getfattr() to set all attr. bits;  currently
  292. X            only the portable format is recognized */
  293. X        {
  294. # ifdef FATTR_FNAME
  295. X            unsigned long getfattr PARMS ((char *);
  296. X            direntry.fattr = getfattr (this_path);
  297. # else
  298. X            unsigned long getfattr PARMS ((ZOOFILE));
  299. X            direntry.fattr = getfattr (this_file);
  300. # endif /* FATTR_FNAME */
  301. X        }
  302. #else
  303. X            direntry.fattr = NO_FATTR;    /* none */
  304. #endif /* FATTR */
  305. X
  306. #ifdef FOLD
  307. X      str_lwr(this_fname);
  308. #endif
  309. X      dosname (this_fname, direntry.fname);  /* MSDOS filename */
  310. X
  311. X   /*
  312. X   Store long filename into direntry.lfname iff it is different from MSDOS
  313. X   filename.  Also store directory name if need_dir is true.  Moved out of 
  314. X   zooadd() so zooadd() doesn't get too big for optimization.
  315. X   */
  316. X   storefname (&direntry, this_path, need_dir);
  317. X
  318. #ifndef PORTABLE
  319. X   }
  320. #endif
  321. X
  322. #ifdef DEBUG
  323. printf ("zooadd:  direntry.lfname = [%s]  direntry.dirname = [%s]\n",
  324. X                  direntry.lfname, direntry.dirname);
  325. #endif
  326. X
  327. X   /* if update option, then we add file if it is already in the archive 
  328. X      AND the archived file is older */
  329. X
  330. X   /* The following logic was derived from a Karnaugh map so it may
  331. X      be hard to understand.  Essentially, if U=update requested,
  332. X      N=new files requested, I=file is already in archive, and
  333. X      R=file being archived is more recent than file already in
  334. X      archive, then the boolean equation is:
  335. X
  336. X      add = U' (N' + I') + U (IR  + I'N)
  337. X   */
  338. X
  339. X   /* Get the filename to use for this addition.  */
  340. X   whichname = choosefname(&direntry);
  341. X
  342. X   /* Get position in archive of any old file of same name, ignoring
  343. X        any directory prefix if need_dir is not true.  Also get its
  344. X        date, time, version flag, and version number. */
  345. X   prev_pos = inlist (fullpath (&direntry), &this_date, &this_time, 
  346. X                &this_version_no, &high_vflag, &high_version_no, 
  347. X                &high_pos, !need_dir);
  348. X
  349. /* define DBG_INLIST for debugging by printing values returned by inlist() */
  350. #ifdef DBG_INLIST
  351. X    printf ("FROM inlist(): prev_pos=%ld, high_pos=%ld\n", prev_pos, high_pos);
  352. X    printf ("this_version_no=%u, high_vflag=%4x, high_version_no=%u\n",
  353. X                this_version_no,    high_vflag,     high_version_no);
  354. #endif
  355. X
  356. X   INLIST = prev_pos > 0;  /* already in archive if positive value */
  357. X   if (INLIST) {
  358. X      int result;
  359. X      result = cmpnum (direntry.date, direntry.time, this_date, this_time);
  360. X      RECENT = result > 0;
  361. X      danger = result < 0;
  362. X   } else
  363. X      danger = 0; /* And RECENT is undefined and should not be used */
  364. X
  365. X   if (
  366. X         !update && (!new || !INLIST) ||
  367. X         update && (INLIST && RECENT || !INLIST && new)
  368. X      )
  369. X         ;  /* then continue and add file */
  370. X   else {
  371. X      if (update && danger)
  372. X         prterror ('w', "Archived copy of %s is newer.\n", whichname);
  373. X      zooclose (this_file);
  374. X      continue;   /* cycle back, skip this file */
  375. X   }
  376. X
  377. #ifdef CHEKDIR
  378. X   /* Don't add if this is a directory */
  379. X   if (isadir (this_file)) {
  380. X      zooclose (this_file);
  381. X      continue;
  382. X   }
  383. #else
  384. # ifdef CHEKUDIR
  385. X   /* Don't add if this is a directory */
  386. X   if (isuadir (this_path)) {
  387. X      zooclose (this_file);
  388. X      continue;
  389. X   }
  390. # endif /* CHEKUDIR */
  391. #endif /* CHEKDIR */
  392. X
  393. X   /* Create directory entry for new file (but don't add just yet) */
  394. X   /* NOTE:  we already got file date and time above for update option */
  395. X    /* add tag, type, timezone, struc, system_id, and var_dir_len */
  396. X    newdir (&direntry);
  397. X
  398. X    if (!genson && zoo_status == NEW_ZOO || 
  399. X                (zoo_header.vdata & VFL_ON) == 0) {
  400. X        direntry.vflag = 0;
  401. X        direntry.version_no = 0;
  402. X    }
  403. X
  404. X   /* 
  405. X   Write a null direntry entry.  Thus, if an error occurs or the program
  406. X   is interrupted, the end of the archive will still be meaningful.
  407. X   Special check needed for first one written.
  408. X   */
  409. X
  410. X   direntry.next = direntry.offset = 0L;     /* trailing null entry */
  411. X   this_dir_offset = zootell (zoo_file);
  412. X   if (!firstfile) {
  413. X      writedir (&direntry, zoo_file);
  414. X   } else {
  415. X      /*
  416. X      Before adding the first file to the archive, we must make sure that
  417. X      the previous directory chain (if any) is properly terminated with a
  418. X      null entry of the right size.  If this is a new archive, we simply
  419. X      write a new null entry of the right size.  If this is an existing
  420. X      archive, we must check the size of the previous trailing null entry. 
  421. X      If it is too small, we will back up to the most recent real directory
  422. X      entry and change its .next field to point to end of file.  
  423. X      */
  424. X
  425. X      if (zoo_status == NEW_ZOO) {
  426. X         writedir (&direntry, zoo_file);        /* write null dir entry */
  427. X      } else {
  428. X         struct direntry tmpentry;
  429. X         long tmppos;
  430. X         int oldlen, newlen;
  431. X         tmppos = zootell (zoo_file);
  432. X         frd_dir (&tmpentry, zoo_file);
  433. #define  DIRLEN(x)   ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len))
  434. X         oldlen = DIRLEN(tmpentry);             /* get length of direntry */
  435. X         newlen = DIRLEN(direntry);             /* ditto */
  436. X
  437. X         if (newlen > oldlen) {                 /* trouble */
  438. X            zooseek (zoo_file, last_old, 0);    /* back to previous entry */
  439. X            frd_dir (&tmpentry, zoo_file);
  440. X            zooseek (zoo_file, 0L, 2);          /* get EOF position */
  441. X            tmpentry.next = zootell (zoo_file);    /* point to EOF */
  442. X            zooseek (zoo_file, last_old, 0);    /* back to previous entry */
  443. X            writedir (&tmpentry, zoo_file);     /* update it */
  444. X            zooseek (zoo_file, 0L, 2);          /* to EOF ... */
  445. X            this_dir_offset = zootell (zoo_file);
  446. X            writedir (&direntry, zoo_file);     /* ...write null dir entry */
  447. X         } else
  448. X            zooseek (zoo_file, tmppos, 0);      /* long enough -- let it be */
  449. X      } /* if (zoo_status == NEW_ZOO) ... */
  450. X   } /* if (!firstfile) ... */
  451. X
  452. X   /* Now `this_dir_offset' is where the next directory entry will go */
  453. X
  454. X   /* first file added goes at EOF to avoid overwriting comments */
  455. X   if (firstfile) {
  456. X      zooseek (zoo_file, 0L, 2);                   /* EOF */
  457. X      direntry.offset = zootell (zoo_file) + SIZ_FLDR;
  458. X   } else {
  459. X      direntry.offset = this_dir_offset + SIZ_DIRL + 
  460. X         direntry.var_dir_len + SIZ_FLDR;
  461. X   }
  462. X
  463. X    if (use_lzh) {
  464. X        direntry.major_ver = MAJOR_LZH_VER;    /* minimum version number needed */
  465. X        direntry.minor_ver = MINOR_LZH_VER;    /* .. to extract */
  466. X    } else {
  467. X        direntry.major_ver = MAJOR_EXT_VER;    /* minimum version number needed */
  468. X        direntry.minor_ver = MINOR_EXT_VER;    /* .. to extract */
  469. X    }
  470. X   direntry.deleted = 0;               /* not deleted, naturally */
  471. X   direntry.comment = 0L;              /* no comment (yet) */
  472. X   direntry.cmt_size = 0;          /* .. so no size either */
  473. X
  474. X   save_position = direntry.offset;          /* save position in case of error */
  475. X
  476. X   (void) zooseek (zoo_file, direntry.offset - SIZ_FLDR, 0);
  477. X   (void) zoowrite (zoo_file, file_leader, SIZ_FLDR);
  478. X
  479. #ifdef PORTABLE
  480. X   prterror ('m', "%-*s -- ", longest, this_path);
  481. #else
  482. X   if (z_fmt)
  483. X      prterror ('m', "%-12s <== %-*s -- ", 
  484. X         direntry.fname, longest, this_path);
  485. X   else
  486. X      prterror ('m', "%-*s -- ", longest, this_path);
  487. X
  488. #endif /* PORTABLE */
  489. X
  490. X   crccode = 0;
  491. #ifndef PORTABLE
  492. X   if (z_fmt) 
  493. X    {
  494. X      direntry.packing_method = tiny_header.packing_method;
  495. X      zooseek (this_file, (long) (sizeof(tiny_header)+tiny_header.cmt_size), 0);
  496. X      status = getfile (this_file, zoo_file, tiny_header.size_now, 1);
  497. X   } else 
  498. #endif
  499. X    if (suppress) {                    /* suppress compression */
  500. X      direntry.packing_method = 0;           /* no compression */
  501. X      status = getfile (this_file, zoo_file, -1L, 1);
  502. X   } else {
  503. #ifdef UNBUF_IO    /* unbuffered I/O */
  504. X        long lseek PARMS ((int, long, int));
  505. X        long tell PARMS ((int));
  506. X        int this_fd, zoo_fd;
  507. #endif
  508. X        if (use_lzh)
  509. X            direntry.packing_method = 2;
  510. X        else
  511. X            direntry.packing_method = 1;
  512. #ifdef UNBUF_IO
  513. #include "UNBUF_IO not currently supported"
  514. X        this_fd = fileno (this_file);                        /* get ..                    */
  515. X        zoo_fd = fileno (zoo_file);                        /* ... file descriptors    */
  516. X        zooseek (zoo_file, zootell (zoo_file), 0);    /* synch */
  517. X        zooseek (this_file, zootell (this_file), 0);    /* synch */
  518. X      status = lzc(this_fd, zoo_fd);                    /* add with compression */
  519. X        zooseek (zoo_file, tell (zoo_fd), 0);            /* resynch    */
  520. X        zooseek (this_file, tell (this_fd), 0);        /* resynch    */
  521. #else
  522. X        if (use_lzh)
  523. X            status = lzh_encode(this_file, zoo_file);
  524. X        else
  525. X            status = lzc(this_file, zoo_file);
  526. #endif /* UNBUF_IO */
  527. X
  528. X   }
  529. X   if (status != 0) { /* if I */
  530. X      ++exit_status;                         /* remember error */
  531. X      if (status == 1)
  532. X         prterror ('F', no_memory);
  533. X      else if (status == 2)
  534. X         prterror ('F', disk_full);
  535. X      else if (status == 3)
  536. X         prterror ('F', "Read error.\n");
  537. X      else
  538. X         prterror ('F', internal_error);
  539. X      success = 0;
  540. X   } else {
  541. X      direntry.next  = zootell (zoo_file);
  542. X      direntry.size_now = direntry.next - direntry.offset;
  543. X
  544. X      /* find and store original size of file just compressed */
  545. /*DEBUG VMS*/ zooseek (this_file, 0L, 2);    /* seek to EOF */
  546. X
  547. X      direntry.org_size = zootell (this_file);  /* should be EOF already */
  548. X
  549. X      /* If the compressed one is bigger, just copy */
  550. X
  551. X      if (direntry.size_now >= direntry.org_size &&   /* if II */
  552. X            direntry.packing_method != 0) {
  553. X         zooseek (zoo_file, save_position, 0);  /* ..restore file pointer */
  554. X         zootrunc (zoo_file);                   /* ..truncate file */
  555. X         direntry.packing_method = 0;           /* ..and just copy */
  556. X         zooseek (this_file, 0L, 0);            /* (but rewind first!) */
  557. X         crccode = 0;                           /* re-start crc from 0 */
  558. X         status = getfile (this_file, zoo_file, -1L, 1);
  559. X         if (status != 0) {  /* if III */
  560. X            success = 0;
  561. X            printf (disk_full);
  562. X            exit_status++;
  563. X         } else {
  564. X            success = 1;
  565. X            direntry.next  = zootell (zoo_file);
  566. X            direntry.size_now = direntry.next - direntry.offset;
  567. X         } /* end if III */
  568. X      } else {
  569. X         success = 1;
  570. X      } /* end if II */
  571. X
  572. X   } /* end if I */
  573. X
  574. X   if (success) {                               /* file successfully added */
  575. X      addcount++;                               /* how many added */
  576. X      direntry.file_crc = crccode;
  577. X
  578. X      /* remember most recent date and time */
  579. X      if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) {
  580. X            latest_date = direntry.date;
  581. X            latest_time = direntry.time;
  582. X      }
  583. X
  584. #if 0
  585. X      /* mark any previous version of this file in archive as deleted */
  586. X      dir2entry.comment = 0L;       /* for later use assigning to direntry */
  587. X      dir2entry.cmt_size = 0;
  588. #endif
  589. X
  590. X      if (!z_fmt)
  591. X         prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now));
  592. X
  593. X        oldcmtsiz = 0;                                /* assume no old comment */
  594. X        oldcmtpos = 0L;
  595. X
  596. X      if (prev_pos > 0) {                                        /* in archive */
  597. X            int delold = 0;                                        /* delete old? */
  598. X            /* if versions active both archive-wide and for file */
  599. X            if ((zoo_header.vdata & VFL_ON) && (high_vflag & VFL_ON)) {
  600. X                /* next test is optimization, to avoid redundant I/O */
  601. X                if (high_pos != prev_pos || this_version_no == 1) {
  602. X                    /* prev highest is no longer highest so adjust vflag */
  603. X                    long save_pos = zootell (zoo_file);            /*DEBUG*/
  604. X                    zooseek (zoo_file, high_pos, 0);
  605. X                    readdir (&dir2entry, zoo_file, 1);
  606. X                    oldcmtpos = dir2entry.comment;
  607. X                    oldcmtsiz = dir2entry.cmt_size;
  608. X                    dir2entry.vflag &= (~VFL_LAST);                /* no longer highest */
  609. X                    zooseek (zoo_file, high_pos, 0);
  610. X                    writedir (&dir2entry, zoo_file);
  611. X                    zooseek (zoo_file, save_pos, 0);                /*DEBUG*/
  612. X                }
  613. X
  614. X                direntry.version_no = high_version_no + 1; /* ..one higher */
  615. X                direntry.vflag = high_vflag;
  616. X                /* now see if we need to delete older version */
  617. X                fgens = high_vflag & VFL_GEN;
  618. X                if (fgens == 0)
  619. X                    fgens = zgens;
  620. X                if (zgens != 0 && zgens < fgens)
  621. X                    fgens = zgens;
  622. X                if (fgens != 0 && direntry.version_no - this_version_no >= fgens) {
  623. X                    delold = 1;
  624. X                    prterror ('M', "replaced+\n");
  625. X                } else
  626. X                    prterror ('M', "added+\n");
  627. X            } else {
  628. X                prterror ('M', "replaced\n");
  629. X                delold = 1;
  630. X            }
  631. X
  632. X            if (delold) {                                            /* deleting old file */
  633. X                long save_pos = zootell (zoo_file);            /*DEBUG*/
  634. X                ++delcount;                                            /* remember to pack */
  635. X                zooseek (zoo_file, prev_pos, 0);
  636. X                readdir (&dir2entry, zoo_file, 1);
  637. X                if (dir2entry.cmt_size != 0) {        /* propagate latest comment */
  638. X                    oldcmtpos = dir2entry.comment;
  639. X                    oldcmtsiz = dir2entry.cmt_size;
  640. X                }
  641. X                dir2entry.deleted = 1;                            /* mark as deleted */
  642. X                /* following line is optimization if only 1 generation */
  643. X                dir2entry.vflag &= (~VFL_LAST);                /* no longer highest */
  644. X                zooseek (zoo_file, prev_pos, 0);
  645. X                writedir (&dir2entry, zoo_file);
  646. X                zooseek (zoo_file, save_pos, 0);                /*DEBUG*/
  647. X            }
  648. X      } else                                                         /* not in archive */
  649. X            prterror ('M', "added\n");
  650. X
  651. X      /* Preserve any old comment if we replaced or superseded the file */
  652. X      direntry.comment = oldcmtpos;
  653. X      direntry.cmt_size = oldcmtsiz;
  654. X
  655. #ifndef PORTABLE
  656. X      /* Copy comment if any from Z format file */
  657. X      if (z_fmt && tiny_header.cmt_size != 0) {
  658. X         zooseek (this_file, (long) sizeof(tiny_header), 0); /* to comment */
  659. X         direntry.comment = zootell (zoo_file);
  660. X         direntry.cmt_size = tiny_header.cmt_size;
  661. X         /* 4th param is 0 for no CRC */
  662. X         getfile (this_file, zoo_file, (long) tiny_header.cmt_size, 0);
  663. X         direntry.next = zootell (zoo_file);
  664. X      } 
  665. #endif
  666. X
  667. X      /* if user requested comments, any previous comment in a Z format
  668. X         file may now be manually overwritten */
  669. X      if (add_comment && !feof (stdin)) {
  670. X         show_comment (&direntry, zoo_file, 1, whichname);
  671. X         get_comment (&direntry, zoo_file, this_path);
  672. X         direntry.next = zootell (zoo_file);    /* update .next ptr */
  673. X      } /* end if */
  674. X
  675. #ifndef PORTABLE
  676. X      /* if adding Z format archive, copy relevant fields from its header */
  677. X      if (z_fmt) {   /* moved out to shorten code & allow optimizer to work */
  678. X         copyfields (&direntry, &tiny_header);
  679. X      }
  680. #endif
  681. X
  682. X      debug((printf ("zooadd:  our new .next = [%lx].\n", direntry.next)))
  683. X
  684. X      {
  685. X         long savepos = zootell (zoo_file);    /* save position */
  686. X         zooseek (zoo_file, this_dir_offset, 0);
  687. X         writedir (&direntry, zoo_file);
  688. X         zooseek (zoo_file, savepos, 0);    /* restore position */
  689. X      }
  690. X
  691. X   } else {                               /* file was not properly added */
  692. X      zooseek (zoo_file, save_position, 0);  /* ..restore file pointer */
  693. X      zootrunc (zoo_file);                   /* ..truncate file */
  694. X   } /* end if */
  695. X   zooclose (this_file);
  696. if (!success)
  697. X   break;
  698. firstfile = 0;
  699. } /* end for */
  700. X
  701. save_position = zootell (zoo_file);
  702. X
  703. /* Write a null direntry entry */
  704. zooseek (zoo_file, save_position, 0);
  705. writenull (zoo_file, MAXDIRSIZE);
  706. zootrunc (zoo_file);  /* truncate */
  707. X
  708. #ifdef NIXTIME
  709. zooclose (zoo_file);
  710. setutime (zoo_path, latest_date, latest_time);
  711. #else
  712. settime (zoo_file, latest_date, latest_time);
  713. zooclose (zoo_file);
  714. #endif
  715. X
  716. if (!addcount) {                    /* no files added */
  717. X   prterror ('m', "No files added.\n");
  718. X   if (zoo_status == NEW_ZOO)
  719. X      unlink (zoo_path);
  720. } else {
  721. X   if (delcount && pack) { /* pack if user asked and found deleted entries */
  722. X      prterror ('M', "-----\nPacking...");
  723. X      zoopack (zoo_path, "PP");
  724. X      prterror ('M', "done\n");
  725. X   }
  726. X
  727. X   /* If files to move & we added some and no error so far, delete originals */
  728. X   if (move && !exit_status)
  729. X      if (kill_files (flist, longest) != 0)
  730. X         exit_status++;
  731. }
  732. X
  733. /* right here we handle archive comment */
  734. if (add_global_comment) {
  735. X    comment(zoo_path, "_A");
  736. X    add_global_comment = 0;
  737. }
  738. X
  739. if (exit_status)
  740. X   zooexit (1);
  741. } /* end zoo_add */
  742. SHAR_EOF
  743. chmod 0644 zooadd.c ||
  744. echo 'restore of zooadd.c failed'
  745. Wc_c="`wc -c < 'zooadd.c'`"
  746. test 26320 -eq "$Wc_c" ||
  747.     echo 'zooadd.c: original size 26320, current size' "$Wc_c"
  748. fi
  749. # ============= zooadd2.c ==============
  750. if test -f 'zooadd2.c' -a X"$1" != X"-c"; then
  751.     echo 'x - skipping zooadd2.c (File already exists)'
  752. else
  753. echo 'x - extracting zooadd2.c (Text)'
  754. sed 's/^X//' << 'SHAR_EOF' > 'zooadd2.c' &&
  755. #ifndef LINT
  756. /* derived from: zooadd2.c 2.14 88/01/27 10:40:32 */
  757. static char sccsid[]="$Id: zooadd2.c,v 1.5 91/07/04 13:33:55 dhesi Exp $";
  758. #endif /* LINT */
  759. X
  760. /*
  761. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  762. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  763. */
  764. #include "options.h"
  765. #include "zoo.h"
  766. #ifndef    OK_STDIO
  767. #include <stdio.h>
  768. #define    OK_STDIO
  769. #endif
  770. #include "various.h"
  771. #include "zooio.h"
  772. #include "zoofns.h"
  773. #include "errors.i"
  774. #include "assert.h"
  775. #include "debug.h"
  776. #include "parse.h"
  777. X
  778. /*
  779. Miscellaneous routines to support zooadd().
  780. */
  781. X
  782. /****************
  783. This function is called with zoo_file positioned to the first
  784. directory entry in an archive.  It skips past all existing files,
  785. counts the number of deleted files, saves the latest data and time
  786. encountered, and adds all filenames encountered to a global list. The
  787. long filename is added if available, else the MSDOS filename is
  788. added.  
  789. */
  790. X
  791. void skip_files (zoo_file, latest_date, latest_time, delcount, 
  792. X                  latest_name, latest_pos)
  793. ZOOFILE zoo_file;
  794. unsigned int *latest_date, *latest_time;
  795. int *delcount;
  796. char latest_name[];
  797. long *latest_pos;
  798. {
  799. X   long save_offset, next_ptr;
  800. X   struct direntry direntry;
  801. X   struct direntry *drp = &direntry;
  802. X
  803. X   *latest_pos = 0L;
  804. X   do {
  805. X      /* read a directory entry */
  806. X      save_offset = zootell (zoo_file);     /* save pos'n of this dir entry */
  807. X      readdir (&direntry, zoo_file, 1);   /* read directory entry */
  808. X      if (drp->next == 0L) {                 /* END OF CHAIN */
  809. X         zooseek (zoo_file, save_offset, 0);      /* back up */
  810. X         break;                                 /* EXIT on end of chain */
  811. X      } else
  812. X         *latest_pos = save_offset;
  813. X      /* remember most recent date and time, for files not marked deleted */
  814. X      if (!drp->deleted)
  815. X         if (drp->date > *latest_date ||
  816. X            (drp->date == *latest_date && drp->time > *latest_time)) {
  817. X               *latest_date = drp->date;
  818. X               *latest_time = drp->time;
  819. X         }
  820. X      next_ptr = drp->next;            /* ptr to next dir entry */
  821. X      if (drp->deleted)
  822. X         ++(*delcount);                      /* count deleted entries */
  823. X      /* add name of file and position of direntry into global list */
  824. X      /* but only if the entry is not deleted */
  825. X      if (!drp->deleted) {
  826. #ifdef FOLD
  827. X            /* IS THIS REALLY NEEDED?  IF SO, WHAT ABOUT drp->lfname? */
  828. X         str_lwr(drp->fname);
  829. #endif
  830. X            /* add full pathname to global list */
  831. X            strcpy (latest_name, fullpath (drp));
  832. X         addfname (latest_name, save_offset, drp->date, drp->time,
  833. X                            drp->vflag, drp->version_no);
  834. X      }
  835. X      zooseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  836. X   } while (next_ptr != 0L);              /* loop terminates on null ptr */
  837. }
  838. X
  839. /*******************/
  840. /* kill_files() deletes all files in the supplied list of pointers to
  841. filenames */
  842. X
  843. int kill_files (flist, pathlength)
  844. char *flist[];                      /* list of ptrs to input fnames */
  845. int pathlength;                     /* length of longest pathname */
  846. {
  847. X   int status = 0;
  848. X   int fptr;
  849. X   prterror ('M', "-----\nErasing added files...\n");
  850. X   for (fptr = 0;  flist[fptr] != NULL; fptr++) {
  851. #ifdef CHEKUDIR
  852. X                if (isuadir(flist[fptr]))
  853. X                    continue;
  854. #else /* CHEKUDIR */
  855. # ifdef CHEKDIR
  856. X                if (isfdir(flist[fptr]))
  857. X                    continue;
  858. # endif /* CHEKDIR */
  859. #endif /* CHEKUDIR */
  860. X      prterror ('m', "%-*s -- ", pathlength, flist[fptr]);
  861. X      if (unlink (flist[fptr]) == 0) {
  862. X         prterror ('M', "erased\n");
  863. X      } else {
  864. X         prterror ('w', "Could not erase %s.\n", flist[fptr]);
  865. X         status = 1;
  866. X      }
  867. X   }
  868. X   return (status);
  869. }
  870. X
  871. #ifndef PORTABLE
  872. /*******************/
  873. void copyfields (drp, thp)
  874. struct direntry *drp;
  875. struct tiny_header *thp;
  876. {
  877. X   drp->org_size = thp->org_size;
  878. X   drp->file_crc = thp->file_crc;
  879. X   drp->size_now = thp->size_now;
  880. X   drp->major_ver = thp->major_ver;
  881. X   drp->minor_ver = thp->minor_ver;
  882. }
  883. #endif
  884. X
  885. /*******************/
  886. /* processes option switches for zooadd() */
  887. void opts_add (option, zootime, quiet, suppress, move, new, pack,
  888. X          update, add_comment, z_fmt, need_dir, inargs, genson,
  889. X             use_lzh, arch_cmnt)
  890. char *option;
  891. int *zootime, *quiet, *suppress, *move, *new, *pack,
  892. X   *update, *add_comment, *z_fmt, *need_dir, *inargs,
  893. X    *genson, *use_lzh, *arch_cmnt;
  894. X
  895. {
  896. X   if (*option == 'T') {
  897. X      (*zootime)++;
  898. X      option++;
  899. X      while (*option) {
  900. X         switch (*option) {
  901. X            case 'q': (*quiet)++; break;
  902. X            default:
  903. X               prterror ('f', inv_option, *option);
  904. X         }
  905. X      option++;
  906. X      }
  907. X   }
  908. X   
  909. X   while (*option) {
  910. X      switch (*option) {
  911. X         case 'a': break;  
  912. X            case 'h': (*use_lzh)++; break;         /* use lzh compression */
  913. X         case 'f': (*suppress)++; break;        /* suppress compression */
  914. X         case 'M': (*move)++; break;            /* delete files after adding them */
  915. X         case 'n': (*new)++; break;             /* add only files not in archive */
  916. X         case 'P': (*pack)++; break;            /* pack after adding */
  917. X         case 'u': (*update)++;   break;        /* add only files already in archive */
  918. X         case 'q': (*quiet)++; break;           /* be quiet */
  919. X         case 'c': (*add_comment)++; break;     /* add comment */
  920. X         case ':': *need_dir = 0; break;        /* don't store directories */
  921. X         case 'I': (*inargs)++; break;            /* get filenames from stdin */
  922. X         case 'C': (*arch_cmnt)++; break;            /* do an archive comment */
  923. /* #ifdef PORTABLE */ /* avoid Turbo C warning about unused param */
  924. X         case 'z': (*z_fmt)++; break;           /* look for Z format files */
  925. /* #endif */
  926. X            case '+': 
  927. X                        *genson = 1;  break;
  928. X            case '-': 
  929. X                        *genson = 0;  break;
  930. X         default:
  931. X            prterror ('f', inv_option, *option);
  932. X      }
  933. X      option++;
  934. X   } /* end while */
  935. X    if (*suppress && *use_lzh)
  936. X        prterror ('f', "\"f\" and \"h\" can't both be used\n");
  937. }
  938. X
  939. /* 
  940. Stores long filename into direntry.lfname iff it is different from MSDOS
  941. filename.  Also stores directory name if need_dir is true.  Moved out of 
  942. zooadd() so zooadd() doesn't get too big for optimization.
  943. */
  944. void storefname (direntry, this_path, need_dir)
  945. struct direntry *direntry;
  946. char *this_path;
  947. int need_dir;
  948. {
  949. X   struct path_st path_st;
  950. X   parse (&path_st, this_path);
  951. X   direntry->lfname[0] = '\0';
  952. X   direntry->namlen = 0;
  953. #ifdef SPECMOD
  954. X   specfname (path_st.lfname);
  955. X   specdir (path_st.dir);
  956. #endif
  957. X   if (strcmp(path_st.lfname,direntry->fname) != 0) {
  958. X         strcpy (direntry->lfname, path_st.lfname); /* full filename */
  959. X         direntry->namlen = strlen(direntry->lfname) + 1;
  960. X   }
  961. X   if (need_dir) {
  962. X      strcpy (direntry->dirname, path_st.dir);   /* directory name */
  963. X      direntry->dirlen = strlen(direntry->dirname) + 1;
  964. X      if (direntry->dirlen == 1) /* don't store trailing null alone */
  965. X         direntry->dirlen = 0;
  966. X   } else {
  967. X      direntry->dirname[0] = '\0';
  968. X      direntry->dirlen = 0;
  969. X   }
  970. }
  971. X
  972. /* 
  973. Function getsdtin() gets a pathname from standard input, cleans
  974. it if necessary by removing any following blanks/tabs and other
  975. junk, and returns it in a static area that is overwritten by each
  976. call.
  977. */
  978. char *getstdin()
  979. {
  980. X    char *chptr;                                    /* temp pointer */
  981. X    static char tempname[PATHSIZE];
  982. X    do {
  983. X        if (fgets (tempname, PATHSIZE, stdin) == NULL)
  984. X            return (NULL);
  985. X        /* remove trailing blank, tab, newline */
  986. X        for (chptr = tempname; *chptr != '\0';  chptr++) {
  987. X            if (
  988. X    /* PURIFY means remove trailing blanks/tabs and all subsequent chars */
  989. #ifdef PURIFY
  990. X                    *chptr == '\t' || *chptr == ' ' ||
  991. #endif
  992. X                    *chptr == '\n'                        /* always remove trailing \n */
  993. X                )
  994. X            {
  995. X    
  996. X                *chptr = '\0';
  997. X                break;
  998. X            }
  999. X        }
  1000. X    } while (*tempname == '\0');                /* get a nonempty line */
  1001. #ifdef FOLD
  1002. X    str_lwr (tempname);
  1003. #endif
  1004. X    return (tempname);
  1005. }
  1006. X
  1007. /* 
  1008. Function newdir() adds some default information to a directory entry.
  1009. This will be a new directory entry added to an archive.
  1010. */
  1011. void newdir (direntry)
  1012. register struct direntry *direntry;
  1013. {
  1014. #ifdef GETTZ
  1015. X    long gettz();
  1016. #endif
  1017. X   direntry->zoo_tag = ZOO_TAG;
  1018. X   direntry->type = 2;                  /* type is now 2 */
  1019. #ifdef GETTZ
  1020. X    direntry->tz = (uchar) (gettz() / (15 * 60)); /* seconds => 15-min units */
  1021. #else
  1022. X   direntry->tz = NO_TZ;                /* timezone unknown */
  1023. #endif
  1024. X   direntry->struc = 0;                 /* unstructured file */
  1025. X   direntry->system_id = SYSID_NIX;     /* identify **IX filesystem */
  1026. X    direntry->vflag = VFL_ON|VFL_LAST;     /* latest version */
  1027. X    direntry->version_no = 1;                    /* begin with version 1 */
  1028. X    /* 1 for namlen, 1 for dirlen, 2 for system id, 3 for attributes,
  1029. X        1 for version flag and 2 for version number */
  1030. X   direntry->var_dir_len = direntry->dirlen + direntry->namlen + 10;
  1031. }
  1032. SHAR_EOF
  1033. chmod 0644 zooadd2.c ||
  1034. echo 'restore of zooadd2.c failed'
  1035. Wc_c="`wc -c < 'zooadd2.c'`"
  1036. test 8758 -eq "$Wc_c" ||
  1037.     echo 'zooadd2.c: original size 8758, current size' "$Wc_c"
  1038. fi
  1039. # ============= zoodel.c ==============
  1040. if test -f 'zoodel.c' -a X"$1" != X"-c"; then
  1041.     echo 'x - skipping zoodel.c (File already exists)'
  1042. else
  1043. echo 'x - extracting zoodel.c (Text)'
  1044. sed 's/^X//' << 'SHAR_EOF' > 'zoodel.c' &&
  1045. #ifndef LINT
  1046. /* @(#) zoodel.c 2.19 88/02/06 21:23:36 */
  1047. /*$Source: /usr/home/dhesi/zoo/RCS/zoodel.c,v $*/
  1048. /*$Id: zoodel.c,v 1.4 91/07/09 01:54:11 dhesi Exp $*/
  1049. static char sccsid[]="$Source: /usr/home/dhesi/zoo/RCS/zoodel.c,v $\n\
  1050. $Id: zoodel.c,v 1.4 91/07/09 01:54:11 dhesi Exp $";
  1051. #endif /* LINT */
  1052. X
  1053. /*
  1054. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  1055. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  1056. */
  1057. #include "options.h"
  1058. /* Deletes or undeletes entries from an archive.  choice=1 requests
  1059. X   deletion and choice=0 requests undeletion. */
  1060. #include "zoo.h"
  1061. #include "portable.h"
  1062. #ifndef    OK_STDIO
  1063. #include <stdio.h>
  1064. #define    OK_STDIO
  1065. #endif
  1066. #include "various.h" /* may not be needed */
  1067. #include "zooio.h"
  1068. #include "zoofns.h"
  1069. #include "errors.i"
  1070. X
  1071. #ifndef NOSIGNAL
  1072. #include <signal.h>
  1073. #endif
  1074. X
  1075. int needed PARMS((char *, struct direntry *, struct zoo_header *));
  1076. int ver_too_high PARMS((struct zoo_header *));
  1077. X
  1078. extern int quiet;
  1079. X
  1080. void zoodel (zoo_path, option, choice)
  1081. char *zoo_path;
  1082. char *option;
  1083. int choice;
  1084. {
  1085. #ifndef NOSIGNAL
  1086. X   T_SIGNAL (*oldsignal)();        /* to save previous SIGINT handler */
  1087. #endif
  1088. X   int delcount = 0;          /* how many entries we [un]deleted */
  1089. X   char matchname[PATHSIZE];  /* will hold full pathname */
  1090. X   register ZOOFILE zoo_file;
  1091. X   struct zoo_header zoo_header;
  1092. X   struct direntry direntry;
  1093. X   unsigned int latest_date = 0;      /* so we can set time of archive later */
  1094. X   unsigned int latest_time = 0;
  1095. X   int pack = 0;              /* pack after deletion? */
  1096. X   int file_deleted = 0;      /* any files deleted? */
  1097. X   int one = 0;               /* del/undel one file only */
  1098. X   int done;                  /* loop control */
  1099. X    int action;                        /* delete/undelete or adjust generation */
  1100. X    int subopt;                        /* sub option to action */
  1101. X    long gencount;                    /* generation count */
  1102. X    int doarchive = 0;            /* whether to adjust archive gen count */
  1103. X    unsigned valtoshow;            /* value to show in informative message */
  1104. X    int dodel = 0;                    /* selection of deleted files */
  1105. X    int selected;                    /* if current direntry selected */
  1106. X
  1107. /* values for action */
  1108. #define NO_ACTION    0    /* nothing */
  1109. #define DEL_UNDEL    1    /* delete or undelete file */
  1110. #define ADJ_LIM    2    /* adjust generation limit */
  1111. #define ADJ_GCNT    3    /* adjust generation count */
  1112. #define GEN_ON        4    /* turn on generations */
  1113. #define GEN_OFF        5    /* turn off generations */
  1114. X
  1115. /* values for subopt */
  1116. #define    SET        0
  1117. #define    INC        1
  1118. X
  1119. action = NO_ACTION;
  1120. if (*option == 'g') {
  1121. X    while (*(++option)) {
  1122. X        switch (*option) {
  1123. X            case 'A': doarchive = 1; break;
  1124. X            case 'q': quiet++; break;
  1125. X            case 'l': action = ADJ_LIM; break;
  1126. X            case 'c': action = ADJ_GCNT; break;
  1127. X            case '=':
  1128. X                subopt = SET; gencount = calc_ofs (++option);
  1129. X                if (action == ADJ_GCNT && gencount == 0)
  1130. X                    prterror ('f', "Generation count must be nonzero.\n");
  1131. X                goto opts_done;
  1132. X            case '+':
  1133. X                if (action == NO_ACTION) {
  1134. X                    if (option[1] =='\0') {
  1135. X                        action = GEN_ON;
  1136. X                        goto opts_done;
  1137. X                    } else
  1138. X                        prterror ('f', garbled);
  1139. X                } else {
  1140. X                    subopt = INC; gencount = calc_ofs (++option);
  1141. X                    goto opts_done;
  1142. X                }
  1143. X            case '-':
  1144. X                if (action == NO_ACTION) {
  1145. X                    if (option[1] =='\0') {
  1146. X                        action = GEN_OFF;
  1147. X                        goto opts_done;
  1148. X                    } else
  1149. X                        prterror ('f', garbled);
  1150. X                } else {
  1151. X                    subopt = INC; gencount = - calc_ofs (++option);
  1152. X                    goto opts_done;
  1153. X                }
  1154. X            case 'd':
  1155. X                dodel++; break;
  1156. X            default:
  1157. X                prterror ('f', garbled);
  1158. X        } /* end switch */
  1159. X    } /* end while */
  1160. X    /* if normal exit from while loop, it means bad command string */
  1161. X    prterror ('f', garbled);
  1162. X    opts_done:                             /* jump here from exit in while loop above */
  1163. X        if (action == NO_ACTION)
  1164. X            prterror ('f', garbled);
  1165. } else {
  1166. X    action = DEL_UNDEL;
  1167. X    while (*(++option)) {
  1168. X        switch (*option) {
  1169. X            case 'P': pack++; break;            /* pack after adding */
  1170. X            case 'q': quiet++; break;           /* be quiet */
  1171. X            case '1': one++; break;             /* del or undel only one file */
  1172. X            default:
  1173. X                prterror ('f', inv_option, *option);
  1174. X        }
  1175. X    } /* end while */
  1176. }
  1177. X
  1178. X   /* Open archive for read/write/binary access.  It must already exist */
  1179. X   if ((zoo_file = zooopen (zoo_path, Z_RDWR)) == NOFILE) {
  1180. X      prterror ('f', could_not_open, zoo_path);
  1181. X   }
  1182. X   
  1183. X   /* read archive header */
  1184. X   frd_zooh (&zoo_header, zoo_file);
  1185. X   if ((zoo_header.zoo_start + zoo_header.zoo_minus) != 0L)
  1186. X      prterror ('f', failed_consistency);
  1187. X   if (ver_too_high (&zoo_header))
  1188. X      prterror ('f', wrong_version, zoo_header.major_ver, zoo_header.minor_ver);
  1189. X
  1190. X    if (doarchive) {                                    /* manipulate archive gen val */
  1191. X        unsigned zoo_date, zoo_time;
  1192. #ifdef GETUTIME
  1193. X        getutime (zoo_path, &zoo_date, &zoo_time);    /* save archive timestamp */
  1194. #else
  1195. X        gettime (zoo_file, &zoo_date, &zoo_time);
  1196. #endif
  1197. X        if (zoo_header.type == 0)
  1198. X            prterror ('f', packfirst);
  1199. X        if (action == ADJ_LIM)    {
  1200. X            unsigned newgencount;    
  1201. X            if (subopt == SET)
  1202. X                newgencount = (unsigned) gencount;
  1203. X            else                                                                        /* INC */
  1204. X                newgencount = (zoo_header.vdata & VFL_GEN) + (unsigned) gencount;
  1205. X            newgencount &= VFL_GEN;            /* reduce to allowed bits */
  1206. X            zoo_header.vdata &= (~VFL_GEN);
  1207. X            zoo_header.vdata |= newgencount;
  1208. X            prterror ('M', "Archive generation limit is now %u\n", newgencount);
  1209. X        } else if (action == GEN_ON) {
  1210. X            zoo_header.vdata |= VFL_ON;
  1211. X            prterror ('M', "Archive generations on\n");
  1212. X        } else if (action == GEN_OFF) {
  1213. X            zoo_header.vdata &= (~VFL_ON);
  1214. X            prterror ('M', "Archive generations off\n");
  1215. X        } else 
  1216. X            prterror ('f', garbled);
  1217. X        zooseek (zoo_file, 0L, 0);        /* back to begining of file */
  1218. X        fwr_zooh (&zoo_header, zoo_file);
  1219. #ifdef NIXTIME
  1220. X        zooclose (zoo_file);
  1221. X        setutime (zoo_path, zoo_date, zoo_time);    /* restore archive timestamp */
  1222. #else
  1223. X        settime (zoo_file, zoo_date, zoo_time);
  1224. X        zooclose (zoo_file);
  1225. #endif
  1226. X        return;
  1227. X    }
  1228. X
  1229. X   zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  1230. X
  1231. X   done = 0;            /* loop not done yet */
  1232. X   while (1) {
  1233. X      long this_dir_offset;
  1234. X      this_dir_offset = zootell (zoo_file);   /* save pos'n of this dir entry */
  1235. X      frd_dir (&direntry, zoo_file);
  1236. X      if (direntry.zoo_tag != ZOO_TAG) {
  1237. X         prterror ('f', bad_directory);
  1238. X      }
  1239. X      if (direntry.next == 0L) {                /* END OF CHAIN */
  1240. X         break;                                 /* EXIT on end of chain */
  1241. X      }
  1242. X
  1243. X        /* select directory entry if it matches criteria */
  1244. X        selected = (
  1245. X                          (action == DEL_UNDEL && direntry.deleted != choice)
  1246. X                  ||
  1247. X                          (action != DEL_UNDEL &&
  1248. X                         (dodel && direntry.deleted ||
  1249. X                                (dodel < 2 && !direntry.deleted))
  1250. X                          )
  1251. X                      );
  1252. X
  1253. X        /* WARNING: convention of choice=1 for deleted entry must be same as
  1254. X        in direntry definition in zoo.h */
  1255. X    
  1256. X        /* Test for "done" so if "one" option requested, [un]del only 1 file */
  1257. X        /* But we go through the whole archive to adjust archive time */
  1258. X
  1259. X        strcpy (matchname, fullpath (&direntry));        /* get full pathname */
  1260. X        if (zoo_header.vdata & VFL_ON)
  1261. X            add_version (matchname, &direntry);            /* add version suffix */
  1262. X
  1263. X        if (!done && selected && needed(matchname, &direntry, &zoo_header)) {
  1264. X            prterror ('m', "%-14s -- ", matchname);
  1265. X            delcount++;
  1266. X            if (action == DEL_UNDEL) {
  1267. X                direntry.deleted = choice;
  1268. X                if (choice)
  1269. X                    file_deleted++;      /* remember if any files actually deleted */
  1270. X            } else {                            /* ADJ_LIM or ADJ_GENCNT */
  1271. X                if (direntry.vflag & VFL_ON) {        /* skip if no versions */
  1272. X                    if (action == ADJ_LIM) {
  1273. X                        unsigned newgencount;
  1274. X                        if (subopt == SET)
  1275. X                            newgencount = (unsigned) gencount;
  1276. X                        else                                                     /* INC */
  1277. X                            newgencount =
  1278. X                                (int) (direntry.vflag & VFL_GEN) + (int) gencount;
  1279. X                        newgencount &= VFL_GEN;
  1280. X                        direntry.vflag &= (~VFL_GEN);
  1281. X                        direntry.vflag |= newgencount;
  1282. X                        valtoshow = newgencount;
  1283. X                    } else {                                                    /* ADJ_GCNT */
  1284. X                        if (subopt == SET)
  1285. X                            direntry.version_no = (unsigned) gencount;
  1286. X                        else                                                     /* INC */
  1287. X                            direntry.version_no += (int) gencount;
  1288. X                        direntry.version_no &= VER_MASK; /* avoid extra bits */
  1289. X                        valtoshow = direntry.version_no;
  1290. X                    }
  1291. X                }
  1292. X            }
  1293. X
  1294. X            zooseek (zoo_file, this_dir_offset, 0);
  1295. X
  1296. #ifndef NOSIGNAL
  1297. X            oldsignal = signal (SIGINT, SIG_IGN);  /* disable ^C for write */
  1298. #endif
  1299. X            if (fwr_dir (&direntry, zoo_file) == -1)
  1300. X                prterror ('f', "Could not write to archive\n");
  1301. #ifndef NOSIGNAL
  1302. X            signal (SIGINT, oldsignal);
  1303. #endif
  1304. X            if (action == DEL_UNDEL)
  1305. X                prterror ('M', choice ? "deleted\n" : "undeleted\n");
  1306. X            else {
  1307. X                if (direntry.vflag & VFL_ON)
  1308. X                    prterror ('M', "adjusted to %u\n", valtoshow);
  1309. X                else
  1310. X                    prterror ('M', "no generations\n");
  1311. X            }
  1312. X            if (one)
  1313. X                done = 1;            /* if 1 option, done after 1 file */
  1314. X        }
  1315. X
  1316. X      /* remember most recent date and time if entry is not deleted */
  1317. X      if (!direntry.deleted)
  1318. X         if (direntry.date > latest_date ||
  1319. X            (direntry.date == latest_date && direntry.time > latest_time)) {
  1320. X               latest_date = direntry.date;
  1321. X               latest_time = direntry.time;
  1322. X         }
  1323. X      zooseek (zoo_file, direntry.next, 0); /* ..seek to next dir entry */
  1324. X   } /* endwhile */
  1325. X
  1326. X   if (!delcount)
  1327. X      printf ("Zoo:  No files matched.\n");
  1328. X   else {
  1329. #ifdef NIXTIME
  1330. X      zooclose (zoo_file);
  1331. X      setutime (zoo_path, latest_date, latest_time);
  1332. #else
  1333. #if 0
  1334. X      fflush (zoo_file);         /* superstition:  might help time stamp */
  1335. #endif
  1336. X      settime (zoo_file, latest_date, latest_time);
  1337. #endif
  1338. X   }
  1339. X
  1340. #ifndef NIXTIME
  1341. zooclose (zoo_file);
  1342. #endif
  1343. X
  1344. if (file_deleted && pack) {   /* pack if files were deleted and user asked */
  1345. X   prterror ('M', "-----\nPacking...");
  1346. X   zoopack (zoo_path, "PP");
  1347. X   prterror ('M', "done\n");
  1348. }
  1349. X
  1350. }
  1351. SHAR_EOF
  1352. chmod 0644 zoodel.c ||
  1353. echo 'restore of zoodel.c failed'
  1354. Wc_c="`wc -c < 'zoodel.c'`"
  1355. test 9676 -eq "$Wc_c" ||
  1356.     echo 'zoodel.c: original size 9676, current size' "$Wc_c"
  1357. fi
  1358. true || echo 'restore of zooext.c failed'
  1359. echo End of part 13, continue with part 14
  1360. exit 0
  1361.