home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / ZOOADD.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  27KB  |  723 lines

  1. #ifndef LINT
  2. /* derived from: zooadd.c 2.34 88/08/15 10:53:11 */
  3. static char sccsid[]="$Source: /usr/home/dhesi/zoo/RCS/zooadd.c,v $\n\
  4. $Id: zooadd.c,v 1.10 91/07/08 23:48:39 dhesi Exp $";
  5. #endif /* LINT */
  6.  
  7. /*
  8. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  9. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  10. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  11. */
  12. #include "options.h"
  13. /* Adds files specified in parameter-list to archive zoo_path. */
  14.  
  15. #define LONGEST    20                    /* assumed length of longest filename */
  16. #include "zoomem.h"             /* to define MAXADD */
  17. #include "zoo.h"
  18. #include "zooio.h"
  19. #include "various.h"
  20. #include "parse.h"
  21. #include "debug.h"
  22.  
  23. #include "portable.h"
  24.  
  25. #include "zoofns.h"
  26. #include "errors.i"
  27. extern int break_hit;
  28. extern int quiet;
  29.  
  30. void show_comment PARMS ((struct direntry *, ZOOFILE, int, char *));
  31. void dosname PARMS ((char *, char *));
  32. void modpath PARMS ((char *));
  33. void opts_add PARMS ((char *, int *, int *, int *, int *, int *, int *,
  34.                int *, int *, int *, int *, int *, int *, int *, int *));
  35. int ver_too_high PARMS ((struct zoo_header *));
  36. void get_comment PARMS ((struct direntry *, ZOOFILE, char *));
  37. void copyfields PARMS ((struct direntry *, struct tiny_header *));
  38. void storefname PARMS ((struct direntry *, char *, int));
  39. char *choosefname PARMS ((struct direntry *));
  40.  
  41. extern struct zoo_header zoo_header;
  42.  
  43. extern char file_leader[];
  44. extern unsigned int crccode;
  45.  
  46. void zooadd(zoo_path, argc, argv, option)
  47. char *zoo_path;      /* pathname of zoo archive to add to */
  48. int argc;            /* how many filespecs supplied */
  49. char **argv;         /* array of pointers to filespecs */
  50. char *option;        /* option string */
  51. {
  52. char *whichname;                          /* which name to show user */
  53. char **flist;                                  /* list of ptrs to input fnames */
  54. int fptr;                                 /* will point to within flist */
  55. ZOOFILE this_file;                        /* file to add */
  56. char zoo_fname[LFNAMESIZE];               /* basename of archive itself */
  57. char zoo_bak[LFNAMESIZE];                 /* name of archive's backup */
  58. char this_fname[LFNAMESIZE];              /* just filename of file to add */
  59. char latest_name[LFNAMESIZE];             /* latest name in archive */
  60. long last_old = 0L;                       /* last direntry in old chain */
  61. ZOOFILE zoo_file;                         /* stream for open archive */
  62. char *this_path;                          /* pathname of file to add */
  63.  
  64. #ifdef NOENUM
  65. #define NEW_ZOO 1
  66. #define OLD_ZOO 2
  67. int zoo_status;
  68. #else
  69. enum {NEW_ZOO, OLD_ZOO} zoo_status;       /* newly created or not */
  70. #endif
  71.  
  72. long this_dir_offset;                     /* pointers to within archive */
  73. long save_position;                       /* pointer to within archive */
  74. long prev_pos;                            /* posn of prev file of same name */
  75. struct direntry direntry;                 /* directory entry */
  76. struct direntry dir2entry;                /* spare */
  77. int status;                               /* error status */
  78. int success;                              /* successful addition of file? */
  79. int addcount = 0;                         /* number added */
  80. int update=0;                             /* only files already in archive */
  81. int suppress=0;                           /* suppress compression */
  82. int new=0;                                /* add only files not in archive */
  83. int zootime = 0;                          /* just set archive time */
  84. int add_comment = 0;                      /* add comment */
  85. int add_global_comment = 0;                    /* archive comment */
  86. int pack = 0;                             /* pack after adding */
  87. int need_dir = 1;                         /* store directories too */
  88. int delcount = 0;                         /* count of deleted entries */
  89. int exit_status = 0;                      /* exit status to set */
  90.  
  91. unsigned int latest_date = 0;             /* to set time on archive itself */
  92. unsigned int latest_time = 0;             /* .. same */
  93. int move = 0;                             /* delete after adding to archive */
  94. int longest;                              /* length of longest pathname added */
  95. int firstfile = 1;                        /* first file being added? */
  96. int z_fmt = 0;                            /* look for Z format files? */
  97. int inargs = 0;                                    /* read filenames from stdin? */
  98.  
  99. #ifndef PORTABLE
  100. struct tiny_header tiny_header;           /* for Z format archives */
  101. #endif
  102.  
  103. unsigned this_version_no;                            /* version no. of old file */
  104. unsigned  high_vflag;                                /* version flag of old file */
  105. unsigned high_version_no;                            /* highest version no of this file */
  106. long high_pos;                                        /* offset of file w/highest ver no */
  107. unsigned int fgens;                                /* gens. to preserve -- file */
  108. unsigned int zgens;                                /* gens. to preserve -- archive */
  109. long oldcmtpos;                                    /* to save old comment */
  110. unsigned int oldcmtsiz;                            /* to save old comment */
  111. int genson = 0;                                    /* whether to turn generations on */
  112.  
  113. int use_lzh = 0;                                    /* whether to use lzh compression */
  114.  
  115. /* on entry option points to first letter */
  116.  
  117. opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack,
  118.           &update, &add_comment, &z_fmt, &need_dir, &inargs, &genson,
  119.              &use_lzh, &add_global_comment);
  120.  
  121. /* POSSIBLE RACE CONDITION BETWEEN TESTING EXISTENCE AND CREATING FILE */
  122. if (exists (zoo_path)) {
  123.     zoo_file = zooopen (zoo_path, Z_RDWR);
  124.    zoo_status = OLD_ZOO;
  125. } else {
  126.    if (!zootime)
  127.         zoo_file = zoocreate (zoo_path);
  128.    else
  129.       zoo_file = NOFILE;     /* don't create if just setting time */
  130.    zoo_status = NEW_ZOO;
  131. }
  132.  
  133. if (zoo_file == NOFILE)
  134.    prterror ('f', could_not_open, zoo_path);
  135. basename(zoo_path, zoo_fname);      /* get basename of archive */
  136. rootname (zoo_path, zoo_bak);       /* name without extension */
  137. strcat (zoo_bak, BACKUP_EXT);       /* name of backup of this archive */
  138.  
  139. /* Now we prepare the archive for adding one or more files.  If the archive
  140. has just been created, we write the archive header */
  141.  
  142. addfname ("",0L,0,0,0,0); /* initialize table of files already in archive */
  143. if (zoo_status == NEW_ZOO) {                 /* newly-created archive */
  144.     if (genson)                                            /* if no generations needed */
  145.         zoo_header.vdata = (VFL_ON|GEN_DEFAULT); /* generations on */
  146.    fwr_zooh (&zoo_header, zoo_file);
  147.     zgens = GEN_DEFAULT;
  148.    zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  149. } else {
  150.    /* read header and rewrite with updated version numbers, preserving
  151.        header type */
  152.    rwheader (&zoo_header, zoo_file, 1);
  153.     zgens = zoo_header.vdata & VFL_GEN;            /* get archive generations */
  154.    /* initialize latest_name to null string */
  155.     /* NOTE:  latest_name is not currently used for anything, but
  156.         may be used in the future for inserting files into the
  157.         archive in alphabetic order. */
  158.    *latest_name = '\0';
  159.  
  160.    /* Skip existing files but add them to a list.  The variable last_old
  161.    gets the tail of the old chain of directory entries */
  162.    skip_files (zoo_file, &latest_date, &latest_time, &delcount,
  163.                latest_name, &last_old);
  164. }
  165. /* The file pointer is now positioned correctly to add a file to archive,
  166. unless the null directory entry is too short.  This will be fixed below. */
  167.  
  168. /* If we are just setting time, do it and run. */
  169. if (zootime) {
  170. #ifdef NIXTIME
  171.    zooclose (zoo_file);
  172.    setutime (zoo_path, latest_date, latest_time);
  173. #else
  174.    settime (zoo_file, latest_date, latest_time);
  175.    zooclose (zoo_file);
  176. #endif
  177.    prterror ('m', "Archive time adjusted.\n");
  178.    zooexit (0);
  179. }
  180.  
  181. /* make list of files, excluding archive and its backup */
  182. longest = LONGEST;
  183. flist = (char **) ealloc(MAXADD);
  184. if (!inargs) {
  185.    makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest);
  186.    /*                                    ^^         ^^       ^^ exclude */
  187. }
  188.  
  189. fptr = 0;    /* ready to get filename (if makelist() was called) or to
  190.                     begin adding filenames (if reading them from stdin) */
  191.  
  192. while (1) {
  193.    unsigned int this_date, this_time;
  194.    int INLIST; /* boolean */
  195.    int RECENT; /* boolean */
  196.    int danger; /* if update requested and disk copy is out of date */
  197.     if (inargs) {
  198.     again: /* loop back if filename was same as archive name or its backup */
  199.         this_path = getstdin();            /* pathname from stdin, in static area */
  200.         if (this_path != NULL) {
  201.             if (samefile (nameptr(zoo_fname),nameptr(this_path)) ||
  202.                         samefile (nameptr(zoo_bak),nameptr(this_path)))
  203.                 goto again;                 /* don't add archive to itself */
  204.             modpath (this_path);
  205.         /* if moving files, add to list for later deletion;  if list overflows,
  206.             terminate addition loop and give warning message */
  207.             if (move) {
  208.                 if (fptr >= MAXADD-2) {
  209.                     prterror ('w', too_many_files, MAXADD-2);
  210.                     this_path = NULL;
  211.                 } else
  212.                     flist[fptr++] = str_dup (this_path);
  213.             }
  214.         }
  215.     } else  {
  216.         this_path = flist[fptr++];
  217.     }
  218.     /* exit the addition loop when no more pathnames are left */
  219.     if (this_path == NULL) {
  220.         /* in case stdin was being read, make sure flist is NULL-terminated */
  221.         flist[fptr] = NULL;
  222.         break;
  223.     }
  224.  
  225.    basename (this_path, this_fname);   /* get just filename for later */
  226.  
  227.    this_file = zooopen(this_path, Z_READ);
  228.    if (this_file == NOFILE) {
  229.       prterror ('e', could_not_open, this_path);
  230.       exit_status++;
  231.       continue;
  232.    }
  233.  
  234. #ifndef PORTABLE
  235.    /* Test to see if this is a Z format file.  We assume the file is Z format
  236.       if (a) tag is correct and (b) type is 1 and (c) embedded filename
  237.       is not longer than FNAMESIZE.
  238.    */
  239.    if (z_fmt) {
  240.       zooread (this_file, (char *) &tiny_header, sizeof(tiny_header));
  241.       if (tiny_header.tinytag == TINYTAG && tiny_header.type == 1 &&
  242.                         strlen (tiny_header.fname) <= FNAMESIZE)
  243.           /* ok */ ;
  244.       else {
  245.          zooclose (this_file);
  246.          prterror ('e', "File %s does not have Z format.\n", this_fname);
  247.          exit_status++;
  248.          continue;
  249.       }
  250.    }
  251. #endif
  252.  
  253.    /* get file time;  also fix name */
  254. #ifndef PORTABLE
  255.    if (z_fmt) {
  256.       direntry.date = tiny_header.date;
  257.       direntry.time = tiny_header.time;
  258.       strcpy (direntry.fname, tiny_header.fname);
  259.       direntry.dirlen = direntry.namlen = 0;
  260.    } else {
  261. #endif
  262.  
  263.       /* Get timstamp of file being added */
  264. #ifdef GETUTIME
  265.       getutime (this_path, &direntry.date, &direntry.time);
  266. #else
  267.       gettime (this_file, &direntry.date, &direntry.time);
  268. #endif
  269.  
  270.         /* save file attributes */
  271. #ifdef FATTR
  272.         /* we expect getfattr() to set all attr. bits;  currently
  273.             only the portable format is recognized */
  274.         {
  275. # ifdef FATTR_FNAME
  276.             unsigned long getfattr PARMS ((char *);
  277.             direntry.fattr = getfattr (this_path);
  278. # else
  279.             unsigned long getfattr PARMS ((ZOOFILE));
  280.             direntry.fattr = getfattr (this_file);
  281. # endif /* FATTR_FNAME */
  282.         }
  283. #else
  284.             direntry.fattr = NO_FATTR;    /* none */
  285. #endif /* FATTR */
  286.  
  287. #ifdef FOLD
  288.       str_lwr(this_fname);
  289. #endif
  290.       dosname (this_fname, direntry.fname);  /* MSDOS filename */
  291.  
  292.    /*
  293.    Store long filename into direntry.lfname iff it is different from MSDOS
  294.    filename.  Also store directory name if need_dir is true.  Moved out of
  295.    zooadd() so zooadd() doesn't get too big for optimization.
  296.    */
  297.    storefname (&direntry, this_path, need_dir);
  298.  
  299. #ifndef PORTABLE
  300.    }
  301. #endif
  302.  
  303. #ifdef DEBUG
  304. printf ("zooadd:  direntry.lfname = [%s]  direntry.dirname = [%s]\n",
  305.                   direntry.lfname, direntry.dirname);
  306. #endif
  307.  
  308.    /* if update option, then we add file if it is already in the archive
  309.       AND the archived file is older */
  310.  
  311.    /* The following logic was derived from a Karnaugh map so it may
  312.       be hard to understand.  Essentially, if U=update requested,
  313.       N=new files requested, I=file is already in archive, and
  314.       R=file being archived is more recent than file already in
  315.       archive, then the boolean equation is:
  316.  
  317.       add = U' (N' + I') + U (IR  + I'N)
  318.    */
  319.  
  320.    /* Get the filename to use for this addition.  */
  321.    whichname = choosefname(&direntry);
  322.  
  323.    /* Get position in archive of any old file of same name, ignoring
  324.         any directory prefix if need_dir is not true.  Also get its
  325.         date, time, version flag, and version number. */
  326.    prev_pos = inlist (fullpath (&direntry), &this_date, &this_time,
  327.                 &this_version_no, &high_vflag, &high_version_no,
  328.                 &high_pos, !need_dir);
  329.  
  330. /* define DBG_INLIST for debugging by printing values returned by inlist() */
  331. #ifdef DBG_INLIST
  332.     printf ("FROM inlist(): prev_pos=%ld, high_pos=%ld\n", prev_pos, high_pos);
  333.     printf ("this_version_no=%u, high_vflag=%4x, high_version_no=%u\n",
  334.                 this_version_no,    high_vflag,     high_version_no);
  335. #endif
  336.  
  337.    INLIST = prev_pos > 0;  /* already in archive if positive value */
  338.    if (INLIST) {
  339.       int result;
  340.       result = cmpnum (direntry.date, direntry.time, this_date, this_time);
  341.       RECENT = result > 0;
  342.       danger = result < 0;
  343.    } else
  344.       danger = 0; /* And RECENT is undefined and should not be used */
  345.  
  346.    if (
  347.          !update && (!new || !INLIST) ||
  348.          update && (INLIST && RECENT || !INLIST && new)
  349.       )
  350.          ;  /* then continue and add file */
  351.    else {
  352.       if (update && danger)
  353.          prterror ('w', "Archived copy of %s is newer.\n", whichname);
  354.       zooclose (this_file);
  355.       continue;   /* cycle back, skip this file */
  356.    }
  357.  
  358. #ifdef CHEKDIR
  359.    /* Don't add if this is a directory */
  360.    if (isadir (this_file)) {
  361.       zooclose (this_file);
  362.       continue;
  363.    }
  364. #else
  365. # ifdef CHEKUDIR
  366.    /* Don't add if this is a directory */
  367.    if (isuadir (this_path)) {
  368.       zooclose (this_file);
  369.       continue;
  370.    }
  371. # endif /* CHEKUDIR */
  372. #endif /* CHEKDIR */
  373.  
  374.    /* Create directory entry for new file (but don't add just yet) */
  375.    /* NOTE:  we already got file date and time above for update option */
  376.     /* add tag, type, timezone, struc, system_id, and var_dir_len */
  377.     newdir (&direntry);
  378.  
  379.     if (!genson && zoo_status == NEW_ZOO ||
  380.                 (zoo_header.vdata & VFL_ON) == 0) {
  381.         direntry.vflag = 0;
  382.         direntry.version_no = 0;
  383.     }
  384.  
  385.    /*
  386.    Write a null direntry entry.  Thus, if an error occurs or the program
  387.    is interrupted, the end of the archive will still be meaningful.
  388.    Special check needed for first one written.
  389.    */
  390.  
  391.    direntry.next = direntry.offset = 0L;     /* trailing null entry */
  392.    this_dir_offset = zootell (zoo_file);
  393.    if (!firstfile) {
  394.       writedir (&direntry, zoo_file);
  395.    } else {
  396.       /*
  397.       Before adding the first file to the archive, we must make sure that
  398.       the previous directory chain (if any) is properly terminated with a
  399.       null entry of the right size.  If this is a new archive, we simply
  400.       write a new null entry of the right size.  If this is an existing
  401.       archive, we must check the size of the previous trailing null entry.
  402.       If it is too small, we will back up to the most recent real directory
  403.       entry and change its .next field to point to end of file.
  404.       */
  405.  
  406.       if (zoo_status == NEW_ZOO) {
  407.          writedir (&direntry, zoo_file);        /* write null dir entry */
  408.       } else {
  409.          struct direntry tmpentry;
  410.          long tmppos;
  411.          int oldlen, newlen;
  412.          tmppos = zootell (zoo_file);
  413.          frd_dir (&tmpentry, zoo_file);
  414. #define  DIRLEN(x)   ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len))
  415.          oldlen = DIRLEN(tmpentry);             /* get length of direntry */
  416.          newlen = DIRLEN(direntry);             /* ditto */
  417.  
  418.          if (newlen > oldlen) {                 /* trouble */
  419.             zooseek (zoo_file, last_old, 0);    /* back to previous entry */
  420.             frd_dir (&tmpentry, zoo_file);
  421.             zooseek (zoo_file, 0L, 2);          /* get EOF position */
  422.             tmpentry.next = zootell (zoo_file);    /* point to EOF */
  423.             zooseek (zoo_file, last_old, 0);    /* back to previous entry */
  424.             writedir (&tmpentry, zoo_file);     /* update it */
  425.             zooseek (zoo_file, 0L, 2);          /* to EOF ... */
  426.             this_dir_offset = zootell (zoo_file);
  427.             writedir (&direntry, zoo_file);     /* ...write null dir entry */
  428.          } else
  429.             zooseek (zoo_file, tmppos, 0);      /* long enough -- let it be */
  430.       } /* if (zoo_status == NEW_ZOO) ... */
  431.    } /* if (!firstfile) ... */
  432.  
  433.    /* Now `this_dir_offset' is where the next directory entry will go */
  434.  
  435.    /* first file added goes at EOF to avoid overwriting comments */
  436.    if (firstfile) {
  437.       zooseek (zoo_file, 0L, 2);                   /* EOF */
  438.       direntry.offset = zootell (zoo_file) + SIZ_FLDR;
  439.    } else {
  440.       direntry.offset = this_dir_offset + SIZ_DIRL +
  441.          direntry.var_dir_len + SIZ_FLDR;
  442.    }
  443.  
  444.     if (use_lzh) {
  445.         direntry.major_ver = MAJOR_LZH_VER;    /* minimum version number needed */
  446.         direntry.minor_ver = MINOR_LZH_VER;    /* .. to extract */
  447.     } else {
  448.         direntry.major_ver = MAJOR_EXT_VER;    /* minimum version number needed */
  449.         direntry.minor_ver = MINOR_EXT_VER;    /* .. to extract */
  450.     }
  451.    direntry.deleted = 0;               /* not deleted, naturally */
  452.    direntry.comment = 0L;              /* no comment (yet) */
  453.    direntry.cmt_size = 0;          /* .. so no size either */
  454.  
  455.    save_position = direntry.offset;          /* save position in case of error */
  456.  
  457.    (void) zooseek (zoo_file, direntry.offset - SIZ_FLDR, 0);
  458.    (void) zoowrite (zoo_file, file_leader, SIZ_FLDR);
  459.  
  460. #ifdef PORTABLE
  461.    prterror ('m', "%-*s -- ", longest, this_path);
  462. #else
  463.    if (z_fmt)
  464.       prterror ('m', "%-12s <== %-*s -- ",
  465.          direntry.fname, longest, this_path);
  466.    else
  467.       prterror ('m', "%-*s -- ", longest, this_path);
  468.  
  469. #endif /* PORTABLE */
  470.  
  471.    crccode = 0;
  472. #ifndef PORTABLE
  473.    if (z_fmt)
  474.     {
  475.       direntry.packing_method = tiny_header.packing_method;
  476.       zooseek (this_file, (long) (sizeof(tiny_header)+tiny_header.cmt_size), 0);
  477.       status = getfile (this_file, zoo_file, tiny_header.size_now, 1);
  478.    } else
  479. #endif
  480.     if (suppress) {                    /* suppress compression */
  481.       direntry.packing_method = 0;           /* no compression */
  482.       status = getfile (this_file, zoo_file, -1L, 1);
  483.    } else {
  484. #ifdef UNBUF_IO    /* unbuffered I/O */
  485.         long lseek PARMS ((int, long, int));
  486.         long tell PARMS ((int));
  487.         int this_fd, zoo_fd;
  488. #endif
  489.         if (use_lzh)
  490.             direntry.packing_method = 2;
  491.         else
  492.             direntry.packing_method = 1;
  493. #ifdef UNBUF_IO
  494. #include "UNBUF_IO not currently supported"
  495.         this_fd = fileno (this_file);                        /* get ..                    */
  496.         zoo_fd = fileno (zoo_file);                        /* ... file descriptors    */
  497.         zooseek (zoo_file, zootell (zoo_file), 0);    /* synch */
  498.         zooseek (this_file, zootell (this_file), 0);    /* synch */
  499.       status = lzc(this_fd, zoo_fd);                    /* add with compression */
  500.         zooseek (zoo_file, tell (zoo_fd), 0);            /* resynch    */
  501.         zooseek (this_file, tell (this_fd), 0);        /* resynch    */
  502. #else
  503.         if (use_lzh)
  504.             status = lzh_encode(this_file, zoo_file);
  505.         else
  506.             status = lzc(this_file, zoo_file);
  507. #endif /* UNBUF_IO */
  508.  
  509.    }
  510.    if (status != 0) { /* if I */
  511.       ++exit_status;                         /* remember error */
  512.       if (status == 1)
  513.          prterror ('F', no_memory);
  514.       else if (status == 2)
  515.          prterror ('F', disk_full);
  516.       else if (status == 3)
  517.          prterror ('F', "Read error.\n");
  518.       else
  519.          prterror ('F', internal_error);
  520.       success = 0;
  521.    } else {
  522.       direntry.next  = zootell (zoo_file);
  523.       direntry.size_now = direntry.next - direntry.offset;
  524.  
  525.       /* find and store original size of file just compressed */
  526. /*DEBUG VMS*/ zooseek (this_file, 0L, 2);    /* seek to EOF */
  527.  
  528.       direntry.org_size = zootell (this_file);  /* should be EOF already */
  529.  
  530.       /* If the compressed one is bigger, just copy */
  531.  
  532.       if (direntry.size_now >= direntry.org_size &&   /* if II */
  533.             direntry.packing_method != 0) {
  534.          zooseek (zoo_file, save_position, 0);  /* ..restore file pointer */
  535.          zootrunc (zoo_file);                   /* ..truncate file */
  536.          direntry.packing_method = 0;           /* ..and just copy */
  537.          zooseek (this_file, 0L, 0);            /* (but rewind first!) */
  538.          crccode = 0;                           /* re-start crc from 0 */
  539.          status = getfile (this_file, zoo_file, -1L, 1);
  540.          if (status != 0) {  /* if III */
  541.             success = 0;
  542.             printf (disk_full);
  543.             exit_status++;
  544.          } else {
  545.             success = 1;
  546.             direntry.next  = zootell (zoo_file);
  547.             direntry.size_now = direntry.next - direntry.offset;
  548.          } /* end if III */
  549.       } else {
  550.          success = 1;
  551.       } /* end if II */
  552.  
  553.    } /* end if I */
  554.  
  555.    if (success) {                               /* file successfully added */
  556.       addcount++;                               /* how many added */
  557.       direntry.file_crc = crccode;
  558.  
  559.       /* remember most recent date and time */
  560.       if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) {
  561.             latest_date = direntry.date;
  562.             latest_time = direntry.time;
  563.       }
  564.  
  565. #if 0
  566.       /* mark any previous version of this file in archive as deleted */
  567.       dir2entry.comment = 0L;       /* for later use assigning to direntry */
  568.       dir2entry.cmt_size = 0;
  569. #endif
  570.  
  571.       if (!z_fmt)
  572.          prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now));
  573.  
  574.         oldcmtsiz = 0;                                /* assume no old comment */
  575.         oldcmtpos = 0L;
  576.  
  577.       if (prev_pos > 0) {                                        /* in archive */
  578.             int delold = 0;                                        /* delete old? */
  579.             /* if versions active both archive-wide and for file */
  580.             if ((zoo_header.vdata & VFL_ON) && (high_vflag & VFL_ON)) {
  581.                 /* next test is optimization, to avoid redundant I/O */
  582.                 if (high_pos != prev_pos || this_version_no == 1) {
  583.                     /* prev highest is no longer highest so adjust vflag */
  584.                     long save_pos = zootell (zoo_file);            /*DEBUG*/
  585.                     zooseek (zoo_file, high_pos, 0);
  586.                     readdir (&dir2entry, zoo_file, 1);
  587.                     oldcmtpos = dir2entry.comment;
  588.                     oldcmtsiz = dir2entry.cmt_size;
  589.                     dir2entry.vflag &= (~VFL_LAST);                /* no longer highest */
  590.                     zooseek (zoo_file, high_pos, 0);
  591.                     writedir (&dir2entry, zoo_file);
  592.                     zooseek (zoo_file, save_pos, 0);                /*DEBUG*/
  593.                 }
  594.  
  595.                 direntry.version_no = high_version_no + 1; /* ..one higher */
  596.                 direntry.vflag = high_vflag;
  597.                 /* now see if we need to delete older version */
  598.                 fgens = high_vflag & VFL_GEN;
  599.                 if (fgens == 0)
  600.                     fgens = zgens;
  601.                 if (zgens != 0 && zgens < fgens)
  602.                     fgens = zgens;
  603.                 if (fgens != 0 && direntry.version_no - this_version_no >= fgens) {
  604.                     delold = 1;
  605.                     prterror ('M', "replaced+\n");
  606.                 } else
  607.                     prterror ('M', "added+\n");
  608.             } else {
  609.                 prterror ('M', "replaced\n");
  610.                 delold = 1;
  611.             }
  612.  
  613.             if (delold) {                                            /* deleting old file */
  614.                 long save_pos = zootell (zoo_file);            /*DEBUG*/
  615.                 ++delcount;                                            /* remember to pack */
  616.                 zooseek (zoo_file, prev_pos, 0);
  617.                 readdir (&dir2entry, zoo_file, 1);
  618.                 if (dir2entry.cmt_size != 0) {        /* propagate latest comment */
  619.                     oldcmtpos = dir2entry.comment;
  620.                     oldcmtsiz = dir2entry.cmt_size;
  621.                 }
  622.                 dir2entry.deleted = 1;                            /* mark as deleted */
  623.                 /* following line is optimization if only 1 generation */
  624.                 dir2entry.vflag &= (~VFL_LAST);                /* no longer highest */
  625.                 zooseek (zoo_file, prev_pos, 0);
  626.                 writedir (&dir2entry, zoo_file);
  627.                 zooseek (zoo_file, save_pos, 0);                /*DEBUG*/
  628.             }
  629.       } else                                                         /* not in archive */
  630.             prterror ('M', "added\n");
  631.  
  632.       /* Preserve any old comment if we replaced or superseded the file */
  633.       direntry.comment = oldcmtpos;
  634.       direntry.cmt_size = oldcmtsiz;
  635.  
  636. #ifndef PORTABLE
  637.       /* Copy comment if any from Z format file */
  638.       if (z_fmt && tiny_header.cmt_size != 0) {
  639.          zooseek (this_file, (long) sizeof(tiny_header), 0); /* to comment */
  640.          direntry.comment = zootell (zoo_file);
  641.          direntry.cmt_size = tiny_header.cmt_size;
  642.          /* 4th param is 0 for no CRC */
  643.          getfile (this_file, zoo_file, (long) tiny_header.cmt_size, 0);
  644.          direntry.next = zootell (zoo_file);
  645.       }
  646. #endif
  647.  
  648.       /* if user requested comments, any previous comment in a Z format
  649.          file may now be manually overwritten */
  650.       if (add_comment && !feof (stdin)) {
  651.          show_comment (&direntry, zoo_file, 1, whichname);
  652.          get_comment (&direntry, zoo_file, this_path);
  653.          direntry.next = zootell (zoo_file);    /* update .next ptr */
  654.       } /* end if */
  655.  
  656. #ifndef PORTABLE
  657.       /* if adding Z format archive, copy relevant fields from its header */
  658.       if (z_fmt) {   /* moved out to shorten code & allow optimizer to work */
  659.          copyfields (&direntry, &tiny_header);
  660.       }
  661. #endif
  662.  
  663.       debug((printf ("zooadd:  our new .next = [%lx].\n", direntry.next)))
  664.  
  665.       {
  666.          long savepos = zootell (zoo_file);    /* save position */
  667.          zooseek (zoo_file, this_dir_offset, 0);
  668.          writedir (&direntry, zoo_file);
  669.          zooseek (zoo_file, savepos, 0);    /* restore position */
  670.       }
  671.  
  672.    } else {                               /* file was not properly added */
  673.       zooseek (zoo_file, save_position, 0);  /* ..restore file pointer */
  674.       zootrunc (zoo_file);                   /* ..truncate file */
  675.    } /* end if */
  676.    zooclose (this_file);
  677. if (!success)
  678.    break;
  679. firstfile = 0;
  680. } /* end for */
  681.  
  682. save_position = zootell (zoo_file);
  683.  
  684. /* Write a null direntry entry */
  685. zooseek (zoo_file, save_position, 0);
  686. writenull (zoo_file, MAXDIRSIZE);
  687. zootrunc (zoo_file);  /* truncate */
  688.  
  689. #ifdef NIXTIME
  690. zooclose (zoo_file);
  691. setutime (zoo_path, latest_date, latest_time);
  692. #else
  693. settime (zoo_file, latest_date, latest_time);
  694. zooclose (zoo_file);
  695. #endif
  696.  
  697. if (!addcount) {                    /* no files added */
  698.    prterror ('m', "No files added.\n");
  699.    if (zoo_status == NEW_ZOO)
  700.       unlink (zoo_path);
  701. } else {
  702.    if (delcount && pack) { /* pack if user asked and found deleted entries */
  703.       prterror ('M', "-----\nPacking...");
  704.       zoopack (zoo_path, "PP");
  705.       prterror ('M', "done\n");
  706.    }
  707.  
  708.    /* If files to move & we added some and no error so far, delete originals */
  709.    if (move && !exit_status)
  710.       if (kill_files (flist, longest) != 0)
  711.          exit_status++;
  712. }
  713.  
  714. /* right here we handle archive comment */
  715. if (add_global_comment) {
  716.     comment(zoo_path, "_A");
  717.     add_global_comment = 0;
  718. }
  719.  
  720. if (exit_status)
  721.    zooexit (1);
  722. } /* end zoo_add */
  723.