home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip201.zip / zip.c < prev    next >
C/C++ Source or Header  |  1993-09-20  |  39KB  |  1,270 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1993 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel 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 "revision.h"
  16. #include "zip.h"
  17. #include "crypt.h"
  18. #ifdef VMS
  19. #  include "VMSmunch.h"
  20. #endif
  21. #if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__)
  22. #  include <process.h>
  23. #endif
  24. #include <signal.h>
  25.  
  26. #ifdef MACOS
  27. #  include <console.h>
  28. #endif
  29.  
  30. #define MAXCOM 256      /* Maximum one-line comment size */
  31.  
  32.  
  33. /* Local option flags */
  34. #define DELETE  0
  35. #define ADD     1
  36. #define UPDATE  2
  37. #define FRESHEN 3
  38. local int action = ADD; /* one of ADD, UPDATE, FRESHEN, or DELETE */
  39. local int comadd = 0;   /* 1=add comments for new files */
  40. local int zipedit = 0;  /* 1=edit zip comment and all file comments */
  41. local int latest = 0;   /* 1=set zip file time to time of latest file */
  42. local ulg before = 0;   /* 0=ignore, else exclude files before this time */
  43. local int test = 0;     /* 1=test zip file with unzip -t */
  44. local int tempdir = 0;  /* 1=use temp directory (-b) */
  45.  
  46. /* Temporary zip file name and file pointer */
  47. local char *tempzip;
  48. local FILE *tempzf;
  49.  
  50. /* Local functions */
  51.  
  52. local void freeup  OF((void));
  53. local void leave   OF((int));
  54. local void handler OF((int));
  55. local void license OF((void));
  56. local void help    OF((void));
  57. local void zipstdout OF((void));
  58. local void check_zipfile OF((char *zipname));
  59. local void get_filters OF((int argc, char **argv));
  60.       int  main     OF((int, char **));
  61. local int count_args OF((char *s));
  62. local void envargs   OF((int *Pargc, char ***Pargv, char *envstr));
  63.  
  64.  
  65.  
  66. local void freeup()
  67. /* Free all allocations in the found list and the zfiles list */
  68. {
  69.   struct flist far *f;  /* steps through found list */
  70.   struct zlist far *z;  /* pointer to next entry in zfiles list */
  71.  
  72.   for (f = found; f != NULL; f = fexpel(f))
  73.     ;
  74.   while (zfiles != NULL)
  75.   {
  76.     z = zfiles->nxt;
  77.     free((voidp *)(zfiles->name));
  78.     if (zfiles->zname != zfiles->name)
  79.       free((voidp *)(zfiles->zname));
  80.     if (zfiles->ext)
  81.       free((voidp *)(zfiles->extra));
  82.     if (zfiles->cext && zfiles->cextra != zfiles->extra)
  83.       free((voidp *)(zfiles->cextra));
  84.     if (zfiles->com)
  85.       free((voidp *)(zfiles->comment));
  86.     farfree((voidp far *)zfiles);
  87.     zfiles = z;
  88.     zcount--;
  89.   }
  90. }
  91.  
  92.  
  93. local void leave(e)
  94. int e;                  /* exit code */
  95. /* Process -o and -m options (if specified), free up malloc'ed stuff, and
  96.    exit with the code e. */
  97. {
  98.   int r;                /* return value from trash() */
  99.   ulg t;                /* latest time in zip file */
  100.   struct zlist far *z;  /* pointer into zfile list */
  101.  
  102.   /* If latest, set time to zip file to latest file in zip file */
  103.   if (latest && zipfile && strcmp(zipfile, "-"))
  104.   {
  105.     diag("changing time of zip file to time of latest file in it");
  106.     /* find latest time in zip file */
  107.     if (zfiles == NULL)
  108.        warn("zip file is empty, can't make it as old as latest entry", "");
  109.     else {
  110.       t = 0;
  111.       for (z = zfiles; z != NULL; z = z->nxt)
  112.         /* Ignore directories in time comparisons */
  113.         if (t < z->tim && z->zname[z->nam-1] != '/')
  114.           t = z->tim;
  115.       /* set modified time of zip file to that time */
  116.       if (t != 0)
  117.         stamp(zipfile, t);
  118.       else
  119.         warn(
  120.          "zip file has only directories, can't make it as old as latest entry",
  121.          "");
  122.     }
  123.   }
  124.   if (tempath != NULL)
  125.   {
  126.     free((voidp *)tempath);
  127.     tempath = NULL;
  128.   }
  129.   if (zipfile != NULL)
  130.   {
  131.     free((voidp *)zipfile);
  132.     zipfile = NULL;
  133.   }
  134.  
  135.  
  136.   /* If dispose, delete all files in the zfiles list that are marked */
  137.   if (dispose)
  138.   {
  139.     diag("deleting files that were added to zip file");
  140.     if ((r = trash()) != ZE_OK)
  141.       err(r, "was deleting moved files and directories");
  142.   }
  143.  
  144.  
  145.   /* Done! */
  146.   freeup();
  147. #ifdef VMS
  148.   exit(0);
  149. #else /* !VMS */
  150.   exit(e);
  151. #endif /* ?VMS */
  152. }
  153.  
  154.  
  155. void err(c, h)
  156. int c;                  /* error code from the ZE_ class */
  157. char *h;                /* message about how it happened */
  158. /* Issue a message for the error, clean up files and memory, and exit. */
  159. {
  160.   static int error_level;
  161.   if (error_level++ > 0) exit(0);  /* avoid recursive err() */
  162.  
  163.   if (h != NULL) {
  164.     if (PERR(c))
  165.       perror("zip error");
  166.     fflush(mesg);
  167.     fprintf(stderr, "\nzip error: %s (%s)\n", errors[c-1], h);
  168.   }
  169.   if (tempzip != NULL)
  170.   {
  171.     if (tempzip != zipfile) {
  172.       if (tempzf != NULL)
  173.         fclose(tempzf);
  174. #ifndef DEBUG
  175.       destroy(tempzip);
  176. #endif
  177.       free((voidp *)tempzip);
  178.     } else {
  179.       /* -g option, attempt to restore the old file */
  180.       int k = 0;                        /* keep count for end header */
  181.       ulg cb = cenbeg;                  /* get start of central */
  182.       struct zlist far *z;  /* steps through zfiles linked list */
  183.  
  184.       fprintf(stderr, "attempting to restore %s to its previous state\n",
  185.          zipfile);
  186.       fseek(tempzf, cenbeg, SEEK_SET);
  187.       tempzn = cenbeg;
  188.       for (z = zfiles; z != NULL; z = z->nxt)
  189.       {
  190.         putcentral(z, tempzf);
  191.         tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  192.         k++;
  193.       }
  194.       putend(k, tempzn - cb, cb, zcomlen, zcomment, tempzf);
  195.       tempzf = NULL;
  196.       fclose(tempzf);
  197.     }
  198.   }
  199.   if (key != NULL)
  200.     free((voidp *)key);
  201.   if (tempath != NULL)
  202.     free((voidp *)tempath);
  203.   if (zipfile != NULL)
  204.     free((voidp *)zipfile);
  205.   freeup();
  206. #ifdef VMS
  207.   c = 0;
  208. #endif
  209.   exit(c);
  210. }
  211.  
  212.  
  213. void error(h)
  214.   char *h;
  215. /* Internal error, should never happen */
  216. {
  217.   err(ZE_LOGIC, h);
  218. }
  219.  
  220. local void handler(s)
  221. int s;                  /* signal number (ignored) */
  222. /* Upon getting a user interrupt, turn echo back on for tty and abort
  223.    cleanly using err(). */
  224. {
  225. #if !defined(MSDOS) && !defined(__human68k__)
  226.   echon();
  227.   putc('\n', stderr);
  228. #endif /* !MSDOS */
  229.   err(ZE_ABORT, "aborting");
  230.   s++;                                  /* keep some compilers happy */
  231. }
  232.  
  233.  
  234. void warn(a, b)
  235. char *a, *b;            /* message strings juxtaposed in output */
  236. /* Print a warning message to stderr and return. */
  237. {
  238.   if (noisy) fprintf(stderr, "zip warning: %s%s\n", a, b);
  239. }
  240.  
  241.  
  242. local void license()
  243. /* Print license information to stdout. */
  244. {
  245.   extent i;             /* counter for copyright array */
  246.  
  247.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
  248.     printf(copyright[i], "zip");
  249.     putchar('\n');
  250.   }
  251.   for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++)
  252.     puts(disclaimer[i]);
  253. }
  254.  
  255.  
  256. local void help()
  257. /* Print help (along with license info) to stdout. */
  258. {
  259.   extent i;             /* counter for help array */
  260.  
  261.   /* help array */
  262.   static char *text[] = {
  263. "",
  264. "Zip %s (%s). Usage:",
  265. "zip [-options] [-b path] [-t mmddyy] [-n suffixes] [zipfile list] [-xi list]",
  266. "  The default action is to add or replace zipfile entries from list, which",
  267. "  can include the special name - to compress standard input.",
  268. "  If zipfile and list are omitted, zip compresses stdin to stdout.",
  269. "  -f   freshen: only changed files  -u   update: only changed or new files",
  270. "  -d   delete entries in zipfile    -m   move into zipfile (delete files)",
  271. "  -k   simulate PKZIP made zipfile  -g   allow growing existing zipfile",
  272. "  -r   recurse into directories     -j   junk (don't record) directory names",
  273. "  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)",
  274. "  -1   compress faster              -9   compress better",
  275. "  -q   quiet operation              -v   verbose operation",
  276. "  -c   add one-line comments        -z   add zipfile comment",
  277. "  -b   use \"path\" for temp file     -t   only do files after \"mmddyy\"",
  278. "  -@   read names from stdin        -o   make zipfile as old as latest entry",
  279. "  -x   exclude the following names  -i   include only the following names",
  280. #ifdef VMS
  281. " \"-F\"  fix zipfile(-FF try harder) \"-D\"  do not add directory entries",
  282. " \"-T\"  test zipfile integrity      \"-L\"  show software license",
  283. "  -w   append the VMS version number to the name stored in the zip file",
  284. " \"-V\"  save VMS file attributes",
  285. #else
  286. "  -F   fix zipfile (-FF try harder) -D   do not add directory entries",
  287. "  -T   test zipfile integrity       -L   show software license",
  288. #endif /* VMS */
  289. #ifdef OS2
  290. "  -E   use the .LONGNAME Extended attribute (if found) as filename",
  291. #endif /* OS2 */
  292. #ifdef S_IFLNK
  293. "  -y   store symbolic links as the link instead of the referenced file",
  294. #endif /* !S_IFLNK */
  295. #if defined(MSDOS) || defined(OS2)
  296. "  -$   include volume label         -S   include system and hidden files",
  297. #endif
  298. #ifdef CRYPT
  299. "  -e   encrypt  (-ee verify key)    -n   don't compress these suffixes"
  300. #else
  301. "  -h   show this help               -n   don't compress these suffixes"
  302. #endif
  303.   };
  304.  
  305.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  306.   {
  307.     printf(copyright[i], "zip");
  308.     putchar('\n');
  309.   }
  310.   for (i = 0; i < sizeof(text)/sizeof(char *); i++)
  311.   {
  312.     printf(text[i], VERSION, REVDATE);
  313.     putchar('\n');
  314.   }
  315. }
  316.  
  317.  
  318. /* Do command line expansion for MSDOS, VMS, ATARI, and AMIGA but not OS/2 */
  319. #if defined(MSVMS) || defined(AMIGA)
  320. #  define PROCNAME(n) (action==ADD||action==UPDATE?wild(n):procname(n))
  321. #else /* !MSVMS */
  322. #  define PROCNAME(n) procname(n)
  323. #endif /* ?MSVMS */
  324.  
  325. local void zipstdout()
  326. /* setup for writing zip file on stdout */
  327. {
  328.   int r;
  329.   mesg = stderr;
  330.   if (isatty(1))
  331.     err(ZE_PARMS, "cannot write zip file to terminal");
  332.   if ((zipfile = malloc(4)) == NULL)
  333.     err(ZE_MEM, "was processing arguments");
  334.   strcpy(zipfile, "-");
  335.   if ((r = readzipfile()) != ZE_OK)
  336.     err(r, zipfile);
  337. }
  338.  
  339.  
  340. local void check_zipfile(zipname)
  341.   char *zipname;
  342.   /* Invoke unzip -t on the given zip file */
  343. {
  344. #if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__)
  345.    if (spawnlp(P_WAIT, "unzip", "unzip", verbose ? "-t" : "-tqq",
  346.                zipname, NULL)) {
  347. #else
  348.    char cmd[128];
  349.    strcpy(cmd, "unzip -t ");
  350.    if (!verbose) strcat(cmd, "-qq ");
  351.    strcat(cmd, zipname);
  352. # ifdef VMS
  353.    if (!system(cmd)) {
  354. # else
  355.    if (system(cmd)) {
  356. # endif
  357. #endif
  358.      fprintf(mesg, "test of %s FAILED\n", zipfile);
  359.      err(ZE_TEST, "original files unmodified");
  360.    }
  361.    if (noisy)
  362.      fprintf(mesg, "test of %s OK\n", zipfile);
  363. }
  364.  
  365. local void get_filters(argc, argv)
  366.   int argc;               /* number of tokens in command line */
  367.   char **argv;            /* command line tokens */
  368. /* Counts number of -i or -x patterns, sets patterns and pcount */
  369. {
  370.   int i;
  371.   int flag = 0;
  372.  
  373.   pcount = 0;
  374.   for (i = 1; i < argc; i++) {
  375.     if (argv[i][0] == '-') {
  376.       if (strrchr(argv[i], 'i') != NULL) {
  377.         flag = 'i';
  378.       } else if (strrchr(argv[i], 'x') != NULL) {
  379.         flag = 'x';
  380.       } else {
  381.         flag = 0;
  382.       }
  383.     } else if (flag) {
  384.       if (patterns != NULL) {
  385.         patterns[pcount].zname = ex2in(argv[i], 0, (int *)NULL);
  386.         patterns[pcount].select = flag;
  387.         if (flag == 'i') icount++;
  388.       }
  389.       pcount++;
  390.     }
  391.   }
  392.   if (pcount == 0 || patterns != NULL) return;
  393.   patterns = (struct plist*) malloc(pcount * sizeof(struct plist));
  394.   if (patterns == NULL)
  395.     err(ZE_MEM, "was creating pattern list");
  396.   get_filters(argc, argv);
  397. }
  398.  
  399.  
  400. int main(argc, argv)
  401. int argc;               /* number of tokens in command line */
  402. char **argv;            /* command line tokens */
  403. /* Add, update, freshen, or delete zip entries in a zip file.  See the
  404.    command help in help() above. */
  405. {
  406.   int a;                /* attributes of zip file */
  407.   ulg c;                /* start of central directory */
  408.   int d;                /* true if just adding to a zip file */
  409.   char *e;              /* malloc'd comment buffer */
  410.   struct flist far *f;  /* steps through found linked list */
  411.   int i;                /* arg counter, root directory flag */
  412.   int k;                /* next argument type, marked counter,
  413.                            comment size, entry count */
  414.   ulg n;                /* total of entry len's */
  415.   int o;                /* true if there were any ZE_OPEN errors */
  416.   char *p;              /* steps through option arguments */
  417.   char *pp;             /* temporary pointer */
  418.   int r;                /* temporary variable */
  419.   int s;                /* flag to read names from stdin */
  420.   ulg t;                /* file time, length of central directory */
  421.   struct zlist far *v;  /* temporary variable */
  422.   struct zlist far * far *w;    /* pointer to last link in zfiles list */
  423.   FILE *x, *y;          /* input and output zip files */
  424.   struct zlist far *z;  /* steps through zfiles linked list */
  425.   char *zipbuf;         /* stdio buffer for the zip file */
  426.   FILE *comment_stream; /* set to stderr if anything is read from stdin */
  427.  
  428. #if defined(__IBMC__) && defined(__DEBUG_ALLOC__)
  429.   {
  430.     extern void DebugMalloc(void);
  431.     atexit(DebugMalloc);
  432.   }
  433. #endif
  434.  
  435.   mesg = (FILE *) stdout; /* cannot be made at link time for VMS */
  436.   comment_stream = (FILE *)stdin;
  437.  
  438.   init_upper();           /* build case map table */
  439.  
  440. #ifdef MACOS
  441.    argc = ccommand(&argv);
  442. #endif
  443.  
  444.   /* Process arguments */
  445.   diag("processing arguments");
  446.   if (argc == 1 && isatty(1))
  447.   {
  448.     help();
  449.     exit(0);
  450.   }
  451.   envargs(&argc, &argv, "ZIPOPT"); /* get options from environment */
  452.  
  453.   zipfile = tempzip = NULL;
  454.   tempzf = NULL;
  455.   d = 0;                        /* disallow adding to a zip file */
  456.   signal(SIGINT, handler);
  457. #ifdef SIGTERM                  /* AMIGADOS and others have no SIGTERM */
  458.   signal(SIGTERM, handler);
  459. #endif
  460.   k = 0;                        /* Next non-option argument type */
  461.   s = 0;                        /* set by -@ if -@ is early */
  462.  
  463.   get_filters(argc, argv);      /* scan first the -x and -i patterns */
  464.  
  465.   for (i = 1; i < argc; i++)
  466.   {
  467.     if (argv[i][0] == '-')
  468.       if (argv[i][1])
  469.         for (p = argv[i]+1; *p; p++)
  470.           switch(*p)
  471.           {
  472.             case '0':
  473.               method = STORE; level = 0; break;
  474.             case '1':  case '2':  case '3':  case '4':
  475.             case '5':  case '6':  case '7':  case '8':  case '9':
  476.                         /* Set the compression efficacy */
  477.               level = *p - '0';  break;
  478.             case 'b':   /* Specify path for temporary file */
  479.               tempdir = 1;
  480.               if (k != 0)
  481.                 err(ZE_PARMS, "use -b before zip file name");
  482.               else
  483.                 k = 1;          /* Next non-option is path */
  484.               break;
  485.             case 'c':   /* Add comments for new files in zip file */
  486.               comadd = 1;  break;
  487.             case 'd':   /* Delete files from zip file */
  488.               if (action != ADD)
  489.                 err(ZE_PARMS, "specify just one action");
  490.               action = DELETE;
  491.               break;
  492.             case 'D':   /* Do not add directory entries */
  493.               dirnames = 0; break;
  494.             case 'e':   /* Encrypt */
  495. #ifndef CRYPT
  496.               err(ZE_PARMS, "encryption not supported");
  497. #else /* CRYPT */
  498.               e = key == NULL ? (char *)NULL : key;
  499.               if ((key = malloc(PWLEN+1)) == NULL)
  500.                 err(ZE_MEM, "was getting encryption password");
  501.               if (getp(e == NULL ? "Enter password: " : "Verify password: ",
  502.                        key, PWLEN+1) == NULL)
  503.                 err(ZE_PARMS, "stderr is not a tty");
  504.               if (e != NULL)
  505.               {
  506.                 r = strcmp(key, e);
  507.                 free((voidp *)e);
  508.                 if (r)
  509.                   err(ZE_PARMS, "password not verified");
  510.               }
  511. #endif /* ?CRYPT */
  512.               break;
  513.             case 'F':   /* fix the zip file */
  514.               fix++; break;
  515.             case 'f':   /* Freshen zip file--overwrite only */
  516.               if (action != ADD)
  517.                 err(ZE_PARMS, "specify just one action");
  518.               action = FRESHEN;
  519.               break;
  520.             case 'g':   /* Allow appending to a zip file */
  521.               d = 1;  break;
  522.             case 'h': case 'H': case '?':  /* Help */
  523.               help();
  524.               leave(ZE_OK);
  525.             case 'j':   /* Junk directory names */
  526.               pathput = 0;  break;
  527.             case 'k':   /* Make entries using DOS names (k for Katz) */
  528.               dosify = 1;  break;
  529.             case 'l':   /* Translate end-of-line */
  530.               translate_eol++; break;
  531.             case 'L':   /* Show license, version */
  532.               license();
  533.               leave(ZE_OK);
  534.             case 'm':   /* Delete files added or updated in zip file */
  535.               dispose = 1;  break;
  536.             case 'n':   /* Don't compress files with a special suffix */
  537.               special = NULL; /* will be set at next argument */
  538.               break;
  539.             case 'o':   /* Set zip file time to time of latest file in it */
  540.               latest = 1;  break;
  541.             case 'p':   /* Store path with name */
  542.               break;            /* (do nothing as annoyance avoidance) */
  543.             case 'q':   /* Quiet operation */
  544.               noisy = 0;
  545.               if (verbose) verbose--;
  546.               break;
  547.             case 'r':   /* Recurse into subdirectories */
  548.               recurse = 1;  break;
  549.             case 'S':
  550.               hidden_files = 1;
  551.               break;
  552.             case 't':   /* Exclude files earlier than specified date */
  553.               if (before)
  554.                 err(ZE_PARMS, "can only have one -t");
  555.               k = 2;  break;
  556.             case 'T':   /* test zip file */
  557.               test = 1; break;
  558.             case 'u':   /* Update zip file--overwrite only if newer */
  559.               if (action != ADD)
  560.                 err(ZE_PARMS, "specify just one action");
  561.               action = UPDATE;
  562.               break;
  563.             case 'v':   /* Mention oddities in zip file structure */
  564.               noisy = 1;
  565.               verbose++;
  566.               break;
  567. #ifdef VMS
  568.             case 'V':   /* Store in VMS format */
  569.               vms_native = 1; break;
  570.             case 'w':   /* Append the VMS version number */
  571.               vmsver = 1;  break;
  572. #endif /* VMS */
  573.             case 'i':   /* Include only the following files */
  574.             case 'x':   /* Exclude following files */
  575.               if (k != 4 &&
  576.                   (k != 3 || (action != UPDATE && action != FRESHEN)))
  577.                 err(ZE_PARMS, "nothing to select from");
  578.               k = 5;
  579.               break;
  580. #ifdef S_IFLNK
  581.             case 'y':   /* Store symbolic links as such */
  582.               linkput = 1;  break;
  583. #endif /* S_IFLNK */
  584.             case 'z':   /* Edit zip file comment */
  585.               zipedit = 1;  break;
  586. #if defined(MSDOS) || defined(OS2)
  587.             case '$':   /* Include volume label */
  588.               volume_label = 1; break;
  589. #endif
  590.             case '@':   /* read file names from stdin */
  591.               comment_stream = NULL;
  592.               if (k < 3)        /* zip file not read yet */
  593.                 s = 1;          /* defer -@ until after zipfile read */
  594.               else if (strcmp(zipfile, "-") == 0)
  595.                 err(ZE_PARMS, "can't use - and -@ together");
  596.               else              /* zip file read--do it now */
  597.                 while ((pp = getnam(errbuf)) != NULL)
  598.                 {
  599.                   if ((r = PROCNAME(pp)) != ZE_OK)
  600.                     if (r == ZE_MISS)
  601.                       warn("name not matched: ", pp);
  602.                     else
  603.                       err(r, pp);
  604.                 }
  605.               break;
  606. #ifdef OS2
  607.             case 'E':
  608.               /* use the .LONGNAME EA (if any) as the file's name. */
  609.               use_longname_ea = 1;
  610.               break;
  611. #endif
  612.             default:
  613.             {
  614.               sprintf(errbuf, "no such option: %c", *p);
  615.               err(ZE_PARMS, errbuf);
  616.             }
  617.           }
  618.       else              /* just a dash */
  619.         switch (k)
  620.         {
  621.         case 0:
  622.           zipstdout();
  623.           k = 3;
  624.           if (s)
  625.             err(ZE_PARMS, "can't use - and -@ together");
  626.           break;
  627.         case 1:
  628.           err(ZE_PARMS, "invalid path");
  629.           break;
  630.         case 2:
  631.           err(ZE_PARMS, "invalid time");
  632.           break;
  633.         case 3:  case 4:
  634.           comment_stream = NULL;
  635.           if ((r = PROCNAME(argv[i])) != ZE_OK)
  636.             if (r == ZE_MISS)
  637.               warn("name not matched: ", argv[i]);
  638.             else
  639.               err(r, argv[i]);
  640.           if (k == 3)
  641.             k = 4;
  642.         }
  643.     else                /* not an option */
  644.     {
  645.       if (special == NULL)
  646.         special = argv[i];
  647.       else if (k == 5)
  648.         break; /* -i and -x arguments already scanned */
  649.       else switch (k)
  650.       {
  651.         case 0:
  652.           if ((zipfile = ziptyp(argv[i])) == NULL)
  653.             err(ZE_MEM, "was processing arguments");
  654.           if ((r = readzipfile()) != ZE_OK)
  655.             err(r, zipfile);
  656.           k = 3;
  657.           if (s)
  658.           {
  659.             while ((pp = getnam(errbuf)) != NULL)
  660.             {
  661.               if ((r = PROCNAME(pp)) != ZE_OK)
  662.                 if (r == ZE_MISS)
  663.                   warn("name not matched: ", pp);
  664.                 else
  665.                   err(r, pp);
  666.             }
  667.             s = 0;
  668.           }
  669.           break;
  670.         case 1:
  671.           if ((tempath = malloc(strlen(argv[i]) + 1)) == NULL)
  672.             err(ZE_MEM, "was processing arguments");
  673.           strcpy(tempath, argv[i]);
  674.           k = 0;
  675.           break;
  676.         case 2:
  677.         {
  678.           int yy, mm, dd;       /* results of sscanf() */
  679.  
  680.           if (sscanf(argv[i], "%2d%2d%2d", &mm, &dd, &yy) != 3 ||
  681.               mm < 1 || mm > 12 || dd < 1 || dd > 31)
  682.             err(ZE_PARMS, "invalid date entered for -t option");
  683.           before = dostime(yy + (yy < 80 ? 2000 : 1900), mm, dd, 0, 0, 0);
  684.           k = 0;
  685.           break;
  686.         }
  687.         case 3:  case 4:
  688.           if ((r = PROCNAME(argv[i])) != ZE_OK)
  689.             if (r == ZE_MISS)
  690.               warn("name not matched: ", argv[i]);
  691.             else
  692.               err(r, argv[i]);
  693.           if (k == 3)
  694.             k = 4;
  695.       }
  696.     }
  697.   }
  698.   if (k < 3) {               /* zip used as filter */
  699.     zipstdout();
  700.     comment_stream = NULL;
  701.     if ((r = procname("-")) != ZE_OK)
  702.       if (r == ZE_MISS)
  703.         warn("name not matched: ", "-");
  704.       else
  705.         err(r, "-");
  706.     k = 4;
  707.     if (s)
  708.       err(ZE_PARMS, "can't use - and -@ together");
  709.   }
  710.  
  711.   /* Clean up selections */
  712.   if (k == 3 && (action == UPDATE || action == FRESHEN)) {
  713.     for (z = zfiles; z != NULL; z = z->nxt) {
  714.       /* if -u or -f with no args, do all */
  715.       z->mark = pcount ? filter(z->zname) : 1;
  716.     }
  717.   }
  718.   if ((r = check_dup()) != ZE_OK)     /* remove duplicates in found list */
  719.     if (r == ZE_PARMS)
  720.       err(r, "cannot repeat names in zip file");
  721.     else
  722.       err(r, "was processing list of files");
  723.  
  724.   if (zcount)
  725.     free((voidp *)zsort);
  726.  
  727.   /* Check option combinations */
  728.   if (special == NULL) 
  729.     err(ZE_PARMS, "missing suffix list");
  730.   if (level == 9 || !strcmp(special, ";") || !strcmp(special, ":"))
  731.     special = NULL; /* compress everything */
  732.  
  733.   if (action == DELETE && (method != BEST || dispose || recurse ||
  734.       key != NULL || comadd || zipedit))
  735.     err(ZE_PARMS, "invalid option(s) used with -d");
  736.   if (linkput && dosify)
  737.     {
  738.       warn("can't use -y with -k, -y ignored", "");
  739.       linkput = 0;
  740.     }
  741.   if (test && !strcmp(zipfile, "-")) {
  742.     warn("can't use -T on stdout, -T ignored", "");
  743.   }
  744.   if ((action != ADD || d) && !strcmp(zipfile, "-"))
  745.     err(ZE_PARMS, "can't use -d,-f,-u or -g on stdout\n");
  746. #ifdef VMS
  747.   if (vms_native && translate_eol)
  748.     err(ZE_PARMS, "can't use -V with -l");
  749. #endif
  750.   if (zcount == 0 && (action != ADD || d))
  751.     warn(zipfile, " not found or empty");
  752.  
  753.  
  754.   /* If -b not specified, make temporary path the same as the zip file */
  755. #if defined(MSDOS) || defined(__human68k__) || defined(AMIGA)
  756.   if (tempath == NULL && ((p = strrchr(zipfile, '/')) != NULL ||
  757. #  ifdef MSDOS
  758.                           (p = strrchr(zipfile, '\\')) != NULL ||
  759. #  endif
  760.                           (p = strrchr(zipfile, ':')) != NULL))
  761.   {
  762.     if (*p == ':')
  763.       p++;
  764. #else
  765.   if (tempath == NULL && (p = strrchr(zipfile, '/')) != NULL)
  766.   {
  767. #endif
  768.     if ((tempath = malloc((int)(p - zipfile) + 1)) == NULL)
  769.       err(ZE_MEM, "was processing arguments");
  770.     r = *p;  *p = 0;
  771.     strcpy(tempath, zipfile);
  772.     *p = (char)r;
  773.   }
  774.  
  775.   /* For each marked entry, if not deleting, check if it exists, and if
  776.      updating or freshening, compare date with entry in old zip file.
  777.      Unmark if it doesn't exist or is too old, else update marked count. */
  778.   diag("stating marked entries");
  779.   k = 0;                        /* Initialize marked count */
  780.   for (z = zfiles; z != NULL; z = z->nxt)
  781.     if (z->mark) {
  782.       if (action != DELETE &&
  783.                 ((t = filetime(z->name, (ulg *)NULL, (long *)NULL)) == 0 ||
  784.                  t < before ||
  785.                  ((action == UPDATE || action == FRESHEN) && t <= z->tim)))
  786.       {
  787.         z->mark = 0;
  788.         z->trash = t && t >= before;    /* delete if -um or -fm */
  789.         if (verbose) {
  790.           fprintf(mesg, "zip diagnostic: %s %s\n", z->name,
  791.                  z->trash ? "up to date" : "missing or early");
  792.         }
  793.       }
  794.       else
  795.         k++;
  796. #if 0
  797.       fprintf(mesg, "t %ld, z->tim %ld, t-tim %ld\n", t, z->tim,
  798.               t-z->tim); /* ??? */
  799. #endif
  800.     }
  801.  
  802.   /* Remove entries from found list that do not exist or are too old */
  803.   diag("stating new entries");
  804.   for (f = found; f != NULL;)
  805.     if (action == DELETE || action == FRESHEN ||
  806.         (t = filetime(f->name, (ulg *)NULL, (long *)NULL)) == 0 ||
  807.         t < before || (namecmp(f->name, zipfile) == 0 && strcmp(zipfile, "-")))
  808.       f = fexpel(f);
  809.     else
  810.       f = f->nxt;
  811.  
  812.   /* Make sure there's something left to do */
  813.   if (k == 0 && found == NULL &&
  814.       !(zfiles != NULL && (latest || fix || zipedit))) {
  815.     if (test && (zfiles != NULL || zipbeg != 0)) {
  816.       check_zipfile(zipfile);
  817.       leave(ZE_OK);
  818.     }
  819.     if (action == UPDATE || action == FRESHEN)
  820.       leave(ZE_OK);
  821.     else if (zfiles == NULL && (latest || fix))
  822.       err(ZE_NAME, zipfile);
  823.     else
  824.       err(ZE_NONE, zipfile);
  825.   }
  826.   d = (d && k == 0 && (zipbeg || zfiles != NULL)); /* d true if appending */
  827.  
  828.  
  829.   /* Before we get carried away, make sure zip file is writeable. This
  830.    * has the undesired side effect of leaving one empty junk file on a WORM,
  831.    * so when the zipfile does not exist already and when -b is specified,
  832.    * the writability check is made in replace().
  833.    */
  834.   if (strcmp(zipfile, "-"))
  835.   {
  836.     if (tempdir && zfiles == NULL && zipbeg == 0) {
  837.       a = 0;
  838.     } else {
  839.        x = zfiles == NULL && zipbeg == 0 ? fopen(zipfile, FOPW) : 
  840.                                            fopen(zipfile, FOPM);
  841.       /* Note: FOPW and FOPM expand to several parameters for VMS */
  842.       if (x == NULL)
  843.         err(ZE_CREAT, zipfile);
  844.       fclose(x);
  845.       a = getfileattr(zipfile);
  846.       if (zfiles == NULL && zipbeg == 0)
  847.         destroy(zipfile);
  848.     }
  849.   }
  850.   else
  851.     a = 0;
  852.  
  853.   /* Throw away the garbage in front of the zip file for -F */
  854.   if (fix) zipbeg = 0;
  855.  
  856.   /* Open zip file and temporary output file */
  857.   diag("opening zip file and creating temporary zip file");
  858.   x = NULL;
  859.   tempzn = 0;
  860.   if (strcmp(zipfile, "-") == 0)
  861.   {
  862. #if defined(MSDOS) || defined(__human68k__)
  863.     /* Set stdout mode to binary for MSDOS systems */
  864.     setmode(fileno(stdout), O_BINARY);
  865.     tempzf = y = fdopen(fileno(stdout), FOPW);
  866. #else
  867.     tempzf = y = stdout;
  868. #endif
  869.     /* tempzip must be malloced so a later free won't barf */
  870.     tempzip = malloc(4);
  871.     if (tempzip == NULL)
  872.       err(ZE_MEM, "allocating temp filename");
  873.     strcpy(tempzip, "-");
  874.   }
  875.   else if (d) /* d true if just appending (-g) */
  876.   {
  877.     if ((y = fopen(zipfile, FOPM)) == NULL)
  878.       err(ZE_NAME, zipfile);
  879.     tempzip = zipfile;
  880.     tempzf = y;
  881.     if (fseek(y, cenbeg, SEEK_SET))
  882.       err(ferror(y) ? ZE_READ : ZE_EOF, zipfile);
  883.     tempzn = cenbeg;
  884.   }
  885.   else
  886.   {
  887.     if ((zfiles != NULL || zipbeg) && (x = fopen(zipfile, FOPR_EX)) == NULL)
  888.       err(ZE_NAME, zipfile);
  889.     if ((tempzip = tempname(zipfile)) == NULL)
  890.       err(ZE_MEM, "allocating temp filename");
  891.     if ((tempzf = y = fopen(tempzip, FOPW)) == NULL)
  892.       err(ZE_TEMP, tempzip);
  893.     if (zipbeg && (r = fcopy(x, y, zipbeg)) != ZE_OK)
  894.       err(r, r == ZE_TEMP ? tempzip : zipfile);
  895.     tempzn = zipbeg;
  896.   }
  897. #ifndef VMS
  898.   /* Use large buffer to speed up stdio: */
  899.   zipbuf = (char *)malloc(ZBSZ);
  900.   if (zipbuf == NULL)
  901.     err(ZE_MEM, tempzip);
  902. # ifdef _IOFBF
  903.   setvbuf(y, zipbuf, _IOFBF, ZBSZ);
  904. # else
  905.   setbuf(y, zipbuf);
  906. # endif /* _IOBUF */
  907. #endif /* VMS */
  908.   o = 0;                                /* no ZE_OPEN errors yet */
  909.  
  910.  
  911.   /* Process zip file, updating marked files */
  912.   if (zfiles != NULL)
  913.     diag("going through old zip file");
  914.   w = &zfiles;
  915.   while ((z = *w) != NULL)
  916.     if (z->mark)
  917.     {
  918.       /* if not deleting, zip it up */
  919.       if (action != DELETE)
  920.       {
  921.         if (noisy)
  922.         {
  923.           fprintf(mesg, "updating: %s", z->zname);
  924.           fflush(mesg);
  925.         }
  926.         if ((r = zipup(z, y)) != ZE_OK && r != ZE_OPEN)
  927.         {
  928.           if (noisy)
  929.           {
  930.             putc('\n', mesg);
  931.             fflush(mesg);
  932.           }
  933.           sprintf(errbuf, "was zipping %s", z->name);
  934.           err(r, errbuf);
  935.         }
  936.         if (r == ZE_OPEN)
  937.         {
  938.           o = 1;
  939.           if (noisy)
  940.           {
  941.             putc('\n', mesg);
  942.             fflush(mesg);
  943.           }
  944.           perror("zip warning");
  945.           warn("could not open for reading: ", z->name);
  946.           warn("will just copy entry over: ", z->zname);
  947.           if ((r = zipcopy(z, x, y)) != ZE_OK)
  948.           {
  949.             sprintf(errbuf, "was copying %s", z->zname);
  950.             err(r, errbuf);
  951.           }
  952.           z->mark = 0;
  953.         }
  954.         w = &z->nxt;
  955.       }
  956.       else
  957.       {
  958.         if (noisy)
  959.         {
  960.           fprintf(mesg, "deleting: %s\n", z->zname);
  961.           fflush(mesg);
  962.         }
  963.         v = z->nxt;                     /* delete entry from list */
  964.         free((voidp *)(z->name));
  965.         free((voidp *)(z->zname));
  966.         if (z->ext)
  967.           free((voidp *)(z->extra));
  968.         if (z->cext && z->cextra != z->extra)
  969.           free((voidp *)(z->cextra));
  970.         if (z->com)
  971.           free((voidp *)(z->comment));
  972.         farfree((voidp far *)z);
  973.         *w = v;
  974.         zcount--;
  975.       }
  976.     }
  977.     else
  978.     {
  979.       /* copy the original entry verbatim */
  980.       if (!d && (r = zipcopy(z, x, y)) != ZE_OK)
  981.       {
  982.         sprintf(errbuf, "was copying %s", z->zname);
  983.         err(r, errbuf);
  984.       }
  985.       w = &z->nxt;
  986.     }
  987.  
  988.  
  989.   /* Process the edited found list, adding them to the zip file */
  990.   diag("zipping up new entries, if any");
  991.   for (f = found; f != NULL; f = fexpel(f))
  992.   {
  993.     /* add a new zfiles entry and set the name */
  994.     if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
  995.       err(ZE_MEM, "was adding files to zip file");
  996.     z->nxt = NULL;
  997.     z->name = f->name;
  998.     f->name = NULL;
  999.     z->zname = f->zname;
  1000.     f->zname = NULL;
  1001.     z->ext = z->cext = z->com = 0;
  1002.     z->mark = 1;
  1003.     z->dosflag = f->dosflag;
  1004.     /* zip it up */
  1005.     if (noisy)
  1006.     {
  1007.       fprintf(mesg, "  adding: %s", z->zname);
  1008.       fflush(mesg);
  1009.     }
  1010.     if ((r = zipup(z, y)) != ZE_OK  && r != ZE_OPEN)
  1011.     {
  1012.       if (noisy)
  1013.       {
  1014.         putc('\n', mesg);
  1015.         fflush(mesg);
  1016.       }
  1017.       sprintf(errbuf, "was zipping %s", z->name);
  1018.       err(r, errbuf);
  1019.     }
  1020.     if (r == ZE_OPEN)
  1021.     {
  1022.       o = 1;
  1023.       if (noisy)
  1024.       {
  1025.         putc('\n', mesg);
  1026.         fflush(mesg);
  1027.       }
  1028.       perror("zip warning");
  1029.       warn("could not open for reading: ", z->name);
  1030.       free((voidp *)(z->name));
  1031.       free((voidp *)(z->zname));
  1032.       farfree((voidp far *)z);
  1033.     }
  1034.     else
  1035.     {
  1036.       *w = z;
  1037.       w = &z->nxt;
  1038.       zcount++;
  1039.     }
  1040.   }
  1041.   if (key != NULL)
  1042.   {
  1043.     free((voidp *)key);
  1044.     key = NULL;
  1045.   }
  1046.  
  1047.  
  1048.   /* Get one line comment for each new entry */
  1049.   if (comadd)
  1050.   {
  1051.     if (comment_stream == NULL) {
  1052.       comment_stream = fdopen(fileno(stderr), "r");
  1053.     }
  1054.     if ((e = malloc(MAXCOM + 1)) == NULL)
  1055.       err(ZE_MEM, "was reading comment lines");
  1056.     for (z = zfiles; z != NULL; z = z->nxt)
  1057.       if (z->mark)
  1058.       {
  1059.         if (noisy)
  1060.           fprintf(mesg, "Enter comment for %s:\n", z->name);
  1061.         if (fgets(e, MAXCOM+1, comment_stream) != NULL)
  1062.         {
  1063.           if ((p = malloc((k = strlen(e))+1)) == NULL)
  1064.           {
  1065.             free((voidp *)e);
  1066.             err(ZE_MEM, "was reading comment lines");
  1067.           }
  1068.           strcpy(p, e);
  1069.           if (p[k-1] == '\n')
  1070.             p[--k] = 0;
  1071.           z->comment = p;
  1072.           z->com = k;
  1073.         }
  1074.       }
  1075.     free((voidp *)e);
  1076.   }
  1077.  
  1078.   /* Get multi-line comment for the zip file */
  1079.   if (zipedit)
  1080.   {
  1081.     if (comment_stream == NULL) {
  1082.       comment_stream = fdopen(fileno(stderr), "r");
  1083.     }
  1084.     if ((e = malloc(MAXCOM + 1)) == NULL)
  1085.       err(ZE_MEM, "was reading comment lines");
  1086.     if (noisy && zcomlen)
  1087.     {
  1088.       fputs("current zip file comment is:\n", mesg);
  1089.       fwrite(zcomment, 1, zcomlen, mesg);
  1090.       if (zcomment[zcomlen-1] != '\n')
  1091.         putc('\n', mesg);
  1092.       free((voidp *)zcomment);
  1093.     }
  1094.     zcomment = malloc(1);
  1095.     *zcomment = 0;
  1096.     if (noisy)
  1097.       fputs("enter new zip file comment (end with .):\n", mesg);
  1098. #if (defined(AMIGA) && (defined(LATTICE)||defined(__SASC)))
  1099.       flushall();  /* tty input/output is out of sync here */
  1100. #endif
  1101.     while (fgets(e, MAXCOM+1, comment_stream) != NULL && strcmp(e, ".\n"))
  1102.     {
  1103.       if (e[(r = strlen(e)) - 1] == '\n')
  1104.         e[--r] = 0;
  1105.       if ((p = malloc((*zcomment ? strlen(zcomment) + 3 : 1) + r)) == NULL)
  1106.       {
  1107.         free((voidp *)e);
  1108.         err(ZE_MEM, "was reading comment lines");
  1109.       }
  1110.       if (*zcomment)
  1111.         strcat(strcat(strcpy(p, zcomment), "\r\n"), e);
  1112.       else
  1113.         strcpy(p, *e ? e : "\r\n");
  1114.       free((voidp *)zcomment);
  1115.       zcomment = p;
  1116.     }
  1117.     zcomlen = strlen(zcomment);
  1118.     free((voidp *)e);
  1119.   }
  1120.  
  1121.  
  1122.   /* Write central directory and end header to temporary zip */
  1123.   diag("writing central directory");
  1124.   k = 0;                        /* keep count for end header */
  1125.   c = tempzn;                   /* get start of central */
  1126.   n = t = 0;
  1127.   for (z = zfiles; z != NULL; z = z->nxt)
  1128.   {
  1129.     if ((r = putcentral(z, y)) != ZE_OK)
  1130.       err(r, tempzip);
  1131.     tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;
  1132.     n += z->len;
  1133.     t += z->siz;
  1134.     k++;
  1135.   }
  1136.   if (k == 0)
  1137.     warn("zip file empty", "");
  1138.   if (verbose)
  1139.     fprintf(mesg, "total bytes=%lu, compressed=%lu -> %d%% savings\n",
  1140.            n, t, percent(n, t));
  1141.   t = tempzn - c;               /* compute length of central */
  1142.   diag("writing end of central directory");
  1143.   if ((r = putend(k, t, c, zcomlen, zcomment, y)) != ZE_OK)
  1144.     err(r, tempzip);
  1145.   tempzf = NULL;
  1146.   if (fclose(y))
  1147.     err(d ? ZE_WRITE : ZE_TEMP, tempzip);
  1148.   if (x != NULL)
  1149.     fclose(x);
  1150.  
  1151.   /* Free some memory before spawning unzip */
  1152.   lm_free();
  1153.  
  1154.   /* Test new zip file before overwriting old one or removing input files */
  1155.   if (test) {
  1156.     check_zipfile(tempzip);
  1157.   }
  1158.   /* Replace old zip file with new zip file, leaving only the new one */
  1159.   if (strcmp(zipfile, "-") && !d)
  1160.   {
  1161.     diag("replacing old zip file with new zip file");
  1162.     if ((r = replace(zipfile, tempzip)) != ZE_OK)
  1163.     {
  1164.       warn("new zip file left as: ", tempzip);
  1165.       free((voidp *)tempzip);
  1166.       tempzip = NULL;
  1167.       err(r, "was replacing the original zip file");
  1168.     }
  1169.     free((voidp *)tempzip);
  1170.   }
  1171.   tempzip = NULL;
  1172.   if (a && strcmp(zipfile, "-")) {
  1173.     setfileattr(zipfile, a);
  1174. #ifdef VMS
  1175.     /* If the zip file existed previously, restore its record format: */
  1176.     if (x != NULL)
  1177.       VMSmunch(zipfile, RESTORE_RTYPE, NULL);
  1178. #endif
  1179.   }
  1180.  
  1181.   /* Finish up (process -o, -m, clean up).  Exit code depends on o. */
  1182.   leave(o ? ZE_OPEN : ZE_OK);
  1183.   return 0; /* just to avoid compiler warning */
  1184. }
  1185.  
  1186. /*****************************************************************
  1187.  | envargs - add default options from environment to command line
  1188.  |----------------------------------------------------------------
  1189.  | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991.
  1190.  | This program is in the public domain.
  1191.  |----------------------------------------------------------------
  1192.  | Minor program notes:
  1193.  |  1. Yes, the indirection is a tad complex
  1194.  |  2. Parenthesis were added where not needed in some cases
  1195.  |     to make the action of the code less obscure.
  1196.  ****************************************************************/
  1197.  
  1198. local void
  1199. envargs(Pargc, Pargv, envstr)
  1200. int *Pargc;
  1201. char ***Pargv;
  1202. char *envstr;
  1203. {
  1204.     char *getenv();
  1205.     char *envptr;                               /* value returned by getenv */
  1206.     char *bufptr;                               /* copy of env info */
  1207.     int argc = 0;                               /* internal arg count */
  1208.     int ch;                                             /* spare temp value */
  1209.     char **argv;                                /* internal arg vector */
  1210.     char **argvect;                             /* copy of vector address */
  1211.  
  1212.     /* see if anything in the environment */
  1213.     envptr = getenv(envstr);
  1214.     if (envptr == NULL || *envptr == 0) return;
  1215.  
  1216.     /* count the args so we can allocate room for them */
  1217.     argc = count_args(envptr);
  1218.     bufptr = malloc(1+strlen(envptr));
  1219.     if (bufptr == NULL)
  1220.         err(ZE_MEM, "Can't get memory for arguments");
  1221.  
  1222.     strcpy(bufptr, envptr);
  1223.  
  1224.     /* allocate a vector large enough for all args */
  1225.     argv = (char **)malloc((argc+*Pargc+1)*sizeof(char *));
  1226.     if (argv == NULL)
  1227.         err(ZE_MEM, "Can't get memory for arguments");
  1228.     argvect = argv;
  1229.  
  1230.     /* copy the program name first, that's always true */
  1231.     *(argv++) = *((*Pargv)++);
  1232.  
  1233.     /* copy the environment args first, may be changed */
  1234.     do {
  1235.         *(argv++) = bufptr;
  1236.         /* skip the arg and any trailing blanks */
  1237.         while ((ch = *bufptr) != '\0' && ch != ' ') ++bufptr;
  1238.         if (ch == ' ') *(bufptr++) = '\0';
  1239.         while ((ch = *bufptr) != '\0' && ch == ' ') ++bufptr;
  1240.     } while (ch);
  1241.  
  1242.     /* now save old argc and copy in the old args */
  1243.     argc += *Pargc;
  1244.     while (--(*Pargc)) *(argv++) = *((*Pargv)++);
  1245.  
  1246.     /* finally, add a NULL after the last arg, like UNIX */
  1247.     *argv = NULL;
  1248.  
  1249.     /* save the values and return */
  1250.     *Pargv = argvect;
  1251.     *Pargc = argc;
  1252. }
  1253.  
  1254. static int
  1255. count_args(s)
  1256. char *s;
  1257. {
  1258.     int count = 0;
  1259.     int ch;
  1260.  
  1261.     do {
  1262.         /* count and skip args */
  1263.         ++count;
  1264.         while ((ch = *s) != '\0' && ch != ' ') ++s;
  1265.         while ((ch = *s) != '\0' && ch == ' ') ++s;
  1266.     } while (ch);
  1267.  
  1268.     return count;
  1269. }
  1270.