home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / compress / zoosrc20.zoo / zoo.c < prev    next >
C/C++ Source or Header  |  1989-07-25  |  10KB  |  317 lines

  1. #ifndef LINT
  2. /* @(#) zoo.c 2.24 88/01/29 00:55:09 */
  3. static char sccsid[]="@(#) zoo.c 2.24 88/01/29 00:55:09";
  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. */
  17. #include "options.h"
  18. #include "zooio.h"
  19. #include "various.h"
  20.  
  21. #include "zoo.h"
  22. #include "zoofns.h"
  23.  
  24. #include "errors.i"
  25. #include "zoomem.h"
  26.  
  27. #ifdef TRACE_IO
  28. int verbose = 0;
  29. #endif
  30.  
  31. #ifdef LINT_ARGS
  32. int instr (char *, char *);
  33. #else
  34. int instr ();
  35. #endif
  36.  
  37. char *out_buf_adr;      /* points to memory allocated for output buffer(s) */
  38. char *in_buf_adr;       /* 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[] = "Usage: zoo {acDeglLPTuUvx}[aAcCdEfInmMNoOpPqu1:/.@n] archive file\n(\"zoo h\" for 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 ((NOTHING));
  95.     spec_init();                            /* system-specific startup code */
  96. #endif
  97.  
  98.    arg_count = argc;
  99.    arg_vector = argv;
  100.    zooname = argv[FIRST_ARG-1];     /* points to name or archive */
  101.  
  102. #ifdef OOZ
  103.    if (argc < 2) {
  104.       putstr (usage1);
  105.       putstr (usage2);
  106.       zooexit (1);
  107.    }
  108. #else
  109. /* else not OOZ */
  110.    if (argc < 2)
  111.       goto show_usage;
  112.    filecount = argc - 3;
  113.    option = strdup(argv[1]);
  114.  
  115. #ifdef TRACE_IO
  116.    if (*option == ':') {         /* for debugging output */
  117.       verbose++;
  118.       option++;                  /* hide the : from other functions */
  119.    }
  120. #endif
  121.  
  122. #ifdef WAIT_PROMPT
  123.    if (*option == 'w') {
  124.         option++;                        /* hide w from other functions */
  125.         printf ("Hit RETURN when ready: ");
  126.         while (getchar() != '\n')
  127.             ;
  128.     }
  129. #endif /* WAIT_PROMPT */
  130.  
  131.    if (*option == 'h' || *option == 'H')
  132.       goto bigusage;
  133.     if (strchr("-acDegflLPTuUvVx", *option) == NULL)
  134.         goto give_list;
  135.  
  136.    if (*option == '-') {
  137.  
  138. #ifdef NOENUM
  139.       cmd = instr (nov_cmds, strlwr(option));
  140. #else
  141.       cmd = (enum choice) instr (nov_cmds, strlwr(option));
  142. #endif
  143.  
  144.       if (strlen(option) < 2 || cmd == NONE)
  145.          goto show_usage;
  146.       if (  ((cmd == ADD || cmd == MOVE || cmd == FRESHEN ||
  147.                   cmd == UPDATE || cmd == DELETE) && argc < 4) ||
  148.             ((cmd == EXTRACT || cmd == TEST || cmd == LIST ||
  149.                      cmd == PRINT || cmd == COMMENT) && argc < 3)) {
  150.          fprintf (stderr, incorrect_args);
  151.          goto show_usage;
  152.       }
  153.    } else {
  154.         char *wheresI;        /* will be null if I option not supplied */
  155.         if    (
  156.                 (
  157.                     strchr("au",*option) &&
  158.                     (
  159.                         (((wheresI = strchr(option,'I')) != 0) &&
  160.                             argc != 3) ||
  161.                         wheresI==NULL && argc < 4
  162.                     )
  163.                 ) ||
  164.                  strchr("DU",*option) && argc < 4 ||
  165.              strchr("cexlvVL",*option) && argc < 3 ||
  166.              strchr("TP",*option)   && argc != 3 ||
  167.                  (*option == 'f' && argc != 2) ||
  168.                  (*option == 'g' &&
  169.                     (strchr(option,'A') == NULL && argc < 4 ||
  170.                      strchr(option,'A') != NULL && argc != 3
  171.                     )
  172.                  )
  173.             ) {
  174.          fprintf (stderr, incorrect_args);
  175.          goto show_usage;
  176.       }
  177.    }
  178. #endif /* end of not OOZ */
  179.  
  180. #ifndef OOZ
  181.    /* if not doing a list and no extension in archive name, add default
  182.    extension */
  183.    if (*option != 'f' && cmd != LIST && strchr("lvVL", *option) == NULL &&
  184.          strchr(nameptr (zooname), EXT_CH) == NULL)
  185.       zooname = newcat (zooname, EXT_DFLT);
  186. #endif
  187.  
  188. /*
  189. Here we allocate a large block of memory for the duration of the program.
  190. lzc() and lzd() will use half of it each.  Routine getfile() will use all
  191. of it.
  192. */
  193. /* fudge factor to avoid off-by-one errors */
  194. #define  FUDGE    10
  195.  
  196. /*                          fudge/2           fudge/2
  197. **             [______________||________________|]
  198. **               output buffer    input buffer
  199. */
  200.    out_buf_adr = emalloc (OUT_BUF_SIZE + IN_BUF_SIZE + FUDGE);
  201.  
  202.    /* input buffer is in top of allocated block */
  203.    in_buf_adr = out_buf_adr + OUT_BUF_SIZE + (FUDGE/2);
  204.  
  205. #ifdef OOZ
  206. zooext(zooname, "\0");     /* just extract -- no fancy stuff   */
  207. zooexit (0);                  /* and exit normally                */
  208. #else
  209. /* else not OOZ -- parse command line and invoke a routine */
  210.    if (cmd != NONE) {
  211.       switch (cmd) {
  212.  
  213.          case ADD:      zooadd (zooname, filecount, &argv[3], "aP:"); break;
  214.          case FRESHEN:  zooadd (zooname, filecount, &argv[3], "auP:"); break;
  215.          case UPDATE:   zooadd (zooname, filecount, &argv[3], "aunP:"); break;
  216.          case MOVE:     zooadd (zooname, filecount, &argv[3], "aMP:"); break;
  217.  
  218.          case EXTRACT:  zooext (zooname, "x"); break;
  219.          case TEST:     zooext (zooname, "xNd"); break;
  220.          case PRINT:    zooext (zooname, "xp"); break;
  221.  
  222.          case DELETE:   zoodel (zooname, "DP",1); break;
  223.          case LIST:     zoolist (&argv[2], "VC", argc-2); break;
  224.          case COMMENT:  comment (zooname, "c"); break;
  225.          default: goto show_usage;
  226.       }
  227.    } else
  228.       switch (*option) {
  229.  
  230.          case 'a':
  231.          case 'u':
  232.          case 'T':   
  233.             zooadd (zooname, filecount, &argv[3], option); break;
  234. #ifdef FILTER
  235.             case 'f':
  236.                 zoofilt (option);  break;
  237. #endif /* FILTER */
  238.          case 'D':
  239.             zoodel (zooname, option, 1); break;
  240.          case 'U':
  241.             zoodel (zooname, option, 0); break;
  242.             case 'g':
  243.                 zoodel (zooname, option, 2); break;
  244.          case 'v':
  245.             case 'V':
  246.          case 'l': 
  247.             zoolist(&argv[2], option, 1); break;
  248.          case 'L': 
  249.             zoolist(&argv[2], option, argc-2); break;
  250.          case 'e':
  251.          case 'x': 
  252.             zooext(zooname, option); break;
  253.          case 'P':
  254.             zoopack (zooname, option); break;
  255.          case 'c':
  256.             comment (zooname, option); break;
  257.          default:
  258.             goto give_list;
  259.       }
  260. zooexit (0);      /* don't fall through */
  261.  
  262. /* usage list including Novice commands */
  263. show_usage:
  264.    fprintf (stderr, "%s%s%s", usage, nov_usage, nov_cmds); zooexit (1);
  265.  
  266. /* brief usage list */
  267. give_list:
  268.     fprintf (stderr, usage); zooexit (1);
  269.  
  270. /* help screen */
  271. bigusage:
  272.  
  273. printf ("Zoo archiver, %s\n", version);
  274. printf("(C) Copyright 1988 Rahul Dhesi -- Noncommercial use permitted\n");
  275.  
  276. printf (usage);
  277. printf ("\nChoose a command from within {} and zero or more modifiers from within [].\n");
  278.  
  279. printf ("E.g.:  `zoo a save /bin/*' will archive all files in /bin into save.zoo.\n");
  280. printf ("(Please see the user manual for a complete description of commands.)\n\n");
  281.  
  282. printf (" Commands in {} mean:         |Modifiers in [] mean:\n");
  283.  
  284. printf ("  a     add files             | a     show archive name(s) in listing\n");
  285. printf ("  c     update comments       | A     apply g or c to archive\n");
  286. printf ("  D     delete stored files   | c     add/list comments\n");
  287. printf ("  e,x   extract files         | d     extract/list deleted files too\n");
  288. printf ("  g     adj. gen. limit/count | dd    extract/list only deleted files\n");
  289. printf ("  l,L,v,V list filenames      | E     erase backup after packing\n");
  290. printf ("  P     pack archive          | f     fast add (no compression) or list\n");
  291. printf ("  T     fix archive datestamp | M     move when adding (erase original)\n");
  292. printf ("  u     add only newer files  | n     add only files not already in archive\n");
  293. printf ("  U     undelete stored files | N     send extracted data to Neverland\n");
  294. #ifdef FILTER
  295. printf ("  f     act as filter         | c/u   compress/uncompress as filter\n");
  296. #endif /* FILTER */
  297. printf (" -----------------------------  O     don't ask \"Overwrite?\"\n");
  298. printf ("  q     be quiet                p     pipe extracted data to standard output\n");
  299. printf ("  :     don't store dir names   /,//  extract full pathnames\n");
  300. printf ("  .     pack to current dir     I     add filenames read from stdin\n");
  301. printf ("  C     show file CRC value     +/-   enable/disable generations\n");
  302. printf ("  S     overwrite newer files   g     list generation limits\n");
  303. printf ("  P     pack after adding       @n    start extract/list at position n\n");
  304.  
  305. #ifdef FATTR
  306. printf ("  m     list file modes         OO    overwrite read-only files\n");
  307. #endif /* FATTR */
  308.  
  309. printf (nov_usage);
  310. printf (nov_cmds);
  311. #endif /* end of not OOZ */
  312.  
  313. /* NOTE:  if allowed to fall through and return without an exit() statement,
  314.    it was printing garbage--corrupted stack?  Why--bug in Microsoft C? */
  315. zooexit (1);
  316. }
  317.