home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 294.lha / iffar_v1.4 / main.c < prev    next >
C/C++ Source or Header  |  1989-10-08  |  6KB  |  214 lines

  1. /* iffar - IFF CAT archiver, main()
  2.  
  3.    By Karl Lehenbauer, version 1.2, release date 5/9/88.
  4.    This code is released to the public domain.
  5.    See the README file for more information.
  6.  
  7. */
  8.  
  9. /* iffar - iff 'cat' archiver, 3/26/88 */
  10.  
  11. #include <stdio.h>
  12. #include "assert.h"
  13.  
  14. /* options */
  15. int verbose = 0;
  16. int insert_after = 0;
  17. int insert_before = 0;
  18. int suppress_creation_message = 0;
  19.  
  20. /* if insert_after or insert_before is set, this global has the relevant
  21.  * name */
  22. char *location_modifier_name;
  23.  
  24. /* variables */
  25. int number_of_arguments;    /* number of args to the program, a global */
  26.  
  27. /* this'll point to the command line's command char string */
  28. char *cmdkeys;
  29.  
  30. /* this'll be the name of the archive file */
  31. char *archive_name;
  32.  
  33. /* this'll contain the command */
  34. char command_key;
  35.  
  36. /* if a command must have some names as arguments, must_have_names can be 
  37.  * called to insure that arguments were supplied.
  38.  * Note that what constitutes the presence of arguments varies whether
  39.  * a positional modifier ('i', 'b' or 'a') was specified.
  40.  */
  41. must_have_names()
  42. {
  43.     if ((insert_before || insert_after) && (number_of_arguments < 5))
  44.     {
  45.         fprintf(stderr,"a positioning element and at least one other element must be specified for\n");
  46.         fprintf(stderr,"the combination of options you have requested.\n");
  47.         usage();
  48.     }
  49.     if (number_of_arguments < 4)
  50.     {
  51.         fprintf(stderr,"at least one archive element must be specified for this option\n");
  52.         usage();
  53.     }
  54. }
  55.  
  56. header()
  57. {
  58.     fprintf(stderr,"iffar - Hackercorp public domain IFF CAT archiver,\n\tVersion 1.4 (9/23/89), by Karl Lehenbauer\n\n");
  59.     fprintf(stderr,"This program maintains archives of IFF FORM and CAT files\n");
  60.     fprintf(stderr,"in a manner that complies with the IFF CAT specification.\n");
  61. }
  62.  
  63. /* usage  print usage text and exit */
  64. usage()
  65. {
  66.     fprintf(stderr,"\nUsage: iffar key [posname] afile name ...\n\n");
  67.     fprintf(stderr,"key can be one of the following:\n");
  68.     fprintf(stderr,"\td\tdelete\n\tr\treplace\n\tq\tquick append\n");
  69.     fprintf(stderr,"\tt\ttable of contents\n\tx\textract\n");
  70.     fprintf(stderr,"and zero or more of the following options:\n");
  71.     fprintf(stderr,"\tv\tverbose\n\ta\tafter\n\ti,b\tbefore\n");
  72.     fprintf(stderr,"\tc\tsuppress creation message\n");
  73.     cleanup();
  74.     exit(1);
  75. }
  76.  
  77. main(argc,argv)
  78. int argc;
  79. char *argv[];
  80. {
  81.     int i, j;
  82.     int archive_fd, files;
  83.     char *nameptr;
  84.     char **entryname_pointers;
  85.     int entrycount;
  86.  
  87.     number_of_arguments = argc;
  88.  
  89.     if (argc < 3)
  90.     {
  91.         header();
  92.         usage();
  93.     }
  94.  
  95.     /* assign nice variable names to various command line arguments */
  96.     cmdkeys = argv[1];
  97.     command_key = *cmdkeys;
  98.     archive_name = argv[2];
  99.     location_modifier_name = (char *)NULL;
  100.     entryname_pointers = &argv[3];
  101.     entrycount = argc - 3;
  102.  
  103.     /* figure out any modifiers specified to the main function requested */
  104.     for (i = 1; i < strlen(cmdkeys); i++)
  105.     {
  106.         switch(cmdkeys[i])
  107.         {
  108.             case 'v':                /* verbose selected, set verbose flag */
  109.                 verbose = 1;
  110.                 break;
  111.  
  112.             /* 'after' option, make sure they've selected 'r' or 'm' as
  113.              * the main command.  Also, make sure they haven't already
  114.              * chosen the insert_before option.  After that, we're OK,
  115.              * so twiddle parameters and break
  116.              */
  117.             case 'a':
  118.                 if (command_key != 'r' && command_key != 'm')
  119.                 {
  120.                     fprintf(stderr,"i, b and a modifiers are only good for r and m options\n");
  121.                     usage();
  122.                 }
  123.                 if (insert_before)
  124.                 {
  125.                     fprintf(stderr,"you can't select 'insert before' and 'insert after'\n");
  126.                     usage();
  127.                 }
  128.                 insert_after = 1;
  129.                 location_modifier_name = argv[3];
  130.                 entryname_pointers = &argv[4];
  131.                 entrycount = argc - 4;
  132.                 break;
  133.  
  134.             /* 'insert before' option, make sure they've selected 'r' or
  135.              * 'm' as the main command.  Also, make sure they haven't already
  136.              * chosen the insert_after option.  After that, we're OK,
  137.              * so twiddle parameters and break
  138.              */
  139.             case 'i':                /* insert; before */
  140.             case 'b':
  141.                 if (command_key != 'r' && command_key != 'm')
  142.                 {
  143.                     fprintf(stderr,"i, b and a modifiers are only good for r and m options\n");
  144.                     usage();
  145.                 }
  146.                 if (insert_after)
  147.                 {
  148.                     fprintf(stderr,"you can't select 'insert before' and 'insert after'\n");
  149.                     usage();
  150.                 }
  151.                 insert_before = 1;
  152.                 location_modifier_name = argv[3];
  153.                 entryname_pointers = &argv[4];
  154.                 entrycount = argc - 4;
  155.                 break;
  156.  
  157.             case 'c':                /* supress creation message */
  158.                 suppress_creation_message = 1;
  159.                 break;
  160.  
  161.             default:                /* unrecognized option */
  162.                 fprintf(stderr,"option '%c' unrecognized\n",cmdkeys[i]);
  163.                 usage();
  164.         }
  165.     }
  166.  
  167.     /* now execute the major command */
  168.  
  169.     switch(command_key)
  170.     {
  171.         case 'd':        /* delete */
  172.             must_have_names();
  173.             delete_entries(archive_name,entryname_pointers,entrycount);
  174.             break;
  175.  
  176.         /* replace - if the archive doesn't exist, fall through to
  177.          * quick append */
  178.         case 'r':
  179.             must_have_names();
  180.             if ((archive_fd = open(archive_name,O_RDONLY)) != -1)
  181.             {
  182.                 close(archive_fd);
  183.                 replace_entries(archive_name,entryname_pointers,entrycount);
  184.                 break;
  185.             }
  186.         case 'q':        /* quick append */
  187.             must_have_names();
  188.             checknew(archive_name);
  189.             if ((archive_fd = open_quick_append(archive_name)) == -1)
  190.                 break;
  191.             quickappend_entries(archive_fd,entryname_pointers,entrycount);
  192.             break;
  193.         case 't':        /* table of contents */
  194.             table_of_contents(archive_name);
  195.             break;
  196.  
  197.         case 'm':        /* move */
  198.             fprintf(stderr,"move option not implemented\n");
  199.             must_have_names();
  200.             break;
  201.         case 'x':        /* extract */
  202.             extract(archive_name,entryname_pointers,entrycount);
  203.             break;
  204.  
  205.         default:
  206.             fprintf(stderr,"requested command (%c) is invalid\n",command_key);
  207.             usage();
  208.     }
  209.     cleanup();
  210.     exit(0);
  211. }
  212.  
  213. /* end of main.c */
  214.