home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c019 / 1.ddi / ARC521_C / ARC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-01  |  10.9 KB  |  385 lines

  1. /*
  2.  * $Header: arc.c,v 1.10 88/06/17 15:22:48 hyc Locked $
  3.  */
  4.  
  5. /*  ARC - Archive utility
  6.   
  7.     Version 5.21, created on 04/22/87 at 15:05:21
  8.   
  9. (C) COPYRIGHT 1985-87 by System Enhancement Associates; ALL RIGHTS RESERVED
  10.   
  11.     By:     Thom Henderson
  12.   
  13.     Description:
  14.      This program is a general archive utility, and is used to maintain
  15.      an archive of files.  An "archive" is a single file that combines
  16.      many files, reducing storage space and allowing multiple files to
  17.      be handled as one.
  18.   
  19.     Instructions:
  20.      Run this program with no arguments for complete instructions.
  21.   
  22.     Programming notes:
  23.      ARC Version 2 differs from version 1 in that archive entries
  24.      are automatically compressed when they are added to the archive,
  25.      making a separate compression step unecessary.     The nature of the
  26.      compression is indicated by the header version number placed in
  27.      each archive entry, as follows:
  28.   
  29.      1 = Old style, no compression
  30.      2 = New style, no compression
  31.      3 = Compression of repeated characters only
  32.      4 = Compression of repeated characters plus Huffman SQueezing
  33.      5 = Lempel-Zev packing of repeated strings (old style)
  34.      6 = Lempel-Zev packing of repeated strings (new style)
  35.      7 = Lempel-Zev Williams packing with improved hash function
  36.      8 = Dynamic Lempel-Zev packing with adaptive reset
  37.      9 = Dynamic Lempel-Zev packing, larger hash table
  38.   
  39.      Type 5, Lempel-Zev packing, was added as of version 4.0
  40.   
  41.      Type 6 is Lempel-Zev packing where runs of repeated characters
  42.      have been collapsed, and was added as of version 4.1
  43.   
  44.      Type 7 is a variation of Lempel-Zev using a different hash
  45.      function which yields speed improvements of 20-25%, and was
  46.      added as of version 4.6
  47.   
  48.      Type 8 is a different implementation of Lempel-Zev, using a
  49.      variable code size and an adaptive block reset, and was added
  50.      as of version 5.0
  51.   
  52.      Type 9 is a slight modification of type 8, first used by Phil
  53.      Katz in his PKARC utilites. The primary difference is the use
  54.      of a hash table twice as large as for type 8, and that this
  55.      algorithm called Squashing, doesn't perform run-length encoding
  56.      on the input data.
  57.   
  58.      Verion 4.3 introduced a temporary file for holding the result
  59.      of the first crunch pass, thus speeding up crunching.
  60.   
  61.      Version 4.4 introduced the ARCTEMP environment string, so that
  62.      the temporary crunch file may be placed on a ramdisk.    Also
  63.      added was the distinction bewteen Adding a file in all cases,
  64.      and Updating a file only if the disk file is newer than the
  65.      corresponding archive entry.
  66.   
  67.      The compression method to use is determined when the file is
  68.      added, based on whichever method yields the smallest result.
  69.   
  70.     Language:
  71.      Computer Innovations Optimizing C86
  72. */
  73. #include <stdio.h>
  74. #include "arc.h"
  75.  
  76. int        strlen();
  77. void        addarc(), delarc(), extarc(), lstarc(), tstarc(), cvtarc(), runarc();
  78. void        abort();
  79. #if    MTS
  80. void        etoa();
  81. #endif
  82. #if    GEMDOS
  83. long        _stksize = 24576;
  84. #endif
  85. char        *strcpy(), *strcat();
  86.  
  87. static char   **lst;        /* files list */
  88. static int    lnum;        /* length of files list */
  89.  
  90. main(num, arg)            /* system entry point */
  91.     int        num;    /* number of arguments */
  92.     char           *arg[];    /* pointers to arguments */
  93. {
  94.     char        opt = 0;/* selected action */
  95.     char           *a;    /* option pointer */
  96.     char           *makefnam();    /* filename fixup routine */
  97.     void        upper();/* case conversion routine */
  98.     char           *index();/* string index utility */
  99.     char           *envfind();    /* environment searcher */
  100.     int        n;    /* index */
  101.     char           *arctemp2, *calloc(), *mktemp();
  102. #if    GEMDOS
  103.     void        exitpause();
  104. #endif
  105. #if    MTS
  106.     fortran void    guinfo();
  107.     char        gotinf[4];
  108. #endif
  109.  
  110.     if (num < 3) {
  111.         printf("ARC - Archive utility, Version 5.21, created on 04/22/87 at 15:05:21\n");
  112. /*        printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  113.         printf(" ALL RIGHTS RESERVED\n\n");
  114.         printf("Please refer all inquiries to:\n\n");
  115.         printf("       System Enhancement Associates\n");
  116.         printf("       21 New Street, Wayne NJ 07470\n\n");
  117.         printf("You may copy and distribute this program freely,");
  118.         printf(" provided that:\n");
  119.         printf("    1)     No fee is charged for such copying and");
  120.         printf(" distribution, and\n");
  121.         printf("    2)     It is distributed ONLY in its original,");
  122.         printf(" unmodified state.\n\n");
  123.         printf("If you like this program, and find it of use, then your");
  124.         printf(" contribution will\n");
  125.         printf("be appreciated.     You may not use this product in a");
  126.         printf(" commercial environment\n");
  127.         printf("or a governmental organization without paying a license");
  128.         printf(" fee of $35.  Site\n");
  129.         printf("licenses and commercial distribution licenses are");
  130.         printf(" available.  A program\n");
  131.         printf("disk and printed documentation are available for $50.\n");
  132.         printf("\nIf you fail to abide by the terms of this license, ");
  133.         printf(" then your conscience\n");
  134.         printf("will haunt you for the rest of your life.\n\n"); */
  135. #if    MSDOS
  136.         printf("Usage: ARC {amufdxerplvtc}[bswnoq][g<password>]");
  137. #endif
  138. #if    GEMDOS
  139.         printf("Usage: ARC {amufdxerplvtc}[bhswnoq][g<password>]");
  140. #endif
  141. #if    UNIX
  142.         printf("Usage: arc {amufdxerplvtc}[biswnoq][g<password>]");
  143. #endif
  144. #if    MTS
  145.         printf("Parameters: {amufdxeplvtc}[biswnoq][g<password>]");
  146. #endif
  147.         printf(" <archive> [<filename> . . .]\n");
  148.         printf("Where:     a   = add files to archive\n");
  149.         printf("     m   = move files to archive\n");
  150.         printf("     u   = update files in archive\n");
  151.         printf("     f   = freshen files in archive\n");
  152.         printf("     d   = delete files from archive\n");
  153.         printf("     x,e = extract files from archive\n");
  154. #if    !MTS
  155.         printf("     r   = run files from archive\n");
  156. #endif
  157.         printf("     p   = copy files from archive to");
  158.         printf(" standard output\n");
  159.         printf("     l   = list files in archive\n");
  160.         printf("     v   = verbose listing of files in archive\n");
  161.         printf("     t   = test archive integrity\n");
  162.         printf("     c   = convert entry to new packing method\n");
  163.         printf("     b   = retain backup copy of archive\n");
  164. #if    GEMDOS
  165.         printf("     h   = hold screen after finishing\n");
  166. #endif
  167. #if    MTS
  168.         printf("     i   = suppress ASCII/EBCDIC translation\n");
  169. #endif
  170. #if    UNIX
  171.         printf("     i   = suppress image mode (translate EOL)\n");
  172. #endif
  173.         printf("     s   = suppress compression (store only)\n");
  174.         printf("     w   = suppress warning messages\n");
  175.         printf("     n   = suppress notes and comments\n");
  176.         printf("     o   = overwrite existing files when");
  177.         printf(" extracting\n");
  178.         printf("     q   = squash instead of crunching\n");
  179.         printf("     g   = Encrypt/decrypt archive entry\n");
  180.         printf("\nAdapted from MSDOS by Howard Chu\n");
  181.         /*
  182.          * printf("\nPlease refer to the program documentation for");
  183.          * printf(" complete instructions.\n"); 
  184.          */
  185. #if    GEMDOS
  186.         exitpause();
  187. #endif
  188.         return 1;
  189.     }
  190.     /* see where temp files go */
  191. #if    !MTS
  192.     arctemp = calloc(1, STRLEN);
  193.     if (!(arctemp2 = envfind("ARCTEMP")))
  194.         arctemp2 = envfind("TMPDIR");
  195.     if (arctemp2) {
  196.         strcpy(arctemp, arctemp2);
  197.         n = strlen(arctemp);
  198.         if (arctemp[n - 1] != CUTOFF)
  199.             arctemp[n] = CUTOFF;
  200.     }
  201. #if    !MSDOS
  202.     strcat(arctemp, mktemp("AXXXXXX"));
  203. #else
  204.     strcat(arctemp, "$ARCTEMP");
  205. #endif
  206. #else
  207.     guinfo("SHFSEP    ", gotinf);
  208.     sepchr[0] = gotinf[0];
  209.     guinfo("SCRFCHAR", gotinf);
  210.     tmpchr[0] = gotinf[0];
  211.     arctemp = "-$$$";
  212.     arctemp[0] = tmpchr[0];
  213. #endif
  214.  
  215. #if    !UNIX
  216.     /* avoid any case problems with arguments */
  217.  
  218.     for (n = 1; n < num; n++)    /* for each argument */
  219.         upper(arg[n]);    /* convert it to uppercase */
  220. #else
  221.     /* avoid case problems with command options */
  222.     upper(arg[1]);        /* convert to uppercase */
  223. #endif
  224.  
  225.     /* create archive names, supplying defaults */
  226.     makefnam(arg[2], ".arc", arcname);
  227.     /* makefnam(".$$$",arcname,newname); */
  228.     sprintf(newname, "%s.arc", arctemp);
  229.     makefnam(".BAK", arcname, bakname);
  230.  
  231.     /* now scan the command and see what we are to do */
  232.  
  233.     for (a = arg[1]; *a; a++) {    /* scan the option flags */
  234. #if    !MTS
  235.         if (index("AMUFDXEPLVTCR", *a)) {    /* if a known command */
  236. #else
  237.         if (index("AMUFDXEPLVTC", *a)) {
  238. #endif
  239.             if (opt)/* do we have one yet? */
  240.                 abort("Cannot mix %c and %c", opt, *a);
  241.             opt = *a;    /* else remember it */
  242.         } else if (*a == 'B')    /* retain backup copy */
  243.             keepbak = 1;
  244.  
  245.         else if (*a == 'W')    /* suppress warnings */
  246.             warn = 0;
  247. #if    !DOS
  248.         else if (*a == 'I')    /* image mode, no ASCII/EBCDIC x-late */
  249.             image = !image;
  250. #endif
  251. #if    GEMDOS
  252.         else if (*a == 'H')    /* pause before exit */
  253.             hold = 1;
  254. #endif
  255.  
  256.         else if (*a == 'N')    /* suppress notes and comments */
  257.             note = 0;
  258.  
  259.         else if (*a == 'O')    /* overwrite file on extract */
  260.             overlay = 1;
  261.  
  262.         else if (*a == 'G') {    /* garble */
  263.             password = a + 1;
  264.             while (*a)
  265.                 a++;
  266.             a--;
  267. #if    MTS
  268.             etoa(password, strlen(password));
  269. #endif
  270.         } else if (*a == 'S')    /* storage kludge */
  271.             nocomp = 1;
  272.  
  273.         else if (*a == 'K')    /* special kludge */
  274.             kludge = 1;
  275.  
  276.         else if (*a == 'Q')    /* use squashing */
  277.             dosquash = 1;
  278.  
  279.         else if (*a == '-' || *a == '/')    /* UNIX and PC-DOS
  280.                              * option markers */
  281.             ;
  282.  
  283.         else
  284.             abort("%c is an unknown command", *a);
  285.     }
  286.  
  287.     if (!opt)
  288.         abort("I have nothing to do!");
  289.  
  290.     /* get the files list set up */
  291.  
  292.     lnum = num - 3;        /* initial length of list */
  293.     lst = (char **) calloc((lnum==0) ? 1:lnum,
  294.                  sizeof(char *));    /* initial list */
  295.     for (n = 3; n < num; n++)
  296.         lst[n - 3] = arg[n];
  297.  
  298.     for (n = 0; n < lnum;) {/* expand indirect references */
  299.         if (*lst[n] == '@')
  300.             expandlst(n);
  301.         else
  302.             n++;
  303.     }
  304.  
  305.     /* act on whatever action command was given */
  306.  
  307.     switch (opt) {        /* action depends on command */
  308.     case 'A':        /* Add */
  309.     case 'M':        /* Move */
  310.     case 'U':        /* Update */
  311.     case 'F':        /* Freshen */
  312.         addarc(lnum, lst, (opt == 'M'), (opt == 'U'), (opt == 'F'));
  313.         break;
  314.  
  315.     case 'D':        /* Delete */
  316.         delarc(lnum, lst);
  317.         break;
  318.  
  319.     case 'E':        /* Extract */
  320.     case 'X':        /* eXtract */
  321.     case 'P':        /* Print */
  322.         extarc(lnum, lst, (opt == 'P'));
  323.         break;
  324.  
  325.     case 'V':        /* Verbose list */
  326.         bose = 1;
  327.     case 'L':        /* List */
  328.         lstarc(lnum, lst);
  329.         break;
  330.  
  331.     case 'T':        /* Test */
  332.         tstarc();
  333.         break;
  334.  
  335.     case 'C':        /* Convert */
  336.         cvtarc(lnum, lst);
  337.         break;
  338. #if    !MTS
  339.     case 'R':        /* Run */
  340.         runarc(lnum, lst);
  341.         break;
  342. #endif
  343.     default:
  344.         abort("I don't know how to do %c yet!", opt);
  345.     }
  346. #if    GEMDOS
  347.     if (hold)
  348.         exitpause();
  349. #endif
  350.     return nerrs;
  351. }
  352. static
  353. expandlst(n)            /* expand an indirect reference */
  354.     int        n;    /* number of entry to expand */
  355. {
  356.     FILE           *lf, *fopen();    /* list file, opener */
  357.     char           *malloc(), *realloc();    /* memory managers */
  358.     char        buf[100];    /* input buffer */
  359.     int        x;    /* index */
  360.     char           *p = lst[n] + 1; /* filename pointer */
  361.  
  362.     if (*p) {        /* use name if one was given */
  363.         makefnam(p, ".CMD", buf);
  364.         if (!(lf = fopen(buf, "r")))
  365.             abort("Cannot read list of files in %s", buf);
  366.     } else
  367.         lf = stdin;    /* else use standard input */
  368.  
  369.     for (x = n + 1; x < lnum; x++)    /* drop reference from the list */
  370.         lst[x - 1] = lst[x];
  371.     lnum--;
  372.  
  373.     while (fscanf(lf, "%99s", buf) > 0) {    /* read in the list */
  374.         if (!(lst =(char **)realloc(lst, (lnum + 1) * sizeof(char *))))
  375.             abort("too many file references");
  376.  
  377.         lst[lnum] = malloc(strlen(buf) + 1);
  378.         strcpy(lst[lnum], buf); /* save the name */
  379.         lnum++;
  380.     }
  381.  
  382.     if (lf != stdin)    /* avoid closing standard input */
  383.         fclose(lf);
  384. }
  385.