home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / OS2ARC_S.ZIP / ARC.C < prev    next >
Text File  |  1987-10-15  |  11KB  |  291 lines

  1. /*  ARC - Archive utility
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =5.12), created on $tag(
  6. TED_DATE DB =02/05/86) at $tag(
  7. TED_TIME DB =22:22:01))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This program is a general archive utility, and is used to maintain
  17.          an archive of files.  An "archive" is a single file that combines
  18.          many files, reducing storage space and allowing multiple files to
  19.          be handled as one.
  20.  
  21.     Instructions:
  22.          Run this program with no arguments for complete instructions.
  23.  
  24.     Programming notes:
  25.          ARC Version 2 differs from version 1 in that archive entries
  26.          are automatically compressed when they are added to the archive,
  27.          making a separate compression step unecessary.  The nature of the
  28.          compression is indicated by the header version number placed in
  29.          each archive entry, as follows:
  30.  
  31.          1 = Old style, no compression
  32.          2 = New style, no compression
  33.          3 = Compression of repeated characters only
  34.          4 = Compression of repeated characters plus Huffman SQueezing
  35.          5 = Lempel-Zev packing of repeated strings (old style)
  36.          6 = Lempel-Zev packing of repeated strings (new style)
  37.          7 = Lempel-Zev Williams packing with improved has function
  38.          8 = Dynamic Lempel-Zev packing with adaptive reset
  39.  
  40.          Type 5, Lempel-Zev packing, was added as of version 4.0
  41.  
  42.          Type 6 is Lempel-Zev packing where runs of repeated characters
  43.          have been collapsed, and was added as of version 4.1
  44.  
  45.          Type 7 is a variation of Lempel-Zev using a different hash
  46.          function which yields speed improvements of 20-25%, and was
  47.          added as of version 4.6
  48.  
  49.          Type 8 is a different implementation of Lempel-Zev, using a
  50.          variable code size and an adaptive block reset, and was added
  51.          as of version 5.0
  52.  
  53.          Verion 4.3 introduced a temporary file for holding the result
  54.          of the first crunch pass, thus speeding up crunching.
  55.  
  56.          Version 4.4 introduced the ARCTEMP environment string, so that
  57.          the temporary crunch file may be placed on a ramdisk.  Also
  58.          added was the distinction bewteen Adding a file in all cases,
  59.          and Updating a file only if the disk file is newer than the
  60.          corresponding archive entry.
  61.  
  62.          The compression method to use is determined when the file is
  63.          added, based on whichever method yields the smallest result.
  64.  
  65.     Language:
  66.          Computer Innovations Optimizing C86
  67. */
  68. #include <stdio.h>
  69. #include "arc.h"
  70.  
  71. main(num,arg)                          /* system entry point */
  72. int num;                               /* number of arguments */
  73. char *arg[];                           /* pointers to arguments */
  74. {
  75.     char opt = 0;                      /* selected action */
  76.     char *a;                           /* option pointer */
  77.     char *makefnam();                  /* filename fixup routine */
  78.     char *strupr();                    /* case conversion routine */
  79.     extern char *strchr();             /* string index utility *//* was index() mpl */
  80.     char *getenv();                    /* environment searcher */ /*mpl*/
  81.     int n;                             /* argument index */
  82.  
  83.     if(num<3)
  84.     {    printf("ARC - Archive utility, version 5.12mpl for Microsoft OS/2\n");
  85.          printf("(C) COPYRIGHT 1985,86 by System Enhancement Associates;");
  86.          printf(" ALL RIGHTS RESERVED\n\n");
  87.          printf("Please refer all inquiries to:\n\n");
  88.          printf("       System Enhancement Associates\n");
  89.          printf("       21 New Street, Wayne NJ 07470\n\n");
  90.          printf("You may copy and distribute this program freely,");
  91.          printf(" provided that:\n");
  92.          printf("    1)   No fee is charged for such copying and");
  93.          printf(" distribution, and\n");
  94.          printf("    2)   It is distributed ONLY in its original,");
  95.          printf(" unmodified state.\n\n");
  96.          printf("If you like this program, and find it of use, then your");
  97.          printf(" contribution will\n");
  98.          printf("be appreciated.  You may not use this product in a");
  99.          printf(" commercial environment\n");
  100.          printf("or a governmental organization without paying a license");
  101.          printf(" fee of $35.  Site\n");
  102.          printf("licenses and commercial distribution licenses are");
  103.          printf(" available.  A program\n");
  104.          printf("disk and printed documentation are available for $50.\n");
  105.          printf("\nIf you fail to abide by the terms of this license, ");
  106.          printf(" then your conscience\n");
  107.          printf("will haunt you for the rest of your life.\n\n");
  108.          printf("Usage: ARC {amufdxerplvtc}[bswn][g<password>]");
  109.          printf(" <archive> [<filename> . . .]\n");
  110.          printf("Where:   a   = add files to archive\n");
  111.          printf("         m   = move files to archive\n");
  112.          printf("         u   = update files in archive\n");
  113.          printf("         f   = freshen files in archive\n");
  114.          printf("         d   = delete files from archive\n");
  115.          printf("         x,e = extract files from archive\n");
  116.          printf("         r   = run files from archive\n");
  117.          printf("         p   = copy files from archive to");
  118.          printf(" standard output\n");
  119.          printf("         l   = list files in archive\n");
  120.          printf("         v   = verbose listing of files in archive\n");
  121.          printf("         t   = test archive integrity\n");
  122.          printf("         c   = convert entry to new packing method\n");
  123.          printf("         b   = retain backup copy of archive\n");
  124.          printf("         s   = suppress compression (store only)\n");
  125.          printf("         w   = suppress warning messages\n");
  126.          printf("         n   = suppress notes and comments\n");
  127.          printf("         g   = Encrypt/decrypt archive entry\n");
  128.          printf("\nPlease refer to the program documentation for");
  129.          printf(" complete instructions.\n");
  130.          return 1;
  131.     }
  132.  
  133.     /* see where temp files go */
  134.  
  135.     if(!(arctemp = getenv("ARCTEMP")))        /*mpl*/
  136.          arctemp = getenv("TEMP");        /*mpl*/
  137.  
  138.     /* avoid any case problems with arguments */
  139.  
  140.     for(n=1; n<num; n++)               /* for each argument */
  141.          strupr(arg[n]);                /* convert it to uppercase */ /* msc uses strupr not upper(); mpl */
  142.  
  143.     /* create archive names, supplying defaults */
  144.  
  145.     makefnam(arg[2],".ARC",arcname);
  146.     makefnam(".$$$",arcname,newname);
  147.     makefnam(".BAK",arcname,bakname);
  148.  
  149.     /* now scan the command and see what we are to do */
  150.  
  151.     for(a=arg[1]; *a; a++)             /* scan the option flags */
  152.     {    if(strchr("AMUFDXEPLVTCR",*a)) /* if a known command *//*mpl*/
  153.          {    if(opt)                  /* do we have one yet? */
  154.                    abort("Cannot mix %c and %c",opt,*a);
  155.               opt = *a;                /* else remember it */
  156.          }
  157.  
  158.          else if(*a=='B')              /* retain backup copy */
  159.               keepbak = 1;
  160.  
  161.          else if(*a=='W')              /* suppress warnings */
  162.               warn = 0;
  163.  
  164.          else if(*a=='N')              /* suppress notes and comments */
  165.               note = 0;
  166.  
  167.          else if(*a=='G')              /* garble */
  168.          {    password = a+1;
  169.               while(*a)
  170.                    a++;
  171.               a--;
  172.          }
  173.  
  174.          else if(*a=='S')              /* storage kludge */
  175.               nocomp = 1;
  176.  
  177.          else if(*a=='K')              /* special kludge */
  178.               kludge = 1;
  179.  
  180.          else if(*a=='-' || *a=='/')   /* UNIX and PC-DOS option markers */
  181.               ;
  182.  
  183.          else abort("%c is an unknown command",*a);
  184.     }
  185.  
  186.     if(!opt)
  187.          abort("I have nothing to do!");
  188.  
  189.     /* act on whatever action command was given */
  190.  
  191.     switch(opt)                        /* action depends on command */
  192.     {
  193.     case 'A':                          /* Add */
  194.     case 'M':                          /* Move */
  195.     case 'U':                          /* Update */
  196.     case 'F':                          /* Freshen */
  197.          addarc(num-3,&arg[3],(opt=='M'),(opt=='U'),(opt=='F'));
  198.          break;
  199.  
  200.     case 'D':                          /* Delete */
  201.          delarc(num-3,&arg[3]);
  202.          break;
  203.  
  204.     case 'E':                          /* Extract */
  205.     case 'X':                          /* eXtract */
  206.     case 'P':                          /* Print */
  207.          extarc(num-3,&arg[3],(opt=='P'));
  208.          break;
  209.  
  210.     case 'V':                          /* Verbose list */
  211.          bose = 1;
  212.     case 'L':                          /* List */
  213.          lstarc(num-3,&arg[3]);
  214.          break;
  215.  
  216.     case 'T':                          /* Test */
  217.          tstarc();
  218.          break;
  219.  
  220.     case 'C':                          /* Convert */
  221.          cvtarc(num-3,&arg[3]);
  222.          break;
  223.  
  224.     case 'R':                          /* Run */
  225.          runarc(num-3,&arg[3]);
  226.          break;
  227.  
  228.     default:
  229.          abort("I don't know how to do %c yet!",opt);
  230.     }
  231.  
  232.     return nerrs;
  233. }
  234.  
  235.  
  236. /*  +-------------------------------------------------------------------+
  237.     |  makefnam(inname,extens,outname)                    |
  238.     |  Creates new file name from extension given, and if        |
  239.     |  the first arg is an extension, name is forcibly put on,        |
  240.     |  else it is only replaced if it isn't already there        |
  241.     +-------------------------------------------------------------------+ */
  242. char *makefnam(inname,extens,outname)
  243. char *inname,*extens,*outname;
  244. {
  245.     extern char *strchr();
  246.     char *x;
  247.     char *y;
  248.  
  249.     if (*inname != '.') {
  250.         y = strchr(extens,'.');
  251.         if (!y) y = "";
  252.         strcpy(outname,inname);
  253.         if (!strchr(outname,'.')) strcat(outname,y);
  254.         return outname;
  255.         }
  256.     else {
  257.         strcpy(outname,extens);
  258.         if (x = strchr(outname,'.')) strcpy(x,inname);
  259.         else strcat(outname,inname);
  260.         return outname;
  261.         }
  262. }
  263.  
  264.  
  265. /*  +-------------------------------------------------------------------+
  266.     |  abort(a,b,c,d,e)                            |
  267.     |  dies.                                |
  268.     +-------------------------------------------------------------------+ */
  269. abort(a,b,c,d,e)
  270. {
  271.     fprintf(stderr,"arc: ");
  272.     fprintf(stderr,a,b,c,d,e);
  273.     exit(255);
  274. }
  275.  
  276.  
  277. /*  +-------------------------------------------------------------------+
  278.     |  alloc(size)                            |
  279.     |  my version of malloc                        |
  280.     +-------------------------------------------------------------------+ */
  281. char *alloc(size)
  282. unsigned int size;
  283. {
  284.     unsigned char *malloc();
  285.     unsigned char *x;
  286.  
  287.     x = malloc(size);
  288.     if (!x) abort("out of heap space");
  289.     return x;
  290. }
  291.