home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ARC521-2.ZIP / ARC.C next >
C/C++ Source or Header  |  1989-12-30  |  12KB  |  431 lines

  1. /*
  2.  * $Header: arc.c,v 1.13 88/11/01 02:22:23 hyc Exp $
  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. #if    UNIX
  77. #include <sys/types.h>
  78. #include <sys/stat.h>
  79. #endif
  80.  
  81. #ifndef MSDOS
  82. int        strlen();
  83. #endif
  84. void        addarc(), delarc(), extarc(), lstarc(), tstarc(), cvtarc(), runarc();
  85. void        abort();
  86. static    void    expandlst();
  87. #if    MTS
  88. void        etoa();
  89. #endif
  90. #if    GEMDOS
  91. long        _stksize = 65536L;
  92. #endif
  93. char        *strcpy(), *strcat();
  94. char        *makefnam();    /* filename fixup routine */
  95.  
  96. static char   **lst;        /* files list */
  97. static int    lnum;        /* length of files list */
  98.  
  99. main(num, arg)            /* system entry point */
  100.     int        num;    /* number of arguments */
  101.     char           *arg[];    /* pointers to arguments */
  102. {
  103.     char        opt = 0;/* selected action */
  104.     char           *a;    /* option pointer */
  105.     void        upper();/* case conversion routine */
  106.     char           *index();/* string index utility */
  107.     char           *envfind();    /* environment searcher */
  108.     int        n;    /* index */
  109.     char           *arctemp2, *calloc(), *mktemp();
  110. #if    GEMDOS
  111.     void        exitpause();
  112.     int        append;
  113. #endif
  114. #if    MTS
  115.     fortran void    guinfo();
  116.     char        gotinf[4];
  117. #endif
  118. #if    UNIX
  119.     struct    stat    sbuf;
  120. #endif
  121.  
  122.     if (num < 3) {
  123.         printf("\nARC - Archive utility, Version 5.21, created on 04/22/87 at 15:05:21\n");
  124. /*        printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  125.         printf(" ALL RIGHTS RESERVED\n\n");
  126.         printf("Please refer all inquiries to:\n\n");
  127.         printf("       System Enhancement Associates\n");
  128.         printf("       21 New Street, Wayne NJ 07470\n\n");
  129.         printf("You may copy and distribute this program freely,");
  130.         printf(" provided that:\n");
  131.         printf("    1)     No fee is charged for such copying and");
  132.         printf(" distribution, and\n");
  133.         printf("    2)     It is distributed ONLY in its original,");
  134.         printf(" unmodified state.\n\n");
  135.         printf("If you like this program, and find it of use, then your");
  136.         printf(" contribution will\n");
  137.         printf("be appreciated.     You may not use this product in a");
  138.         printf(" commercial environment\n");
  139.         printf("or a governmental organization without paying a license");
  140.         printf(" fee of $35.  Site\n");
  141.         printf("licenses and commercial distribution licenses are");
  142.         printf(" available.  A program\n");
  143.         printf("disk and printed documentation are available for $50.\n");
  144.         printf("\nIf you fail to abide by the terms of this license, ");
  145.         printf(" then your conscience\n");
  146.         printf("will haunt you for the rest of your life.\n\n"); */
  147. #if    MSDOS
  148.         printf("\nUsage: ARC {amufdxerplvtc}[bswnoq][g<password>]");
  149. #endif
  150. #if    GEMDOS
  151.         printf("Usage: ARC {amufdxerplvtc}[bhswnoq][g<password>]");
  152. #endif
  153. #if    UNIX
  154.         printf("Usage: arc {amufdxerplvtc}[biswnoq][g<password>]");
  155. #endif
  156. #if    MTS
  157.         printf("Parameters: {amufdxeplvtc}[biswnoq][g<password>]");
  158. #endif
  159.         printf(" <archive> [<filename> . . .]\n");
  160.         printf("\nWhere: a   = add files to archive\n");
  161.         printf("       m   = move files to archive\n");
  162.         printf("       u   = update files in archive\n");
  163.         printf("       f   = freshen files in archive\n");
  164.         printf("       d   = delete files from archive\n");
  165.         printf("       x,e = extract files from archive\n");
  166. #if    !MTS
  167.         printf("       r   = run files from archive\n");
  168. #endif
  169.         printf("       p   = copy files from archive to standard output\n");
  170.         printf("       l   = list files in archive\n");
  171.         printf("       v   = verbose listing of files in archive\n");
  172.         printf("       t   = test archive integrity\n");
  173.         printf("       c   = convert entry to new packing method\n");
  174.         printf("       b   = retain backup copy of archive\n");
  175. #if    GEMDOS
  176.         printf("       h   = hold screen after finishing\n");
  177. #endif
  178. #if    MTS
  179.         printf("       i   = suppress ASCII/EBCDIC translation\n");
  180. #endif
  181. #if    UNIX
  182.         printf("       i   = suppress image mode (translate EOL)\n");
  183. #endif
  184.         printf("       s   = suppress compression (store only)\n");
  185.         printf("       w   = suppress warning messages\n");
  186.         printf("       n   = suppress notes and comments\n");
  187.         printf("       o   = overwrite existing files when extracting\n");
  188.         printf("       q   = squash instead of crunching\n");
  189.         printf("       g   = Encrypt/decrypt archive entry\n");
  190. #ifndef MSDOS
  191.         printf("\nAdapted from MSDOS by Howard Chu\n");
  192. #endif
  193.         /*
  194.          * printf("\nPlease refer to the program documentation for");
  195.          * printf(" complete instructions.\n");
  196.          */
  197. #if    GEMDOS
  198.         exitpause();
  199. #endif
  200.         return 1;
  201.     }
  202.     /* see where temp files go */
  203. #if    !MTS
  204.     arctemp = calloc(1, STRLEN);
  205.     if (!(arctemp2 = envfind("ARCTEMP")))
  206.         arctemp2 = envfind("TMP");
  207.     if (arctemp2) {
  208.         strcpy(arctemp, arctemp2);
  209.         n = strlen(arctemp);
  210.         if (arctemp[n - 1] != CUTOFF)
  211.             arctemp[n] = CUTOFF;
  212.     }
  213. #if    UNIX
  214.     else    strcpy(arctemp, "/tmp/");
  215. #endif
  216. #if    !MSDOS
  217.     {
  218.         static char tempname[] = "AXXXXXX";
  219.         strcat(arctemp, mktemp(tempname));
  220.     }
  221. #else
  222.     strcat(arctemp, "ARXXXXXX");
  223.     mktemp(arctemp);
  224.     arctemp2 = NULL;
  225. #endif
  226. #else
  227.     guinfo("SHFSEP    ", gotinf);
  228.     sepchr[0] = gotinf[0];
  229.     guinfo("SCRFCHAR", gotinf);
  230.     tmpchr[0] = gotinf[0];
  231.     arctemp = "-$$$";
  232.     arctemp[0] = tmpchr[0];
  233. #endif
  234.  
  235. #if    !UNIX
  236.     /* avoid any case problems with arguments */
  237.  
  238.     for (n = 1; n < num; n++)    /* for each argument */
  239.         upper(arg[n]);    /* convert it to uppercase */
  240. #else
  241.     /* avoid case problems with command options */
  242.     upper(arg[1]);        /* convert to uppercase */
  243. #endif
  244.  
  245.     /* create archive names, supplying defaults */
  246. #if    UNIX
  247.     if (!stat(arg[2],&sbuf)) {
  248.         if ((sbuf.st_mode & S_IFMT) == S_IFDIR)
  249.             makefnam(arg[2],".arc",arcname);
  250.         else
  251.             strcpy(arcname,arg[2]);
  252.     } else
  253.         makefnam(arg[2],".arc",arcname);
  254. #else
  255.     makefnam(arg[2], ".ARC", arcname);
  256. #endif
  257.     /* makefnam(".$$$",arcname,newname); */
  258.     sprintf(newname, "%s.arc", arctemp);
  259.     makefnam(".BAK", arcname, bakname);
  260.  
  261.     /* now scan the command and see what we are to do */
  262.  
  263.     for (a = arg[1]; *a; a++) {    /* scan the option flags */
  264. #if    !MTS
  265.         if (index("AMUFDXEPLVTCR", *a)) {    /* if a known command */
  266. #else
  267.         if (index("AMUFDXEPLVTC", *a)) {
  268. #endif
  269.             if (opt)/* do we have one yet? */
  270.                 abort("Cannot mix %c and %c", opt, *a);
  271.             opt = *a;    /* else remember it */
  272.         } else if (*a == 'B')    /* retain backup copy */
  273.             keepbak = 1;
  274.  
  275.         else if (*a == 'W')    /* suppress warnings */
  276.             warn = 0;
  277. #if    !DOS
  278.         else if (*a == 'I')    /* image mode, no ASCII/EBCDIC x-late */
  279.             image = !image;
  280. #endif
  281. #if    GEMDOS
  282.         else if (*a == 'H')    /* pause before exit */
  283.             hold = 1;
  284. #endif
  285.  
  286.         else if (*a == 'N')    /* suppress notes and comments */
  287.             note = 0;
  288.  
  289.         else if (*a == 'O')    /* overwrite file on extract */
  290.             overlay = 1;
  291.  
  292.         else if (*a == 'G') {    /* garble */
  293.             password = a + 1;
  294.             while (*a)
  295.                 a++;
  296.             a--;
  297. #if    MTS
  298.             etoa(password, strlen(password));
  299. #endif
  300.         } else if (*a == 'S')    /* storage kludge */
  301.             nocomp = 1;
  302.  
  303.         else if (*a == 'K')    /* special kludge */
  304.             kludge = 1;
  305.  
  306.         else if (*a == 'Q')    /* use squashing */
  307.             dosquash = 1;
  308.  
  309.         else if (*a == '-' || *a == '/')    /* UNIX and PC-DOS
  310.                              * option markers */
  311.             ;
  312.  
  313.         else
  314.             abort("%c is an unknown command", *a);
  315.     }
  316.  
  317.     if (!opt)
  318.         abort("I have nothing to do!");
  319.  
  320.     /* get the files list set up */
  321.  
  322.     lnum = num - 3;        /* initial length of list */
  323.     lst = (char **) calloc((lnum==0) ? 1:lnum,
  324.                  sizeof(char *));    /* initial list */
  325.     for (n = 3; n < num; n++)
  326.         lst[n - 3] = arg[n];
  327.  
  328.     for (n = 0; n < lnum;) {/* expand indirect references */
  329.         if (*lst[n] == '@')
  330.             expandlst(n);
  331. #if    GEMDOS        /* redirect stdout from the desktop...*/
  332.         else if (*lst[n] == '>') {
  333.             arctemp2 = (++lst[n]);
  334.             lst[n] = lst[lnum-1];    /* delete this entry */
  335.             lnum--;
  336.             if (arctemp2[0] == '>') {
  337.                 append = 1;
  338.                 arctemp2++;
  339.             }
  340.             else    append = 0;
  341.         }
  342. #endif
  343.         else
  344.             n++;
  345.     }
  346. #if    GEMDOS
  347.     if (arctemp2)
  348.         freopen(arctemp2,append ? "a" : "w",stdout);
  349. #endif
  350.  
  351.     /* act on whatever action command was given */
  352.  
  353.     switch (opt) {        /* action depends on command */
  354.     case 'A':        /* Add */
  355.     case 'M':        /* Move */
  356.     case 'U':        /* Update */
  357.     case 'F':        /* Freshen */
  358.         addarc(lnum, lst, (opt == 'M'), (opt == 'U'), (opt == 'F'));
  359.         break;
  360.  
  361.     case 'D':        /* Delete */
  362.         delarc(lnum, lst);
  363.         break;
  364.  
  365.     case 'E':        /* Extract */
  366.     case 'X':        /* eXtract */
  367.     case 'P':        /* Print */
  368.         extarc(lnum, lst, (opt == 'P'));
  369.         break;
  370.  
  371.     case 'V':        /* Verbose list */
  372.         bose = 1;
  373.     case 'L':        /* List */
  374.         lstarc(lnum, lst);
  375.         break;
  376.  
  377.     case 'T':        /* Test */
  378.         tstarc();
  379.         break;
  380.  
  381.     case 'C':        /* Convert */
  382.         cvtarc(lnum, lst);
  383.         break;
  384. #if    !MTS
  385.     case 'R':        /* Run */
  386.         runarc(lnum, lst);
  387.         break;
  388. #endif
  389.     default:
  390.         abort("I don't know how to do %c yet!", opt);
  391.     }
  392. #if    GEMDOS
  393.     if (hold)
  394.         exitpause();
  395. #endif
  396.     return nerrs;
  397. }
  398. static    void
  399. expandlst(n)            /* expand an indirect reference */
  400.     int        n;    /* number of entry to expand */
  401. {
  402.     FILE           *lf, *fopen();    /* list file, opener */
  403.     char           *malloc(), *realloc();    /* memory managers */
  404.     char        buf[100];    /* input buffer */
  405.     int        x;    /* index */
  406.     char           *p = lst[n] + 1; /* filename pointer */
  407.  
  408.     if (*p) {        /* use name if one was given */
  409.         makefnam(p, ".CMD", buf);
  410.         if (!(lf = fopen(buf, "r")))
  411.             abort("Cannot read list of files in %s", buf);
  412.     } else
  413.         lf = stdin;    /* else use standard input */
  414.  
  415.     for (x = n + 1; x < lnum; x++)    /* drop reference from the list */
  416.         lst[x - 1] = lst[x];
  417.     lnum--;
  418.  
  419.     while (fscanf(lf, "%99s", buf) > 0) {    /* read in the list */
  420.         if (!(lst =(char **)realloc(lst, (lnum + 1) * sizeof(char *))))
  421.             abort("too many file references");
  422.  
  423.         lst[lnum] = malloc(strlen(buf) + 1);
  424.         strcpy(lst[lnum], buf); /* save the name */
  425.         lnum++;
  426.     }
  427.  
  428.     if (lf != stdin)    /* avoid closing standard input */
  429.         fclose(lf);
  430. }
  431.