home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / zip.c < prev    next >
C/C++ Source or Header  |  1997-10-26  |  59KB  |  2,037 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  zip.c by Mark Adler.
  13.  */
  14.  
  15. #include "zip.h"
  16. #ifdef WINDLL
  17. #include <windows.h>
  18. #include "windll/windll.h"
  19. #endif
  20. #include "revision.h"
  21. #include "crypt.h"
  22. #include "ttyio.h"
  23. #ifdef VMS
  24. #  include "vms/vmsmunch.h"
  25. #endif
  26.  
  27. #if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k)
  28. #  include <process.h>
  29. #endif
  30.  
  31. #include <signal.h>
  32.  
  33. #define MAXCOM 256      /* Maximum one-line comment size */
  34.  
  35.  
  36. /* Local option flags */
  37. #ifndef DELETE
  38. #define DELETE  0
  39. #endif
  40. #define ADD     1
  41. #define UPDATE  2
  42. #define FRESHEN 3
  43. local int action = ADD; /* one of ADD, UPDATE, FRESHEN, or DELETE */
  44. local int comadd = 0;   /* 1=add comments for new files */
  45. local int zipedit = 0;  /* 1=edit zip comment and all file comments */
  46. local int latest = 0;   /* 1=set zip file time to time of latest file */
  47. local ulg before = 0;   /* 0=ignore, else exclude files before this time */
  48. local ulg after = 0;    /* 0=ignore, else exclude files newer than this time */
  49. local int test = 0;     /* 1=test zip file with unzip -t */
  50. local int tempdir = 0;  /* 1=use temp directory (-b) */
  51. local int junk_sfx = 0; /* 1=junk the sfx prefix */
  52. #ifdef AMIGA
  53. local int filenotes = 0; /* 1=take comments from AmigaDOS filenotes */
  54. #endif
  55.  
  56. #ifdef EBCDIC
  57. int aflag = __EBCDIC;   /* Convert EBCDIC to ASCII or stay EBCDIC ? */
  58. #endif
  59. #ifdef CMS_MVS
  60. int bflag = 0;          /* Use text mode as default */
  61. #endif
  62.  
  63. #ifdef QDOS
  64. char _version[] = VERSION;
  65. #endif
  66.  
  67. /* Temporary zip file name and file pointer */
  68. local char *tempzip;
  69. local FILE *tempzf;
  70.  
  71. #if CRYPT
  72. /* Pointer to crc_table, needed in crypt.c */
  73. ulg near *crc_32_tab;
  74. #endif
  75.  
  76. /* Local functions */
  77.  
  78. local void freeup  OF((void));
  79. local int  finish  OF((int));
  80. #ifndef WINDLL
  81. local void handler OF((int));
  82. local void license OF((void));
  83. #ifndef VMSCLI
  84. local void help    OF((void));
  85. #endif /* !VMSCLI */
  86. #endif /* !WINDLL */
  87. local int get_filters OF((int argc, char **argv));
  88. #ifndef WINDLL
  89. local void check_zipfile OF((char *zipname, char *zippath));
  90. local void version_info OF((void));
  91. local void zipstdout OF((void));
  92. #endif
  93.  
  94. local void freeup()
  95. /* Free all allocations in the found list and the zfiles list */
  96. {
  97.   struct flist far *f;  /* steps through found list */
  98.   struct zlist far *z;  /* pointer to next entry in zfiles list */
  99.  
  100.   for (f = found; f != NULL; f = fexpel(f))
  101.     ;
  102.   while (zfiles != NULL)
  103.   {
  104.     z = zfiles->nxt;
  105.     if (zfiles->zname && zfiles->zname != zfiles->name)
  106.       free((zvoid *)(zfiles->zname));
  107.     if (zfiles->name)
  108.       free((zvoid *)(zfiles->name));
  109.     if (zfiles->iname)
  110.       free((zvoid *)(zfiles->iname));
  111.     if (zfiles->cext && zfiles->cextra && zfiles->cextra != zfiles->extra)
  112.       free((zvoid *)(zfiles->cextra));
  113.     if (zfiles->ext && zfiles->extra)
  114.       free((zvoid *)(zfiles->extra));
  115.     if (zfiles->com && zfiles->comment)
  116.       free((zvoid *)(zfiles->comment));
  117.     farfree((zvoid far *)zfiles);
  118.     zfiles = z;
  119.     zcount--;
  120.   }
  121. }
  122.  
  123. local int finish(e)
  124. int e;                  /* exit code */
  125. /* Process -o and -m options (if specified), free up malloc'ed stuff, and
  126.    exit with the code e. */
  127. {
  128.   int r;                /* return value from trash() */
  129.   ulg t;                /* latest time in zip file */
  130.   struct zlist far *z;  /* pointer into zfile list */
  131.  
  132.   /* If latest, set time to zip file to latest file in zip file */
  133.   if (latest && zipfile && strcmp(zipfile, "-"))
  134.   {
  135.     diag("changing time of zip file to time of latest file in it");
  136.     /* find latest time in zip file */
  137.     if (zfiles == NULL)
  138.        zipwarn("zip file is empty, can't make it as old as latest entry", "");
  139.     else {
  140.       t = 0;
  141.       for (z = zfiles; z != NULL; z = z->nxt)
  142.         /* Ignore directories in time comparisons */
  143. #ifdef USE_EF_UT_TIME
  144.         if (z->zname[z->nam-1] != '/')
  145.         {
  146.           iztimes z_utim;
  147.           ulg z_tim;
  148.  
  149.           z_tim = ((get_ef_ut_ztime(z, &z_utim) & EB_UT_FL_MTIME) ?
  150.                    unix2dostime(&z_utim.mtime) : z->tim);
  151.           if (t < z_tim)
  152.             t = z_tim;
  153.         }
  154. #else /* !USE_EF_UT_TIME */
  155.         if (z->zname[z->nam-1] != '/' && t < z->tim)
  156.           t = z->tim;
  157. #endif /* ?USE_EF_UT_TIME */
  158.       /* set modified time of zip file to that time */
  159.       if (t != 0)
  160.         stamp(zipfile, t);
  161.       else
  162.         zipwarn(
  163.          "zip file has only directories, can't make it as old as latest entry",
  164.          "");
  165.     }
  166.   }
  167.   if (tempath != NULL)
  168.   {
  169.     free((zvoid *)tempath);
  170.     tempath = NULL;
  171.   }
  172.   if (zipfile != NULL)
  173.   {
  174.     free((zvoid *)zipfile);
  175.     zipfile = NULL;
  176.   }
  177.   if (zcomment != NULL)
  178.   {
  179.     free((zvoid *)zcomment);
  180.     zcomment = NULL;
  181.   }
  182.  
  183.  
  184.   /* If dispose, delete all files in the zfiles list that are marked */
  185.   if (dispose)
  186.   {
  187.     diag("deleting files that were added to zip file");
  188.     if ((r = trash()) != ZE_OK)
  189.       ZIPERR(r, "was deleting moved files and directories");
  190.   }
  191.  
  192.  
  193.   /* Done! */
  194.   freeup();
  195.   return e;
  196. }
  197.  
  198. void ziperr(c, h)
  199. int c;                  /* error code from the ZE_ class */
  200. char *h;                /* message about how it happened */
  201. /* Issue a message for the error, clean up files and memory, and exit. */
  202. {
  203. #ifndef WINDLL
  204.   static int error_level = 0;
  205.  
  206.   if (error_level++ > 0)
  207.      EXIT(0);  /* avoid recursive ziperr() */
  208. #endif
  209.  
  210.   if (h != NULL) {
  211.     if (PERR(c))
  212.       perror("zip error");
  213.     fflush(mesg);
  214.     fprintf(stderr, "\nzip error: %s (%s)\n", errors[c-1], h);
  215.   }
  216.   if (tempzip != NULL)
  217.   {
  218.     if (tempzip != zipfile) {
  219.       if (tempzf != NULL)
  220.         fclose(tempzf);
  221. #ifndef DEBUG
  222.       destroy(tempzip);
  223. #endif
  224.       free((zvoid *)tempzip);
  225.     } else {
  226.       /* -g option, attempt to restore the old file */
  227.       int k = 0;                        /* keep count for end header */
  228.       ulg cb = cenbeg;                  /* get start of central */
  229.       struct zlist far *z;  /* steps through zfiles linked list */
  230.  
  231.       fprintf(stderr, "attempting to restore %s to its previous state\n",
  232.          zipfile);
  233.       fseek(tempzf, cenbeg, SEEK_SET);
  234.       tempzn = cenbeg;
  235.       for (z = zfiles; z != NULL; z = z->nxt)
  236.       {
  237.         putcentral(z, tempzf);
  238.         tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  239.         k++;
  240.       }
  241.       putend(k, tempzn - cb, cb, zcomlen, zcomment, tempzf);
  242.       tempzf = NULL;
  243.       fclose(tempzf);
  244.     }
  245.   }
  246.   if (key != NULL) {
  247.     free((zvoid *)key);
  248.     key = NULL;
  249.   }
  250.   if (tempath != NULL) {
  251.     free((zvoid *)tempath);
  252.     tempath = NULL;
  253.   }
  254.   if (zipfile != NULL) {
  255.     free((zvoid *)zipfile);
  256.     zipfile = NULL;
  257.   }
  258.   if (zcomment != NULL) {
  259.     free((zvoid *)zcomment);
  260.     zcomment = NULL;
  261.   }
  262.   freeup();
  263. #ifndef WINDLL
  264.   EXIT(c);
  265. #else
  266.   return;
  267. #endif
  268. }
  269.  
  270.  
  271. void error(h)
  272.   char *h;
  273. /* Internal error, should never happen */
  274. {
  275.   ziperr(ZE_LOGIC, h);
  276. }
  277.  
  278. #ifndef WINDLL
  279. local void handler(s)
  280. int s;                  /* signal number (ignored) */
  281. /* Upon getting a user interrupt, turn echo back on for tty and abort
  282.    cleanly using ziperr(). */
  283. {
  284. #if defined(AMIGA) && defined(__SASC)
  285.    _abort();
  286. #else
  287. #if !defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS)
  288.   echon();
  289.   putc('\n', stderr);
  290. #endif /* !MSDOS */
  291. #endif /* AMIGA && __SASC */
  292.   ziperr(ZE_ABORT, "aborting");
  293.   s++;                                  /* keep some compilers happy */
  294. }
  295. #endif /* !WINDLL */
  296.  
  297. void zipwarn(a, b)
  298. char *a, *b;            /* message strings juxtaposed in output */
  299. /* Print a warning message to stderr and return. */
  300. {
  301.   if (noisy) fprintf(stderr, "\tzip warning: %s%s\n", a, b);
  302. }
  303.  
  304. #ifndef WINDLL
  305. local void license()
  306. /* Print license information to stdout. */
  307. {
  308.   extent i;             /* counter for copyright array */
  309.  
  310.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
  311.     printf(copyright[i], "zip");
  312.     putchar('\n');
  313.   }
  314.   for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++)
  315.     puts(disclaimer[i]);
  316. }
  317.  
  318. #ifdef VMSCLI
  319. void help()
  320. #else
  321. local void help()
  322. #endif
  323. /* Print help (along with license info) to stdout. */
  324. {
  325.   extent i;             /* counter for help array */
  326.  
  327.   /* help array */
  328.   static ZCONST char *text[] = {
  329. #ifdef VMS
  330. "Zip %s (%s). Usage: zip==\"$disk:[dir]zip.exe\"",
  331. #else
  332. "Zip %s (%s). Usage:",
  333. #endif
  334. "zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]",
  335. "  The default action is to add or replace zipfile entries from list, which",
  336. "  can include the special name - to compress standard input.",
  337. "  If zipfile and list are omitted, zip compresses stdin to stdout.",
  338. "  -f   freshen: only changed files  -u   update: only changed or new files",
  339. "  -d   delete entries in zipfile    -m   move into zipfile (delete files)",
  340. "  -R   recurse into directories     -j   junk (don't record) directory names",
  341. "  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)",
  342. "  -1   compress faster              -9   compress better",
  343. "  -q   quiet operation              -v   verbose operation/print version info",
  344. "  -c   add one-line comments        -z   add zipfile comment",
  345. "  -@   read names from stdin        -o   make zipfile as old as latest entry",
  346. "  -x   exclude the following names  -i   include only the following names",
  347. #ifdef EBCDIC
  348. #ifdef CMS_MVS
  349. "  -a   translate to ASCII           -B   force binary read (text is default)",
  350. #else  /* !CMS_MVS */
  351. "  -a   translate to ASCII",
  352. #endif /* ?CMS_MVS */
  353. #endif /* EBCDIC */
  354. #ifdef VMS
  355. " \"-F\"  fix zipfile(\"-FF\" try harder) \"-D\"  do not add directory entries",
  356. " \"-A\"  adjust self-extracting exe  \"-J\"  junk zipfile prefix (unzipsfx)",
  357. " \"-T\"  test zipfile integrity      \"-X\"  eXclude eXtra file attributes",
  358. " \"-V\"  save VMS file attributes     -w   append version number to stored name",
  359. #else
  360. "  -F   fix zipfile (-FF try harder) -D   do not add directory entries",
  361. "  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)",
  362. "  -T   test zipfile integrity       -X   eXclude eXtra file attributes",
  363. #endif /* VMS */
  364. #ifdef WIN32
  365. "  -!   use privileges (if granted) to obtain all aspects of WinNT security",
  366. #endif /* WIN32 */
  367. #ifdef OS2
  368. "  -E   use the .LONGNAME Extended attribute (if found) as filename",
  369. #endif /* OS2 */
  370. #ifdef S_IFLNK
  371. "  -y   store symbolic links as the link instead of the referenced file",
  372. #endif /* !S_IFLNK */
  373. #if defined(MSDOS) || defined(OS2)
  374. "  -$   include volume label         -S   include system and hidden files",
  375. #endif
  376. #ifdef AMIGA
  377. #  if CRYPT
  378. "  -N   store filenotes as comments  -e   encrypt",
  379. "  -h   show this help               -n   don't compress these suffixes"
  380. #  else
  381. "  -N   store filenotes as comments  -n   don't compress these suffixes"
  382. #  endif
  383. #else /* !AMIGA */
  384. #  if CRYPT
  385. "  -e   encrypt                      -n   don't compress these suffixes"
  386. #  else
  387. "  -h   show this help               -n   don't compress these suffixes"
  388. #  endif
  389. #endif /* ?AMIGA */
  390. #ifdef RISCOS
  391. ,"  -I   don't scan through Image files"
  392. #endif
  393.   };
  394.  
  395.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  396.   {
  397.     printf(copyright[i], "zip");
  398.     putchar('\n');
  399.   }
  400.   for (i = 0; i < sizeof(text)/sizeof(char *); i++)
  401.   {
  402.     printf(text[i], VERSION, REVDATE);
  403.     putchar('\n');
  404.   }
  405. }
  406.  
  407. /*
  408.  * XXX version_info() in a separate file
  409.  */
  410. local void version_info()
  411. /* Print verbose info about program version and compile time options
  412.    to stdout. */
  413. {
  414.   extent i;             /* counter in text arrays */
  415.   char *envptr;
  416.  
  417.   /* Options info array */
  418.   static ZCONST char *comp_opts[] = {
  419. #ifdef ASM_CRC
  420.     "ASM_CRC",
  421. #endif
  422. #ifdef ASMV
  423.     "ASMV",
  424. #endif
  425. #ifdef DYN_ALLOC
  426.     "DYN_ALLOC",
  427. #endif
  428. #ifdef MMAP
  429.     "MMAP",
  430. #endif
  431. #ifdef BIG_MEM
  432.     "BIG_MEM",
  433. #endif
  434. #ifdef MEDIUM_MEM
  435.     "MEDIUM_MEM",
  436. #endif
  437. #ifdef SMALL_MEM
  438.     "SMALL_MEM",
  439. #endif
  440. #ifdef DEBUG
  441.     "DEBUG",
  442. #endif
  443. #ifdef USE_EF_UT_TIME
  444.     "USE_EF_UT_TIME",
  445. #endif
  446. #ifdef NTSD_EAS
  447.     "NTSD_EAS",
  448. #endif
  449. #ifdef VMS
  450. #ifdef VMSCLI
  451.     "VMSCLI",
  452. #endif
  453. #ifdef VMS_IM_EXTRA
  454.     "VMS_IM_EXTRA",
  455. #endif
  456. #ifdef VMS_PK_EXTRA
  457.     "VMS_PK_EXTRA",
  458. #endif
  459. #endif /* VMS */
  460. #if CRYPT && defined(PASSWD_FROM_STDIN)
  461.     "PASSWD_FROM_STDIN",
  462. #endif /* CRYPT & PASSWD_FROM_STDIN */
  463.     NULL
  464.   };
  465.  
  466.   static ZCONST char *zipenv_names[] = {
  467. #ifndef VMS
  468. #  ifndef RISCOS
  469.     "ZIP"
  470. #  else /* RISCOS */
  471.     "Zip$Options"
  472. #  endif /* ?RISCOS */
  473. #else /* VMS */
  474.     "ZIP_OPTS"
  475. #endif /* ?VMS */
  476.     ,"ZIPOPT"
  477. #ifdef AZTEC_C
  478.     ,     /* extremely lame compiler bug workaround */
  479. #endif
  480. #if (defined(__EMX__) && !defined(__RSXNT__))
  481.     ,"EMX"
  482.     ,"EMXOPT"
  483. #endif
  484. #ifdef __GO32__
  485.     ,"GO32"
  486.     ,"GO32TMP"
  487. #endif
  488. #ifdef RISCOS
  489.     ,"Zip$Exts"
  490. #endif
  491.   };
  492.  
  493.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  494.   {
  495.     printf(copyright[i], "zip");
  496.     putchar('\n');
  497.   }
  498.  
  499.   for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
  500.   {
  501.     printf(versinfolines[i], "Zip", VERSION, REVDATE);
  502.     putchar('\n');
  503.   }
  504.  
  505.   version_local();
  506.  
  507.   puts("Zip special compilation options:");
  508. #if WSIZE != 0x8000
  509.   printf("\tWSIZE=%u\n", WSIZE);
  510. #endif
  511.   for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
  512.   {
  513.     printf("\t%s\n",comp_opts[i]);
  514.   }
  515. #if CRYPT
  516.   printf("\t[encryption, version %d.%d%s of %s]\n",
  517.             CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
  518.   ++i;
  519. #endif /* CRYPT */
  520.   if (i == 0)
  521.       puts("\t[none]");
  522.  
  523.   puts("\nZip environment options:");
  524.   for (i = 0; i < sizeof(zipenv_names)/sizeof(char *); i++)
  525.   {
  526.     envptr = getenv(zipenv_names[i]);
  527.     printf("%16s:  %s\n", zipenv_names[i],
  528.            ((envptr == (char *)NULL || *envptr == 0) ? "[none]" : envptr));
  529.   }
  530. }
  531. #endif /* !WINDLL */
  532.  
  533.  
  534. #ifndef PROCNAME
  535. #  define PROCNAME(n) procname(n)
  536. #endif /* PROCNAME */
  537.  
  538. #ifndef WINDLL
  539. local void zipstdout()
  540. /* setup for writing zip file on stdout */
  541. {
  542.   int r;
  543.   mesg = stderr;
  544.   if (isatty(1))
  545.     ziperr(ZE_PARMS, "cannot write zip file to terminal");
  546.   if ((zipfile = malloc(4)) == NULL)
  547.     ziperr(ZE_MEM, "was processing arguments");
  548.   strcpy(zipfile, "-");
  549.   if ((r = readzipfile()) != ZE_OK)
  550.     ziperr(r, zipfile);
  551. }
  552.  
  553. local void check_zipfile(zipname, zippath)
  554.   char *zipname;
  555.   char *zippath;
  556.   /* Invoke unzip -t on the given zip file */
  557. {
  558. #if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__)
  559.    int status, len;
  560.    char *path, *p;
  561.  
  562.    status = spawnlp(P_WAIT, "unzip", "unzip", verbose ? "-t" : "-tqq",
  563.                     zipname, NULL);
  564. /*
  565.  * unzip isn't in PATH range, assume an absolute path to zip in argv[0]
  566.  * and hope that unzip is in the same directory.
  567.  */
  568.    if (status == -1) {
  569.      p = strrchr(zippath, '\\');
  570.      path = strrchr((p == NULL ? zippath : p), '/');
  571.      if (path != NULL)
  572.        p = path;
  573.      if (p != NULL) {
  574.        len = (int)(p - zippath) + 1;
  575.        if ((path = malloc(len + sizeof("unzip.exe"))) == NULL)
  576.          ziperr(ZE_MEM, "was creating unzip path");
  577.        memcpy(path, zippath, len);
  578.        strcpy(&path[len], "unzip.exe");
  579.        status = spawnlp(P_WAIT, path, "unzip", verbose ? "-t" : "-tqq",
  580.                         zipname, NULL);
  581.        free(path);
  582.      }
  583.      if (status == -1)
  584.        perror("unzip");
  585.    }
  586.    if (status != 0) {
  587. #else /* (MSDOS && !__GO32__) || __human68k__ */
  588.    char cmd[FNMAX+16];
  589.  
  590.    /* Tell picky compilers to shut up about unused variables */
  591.    zippath = zippath;
  592.  
  593.    strcpy(cmd, "unzip -t ");
  594. #ifdef QDOS
  595.    strcat(cmd, "-Q4 ");
  596. #endif
  597.    if (!verbose) strcat(cmd, "-qq ");
  598.    if ((int)strlen(zipname) > FNMAX) {
  599.      error("zip filename too long");
  600.    }
  601. # ifdef UNIX
  602.    strcat(cmd, "'");    /* accept space or $ in name */
  603.    strcat(cmd, zipname);
  604.    strcat(cmd, "'");
  605. # else
  606.    strcat(cmd, zipname);
  607. # endif
  608. # ifdef VMS
  609.    if (!system(cmd)) {
  610. # else
  611.    if (system(cmd)) {
  612. # endif
  613. #endif /* ?((MSDOS && !__GO32__) || __human68k__) */
  614.      fprintf(mesg, "test of %s FAILED\n", zipfile);
  615.      ziperr(ZE_TEST, "original files unmodified");
  616.    }
  617.    if (noisy)
  618.      fprintf(mesg, "test of %s OK\n", zipfile);
  619. }
  620. #endif /* !WINDLL */
  621.  
  622. local int get_filters(argc, argv)
  623.   int argc;               /* number of tokens in command line */
  624.   char **argv;            /* command line tokens */
  625. /* Counts number of -i or -x patterns, sets patterns and pcount */
  626. {
  627.   int i;
  628.   int flag = 0, archive_seen = 0;
  629.   char *iname, *p = NULL;
  630.   FILE *fp;
  631.  
  632.   pcount = 0;
  633.   for (i = 1; i < argc; i++) {
  634.     if (argv[i][0] == '-') {
  635.       if ((p = strrchr(argv[i], 'i')) != NULL ||
  636.           (p = strrchr(argv[i], 'x')) != NULL) {
  637.         flag = *p;
  638.         p = p[1] == '@' ? p + 2 : NULL;
  639.         if (p != NULL && patterns == NULL) {
  640.           fp = fopen(p, "r");
  641.           if (fp == NULL) {
  642.             ZIPERR(ZE_NAME, p);
  643.           }
  644.           while (fgets(errbuf, FNMAX, fp) != NULL)
  645.             pcount++;
  646.           fclose(fp);
  647.         }
  648.       } else if (strrchr(argv[i], 'R') != NULL) {
  649.         flag = 'R';
  650.       } else if (flag != 'R') {
  651.         flag = 0;
  652.       }
  653.     }
  654.     if (flag && archive_seen) {
  655.       if (patterns != NULL) {
  656.         if (p != NULL) {
  657.           fp = fopen(p, "r");
  658.           if (fp == NULL) {
  659.             ZIPERR(ZE_NAME, p);
  660.           }
  661.           while ((p = getnam(errbuf, fp)) != NULL) {
  662.             iname = ex2in(p, 0, (int *)NULL);
  663.             patterns[pcount].zname = (iname != NULL ? in2ex(iname) : NULL);
  664.             if (iname != NULL)
  665.               free(iname);
  666.             patterns[pcount].select = flag;
  667.             if (flag != 'x')
  668.               icount++;
  669.             pcount++;
  670.           }
  671.           fclose(fp);
  672.           p = NULL;
  673.         }
  674.         else if (argv[i][0] != '-') {
  675.           iname = ex2in(argv[i], 0, (int *)NULL);
  676.           patterns[pcount].zname = (iname != NULL ? in2ex(iname) : NULL);
  677.           if (iname != NULL)
  678.             free(iname);
  679.           patterns[pcount].select = flag;
  680.           if (flag != 'x')
  681.             icount++;
  682.           pcount++;
  683.         }
  684.       }
  685.       else if (p == NULL)
  686.        pcount++;
  687.     } else
  688.       archive_seen = 1;     /* first non-flag arg is archive name */
  689.   }
  690.   if (pcount == 0 || patterns != NULL) return ZE_OK;
  691.   patterns = (struct plist*) malloc(pcount * sizeof(struct plist));
  692.   if (patterns == NULL) {
  693.     ZIPERR(ZE_MEM, "was creating pattern list");
  694.     }
  695.   return get_filters(argc, argv);
  696. }
  697.  
  698. #if CRYPT
  699. #ifndef WINDLL
  700. int encr_passwd(modeflag, pwbuf, size, zfn)
  701. int modeflag;
  702. char *pwbuf;
  703. int size;
  704. ZCONST char *zfn;
  705. {
  706.     char *prompt;
  707.  
  708.     /* Tell picky compilers to shut up about unused variables */
  709.     zfn = zfn;
  710.  
  711.     prompt = (modeflag == ZP_PW_VERIFY) ?
  712.               "Verify password: " : "Enter password: ";
  713.  
  714.     if (getp(prompt, pwbuf, size) == NULL) {
  715.       ziperr(ZE_PARMS, "stderr is not a tty");
  716.     }
  717.     return IZ_PW_ENTERED;
  718. }
  719. #endif /* !WINDLL */
  720. #else /* !CRYPT */
  721. int encr_passwd(modeflag, pwbuf, size, zfn)
  722. int modeflag;
  723. char *pwbuf;
  724. int size;
  725. ZCONST char *zfn;
  726. {
  727.     /* Tell picky compilers to shut up about unused variables */
  728.     modeflag = modeflag; pwbuf = pwbuf; size = size; zfn = zfn;
  729.  
  730.     return ZE_LOGIC;    /* This function should never be called! */
  731. }
  732. #endif /* CRYPT */
  733.  
  734. #ifndef USE_ZIPMAIN
  735. int main(argc, argv)
  736. #else
  737. int zipmain(argc, argv)
  738. #endif
  739. int argc;               /* number of tokens in command line */
  740. char **argv;            /* command line tokens */
  741. /* Add, update, freshen, or delete zip entries in a zip file.  See the
  742.    command help in help() above. */
  743. {
  744.   int a;                /* attributes of zip file */
  745.   ulg c;                /* start of central directory */
  746.   int d;                /* true if just adding to a zip file */
  747.   char *e;              /* malloc'd comment buffer */
  748.   struct flist far *f;  /* steps through found linked list */
  749.   int i;                /* arg counter, root directory flag */
  750.   int k;                /* next argument type, marked counter,
  751.                            comment size, entry count */
  752.   ulg n;                /* total of entry len's */
  753.   int o;                /* true if there were any ZE_OPEN errors */
  754.   char *p;              /* steps through option arguments */
  755.   char *pp;             /* temporary pointer */
  756.   ulg *cmptime = NULL;  /* pointer to 'before' or 'after' */
  757.   int r;                /* temporary variable */
  758.   int s;                /* flag to read names from stdin */
  759.   ulg t;                /* file time, length of central directory */
  760.   int first_listarg = 0;/* index of first arg of "process these files" list */
  761.   struct zlist far *v;  /* temporary variable */
  762.   struct zlist far * far *w;    /* pointer to last link in zfiles list */
  763.   FILE *x, *y;          /* input and output zip files */
  764.   struct zlist far *z;  /* steps through zfiles linked list */
  765. #if (!defined(VMS) && !defined(CMS_MVS))
  766.   char *zipbuf;         /* stdio buffer for the zip file */
  767. #endif /* !VMS && !CMS_MVS */
  768.   FILE *comment_stream; /* set to stderr if anything is read from stdin */
  769.  
  770. #if defined(__IBMC__) && defined(__DEBUG_ALLOC__)
  771.   {
  772.     extern void DebugMalloc(void);
  773.     atexit(DebugMalloc);
  774.   }
  775. #endif
  776. #ifdef QDOS
  777.   {
  778.       extern void QDOSexit(void);
  779.       atexit(QDOSexit);
  780.   }
  781. #endif
  782.  
  783. #ifdef RISCOS
  784.   set_prefix();
  785. #endif
  786.  
  787. /* Re-initialize global variables to make the zip dll re-entrant. It is
  788.  * possible that we could get away with not re-initializing all of these
  789.  * but better safe than sorry.
  790.  */
  791. #ifdef WINDLL
  792.   action = ADD; /* one of ADD, UPDATE, FRESHEN, or DELETE */
  793.   comadd = 0;   /* 1=add comments for new files */
  794.   zipedit = 0;  /* 1=edit zip comment and all file comments */
  795.   latest = 0;   /* 1=set zip file time to time of latest file */
  796.   before = 0;   /* 0=ignore, else exclude files before this time */
  797.   after = 0;    /* 0=ignore, else exclude files newer than this time */
  798.   test = 0;     /* 1=test zip file with unzip -t */
  799.   tempdir = 0;  /* 1=use temp directory (-b) */
  800.   junk_sfx = 0; /* 1=junk the sfx prefix */
  801.   zipstate = -1;
  802.   tempzip = NULL;
  803.   fcount = 0;
  804.   recurse = 0;         /* 1=recurse into directories; 2=match filenames */
  805.   dispose = 0;         /* 1=remove files after put in zip file */
  806.   pathput = 1;         /* 1=store path with name */
  807.   method = BEST;       /* one of BEST, DEFLATE (only), or STORE (only) */
  808.   dosify = 0;          /* 1=make new entries look like MSDOS */
  809.   verbose = 0;         /* 1=report oddities in zip file structure */
  810.   fix = 0;             /* 1=fix the zip file */
  811.   adjust = 0;          /* 1=adjust offsets for sfx'd file (keep preamble) */
  812.   level = 6;           /* 0=fastest compression, 9=best compression */
  813.   translate_eol = 0;   /* Translate end-of-line LF -> CR LF */
  814. #ifdef WIN32
  815.   use_longname_ea = 0; /* 1=use the .LONGNAME EA as the file's name */
  816.   use_privileges = 0;  /* 1=use security privileges overrides */
  817. #endif
  818.   hidden_files = 0;    /* process hidden and system files */
  819.   volume_label = 0;    /* add volume label */
  820.   dirnames = 1;        /* include directory entries by default */
  821.   linkput = 0;         /* 1=store symbolic links as such */
  822.   noisy = 1;           /* 0=quiet operation */
  823.   extra_fields = 1;    /* 0=do not create extra fields */
  824.   special = ".Z:.zip:.zoo:.arc:.lzh:.arj"; /* List of special suffixes */
  825.   key = NULL;          /* Scramble password if scrambling */
  826.   tempath = NULL;      /* Path for temporary files */
  827.   found = NULL;        /* where in found, or new found entry */
  828.   fnxt = &found;
  829.   patterns = NULL;     /* List of patterns to be matched */
  830.   pcount = 0;          /* number of patterns */
  831.   icount = 0;          /* number of include only patterns */
  832. #endif /* WINDLL */
  833.  
  834.   mesg = (FILE *) stdout; /* cannot be made at link time for VMS */
  835.   comment_stream = (FILE *)stdin;
  836.  
  837.   init_upper();           /* build case map table */
  838.  
  839. #ifdef AMIGA
  840.   if (!VALID_TIMEZONE(p))
  841.     extra_fields = 0;     /* disable storing "UT" time stamps */
  842. #endif /* AMIGA */
  843. #ifdef DOS
  844.   if ((p = getenv("TZ")) == NULL || *p == '\0')
  845.     extra_fields = 0;     /* disable storing "UT" time stamps */
  846. #endif /* DOS */
  847.  
  848. #ifdef VMSCLI
  849.     {
  850.         ulg status = vms_zip_cmdline(&argc, &argv);
  851.         if (!(status & 1))
  852.             return status;
  853.     }
  854. #endif /* VMSCLI */
  855.  
  856.    /* extract extended argument list from environment */
  857.    expand_args(&argc, &argv);
  858.  
  859.  
  860. #ifndef WINDLL
  861.   /* Process arguments */
  862.   diag("processing arguments");
  863.   /* First, check if just the help or version screen should be displayed */
  864.   if (isatty(1)) {              /* output screen is available */
  865.     if (argc == 1)
  866.     {                           /* show help screen */
  867. #ifdef VMSCLI
  868.       VMSCLI_help();
  869. #else
  870.       help();
  871. #endif
  872.       EXIT(0);
  873.     }
  874.     else if (argc == 2 && strcmp(argv[1], "-v") == 0)
  875.     {                           /* show diagnostic version info */
  876.       version_info();
  877.       EXIT(0);
  878.     }
  879.   }
  880. #ifndef VMS
  881. #  ifndef RISCOS
  882.   envargs(&argc, &argv, "ZIPOPT", "ZIP");  /* get options from environment */
  883. #  else /* RISCOS */
  884.   envargs(&argc, &argv, "ZIPOPT", "Zip$Options");  /* get options from environment */
  885.   getRISCOSexts("Zip$Exts");        /* get the extensions to swap from environment */
  886. #  endif /* ? RISCOS */
  887. #else /* VMS */
  888.   envargs(&argc, &argv, "ZIPOPT", "ZIP_OPTS");  /* 4th arg for unzip compat. */
  889. #endif /* ?VMS */
  890. #endif /* !WINDLL */
  891.  
  892.   zipfile = tempzip = NULL;
  893.   tempzf = NULL;
  894.   d = 0;                        /* disallow adding to a zip file */
  895. #ifndef WINDLL
  896.   signal(SIGINT, handler);
  897. #ifdef SIGTERM                  /* AMIGADOS and others have no SIGTERM */
  898.   signal(SIGTERM, handler);
  899. #endif
  900. #endif /* !WINDLL */
  901.   k = 0;                        /* Next non-option argument type */
  902.   s = 0;                        /* set by -@ if -@ is early */
  903.  
  904.   r = get_filters(argc, argv);      /* scan first the -x and -i patterns */
  905. #ifdef WINDLL
  906.   if (r != ZE_OK)
  907.      return r;
  908. #endif
  909.  
  910.   for (i = 1; i < argc; i++)
  911.   {
  912.     if (argv[i][0] == '-')
  913.       if (argv[i][1])
  914.         for (p = argv[i]+1; *p; p++)
  915.           switch (*p)
  916.           {
  917. #ifdef EBCDIC
  918.             case 'a':
  919.               aflag = ASCII;
  920.               printf("Translating to ASCII...\n");
  921.               break;
  922. #endif /* EBCDIC */
  923. #ifdef CMS_MVS
  924.             case 'B':
  925.               bflag = 1;
  926.               printf("Using binary mode...\n");
  927.               break;
  928. #endif /* CMS_MVS */
  929.             case '0':
  930.               method = STORE; level = 0; break;
  931.             case '1':  case '2':  case '3':  case '4':
  932.             case '5':  case '6':  case '7':  case '8':  case '9':
  933.                         /* Set the compression efficacy */
  934.               level = *p - '0';  break;
  935.             case 'A':   /* Adjust unzipsfx'd zipfile:  adjust offsets only */
  936.               adjust = 1; break;
  937.             case 'b':   /* Specify path for temporary file */
  938.               tempdir = 1;
  939.               if (k != 0) {
  940.                 ZIPERR(ZE_PARMS, "use -b before zip file name");
  941.               }
  942.               else
  943.                 k = 1;          /* Next non-option is path */
  944.               break;
  945.             case 'c':   /* Add comments for new files in zip file */
  946.               comadd = 1;  break;
  947.             case 'd':   /* Delete files from zip file */
  948.               if (action != ADD) {
  949.                 ZIPERR(ZE_PARMS, "specify just one action");
  950.               }
  951.               action = DELETE;
  952.               break;
  953.             case 'D':   /* Do not add directory entries */
  954.               dirnames = 0; break;
  955.             case 'e':   /* Encrypt */
  956. #if !CRYPT
  957.               ZIPERR(ZE_PARMS, "encryption not supported");
  958. #else /* CRYPT */
  959.               if (key == NULL) {
  960.                 if ((key = malloc(PWLEN+1)) == NULL) {
  961.                   ZIPERR(ZE_MEM, "was getting encryption password");
  962.                 }
  963.                 r = encr_passwd(ZP_PW_ENTER, key, PWLEN+1, zipfile);
  964.                 if (r != IZ_PW_ENTERED) {
  965.                   if (r < IZ_PW_ENTERED)
  966.                     r = ZE_PARMS;
  967.                   ZIPERR(r, "was getting encryption password");
  968.                 }
  969.                 if (*key == '\0') {
  970.                   ZIPERR(ZE_PARMS, "zero length password not allowed");
  971.                 }
  972.                 if ((e = malloc(PWLEN+1)) == NULL) {
  973.                   ZIPERR(ZE_MEM, "was verifying encryption password");
  974.                 }
  975.                 r = encr_passwd(ZP_PW_VERIFY, e, PWLEN+1, zipfile);
  976.                 if (r != IZ_PW_ENTERED && r != IZ_PW_SKIPVERIFY) {
  977.                   free((zvoid *)e);
  978.                   if (r < ZE_OK) r = ZE_PARMS;
  979.                   ZIPERR(r, "was verifying encryption password");
  980.                 }
  981.                 r = ((r == IZ_PW_SKIPVERIFY) ? 0 : strcmp(key, e));
  982.                 free((zvoid *)e);
  983.                 if (r) {
  984.                   ZIPERR(ZE_PARMS, "password verification failed");
  985.                 }
  986.               }
  987. #endif /* !CRYPT */
  988.               break;
  989.             case 'F':   /* fix the zip file */
  990.               fix++; break;
  991.             case 'f':   /* Freshen zip file--overwrite only */
  992.               if (action != ADD) {
  993.                 ZIPERR(ZE_PARMS, "specify just one action");
  994.               }
  995.               action = FRESHEN;
  996.               break;
  997.             case 'g':   /* Allow appending to a zip file */
  998.               d = 1;  break;
  999. #ifndef WINDLL
  1000.             case 'h': case 'H': case '?':  /* Help */
  1001. #ifdef VMSCLI
  1002.               VMSCLI_help();
  1003. #else
  1004.               help();
  1005. #endif
  1006.               RETURN(finish(ZE_OK));
  1007. #endif /* !WINDLL */
  1008.  
  1009. #ifdef RISCOS
  1010.             case 'I':   /* Don't scan through Image files */
  1011.               scanimage = 0;
  1012.               break;
  1013. #endif
  1014.             case 'j':   /* Junk directory names */
  1015.               pathput = 0;  break;
  1016.             case 'J':   /* Junk sfx prefix */
  1017.               junk_sfx = 1;  break;
  1018.             case 'k':   /* Make entries using DOS names (k for Katz) */
  1019.               dosify = 1;  break;
  1020.             case 'l':   /* Translate end-of-line */
  1021.               translate_eol++; break;
  1022. #ifndef WINDLL
  1023.             case 'L':   /* Show license */
  1024.               license();
  1025.               RETURN(finish(ZE_OK));
  1026. #endif
  1027.             case 'm':   /* Delete files added or updated in zip file */
  1028.               dispose = 1;  break;
  1029.             case 'n':   /* Don't compress files with a special suffix */
  1030.               special = NULL; /* will be set at next argument */
  1031.               break;
  1032. #ifdef AMIGA
  1033.             case 'N':   /* Get zipfile comments from AmigaDOS filenotes */
  1034.               filenotes = 1; break;
  1035. #endif
  1036.             case 'o':   /* Set zip file time to time of latest file in it */
  1037.               latest = 1;  break;
  1038.             case 'p':   /* Store path with name */
  1039.               break;            /* (do nothing as annoyance avoidance) */
  1040.             case 'P':   /* password for encryption */
  1041.               if (k != 0) {
  1042.                 ZIPERR(ZE_PARMS, "use -P before zip file name");
  1043.               }
  1044.               if (key != NULL) {
  1045.                 ZIPERR(ZE_PARMS, "can only have one -P");
  1046.               }
  1047. #if CRYPT
  1048.               k = 7;
  1049. #else
  1050.               ZIPERR(ZE_PARMS, "encryption not supported");
  1051. #endif /* CRYPT */
  1052.               break;
  1053. #if defined(QDOS) || defined(QLZIP)
  1054.             case 'Q':
  1055.               qlflag  = strtol((p+1), &p, 10);
  1056.               if (qlflag == 0) qlflag = 4;
  1057.               p--;
  1058.               break;
  1059. #endif
  1060.             case 'q':   /* Quiet operation */
  1061.               noisy = 0;
  1062.               if (verbose) verbose--;
  1063.               break;
  1064.             case 'r':   /* Recurse into subdirectories, match full path */
  1065.               if (recurse == 2) {
  1066.                 ZIPERR(ZE_PARMS, "do not specify both -r and -R");
  1067.               }
  1068.               recurse = 1;  break;
  1069.             case 'R':   /* Recurse into subdirectories, match filename */
  1070.               if (recurse == 1) {
  1071.                 ZIPERR(ZE_PARMS, "do not specify both -r and -R");
  1072.               }
  1073.               recurse = 2;  break;
  1074. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(ATARI)
  1075.             case 'S':
  1076.               hidden_files = 1; break;
  1077. #endif /* MSDOS || OS2 || WIN32 || ATARI */
  1078.             case 't':   /* Exclude files earlier than specified date */
  1079.               if (p[1] == 't') {
  1080.                 ++p;
  1081.                 cmptime = &after;
  1082.               } else {
  1083.                 cmptime = &before;
  1084.               }
  1085.               if (*cmptime) {
  1086.                 ZIPERR(ZE_PARMS, (cmptime == &after ?
  1087.                        "can only have one -tt" : "can only have one -t"));
  1088.               }
  1089.               k = 2;  break;
  1090.             case 'T':   /* test zip file */
  1091.               test = 1; break;
  1092.             case 'u':   /* Update zip file--overwrite only if newer */
  1093.               if (action != ADD) {
  1094.                 ZIPERR(ZE_PARMS, "specify just one action");
  1095.               }
  1096.               action = UPDATE;
  1097.               break;
  1098.             case 'v':   /* Mention oddities in zip file structure */
  1099.               noisy = 1;
  1100.               verbose++;
  1101.               break;
  1102. #ifdef VMS
  1103.             case 'V':   /* Store in VMS format */
  1104.               vms_native = 1; break;
  1105.             case 'w':   /* Append the VMS version number */
  1106.               vmsver = 1;  break;
  1107. #endif /* VMS */
  1108.             case 'i':   /* Include only the following files */
  1109.             case 'x':   /* Exclude following files */
  1110.               if (p[1] == '@' && p[2] != '\0') {
  1111.                 k = 5;
  1112.                 goto nextarg;
  1113.               }
  1114.               if (k != 4 &&
  1115.                   (k != 3 || (action != UPDATE && action != FRESHEN))) {
  1116.                 ZIPERR(ZE_PARMS, "nothing to select from");
  1117.               }
  1118.               k = 5;
  1119.               break;
  1120. #ifdef S_IFLNK
  1121.             case 'y':   /* Store symbolic links as such */
  1122.               linkput = 1;  break;
  1123. #endif /* S_IFLNK */
  1124.             case 'z':   /* Edit zip file comment */
  1125.               zipedit = 1;  break;
  1126. #if defined(MSDOS) || defined(OS2)
  1127.             case '$':   /* Include volume label */
  1128.               volume_label = 1; break;
  1129. #endif
  1130.             case '@':   /* read file names from stdin */
  1131.               comment_stream = NULL;
  1132.               if (k < 3)        /* zip file not read yet */
  1133.                 s = 1;          /* defer -@ until after zipfile read */
  1134.               else if (strcmp(zipfile, "-") == 0) {
  1135.                 ZIPERR(ZE_PARMS, "can't use - and -@ together");
  1136.               }
  1137.               else              /* zip file read--do it now */
  1138.                 while ((pp = getnam(errbuf, stdin)) != NULL)
  1139.                 {
  1140.                   k = 4;
  1141.                   if ((r = PROCNAME(pp)) != ZE_OK)
  1142.                     if (r == ZE_MISS)
  1143.                       zipwarn("name not matched: ", pp);
  1144.                     else {
  1145.                       ZIPERR(r, pp);
  1146.                     }
  1147.                 }
  1148.               break;
  1149.             case 'X':
  1150.               extra_fields = 0;
  1151.               break;
  1152. #ifdef OS2
  1153.             case 'E':
  1154.               /* use the .LONGNAME EA (if any) as the file's name. */
  1155.               use_longname_ea = 1;
  1156.               break;
  1157. #endif
  1158. #ifdef WIN32
  1159.             case '!':
  1160.               /* use security privilege overrides */
  1161.               use_privileges = 1;
  1162.               break;
  1163. #endif
  1164.             default:
  1165.             {
  1166.               sprintf(errbuf, "no such option: %c", *p);
  1167.               ZIPERR(ZE_PARMS, errbuf);
  1168.             }
  1169.           }
  1170.       else              /* just a dash */
  1171.         switch (k)
  1172.         {
  1173. #ifndef WINDLL
  1174.         case 0:
  1175.           zipstdout();
  1176.           k = 3;
  1177.           if (s) {
  1178.             ZIPERR(ZE_PARMS, "can't use - and -@ together");
  1179.           }
  1180.           break;
  1181. #endif
  1182.         case 1:
  1183.           ZIPERR(ZE_PARMS, "invalid path");
  1184.           break;
  1185.         case 2:
  1186.           ZIPERR(ZE_PARMS, "invalid time");
  1187.           break;
  1188.         case 3:  case 4:
  1189.           comment_stream = NULL;
  1190.           if ((r = PROCNAME(argv[i])) != ZE_OK)
  1191.             if (r == ZE_MISS)
  1192.               zipwarn("name not matched: ", argv[i]);
  1193.             else {
  1194.               ZIPERR(r, argv[i]);
  1195.             }
  1196.           if (k == 3) {
  1197.             first_listarg = i;
  1198.             k = 4;
  1199.           }
  1200.         }
  1201.     else                /* not an option */
  1202.     {
  1203.       if (special == NULL)
  1204.         special = argv[i];
  1205.       else if (k == 5)
  1206.         break; /* -i and -x arguments already scanned */
  1207.       else if (k == 6) {
  1208. #ifdef AMIGA
  1209.         if ((r = PROCNAME("")) != ZE_OK)
  1210. #else
  1211.         if ((r = PROCNAME(".")) != ZE_OK)
  1212. #endif
  1213.           if (r == ZE_MISS)
  1214.             zipwarn("name not matched: ", argv[i]);
  1215.           else {
  1216.             ZIPERR(r, argv[i]);
  1217.           }
  1218.         break;
  1219.       }
  1220.       else switch (k)
  1221.       {
  1222.         case 0:
  1223.           if ((zipfile = ziptyp(argv[i])) == NULL) {
  1224.             ZIPERR(ZE_MEM, "was processing arguments");
  1225.           }
  1226.           if ((r = readzipfile()) != ZE_OK) {
  1227.             ZIPERR(r, zipfile);
  1228.           }
  1229.           k = 3;
  1230.           if (s)
  1231.           {
  1232.             while ((pp = getnam(errbuf, stdin)) != NULL)
  1233.             {
  1234.               k = 4;
  1235.               if ((r = PROCNAME(pp)) != ZE_OK)
  1236.                 if (r == ZE_MISS)
  1237.                   zipwarn("name not matched: ", pp);
  1238.                 else {
  1239.                   ZIPERR(r, pp);
  1240.                 }
  1241.             }
  1242.             s = 0;
  1243.           }
  1244.           if (recurse == 2)
  1245.             k = 6;
  1246.           break;
  1247.         case 1:
  1248.           if ((tempath = malloc(strlen(argv[i]) + 1)) == NULL) {
  1249.             ZIPERR(ZE_MEM, "was processing arguments");
  1250.           }
  1251.           strcpy(tempath, argv[i]);
  1252.           k = 0;
  1253.           break;
  1254.         case 2:
  1255.         {
  1256.           int yyyy, mm, dd;       /* results of sscanf() */
  1257.  
  1258.           if (sscanf(argv[i], "%2d%2d%4d", &mm, &dd, &yyyy) != 3 ||
  1259.               mm < 1 || mm > 12 || dd < 1 || dd > 31) {
  1260.             ZIPERR(ZE_PARMS, (cmptime == &after ?
  1261.                    "invalid date entered for -tt option" :
  1262.                    "invalid date entered for -t option"));
  1263.           }
  1264.           *cmptime = dostime(yyyy, mm, dd, 0, 0, 0);
  1265.           k = 0;
  1266.           break;
  1267.         }
  1268.         case 3:  case 4:
  1269.           if ((r = PROCNAME(argv[i])) != ZE_OK)
  1270.             if (r == ZE_MISS)
  1271.               zipwarn("name not matched: ", argv[i]);
  1272.             else {
  1273.               ZIPERR(r, argv[i]);
  1274.             }
  1275.           if (k == 3) {
  1276.             first_listarg = i;
  1277.             k = 4;
  1278.           }
  1279.           break;
  1280.         case 7:
  1281.           if ((key = malloc(strlen(argv[i]) + 1)) == NULL) {
  1282.             ZIPERR(ZE_MEM, "was processing arguments");
  1283.           }
  1284.           strcpy(key, argv[i]);
  1285.           k = 0;
  1286.       }
  1287.     }
  1288. nextarg: ;
  1289.   }
  1290.   if (k == 7 || k == 1) {
  1291.     ZIPERR(ZE_PARMS, "missing argument for -b or -P");
  1292.   }
  1293.  
  1294. #if (defined(MSDOS) || defined(OS2)) && !defined(WIN32)
  1295.   if ((k == 3 || k == 4) && volume_label == 1) {
  1296.     PROCNAME(NULL);
  1297.     k = 4;
  1298.   }
  1299. #endif
  1300.  
  1301. #ifndef WINDLL
  1302.   if (k < 3) {               /* zip used as filter */
  1303.     zipstdout();
  1304.     comment_stream = NULL;
  1305.     if ((r = procname("-")) != ZE_OK)
  1306.       if (r == ZE_MISS)
  1307.         zipwarn("name not matched: ", "-");
  1308.       else {
  1309.         ZIPERR(r, "-");
  1310.       }
  1311.     k = 4;
  1312.     if (s) {
  1313.       ZIPERR(ZE_PARMS, "can't use - and -@ together");
  1314.     }
  1315.   }
  1316. #endif
  1317.  
  1318.   /* Clean up selections ("3 <= k <= 5" now) */
  1319.   if (k != 4 && first_listarg == 0 &&
  1320.       (action == UPDATE || action == FRESHEN)) {
  1321.     /* if -u or -f with no args, do all, but, when present, apply filters */
  1322.     for (z = zfiles; z != NULL; z = z->nxt) {
  1323.       z->mark = pcount ? filter(z->zname) : 1;
  1324.     }
  1325.   }
  1326.   if ((r = check_dup()) != ZE_OK)     /* remove duplicates in found list */
  1327.     if (r == ZE_PARMS) {
  1328.       ZIPERR(r, "cannot repeat names in zip file");
  1329.     }
  1330.     else {
  1331.       ZIPERR(r, "was processing list of files");
  1332.     }
  1333.  
  1334.   if (zcount)
  1335.     free((zvoid *)zsort);
  1336.  
  1337.   /* Check option combinations */
  1338.   if (special == NULL) {
  1339.     ZIPERR(ZE_PARMS, "missing suffix list");
  1340.   }
  1341.   if (level == 9 || !strcmp(special, ";") || !strcmp(special, ":"))
  1342.     special = NULL; /* compress everything */
  1343.  
  1344.   if (action == DELETE && (method != BEST || dispose || recurse ||
  1345.       key != NULL || comadd || zipedit)) {
  1346.     zipwarn("invalid option(s) used with -d; ignored.","");
  1347.     /* reset flags - needed? */
  1348.     method  = BEST;
  1349.     dispose = 0;
  1350.     recurse = 0;
  1351.     if (key != NULL)
  1352.       free((zvoid *)key);
  1353.     key     = NULL;
  1354.     comadd  = 0;
  1355.     zipedit = 0;
  1356.   }
  1357.   if (linkput && dosify)
  1358.     {
  1359.       zipwarn("can't use -y with -k, -y ignored", "");
  1360.       linkput = 0;
  1361.     }
  1362.   if (fix && adjust)
  1363.     {
  1364.       zipwarn("can't use -F with -A, -F ignored", "");
  1365.     }
  1366.   if (test && !strcmp(zipfile, "-")) {
  1367.     test = 0;
  1368.     zipwarn("can't use -T on stdout, -T ignored", "");
  1369.   }
  1370.   if ((action != ADD || d) && !strcmp(zipfile, "-")) {
  1371.     ZIPERR(ZE_PARMS, "can't use -d,-f,-u or -g on stdout\n");
  1372.   }
  1373. #ifdef EBCDIC
  1374.   if (aflag==ASCII && !translate_eol) {
  1375.     /* Translation to ASCII implies EOL translation!
  1376.      * The default translation mode is "UNIX" mode (single LF terminators).
  1377.      */
  1378.     translate_eol = 1;
  1379.   }
  1380. #endif
  1381. #ifdef CMS_MVS
  1382.   if (aflag==ASCII && bflag)
  1383.     ZIPERR(ZE_PARMS, "can't use -a with -B");
  1384. #endif
  1385. #ifdef VMS
  1386.   if (!extra_fields && vms_native)
  1387.     {
  1388.       zipwarn("can't use -V with -X, -V ignored", "");
  1389.       vms_native = 0;
  1390.     }
  1391.   if (vms_native && translate_eol)
  1392.     ZIPERR(ZE_PARMS, "can't use -V with -l");
  1393. #endif
  1394.   if (zcount == 0 && (action != ADD || d)) {
  1395.     zipwarn(zipfile, " not found or empty");
  1396. #ifdef WINDLL
  1397.     return ZE_NAME;
  1398. #endif
  1399.   }
  1400.  
  1401. /*
  1402.  * XXX make some kind of mktemppath() function for each OS.
  1403.  */
  1404.  
  1405. #ifndef VM_CMS
  1406. /* For CMS, leave tempath NULL.  A-disk will be used as default. */
  1407.   /* If -b not specified, make temporary path the same as the zip file */
  1408. #if defined(MSDOS) || defined(__human68k__) || defined(AMIGA)
  1409.   if (tempath == NULL && ((p = strrchr(zipfile, '/')) != NULL ||
  1410. #  ifdef MSDOS
  1411.                           (p = strrchr(zipfile, '\\')) != NULL ||
  1412. #  endif /* MSDOS */
  1413.                           (p = strrchr(zipfile, ':')) != NULL))
  1414.   {
  1415.     if (*p == ':')
  1416.       p++;
  1417. #else
  1418. #ifdef RISCOS
  1419.   if (tempath == NULL && (p = strrchr(zipfile, '.')) != NULL)
  1420.   {
  1421. #else
  1422. #ifdef QDOS
  1423.   if (tempath == NULL && (p = LastDir(zipfile)) != NULL)
  1424.   {
  1425. #else
  1426.   if (tempath == NULL && (p = strrchr(zipfile, '/')) != NULL)
  1427.   {
  1428. #endif /* QDOS */
  1429. #endif /* RISCOS */
  1430. #endif /* MSDOS || __human68k__ || AMIGA */
  1431.     if ((tempath = malloc((int)(p - zipfile) + 1)) == NULL) {
  1432.       ZIPERR(ZE_MEM, "was processing arguments");
  1433.     }
  1434.     r = *p;  *p = 0;
  1435.     strcpy(tempath, zipfile);
  1436.     *p = (char)r;
  1437.   }
  1438. #endif /* VM_CMS */
  1439.  
  1440.   /* For each marked entry, if not deleting, check if it exists, and if
  1441.      updating or freshening, compare date with entry in old zip file.
  1442.      Unmark if it doesn't exist or is too old, else update marked count. */
  1443.   diag("stating marked entries");
  1444.   k = 0;                        /* Initialize marked count */
  1445.   for (z = zfiles; z != NULL; z = z->nxt)
  1446.     if (z->mark) {
  1447. #ifdef USE_EF_UT_TIME
  1448.       iztimes f_utim, z_utim;
  1449. #endif /* USE_EF_UT_TIME */
  1450.       sprintf(errbuf, "zip diagnostic: marked file=%s\n", z->zname);
  1451.       diag(errbuf);
  1452.  
  1453.       if (action != DELETE &&
  1454. #ifdef USE_EF_UT_TIME
  1455.           ((t = filetime(z->name, (ulg *)NULL, (long *)NULL, &f_utim))
  1456. #else /* !USE_EF_UT_TIME */
  1457.           ((t = filetime(z->name, (ulg *)NULL, (long *)NULL, (iztimes *)NULL))
  1458. #endif /* ?USE_EF_UT_TIME */
  1459.               == 0 ||
  1460.            t < before || (after && t >= after) ||
  1461.            ((action == UPDATE || action == FRESHEN) &&
  1462. #ifdef USE_EF_UT_TIME
  1463.             ((get_ef_ut_ztime(z, &z_utim) & EB_UT_FL_MTIME) ?
  1464.              f_utim.mtime <= ROUNDED_TIME(z_utim.mtime) : t <= z->tim)
  1465. #else /* !USE_EF_UT_TIME */
  1466.             t <= z->tim
  1467. #endif /* ?USE_EF_UT_TIME */
  1468.            )))
  1469.       {
  1470.         z->mark = comadd ? 2 : 0;
  1471.         z->trash = t && t >= before &&
  1472.                    (after == 0 || t < after);   /* delete if -um or -fm */
  1473.         if (verbose) {
  1474.           fprintf(mesg, "zip diagnostic: %s %s\n", z->zname,
  1475.                  z->trash ? "up to date" : "missing or early");
  1476.         }
  1477.       }
  1478.       else
  1479.         k++;
  1480.     }
  1481.  
  1482.   /* Remove entries from found list that do not exist or are too old */
  1483.   diag("stating new entries");
  1484.   sprintf(errbuf, "zip diagnostic: fcount=%d\n", (int)fcount);
  1485.   diag(errbuf);
  1486.   for (f = found; f != NULL;) {
  1487.     sprintf(errbuf, "zip diagnostic: new file=%s\n", f->zname);
  1488.     diag(errbuf);
  1489.     if (action == DELETE || action == FRESHEN ||
  1490.         (t = filetime(f->name, (ulg *)NULL, (long *)NULL, (iztimes *)NULL))
  1491.            == 0 ||
  1492.         t < before || (after && t >= after) ||
  1493.         (namecmp(f->zname, zipfile) == 0 && strcmp(zipfile, "-")))
  1494.       f = fexpel(f);
  1495.     else
  1496.       f = f->nxt;
  1497.   }
  1498.  
  1499.   /* Make sure there's something left to do */
  1500.   if (k == 0 && found == NULL &&
  1501.       !(zfiles != NULL &&
  1502.         (latest || fix || adjust || junk_sfx || comadd || zipedit))) {
  1503.     if (test && (zfiles != NULL || zipbeg != 0)) {
  1504. #ifndef WINDLL
  1505.       check_zipfile(zipfile, argv[0]);
  1506. #endif
  1507.       RETURN(finish(ZE_OK));
  1508.     }
  1509.     if (action == UPDATE || action == FRESHEN) {
  1510.       RETURN(finish(ZE_OK));
  1511.     }
  1512.     else if (zfiles == NULL && (latest || fix || adjust || junk_sfx)) {
  1513.       ZIPERR(ZE_NAME, zipfile);
  1514.     }
  1515. #ifndef WINDLL
  1516.     else if (recurse && (pcount == 0) && (first_listarg > 0)) {
  1517. #ifdef VMS
  1518.       strcpy(errbuf, "try: zip \"");
  1519.       for (i = 1; i < (first_listarg - 1); i++)
  1520.         strcat(strcat(errbuf, argv[i]), "\" ");
  1521.       strcat(strcat(errbuf, argv[i]), " *.* -i");
  1522. #else /* !VMS */
  1523.       strcpy(errbuf, "try: zip");
  1524.       for (i = 1; i < first_listarg; i++)
  1525.         strcat(strcat(errbuf, " "), argv[i]);
  1526. #  ifdef AMIGA
  1527.       strcat(errbuf, " \"\" -i");
  1528. #  else
  1529.       strcat(errbuf, " . -i");
  1530. #  endif
  1531. #endif /* ?VMS */
  1532.       for (i = first_listarg; i < argc; i++)
  1533.         strcat(strcat(errbuf, " "), argv[i]);
  1534.       ZIPERR(ZE_NONE, errbuf);
  1535.     }
  1536. #endif /* !WINDLL */
  1537.     else {
  1538.       ZIPERR(ZE_NONE, zipfile);
  1539.     }
  1540.   }
  1541.   d = (d && k == 0 && (zipbeg || zfiles != NULL)); /* d true if appending */
  1542.  
  1543. #if CRYPT
  1544.   /* Initialize the crc_32_tab pointer, when encryption was requested. */
  1545.   if (key != NULL)
  1546.     crc_32_tab = (ulg near *)get_crc_table();
  1547. #endif /* CRYPT */
  1548.  
  1549.   /* Before we get carried away, make sure zip file is writeable. This
  1550.    * has the undesired side effect of leaving one empty junk file on a WORM,
  1551.    * so when the zipfile does not exist already and when -b is specified,
  1552.    * the writability check is made in replace().
  1553.    */
  1554.   if (strcmp(zipfile, "-"))
  1555.   {
  1556.     if (tempdir && zfiles == NULL && zipbeg == 0) {
  1557.       a = 0;
  1558.     } else {
  1559.        x = zfiles == NULL && zipbeg == 0 ? fopen(zipfile, FOPW) :
  1560.                                            fopen(zipfile, FOPM);
  1561.       /* Note: FOPW and FOPM expand to several parameters for VMS */
  1562.       if (x == NULL) {
  1563.         ZIPERR(ZE_CREAT, zipfile);
  1564.       }
  1565.       fclose(x);
  1566.       a = getfileattr(zipfile);
  1567.       if (zfiles == NULL && zipbeg == 0)
  1568.         destroy(zipfile);
  1569.     }
  1570.   }
  1571.   else
  1572.     a = 0;
  1573.  
  1574.   /* Throw away the garbage in front of the zip file for -J */
  1575.   if (junk_sfx) zipbeg = 0;
  1576.  
  1577.   /* Open zip file and temporary output file */
  1578.   diag("opening zip file and creating temporary zip file");
  1579.   x = NULL;
  1580.   tempzn = 0;
  1581.   if (strcmp(zipfile, "-") == 0)
  1582.   {
  1583. #if defined(MSDOS) || defined(__human68k__)
  1584.     /* Set stdout mode to binary for MSDOS systems */
  1585. #  ifdef __HIGHC__
  1586.     setmode(stdout, _BINARY);
  1587. #  else
  1588.     setmode(fileno(stdout), O_BINARY);
  1589. #  endif
  1590.     tempzf = y = fdopen(fileno(stdout), FOPW);
  1591. #else
  1592.     tempzf = y = stdout;
  1593. #endif
  1594.     /* tempzip must be malloced so a later free won't barf */
  1595.     tempzip = malloc(4);
  1596.     if (tempzip == NULL) {
  1597.       ZIPERR(ZE_MEM, "allocating temp filename");
  1598.     }
  1599.     strcpy(tempzip, "-");
  1600.   }
  1601.   else if (d) /* d true if just appending (-g) */
  1602.   {
  1603.     if ((y = fopen(zipfile, FOPM)) == NULL) {
  1604.       ZIPERR(ZE_NAME, zipfile);
  1605.     }
  1606.     tempzip = zipfile;
  1607.     tempzf = y;
  1608.     if (fseek(y, cenbeg, SEEK_SET)) {
  1609.       ZIPERR(ferror(y) ? ZE_READ : ZE_EOF, zipfile);
  1610.     }
  1611.     tempzn = cenbeg;
  1612.   }
  1613.   else
  1614.   {
  1615.     if ((zfiles != NULL || zipbeg) && (x = fopen(zipfile, FOPR_EX)) == NULL) {
  1616.       ZIPERR(ZE_NAME, zipfile);
  1617.     }
  1618.     if ((tempzip = tempname(zipfile)) == NULL) {
  1619.       ZIPERR(ZE_MEM, "allocating temp filename");
  1620.     }
  1621.     if ((tempzf = y = fopen(tempzip, FOPW)) == NULL) {
  1622.       ZIPERR(ZE_TEMP, tempzip);
  1623.     }
  1624.   }
  1625.  
  1626. #if (!defined(VMS) && !defined(CMS_MVS))
  1627.   /* Use large buffer to speed up stdio: */
  1628. #if (defined(_IOFBF) || !defined(BUFSIZ))
  1629.   zipbuf = (char *)malloc(ZBSZ);
  1630. #else
  1631.   zipbuf = (char *)malloc(BUFSIZ);
  1632. #endif
  1633.   if (zipbuf == NULL) {
  1634.     ZIPERR(ZE_MEM, tempzip);
  1635.   }
  1636. # ifdef _IOFBF
  1637.   setvbuf(y, zipbuf, _IOFBF, ZBSZ);
  1638. # else
  1639.   setbuf(y, zipbuf);
  1640. # endif /* _IOBUF */
  1641. #endif /* !VMS  && !CMS_MVS */
  1642.  
  1643.   if (strcmp(zipfile, "-") != 0 && !d)  /* this must go *after* set[v]buf */
  1644.   {
  1645.     if (zipbeg && (r = fcopy(x, y, zipbeg)) != ZE_OK) {
  1646.       ZIPERR(r, r == ZE_TEMP ? tempzip : zipfile);
  1647.       }
  1648.     tempzn = zipbeg;
  1649.   }
  1650.  
  1651.   o = 0;                                /* no ZE_OPEN errors yet */
  1652.  
  1653.  
  1654.   /* Process zip file, updating marked files */
  1655. #ifdef DEBUG
  1656.   if (zfiles != NULL)
  1657.     diag("going through old zip file");
  1658. #endif
  1659.   w = &zfiles;
  1660.   while ((z = *w) != NULL) {
  1661.     if (z->mark == 1)
  1662.     {
  1663.       /* if not deleting, zip it up */
  1664.       if (action != DELETE)
  1665.       {
  1666.         if (noisy)
  1667.         {
  1668.           if (action == FRESHEN)
  1669.              fprintf(mesg, "freshening: %s", z->zname);
  1670.           else
  1671.              fprintf(mesg, "updating: %s", z->zname);
  1672.           fflush(mesg);
  1673.         }
  1674.         if ((r = zipup(z, y)) != ZE_OK && r != ZE_OPEN && r != ZE_MISS)
  1675.         {
  1676.           if (noisy)
  1677.           {
  1678. #ifndef WINDLL
  1679.             putc('\n', mesg);
  1680.             fflush(mesg);
  1681. #else
  1682.             fprintf(stdout,"\n");
  1683. #endif
  1684.           }
  1685.           sprintf(errbuf, "was zipping %s", z->name);
  1686.           ZIPERR(r, errbuf);
  1687.         }
  1688.         if (r == ZE_OPEN || r == ZE_MISS)
  1689.         {
  1690.           o = 1;
  1691.           if (noisy)
  1692.           {
  1693. #ifndef WINDLL
  1694.             putc('\n', mesg);
  1695.             fflush(mesg);
  1696. #else
  1697.             fprintf(stdout,"\n");
  1698. #endif
  1699.           }
  1700.           if (r == ZE_OPEN) {
  1701.             perror(z->zname);
  1702.             zipwarn("could not open for reading: ", z->zname);
  1703.           } else {
  1704.             zipwarn("file and directory with the same name: ", z->zname);
  1705.           }
  1706.           zipwarn("will just copy entry over: ", z->zname);
  1707.           if ((r = zipcopy(z, x, y)) != ZE_OK)
  1708.           {
  1709.             sprintf(errbuf, "was copying %s", z->zname);
  1710.             ZIPERR(r, errbuf);
  1711.           }
  1712.           z->mark = 0;
  1713.         }
  1714.         w = &z->nxt;
  1715.       }
  1716.       else
  1717.       {
  1718.         if (noisy)
  1719.         {
  1720.           fprintf(mesg, "deleting: %s\n", z->zname);
  1721.           fflush(mesg);
  1722.         }
  1723.         v = z->nxt;                     /* delete entry from list */
  1724.         free((zvoid *)(z->iname));
  1725.         free((zvoid *)(z->zname));
  1726.         if (z->ext)
  1727.           free((zvoid *)(z->extra));
  1728.         if (z->cext && z->cextra != z->extra)
  1729.           free((zvoid *)(z->cextra));
  1730.         if (z->com)
  1731.           free((zvoid *)(z->comment));
  1732.         farfree((zvoid far *)z);
  1733.         *w = v;
  1734.         zcount--;
  1735.       }
  1736.     }
  1737.     else
  1738.     {
  1739.       /* copy the original entry verbatim */
  1740.       if (!d && (r = zipcopy(z, x, y)) != ZE_OK)
  1741.       {
  1742.         sprintf(errbuf, "was copying %s", z->zname);
  1743.         ZIPERR(r, errbuf);
  1744.       }
  1745.       w = &z->nxt;
  1746.     }
  1747.   }
  1748.  
  1749.  
  1750.   /* Process the edited found list, adding them to the zip file */
  1751.   diag("zipping up new entries, if any");
  1752.   sprintf(errbuf, "zip diagnostic: fcount=%d\n", (int)fcount);
  1753.   diag(errbuf);
  1754.   for (f = found; f != NULL; f = fexpel(f))
  1755.   {
  1756.     /* add a new zfiles entry and set the name */
  1757.     if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  1758.       ZIPERR(ZE_MEM, "was adding files to zip file");
  1759.     }
  1760.     z->nxt = NULL;
  1761.     z->name = f->name;
  1762.     f->name = NULL;
  1763.     z->iname = f->iname;
  1764.     f->iname = NULL;
  1765.     z->zname = f->zname;
  1766.     f->zname = NULL;
  1767.     z->ext = z->cext = z->com = 0;
  1768.     z->extra = z->cextra = NULL;
  1769.     z->mark = 1;
  1770.     z->dosflag = f->dosflag;
  1771.     /* zip it up */
  1772.     if (noisy)
  1773.     {
  1774.       fprintf(mesg, "  adding: %s", z->zname);
  1775.       fflush(mesg);
  1776.     }
  1777.     if ((r = zipup(z, y)) != ZE_OK  && r != ZE_OPEN && r != ZE_MISS)
  1778.     {
  1779.       if (noisy)
  1780.       {
  1781. #ifndef WINDLL
  1782.         putc('\n', mesg);
  1783.         fflush(mesg);
  1784. #else
  1785.         fprintf(stdout,"\n");
  1786. #endif
  1787.       }
  1788.       sprintf(errbuf, "was zipping %s", z->zname);
  1789.       ZIPERR(r, errbuf);
  1790.     }
  1791.     if (r == ZE_OPEN || r == ZE_MISS)
  1792.     {
  1793.       o = 1;
  1794.       if (noisy)
  1795.       {
  1796. #ifndef WINDLL
  1797.         putc('\n', mesg);
  1798.         fflush(mesg);
  1799. #else
  1800.         fprintf(stdout,"\n");
  1801. #endif
  1802.       }
  1803.       if (r == ZE_OPEN) {
  1804.         perror("zip warning");
  1805.         zipwarn("could not open for reading: ", z->zname);
  1806.       } else {
  1807.         zipwarn("file and directory with the same name: ", z->zname);
  1808.       }
  1809.       free((zvoid *)(z->name));
  1810.       free((zvoid *)(z->iname));
  1811.       free((zvoid *)(z->zname));
  1812.       farfree((zvoid far *)z);
  1813.     }
  1814.     else
  1815.     {
  1816.       *w = z;
  1817.       w = &z->nxt;
  1818.       zcount++;
  1819.     }
  1820.   }
  1821.   if (key != NULL)
  1822.   {
  1823.     free((zvoid *)key);
  1824.     key = NULL;
  1825.   }
  1826.  
  1827.  
  1828.   /* Get one line comment for each new entry */
  1829. #ifdef AMIGA
  1830.   if (comadd || filenotes)
  1831.   {
  1832.     if (comadd)
  1833. #else
  1834.   if (comadd)
  1835.   {
  1836. #endif
  1837.     {
  1838.       if (comment_stream == NULL) {
  1839. #ifndef RISCOS
  1840.         comment_stream = (FILE*)fdopen(fileno(stderr), "r");
  1841. #else
  1842.         comment_stream = stderr;
  1843. #endif
  1844.       }
  1845.       if ((e = malloc(MAXCOM + 1)) == NULL) {
  1846.         ZIPERR(ZE_MEM, "was reading comment lines");
  1847.       }
  1848.     }
  1849.     for (z = zfiles; z != NULL; z = z->nxt)
  1850.       if (z->mark)
  1851. #ifdef AMIGA
  1852.         if (filenotes && (p = GetComment(z->zname)))
  1853.         {
  1854.           if (z->comment = malloc(k = strlen(p)+1))
  1855.           {
  1856.             z->com = k;
  1857.             strcpy(z->comment, p);
  1858.           }
  1859.           else
  1860.           {
  1861.             free((zvoid *)e);
  1862.             ZIPERR(ZE_MEM, "was reading filenotes");
  1863.           }
  1864.         }
  1865.         else if (comadd)
  1866. #endif
  1867.         {
  1868.           if (noisy)
  1869.             fprintf(mesg, "Enter comment for %s:\n", z->zname);
  1870.           if (fgets(e, MAXCOM+1, comment_stream) != NULL)
  1871.           {
  1872.             if ((p = malloc((k = strlen(e))+1)) == NULL)
  1873.             {
  1874.               free((zvoid *)e);
  1875.               ZIPERR(ZE_MEM, "was reading comment lines");
  1876.             }
  1877.             strcpy(p, e);
  1878.             if (p[k-1] == '\n')
  1879.               p[--k] = 0;
  1880.             z->comment = p;
  1881.             z->com = k;
  1882.           }
  1883.         }
  1884. #ifdef AMIGA
  1885.     if (comadd)
  1886.       free((zvoid *)e);
  1887.     GetComment(NULL);           /* makes it free its internal storage */
  1888. #else
  1889.     free((zvoid *)e);
  1890. #endif
  1891.   }
  1892.  
  1893.   /* Get multi-line comment for the zip file */
  1894.   if (zipedit)
  1895.   {
  1896. #ifndef WINDLL
  1897.     if (comment_stream == NULL) {
  1898. #ifndef RISCOS
  1899.       comment_stream = (FILE*)fdopen(fileno(stderr), "r");
  1900. #else
  1901.       comment_stream = stderr;
  1902. #endif
  1903.     }
  1904.     if ((e = malloc(MAXCOM + 1)) == NULL) {
  1905.       ZIPERR(ZE_MEM, "was reading comment lines");
  1906.     }
  1907.     if (noisy && zcomlen)
  1908.     {
  1909.       fputs("current zip file comment is:\n", mesg);
  1910.       fwrite(zcomment, 1, zcomlen, mesg);
  1911.       if (zcomment[zcomlen-1] != '\n')
  1912.         putc('\n', mesg);
  1913.       free((zvoid *)zcomment);
  1914.     }
  1915.     if ((zcomment = malloc(1)) == NULL)
  1916.       ZIPERR(ZE_MEM, "was setting comments to null");
  1917.     zcomment[0] = '\0';
  1918.     if (noisy)
  1919.       fputs("enter new zip file comment (end with .):\n", mesg);
  1920. #if (defined(AMIGA) && (defined(LATTICE)||defined(__SASC)))
  1921.     flushall();  /* tty input/output is out of sync here */
  1922. #endif
  1923.     while (fgets(e, MAXCOM+1, comment_stream) != NULL && strcmp(e, ".\n"))
  1924.     {
  1925.       if (e[(r = strlen(e)) - 1] == '\n')
  1926.         e[--r] = 0;
  1927.       if ((p = malloc((*zcomment ? strlen(zcomment) + 3 : 1) + r)) == NULL)
  1928.       {
  1929.         free((zvoid *)e);
  1930.         ZIPERR(ZE_MEM, "was reading comment lines");
  1931.       }
  1932.       if (*zcomment)
  1933.         strcat(strcat(strcpy(p, zcomment), "\r\n"), e);
  1934.       else
  1935.         strcpy(p, *e ? e : "\r\n");
  1936.       free((zvoid *)zcomment);
  1937.       zcomment = p;
  1938.     }
  1939.     free((zvoid *)e);
  1940. #else /* WINDLL */
  1941.     comment(zcomlen);
  1942.     if ((p = malloc(strlen(szCommentBuf)+1)) == NULL) {
  1943.       ZIPERR(ZE_MEM, "was setting comments to null");
  1944.     }
  1945.     if (szCommentBuf[0] != '\0')
  1946.        lstrcpy(p, szCommentBuf);
  1947.     else
  1948.        p[0] = '\0';
  1949.     free((zvoid *)zcomment);
  1950.     GlobalUnlock(hStr);
  1951.     GlobalFree(hStr);
  1952.     zcomment = p;
  1953. #endif /* WINDLL */
  1954.     zcomlen = strlen(zcomment);
  1955.   }
  1956.  
  1957.  
  1958.   /* Write central directory and end header to temporary zip */
  1959.   diag("writing central directory");
  1960.   k = 0;                        /* keep count for end header */
  1961.   c = tempzn;                   /* get start of central */
  1962.   n = t = 0;
  1963.   for (z = zfiles; z != NULL; z = z->nxt)
  1964.   {
  1965.     if ((r = putcentral(z, y)) != ZE_OK) {
  1966.       ZIPERR(r, tempzip);
  1967.     }
  1968.     tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  1969.     n += z->len;
  1970.     t += z->siz;
  1971.     k++;
  1972.   }
  1973.   if (k == 0)
  1974.     zipwarn("zip file empty", "");
  1975.   if (verbose)
  1976.     fprintf(mesg, "total bytes=%lu, compressed=%lu -> %d%% savings\n",
  1977.            n, t, percent(n, t));
  1978.   t = tempzn - c;               /* compute length of central */
  1979.   diag("writing end of central directory");
  1980.   if ((r = putend(k, t, c, zcomlen, zcomment, y)) != ZE_OK) {
  1981.     ZIPERR(r, tempzip);
  1982.   }
  1983.   tempzf = NULL;
  1984.   if (fclose(y)) {
  1985.     ZIPERR(d ? ZE_WRITE : ZE_TEMP, tempzip);
  1986.   }
  1987.   if (x != NULL)
  1988.     fclose(x);
  1989.  
  1990.   /* Free some memory before spawning unzip */
  1991.   lm_free();
  1992.  
  1993. #ifndef WINDLL
  1994.   /* Test new zip file before overwriting old one or removing input files */
  1995.   if (test)
  1996.     check_zipfile(tempzip, argv[0]);
  1997. #endif
  1998.   /* Replace old zip file with new zip file, leaving only the new one */
  1999.   if (strcmp(zipfile, "-") && !d)
  2000.   {
  2001.     diag("replacing old zip file with new zip file");
  2002.     if ((r = replace(zipfile, tempzip)) != ZE_OK)
  2003.     {
  2004.       zipwarn("new zip file left as: ", tempzip);
  2005.       free((zvoid *)tempzip);
  2006.       tempzip = NULL;
  2007.       ZIPERR(r, "was replacing the original zip file");
  2008.     }
  2009.     free((zvoid *)tempzip);
  2010.   }
  2011.   tempzip = NULL;
  2012.   if (a && strcmp(zipfile, "-")) {
  2013.     setfileattr(zipfile, a);
  2014. #ifdef VMS
  2015.     /* If the zip file existed previously, restore its record format: */
  2016.     if (x != NULL)
  2017.       (void)VMSmunch(zipfile, RESTORE_RTYPE, NULL);
  2018. #endif
  2019.   }
  2020.  
  2021. #ifdef __BEOS__
  2022.   /* Set the filetype of the zipfile to "application/zip" */
  2023.   setfiletype( zipfile, "application/zip" );
  2024. #endif
  2025.  
  2026. #ifdef RISCOS
  2027.   /* Set the filetype of the zipfile to &DDC */
  2028.   setfiletype(zipfile,0xDDC);
  2029. #endif
  2030.  
  2031.   /* Finish up (process -o, -m, clean up).  Exit code depends on o. */
  2032. #if (!defined(VMS) && !defined(CMS_MVS))
  2033.   free((zvoid *) zipbuf);
  2034. #endif /* !VMS && !CMS_MVS */
  2035.   RETURN(finish(o ? ZE_OPEN : ZE_OK));
  2036. }
  2037.