home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / arcppc / src / arc.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  12KB  |  425 lines

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