home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / ZOO.C < prev    next >
C/C++ Source or Header  |  1987-02-07  |  9KB  |  274 lines

  1. /* zoo.c -- includes main() function */
  2. /*
  3. Copyright (C) 1986 Rahul Dhesi -- All rights reserved
  4. */
  5. #include "options.h"
  6. #include <stdio.h>
  7. #include "various.h"
  8.  
  9. #include "zoo.h"
  10. #include "zoofns.h"
  11.  
  12. #include "errors.i"
  13. #include "zoomem.h"
  14.  
  15. #ifdef DEBUG
  16. int verbose = 0;
  17. #endif
  18.  
  19. char *out_buf_adr;      /* points to memory allocated for output buffer(s) */
  20. char *in_buf_adr;       /* points to memory allocated for input buffer */
  21.  
  22. /* static declarations */
  23. int quiet = 0;             /* whether to be quiet */
  24. int next_arg = FIRST_ARG; /* filenames start at this position */
  25. int arg_count;          /* count of arguments supplied to program */
  26. char **arg_vector;      /* vector of arguments supplied to program */
  27.  
  28. /* Suppress loading of some Microsoft C library routines. */
  29. #ifdef NDEBUG
  30. #ifndef PORTABLE
  31. _nullcheck() {}         /* prevent loading of Microsoft's null ptr check */
  32. #endif
  33. #endif
  34.  
  35. #ifndef PORTABLE
  36. _setenvp() {}           /* prevent loading of Microsoft's _setenvp */
  37. #endif
  38.  
  39. main(argc,argv)
  40. register int argc;
  41. register char **argv;
  42. {
  43.    char *zooname;          /* synonym for argv[2] -- to make life easier */
  44. #ifndef OOZ
  45.    static char incorrect_args[] = "Incorrect number of arguments.\n";
  46.    int filecount;          /* how many filespecs supplied */
  47. #endif /* OOZ */
  48.  
  49. #ifdef OOZ
  50.       static char usage1[] = "Public domain fast Ooz extractor, INTERNAL USE TEST VERSION by Rahul Dhesi\n";
  51.       static char usage2[] = "Usage:  ooz archive[.zoo] [file] ...\n";
  52.  
  53. #else
  54. /* else not OOZ */
  55.       static char usage[] = "Usage:  zoo {acDelLPTuUvx}[acdEfnMNoOpPquz1:/] archive file (\"zoo h\" for help)\n";
  56.       static char nov_usage[] = 
  57.           "\nNovice usage:  zoo -cmd archive[.zoo] file...  where -cmd is one of these:\n";
  58.       char *option;
  59.  
  60.       static char nov_cmds[] = 
  61.          /* ADD=0EXT=5    MOV=14TES=20PRI=26 DEL=33  LIS=41UPD=47  FRE=55   COMMENT=64 */
  62.            "-add -extract -move -test -print -delete -list -update -freshen -comment\n";
  63.  
  64. #ifdef NOENUM
  65. #define NONE   -1
  66. #define ADD    0
  67. #define EXTRACT 5
  68. #define MOVE   14
  69. #define TEST   20
  70. #define PRINT  26
  71. #define DELETE 33
  72. #define LIST   41
  73. #define UPDATE 47
  74. #define FRESHEN   55
  75. #define COMMENT   64
  76.  
  77. int cmd = NONE;
  78.  
  79. #else
  80.    enum choice {
  81.       NONE=-1, ADD=0, EXTRACT=5, MOVE=14, TEST=20, PRINT=26, 
  82.       DELETE=33, LIST=41, UPDATE=47, FRESHEN=55, COMMENT=64
  83.    };
  84.    enum choice cmd = NONE;          /* assume no Novice command */
  85. #endif
  86.  
  87. #endif /* end of not OOZ */
  88.  
  89. #ifdef SETBUF
  90. /* set stdout to unbuffered */
  91. setbuf (stdout, NULL);
  92. #endif
  93.  
  94.    arg_count = argc;
  95.    arg_vector = argv;
  96.    zooname = argv[FIRST_ARG-1];     /* points to name or archive */
  97.  
  98. #ifdef OOZ
  99.    if (argc < 2) {
  100.       putstr (usage1);
  101.       putstr (usage2);
  102.       exit (1);
  103.    }
  104. #else
  105. /* else not OOZ */
  106.    if (argc < 2)
  107.       goto show_usage;
  108.    filecount = argc - 3;
  109.    option = strdup(argv[1]);
  110.  
  111. #ifdef DEBUG
  112.    if (*option == ':') {         /* for debugging output */
  113.       verbose++;
  114.       option++;                  /* hide the $ from other functions */
  115.    }
  116. #endif
  117.  
  118.    if (*option == 'h' || *option == 'H')
  119.       goto bigusage;
  120.    if (*option == '-') {
  121.  
  122. #ifdef NOENUM
  123.       cmd = index (nov_cmds, strlwr(option));
  124. #else
  125.       cmd = (enum choice) index (nov_cmds, strlwr(option));
  126. #endif
  127.  
  128.       if (strlen(option) < 2 || cmd == NONE)
  129.          goto show_usage;
  130.       if (  ((cmd == ADD || cmd == MOVE || cmd == FRESHEN || 
  131.                   cmd == UPDATE || cmd == DELETE) && argc < 4) ||
  132.             ((cmd == EXTRACT || cmd == TEST || cmd == LIST ||
  133.                      cmd == PRINT || cmd == COMMENT) && argc < 3)) {
  134.          fprintf (stderr, incorrect_args);
  135.          goto show_usage;
  136.       }
  137.    } else {
  138.       if    (strchr("aDuU",*option) && argc < 4 ||
  139.              strchr("cexlL",*option) && argc < 3 ||
  140.              strchr("TP",*option)   && argc != 3)  {
  141.          fprintf (stderr, incorrect_args);
  142.          goto show_usage;
  143.       }
  144.    }
  145. #endif /* end of not OOZ */
  146.  
  147. #ifndef OOZ
  148.    /* if not doing a list and no extension in archive name, add default 
  149.    extension */
  150.    if (cmd != LIST && strchr("lvL", *option) == NULL &&
  151.          strchr(nameptr (zooname), EXT_CH) == NULL)
  152.       zooname = newcat (zooname, EXT_DFLT);
  153. #endif
  154.  
  155. #ifndef PORTABLE
  156. #ifndef OOZ
  157. /* only need to ensure integrity of created archive */
  158.    break_off();   /* break = off -- specific to MSDOS */
  159. #endif
  160. #endif
  161.  
  162. /* 
  163. Here we allocate a large block of memory for the duration of the program.
  164. lzc() and lzd() will use half of it each.  Routine getfile() will use all
  165. of it. 
  166. */
  167. /* fudge factor to avoid off-by-one errors */
  168. #define  FUDGE    10
  169.  
  170. /*                          fudge/2           fudge/2
  171. **             [______________||________________|]
  172. **               output buffer    input buffer
  173. */
  174.  
  175.    out_buf_adr = emalloc (OUT_BUF_SIZE + IN_BUF_SIZE + FUDGE);
  176.  
  177.    /* input buffer is in top of allocated block */
  178.    in_buf_adr = out_buf_adr + OUT_BUF_SIZE + (FUDGE/2);
  179.  
  180. #ifdef OOZ
  181. zooext(zooname, "\0");     /* just extract -- no fancy stuff   */
  182. exit (0);                  /* and exit normally                */
  183. #else
  184. /* else not OOZ -- parse command line and invoke a routine */
  185.    if (cmd != NONE) {
  186.       switch (cmd) {
  187.  
  188.          case ADD:      zooadd (zooname, filecount, &argv[3], "aP"); break;
  189.          case FRESHEN:  zooadd (zooname, filecount, &argv[3], "auP"); break;
  190.          case UPDATE:   zooadd (zooname, filecount, &argv[3], "aunP"); break;
  191.          case MOVE:     zooadd (zooname, filecount, &argv[3], "aMP"); break;
  192.  
  193.          case EXTRACT:  zooext (zooname, "x"); break;
  194.          case TEST:     zooext (zooname, "xNd"); break;
  195.          case PRINT:    zooext (zooname, "xp"); break;
  196.  
  197.          case DELETE:   zoodel (zooname, "DP",1); break;
  198.          case LIST:     zoolist (&argv[2], "v", argc-2); break;
  199.          case COMMENT:  comment (zooname, "c"); break;
  200.          default: goto show_usage;
  201.       }
  202.    } else
  203.       switch (*option) {
  204.          case 'a': 
  205.          case 'u':
  206.          case 'T':   
  207.             zooadd (zooname, filecount, &argv[3], option); break;
  208.          case 'D':
  209.             zoodel (zooname, option, 1); break;
  210.          case 'U':
  211.             zoodel (zooname, option, 0); break;
  212.          case 'v':
  213.          case 'l': 
  214.             zoolist(&argv[2], option, 1); break;
  215.          case 'L': 
  216.             zoolist(&argv[2], option, argc-2); break;
  217.          case 'e':
  218.          case 'x': 
  219.             zooext(zooname, option); break;
  220.          case 'P':
  221.             zoopack (zooname, option); break;
  222.          case 'c':
  223.             comment (zooname, option); break;
  224.          default:
  225.             fprintf (stderr, usage); exit (1); break;
  226.       }
  227. exit (0);      /* don't fall through */
  228.  
  229. show_usage:
  230.    fprintf (stderr, "%s%s%s", usage, nov_usage, nov_cmds);
  231.    exit (1);
  232.  
  233. bigusage:
  234.  
  235. printf ("Zoo archiver, version 1.41 (GENERIC, 1987/02/07)\n");
  236. printf("(C) Copyright 1986, 1987 Rahul Dhesi -- Noncommercial use permitted\n");
  237.  
  238. printf (usage);
  239. printf ("\nChoose a command from within {} and zero or more modifiers from within []\n");
  240.  
  241. printf ("E.g.:  `zoo x bin/backups' will extract all files from bin/backups.zoo\n\n");
  242.  
  243. printf (" Commands in {} mean:         |Modifiers in [] mean:\n");
  244.  
  245. printf ("  a     add files             | a     show archive name(s) in listing\n");
  246. printf ("  c     update comments       | c     add/list comments\n");
  247. printf ("  D     delete stored files   | d     extract/list deleted files too\n");
  248. printf ("  e,x   extract files         | dd    extract/list only deleted files\n");
  249. printf ("  l,v,L list filenames        | E     erase backup after packing\n");
  250. printf ("  P     pack archive          | f     fast add (no compression) or list\n");
  251. printf ("  T     fix archive datestamp | M     move when adding (erase original)\n");
  252. printf ("  u     add only newer files  | n     add only files not already in archive\n");
  253. printf ("  U     undelete stored files | N     send extracted data to Neverland\n");
  254. printf (" -----------------------------  O,oo  don't ask \"Overwrite?\"\n");
  255. printf ("  q     be quiet                p     pipe extracted data to standard output\n");
  256. printf ("  :     don't store dir names   /,//  extract full pathnames\n");
  257.  
  258. #ifdef PORTABLE
  259. printf ("  P     pack after adding       @n    start extract/list at position n\n");
  260. /* nothing */
  261. #else
  262. printf ("  z     add/extract Z format    @n    start extract/list at position n\n");
  263. #endif /* ndef PORTABLE */
  264.  
  265.  
  266. printf (nov_usage);
  267. printf (nov_cmds);
  268. #endif /* end of not OOZ */
  269.  
  270. /* NOTE:  if allowed to fall through and return without an exit() statement,
  271.    it was printing garbage--corrupted stack?  Why--bug in Microsoft C? */
  272. exit (1);
  273. }
  274.