home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / zip.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  54KB  |  1,962 lines

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