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

  1. #ifndef LINT
  2. /* derived from: zoo.c 2.24 88/01/29 00:55:09 */
  3. static char sccsid[]="$Id: zoo.c,v 1.21 91/07/09 02:36:40 dhesi Exp $";
  4. #endif /* LINT */
  5.  
  6. #if 0
  7. #define TRACEI(item)    printf("line %d: %s= %d\n", __LINE__, #item, item)
  8. #define TRACES(item)    printf("line %d: %s= [%s]\n", __LINE__, #item, item)
  9. #endif
  10.  
  11. extern char version[];
  12.  
  13. /*
  14. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  15. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  16. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  17. */
  18. #include "options.h"
  19. #include "zooio.h"
  20. #include "various.h"
  21.  
  22. #include "zoo.h"
  23. #include "zoofns.h"
  24.  
  25. #include "errors.i"
  26. #include "zoomem.h"
  27.  
  28. static void ms_help();
  29. static void wait_return();
  30.  
  31. #ifdef TRACE_IO
  32. int verbose = 0;
  33. #endif
  34.  
  35. int instr PARMS ((char *, char *));
  36.  
  37. char *out_buf_adr = NULL;      /* points to memory allocated for output buffer(s) */
  38. char *in_buf_adr = NULL;       /* points to memory allocated for input buffer */
  39.  
  40. /* static declarations */
  41. int quiet = 0;             /* whether to be quiet */
  42. int next_arg = FIRST_ARG; /* filenames start at this position */
  43. int arg_count;          /* count of arguments supplied to program */
  44. char **arg_vector;      /* vector of arguments supplied to program */
  45.  
  46. main(argc,argv)
  47. register int argc;
  48. register char **argv;
  49. {
  50.    char *zooname;          /* synonym for argv[2] -- to make life easier */
  51. #ifndef OOZ
  52.    static char incorrect_args[] = "Incorrect number of arguments.\n";
  53.    int filecount;          /* how many filespecs supplied */
  54. #endif /* OOZ */
  55.  
  56. #ifdef OOZ
  57. #else
  58. /* else not OOZ */
  59.       static char usage[] = "\nUsage: zoo {acDeglLPTuUvx}[aAcCdEfInmMNoOpPqu1:/.@n] archive file\n(\"zoo h\" for help, \"zoo H\" for extended help)\n";
  60.       static char nov_usage[] =
  61.           "\nNovice usage:  zoo -cmd archive[.zoo] file...  where -cmd is one of these:\n";
  62.       char *option;
  63.  
  64.       static char nov_cmds[] =
  65.          /* ADD=0EXT=5    MOV=14TES=20PRI=26 DEL=33  LIS=41UPD=47  FRE=55   COMMENT=64 */
  66.            "-add -extract -move -test -print -delete -list -update -freshen -comment\n";
  67.  
  68. #ifdef NOENUM
  69. #define NONE   -1
  70. #define ADD    0
  71. #define EXTRACT 5
  72. #define MOVE   14
  73. #define TEST   20
  74. #define PRINT  26
  75. #define DELETE 33
  76. #define LIST   41
  77. #define UPDATE 47
  78. #define FRESHEN   55
  79. #define COMMENT   64
  80.  
  81. int cmd = NONE;
  82.  
  83. #else
  84.    enum choice {
  85.       NONE = -1, ADD = 0, EXTRACT = 5, MOVE = 14, TEST = 20, PRINT = 26,
  86.       DELETE = 33, LIST = 41, UPDATE = 47, FRESHEN = 55, COMMENT = 64
  87.    };
  88.    enum choice cmd = NONE;          /* assume no Novice command */
  89. #endif
  90.  
  91. #endif /* end of not OOZ */
  92.  
  93. #ifdef SPECINIT
  94.     void spec_init PARMS ((void));
  95.     spec_init();                            /* system-specific startup code */
  96. #endif
  97.  
  98.     /* make sure T_UINT16 is an unsigned 16-bit type, exactly.  This
  99.         code is included only if T_UINT16 was defined by default at the
  100.         end of options.h. */
  101. #ifdef CHECK_TUINT
  102.     {
  103.         T_UINT16 i;
  104.         int status = 0;
  105.         i = ((unsigned) 1) << 15;
  106.         if (i < 0)
  107.             status = 1;
  108.         if (i != ((unsigned) 1) << 15)
  109.             status = 1;
  110.         i *= 2;
  111.         if (i != 0)
  112.             status = 1;
  113.         if (status != 0)
  114.             prterror('w', "Configuration problem: T_UINT16 is not 16 bits\n");
  115.     }
  116. #endif
  117.  
  118.    arg_count = argc;
  119.    arg_vector = argv;
  120.    zooname = argv[FIRST_ARG-1];     /* points to name or archive */
  121.  
  122. #ifdef OOZ
  123.    if (argc < 2) {
  124.       putstr (usage1);
  125.       putstr (usage2);
  126.       zooexit (1);
  127.    }
  128. #else
  129. /* else not OOZ */
  130.    if (argc < 2)
  131.       goto show_usage;
  132.    filecount = argc - 3;
  133.    option = str_dup(argv[1]);
  134.  
  135. #ifdef TRACE_IO
  136.    if (*option == ':') {         /* for debugging output */
  137.       verbose++;
  138.       option++;                  /* hide the : from other functions */
  139.    }
  140. #endif
  141.  
  142. #ifdef WAIT_PROMPT
  143.    if (*option == 'w') {
  144.         option++;                        /* hide w from other functions */
  145.         wait_return();
  146.     }
  147. #endif /* WAIT_PROMPT */
  148.  
  149.    if (*option == 'H') ms_help(option);
  150.    if (*option == 'h' || *option == 'H')
  151.       goto bigusage;
  152.     if (strchr("-acDegflLPTuUvVx", *option) == NULL)
  153.         goto give_list;
  154.  
  155.    if (*option == '-') {
  156.  
  157. #ifdef NOENUM
  158.       cmd = instr (nov_cmds, str_lwr(option));
  159. #else
  160.       cmd = (enum choice) instr (nov_cmds, str_lwr(option));
  161. #endif
  162.  
  163.       if (strlen(option) < 2 || cmd == NONE)
  164.          goto show_usage;
  165.       if (  ((cmd == ADD || cmd == MOVE || cmd == FRESHEN ||
  166.                   cmd == UPDATE || cmd == DELETE) && argc < 4) ||
  167.             ((cmd == EXTRACT || cmd == TEST || cmd == LIST ||
  168.                      cmd == PRINT || cmd == COMMENT) && argc < 3)) {
  169.          fprintf (stderr, incorrect_args);
  170.          goto show_usage;
  171.       }
  172.    } else {
  173.         char *wheresI;        /* will be null if I option not supplied */
  174.         if    (
  175.                 (
  176.                     strchr("au",*option) &&
  177.                     (
  178.                         (((wheresI = strchr(option,'I')) != 0) &&
  179.                             argc != 3) ||
  180.                         wheresI==NULL && argc < 4
  181.                     )
  182.                 ) ||
  183.                  strchr("DU",*option) && argc < 4 ||
  184.              strchr("cexlvVL",*option) && argc < 3 ||
  185.              strchr("TP",*option)   && argc != 3 ||
  186.                  (*option == 'f' && argc != 2) ||
  187.                  (*option == 'g' &&
  188.                     (strchr(option,'A') == NULL && argc < 4 ||
  189.                      strchr(option,'A') != NULL && argc != 3
  190.                     )
  191.                  )
  192.             ) {
  193.          fprintf (stderr, incorrect_args);
  194.          goto show_usage;
  195.       }
  196.    }
  197. #endif /* end of not OOZ */
  198.  
  199. #ifndef OOZ
  200.    /* if not doing a list and no extension in archive name, add default
  201.    extension */
  202.    if (*option != 'f' && cmd != LIST && strchr("lvVL", *option) == NULL &&
  203.          strchr(nameptr (zooname), EXT_CH) == NULL)
  204.       zooname = newcat (zooname, EXT_DFLT);
  205. #endif
  206.  
  207. /*
  208. Here we allocate a large block of memory for the duration of the program.
  209. lzc() and lzd() will use half of it each.  Routine getfile() will use all
  210. of it.  Routine decode() will use the first 8192 bytes of it.  Routine
  211. encode() will use all of it. */
  212.  
  213. /*                          fudge/2           fudge/2
  214. **             [______________||________________|]
  215. **               output buffer    input buffer
  216. */
  217.    out_buf_adr = ealloc (MEM_BLOCK_SIZE);
  218.    in_buf_adr = out_buf_adr + OUT_BUF_SIZE + (FUDGE/2);
  219.  
  220. #ifdef OOZ
  221. zooext(zooname, "\0");     /* just extract -- no fancy stuff   */
  222. zooexit (0);                  /* and exit normally                */
  223. #else
  224. /* else not OOZ -- parse command line and invoke a routine */
  225.    if (cmd != NONE) {
  226.       switch (cmd) {
  227.  
  228.          case ADD:      zooadd (zooname, filecount, &argv[3], "aP:"); break;
  229.          case FRESHEN:  zooadd (zooname, filecount, &argv[3], "auP:"); break;
  230.          case UPDATE:   zooadd (zooname, filecount, &argv[3], "aunP:"); break;
  231.          case MOVE:     zooadd (zooname, filecount, &argv[3], "aMP:"); break;
  232.  
  233.          case EXTRACT:  zooext (zooname, "x"); break;
  234.          case TEST:     zooext (zooname, "xNd"); break;
  235.          case PRINT:    zooext (zooname, "xp"); break;
  236.  
  237.          case DELETE:   zoodel (zooname, "DP",1); break;
  238.          case LIST:     zoolist (&argv[2], "VC", argc-2); break;
  239.          case COMMENT:  comment (zooname, "c"); break;
  240.          default: goto show_usage;
  241.       }
  242.    } else
  243.       switch (*option) {
  244.  
  245.          case 'a':
  246.          case 'u':
  247.          case 'T':
  248.             zooadd (zooname, filecount, &argv[3], option); break;
  249. #ifdef FILTER
  250.             case 'f':
  251.                 zoofilt (option);  break;
  252. #endif /* FILTER */
  253.          case 'D':
  254.             zoodel (zooname, option, 1); break;
  255.          case 'U':
  256.             zoodel (zooname, option, 0); break;
  257.             case 'g':
  258.                 zoodel (zooname, option, 2); break;
  259.          case 'v':
  260.             case 'V':
  261.          case 'l':
  262.             zoolist(&argv[2], option, 1); break;
  263.          case 'L':
  264.             zoolist(&argv[2], option, argc-2); break;
  265.          case 'e':
  266.          case 'x':
  267.             zooext(zooname, option); break;
  268.          case 'P':
  269.             zoopack (zooname, option); break;
  270.          case 'c':
  271.             comment (zooname, option); break;
  272.          default:
  273.             goto give_list;
  274.       }
  275. zooexit (0);      /* don't fall through */
  276.  
  277. /* usage list including Novice commands */
  278. show_usage:
  279.    printf ("\nZoo archiver, %s\n", version);
  280.    printf ("(C) Copyright 1988 Rahul Dhesi -- Noncommercial use permitted\n");
  281.    printf ("%s%s%s", usage, nov_usage, nov_cmds);
  282.     zooexit (1);
  283.  
  284. /* brief usage list */
  285. give_list:
  286.     fprintf (stderr, usage); zooexit (1);
  287.  
  288. /* help screen */
  289. bigusage:
  290. printf("\n\n\n\n\n\n\n\n");
  291. printf ("Zoo archiver, %s\n", version);
  292. printf("(C) Copyright 1991 Rahul Dhesi -- Noncommercial use permitted\n");
  293.  
  294. printf (usage);
  295. printf ("\nChoose a command from within {} and zero or more modifiers from within [].\n");
  296.  
  297. printf ("E.g.:  `zoo a save /bin/*' will archive all files in /bin into save.zoo.\n");
  298. printf ("(Please see the user manual for a complete description of commands.)\n\n");
  299. printf (nov_usage);
  300. printf (nov_cmds);
  301. printf ("\n\n\n\n");
  302. wait_return();    /* print msg & wait for RETURN */
  303.  
  304. printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  305. printf (usage);
  306.  
  307. printf (" Commands in {} mean:         |Modifiers in [] mean:\n");
  308.  
  309. printf ("  a     add files             | a     show archive name(s) in listing\n");
  310. printf ("  c     update comments       | A     apply g or c to archive\n");
  311. printf ("  D     delete stored files   | c     add/list comments\n");
  312. printf ("  e,x   extract files         | d     extract/list deleted files too\n");
  313. printf ("  g     adj. gen. limit/count | dd    extract/list only deleted files\n");
  314. printf ("  l,L,v,V list filenames      | E     erase backup after packing\n");
  315. printf ("  P     pack archive          | f     fast add (no compression) or list\n");
  316. printf ("  T     fix archive datestamp | M     move when adding (erase original)\n");
  317. printf ("  u     add only newer files  | n     add only files not already in archive\n");
  318. printf ("  U     undelete stored files | N     send extracted data to Nowhere\n");
  319. #ifdef FILTER
  320. printf ("  f     act as filter         | c/u   compress/uncompress as filter\n");
  321. #endif /* FILTER */
  322. printf (" -----------------------------  O     don't ask \"Overwrite?\"\n");
  323. printf ("  q     be quiet                p     pipe extracted data to standard output\n");
  324. printf ("  :     don't store dir names   /,//  extract full pathnames\n");
  325. printf ("  .     pack to current dir     I     add filenames read from stdin\n");
  326. printf ("  C     show file CRC value     +/-   enable/disable generations\n");
  327. printf ("  S     overwrite newer files   g     list generation limits\n");
  328. printf ("  P     pack after adding       @n    start extract/list at position n\n");
  329.  
  330. #ifdef FATTR
  331. printf ("  m     list file modes         OO    overwrite read-only files\n");
  332. #endif /* FATTR */
  333. printf ("  C     change archive cmnt     h     use high-compression method\n");
  334. #endif /* end of not OOZ */
  335.  
  336. /* NOTE:  if allowed to fall through and return without an exit() statement,
  337.    it was printing garbage--corrupted stack?  Why--bug in Microsoft C? */
  338. zooexit (1);
  339. return 1;    /* keep lint & compilers happy */
  340. }
  341.  
  342. /* multi-screen help facility thanks to Bill Davidsen */
  343.  
  344. /* help screens */
  345. static char *scrn1[] = {
  346.    "",
  347.     "command line format:",
  348.     "  zoo {command}[options] archive files(s)",
  349.     "",
  350.     "Commands:",
  351.     " a  add files",
  352.     "    u - update, replace only if file is newer than saved version",
  353.     "    n - new, add if file is not in archive",
  354.     "    f - fast, don't compress at all",
  355.     "    h - high performance compressor, slower than default",
  356.     "    M - move files to archive, delete after saving",
  357.     "    c - add a comment to each file added",
  358.     "    C - add a comment to the archive as a whole",
  359.     "    : - strip directory names, save filenames only",
  360.     "    q - quiet (qq suppresses warnings, qqq suppresses nonfatal errors too)",
  361.     "    P - pack after adding, remove overwritten or deleted files",
  362.     "        (leaves a .bak file, use PP to overwrite it)",
  363.     "    I - read filenames from standard input",
  364.     "    + - enable generations",
  365.     (char *)NULL
  366. };
  367.  
  368. static char *scrn2[] = {
  369.    "",
  370.     " e  extract files",
  371.     " x  extract files",
  372.     "    : - extract to current directory (ignore pathnames)",
  373.    "    . - make absolute pathnames relative to current directory",
  374.     "        (name /etc/hosts becomes ./etc/hosts)",
  375.     "    / - extract to subdirs, // create subdirs as needed - default",
  376.     "        (the sequence :/ may be used to use but not create subdirs)",
  377.     "    q - quiet",
  378.     "    d - extract deleted files, too. dd extract *only* deleted files",
  379.     "    N - extract to nowhere. Used to test the archive with xN or xNq",
  380.     "    p - extract for pipe to standard output. Use q to avoid header",
  381.     "",
  382. #ifdef FATTR
  383.     "    O - overwrite without asking, OO overwrites readonly files",
  384. #else
  385.     "    O - overwrite without asking",
  386. #endif
  387.     "    S - overwrite superceded (newer) files",
  388.     (char *)NULL
  389. };
  390.  
  391. static char *scrn3[] = {
  392.    "",
  393.     " l  list archive info",
  394.     " L  list info for multiple archives",
  395.     " v  list verbose file info and archive comment",
  396.     " V  list verbose file info, archive and file comments",
  397.     "    v - verbose (same as v command, used with L for multiple files",
  398.     "    V - verbose with file comments",
  399.     "    C - show CRC",
  400.     "    a - show archive name in file listing (useful with L)",
  401. #ifdef FATTR
  402.     "    m - mode, show file modes in octal",
  403. #endif
  404.     "    d - show deleted files",
  405.     "    q - quiet, shows only file info, no comments or headers",
  406.     "    f - fast, lists only filename, no pathname, multiple columns",
  407.     "    1 - one column output (for the f option)",
  408.     "",
  409.     " c  comment changes, change or add comments to listed files",
  410.     "    (changes all file comments if no files given)",
  411.     "    A - only change archive comment",
  412.     (char *)NULL
  413. };
  414.  
  415. static char *scrn4[] = {
  416.    "",
  417.     " P  pack archive, remove deleted or overwritten files",
  418.     "    E - erase the .bak file when done",
  419.     "",
  420.     " D  delete files by name",
  421.     "    P - pack after deletion, use PP if .bak file exists",
  422.     "    q - quiet",
  423.     "",
  424.     " T  timestamp adjust, make archive age of newest file",
  425.     "",
  426.     " g  generation commands",
  427.     "    l - set generation limit on files",
  428.     "    A - apply limit to archive rather than a file (with gl)",
  429.     "",
  430.     " f  filter, copy stdin to stdout with [de]compression",
  431.     "    c - compress",
  432.     "    u - uncompress",
  433.     "    h - use the high compression method",
  434.     (char *)NULL
  435. };
  436.  
  437. static char *scrn5[] = {
  438.    "",
  439.     "Examples:",
  440.     "",
  441.     "# just add a few files",
  442.     "    zoo a arch file1 files",
  443.     "# add C source files in subdirectories",
  444.     "    zoo a test part1/*.c part2/*.c",
  445.     "# add documentation files with high compression",
  446.     "    zoo ah test *.doc",
  447.     "",
  448.     "# extract all files",
  449.     "    zoo x test",
  450.     "# extract files into the current directory",
  451.     "    zoo x: test",
  452.     "# extract a single file and sort before listing",
  453.     "    zoo xp test users.lst | sort",
  454.     "",
  455.     "# list the contents and archive comments",
  456.     "    zoo v arch",
  457.     "# list all files in all archives",
  458.     "    zoo L xxx.zoo /doc/*.zoo ../*.zoo",
  459.     (char *)NULL
  460. };
  461.  
  462. static char **screens[] = {
  463.     scrn1,        /* intro and add */
  464.     scrn2,        /* extract */
  465.     scrn3,        /* list commands */
  466.     scrn4,        /* other commands */
  467.     scrn5,        /* add and extract examples */
  468.     (char **)NULL
  469. };
  470.  
  471. /* multi-screen help routine */
  472. static void ms_help(options)
  473. char *options;
  474. {
  475. #ifndef SZ_SCREEN            /* screen size can be overridden in options.h */
  476. # define SZ_SCREEN 24
  477. #endif
  478.     int scrnlen = SZ_SCREEN;
  479.     char ***curscreen, **curline;
  480.     int linecount;
  481.  
  482.     /* if "Hnn" output in nn line format */
  483.     if (++options) sscanf(options, "%d", &scrnlen);
  484.     if (scrnlen < 2)
  485.         scrnlen = SZ_SCREEN;
  486.  
  487.     /* loop thru screens */
  488.     for (curscreen = screens; *curscreen != NULL; ++curscreen) {
  489.         printf("\n\n\n\n");
  490.         linecount = scrnlen;
  491.         curline = *curscreen;
  492.         while (*curline != NULL) {
  493.             printf("%s\n", *(curline++));
  494.             --linecount;
  495.         }
  496.  
  497.         /* slew page */
  498.         while (--linecount != 0) putchar('\n');
  499.         wait_return();    /* print msg & wait for RETURN */
  500.     }
  501.  
  502.     exit(0);
  503. }
  504.  
  505. /* wait_return prints a message, then waits until user hits RETURN key,
  506. then returns.  Special cases:  (a) if not interactive (as tested with
  507. isatty() if available), it returns immediately; (b) while waiting for
  508. RETURN, if EOF occurs, it causes zooexit(0) */
  509.  
  510. static void wait_return()
  511. {
  512. #ifdef HAVE_ISATTY
  513.     if (!isatty(fileno(stdout)) || !isatty(fileno(stdin)))
  514.         return;
  515. #endif
  516.     (void) printf("Hit RETURN (or ENTER) key to continue...");
  517.     for ( ; ;) {
  518.         int key;
  519.         key = getchar();
  520.         if (key == EOF)
  521.             zooexit(0);
  522.         if (key == '\n' || key == '\r')
  523.             return;
  524.     }
  525. }
  526.