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

  1. /*
  2.  * $Header: arcext.c,v 1.5 88/06/01 19:26:31 hyc Locked $
  3.  */
  4.  
  5. /*
  6.  * ARC - Archive utility - ARCEXT
  7.  *
  8.  * Version 2.19, created on 10/24/86 at 14:53:32
  9.  *
  10.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  11.  *
  12.  * By:  Thom Henderson
  13.  *
  14.  * Description: This file contains the routines used to extract files from an
  15.  * archive.
  16.  *
  17.  * Language: Computer Innovations Optimizing C86
  18.  */
  19. #include <stdio.h>
  20. #include "arc.h"
  21. #include <ctype.h>
  22.  
  23. void    openarc(), closearc(), setstamp();
  24. int    free(), match(), readhdr(), unpack();
  25. char    *strcpy(), *strcat();
  26.  
  27. void
  28. extarc(num, arg, prt)        /* extract files from archive */
  29.     int             num;    /* number of arguments */
  30.     char           *arg[];    /* pointers to arguments */
  31.     int             prt;        /* true if printing */
  32. {
  33.     struct heads    hdr;    /* file header */
  34.     int             save;    /* true to save current file */
  35.     int             did[MAXARG];/* true when argument was used */
  36.     char           *i, *rindex();    /* string index */
  37.     char          **name, *malloc();    /* name pointer list,
  38.                          * allocator */
  39.     int             n;    /* index */
  40.     void            extfile();
  41.  
  42.     name = (char **) malloc(num * sizeof(char *));    /* get storage for name
  43.                              * pointers */
  44.  
  45.     for (n = 0; n < num; n++) {    /* for each argument */
  46.         did[n] = 0;    /* reset usage flag */
  47. #if    !MTS
  48.         if (!(i = rindex(arg[n], '\\')))    /* find start of name */
  49.             if (!(i = rindex(arg[n], '/')))
  50.                 if (!(i = rindex(arg[n], ':')))
  51.                     i = arg[n] - 1;
  52. #else
  53.         if (!(i = rindex(arg[n], sepchr[0])))
  54.             if (arg[n][0] != tmpchr[0])
  55.                 i = arg[n] - 1;
  56.             else
  57.                 i = arg[n];
  58. #endif
  59.         name[n] = i + 1;
  60.     }
  61.  
  62.  
  63.     openarc(0);        /* open archive for reading */
  64.  
  65.     if (num) {        /* if files were named */
  66.         while (readhdr(&hdr, arc)) {    /* while more files to check */
  67.             save = 0;    /* reset save flag */
  68.             for (n = 0; n < num; n++) {    /* for each template
  69.                              * given */
  70.                 if (match(hdr.name, name[n])) {
  71.                     save = 1;    /* turn on save flag */
  72.                     did[n] = 1;    /* turn on usage flag */
  73.                     break;    /* stop looking */
  74.                 }
  75.             }
  76.  
  77.             if (save)    /* extract if desired, else skip */
  78.                 extfile(&hdr, arg[n], prt);
  79.             else
  80.                 fseek(arc, hdr.size, 1);
  81.         }
  82.     } else
  83.         while (readhdr(&hdr, arc))    /* else extract all files */
  84.             extfile(&hdr, "", prt);
  85.  
  86.     closearc(0);        /* close archive after reading */
  87.  
  88.     if (note) {
  89.         for (n = 0; n < num; n++) {    /* report unused args */
  90.             if (!did[n]) {
  91.                 printf("File not found: %s\n", arg[n]);
  92.                 nerrs++;
  93.             }
  94.         }
  95.     }
  96.     free(name);
  97. }
  98.  
  99. void
  100. extfile(hdr, path, prt)        /* extract a file */
  101.     struct heads   *hdr;    /* pointer to header data */
  102.     char           *path;    /* pointer to path name */
  103.     int             prt;    /* true if printing */
  104. {
  105.     FILE           *f, *fopen();    /* extracted file, opener */
  106.     char            buf[STRLEN];    /* input buffer */
  107.     char            fix[STRLEN];    /* fixed name buffer */
  108.     char           *i, *rindex();    /* string index */
  109.  
  110.     if (prt) {        /* printing is much easier */
  111.         unpack(arc, stdout, hdr);    /* unpack file from archive */
  112.         printf("\f");    /* eject the form */
  113.         return;        /* see? I told you! */
  114.     }
  115.     strcpy(fix, path);    /* note path name template */
  116. #if    !MTS
  117.     if (*path) {
  118.     if (!(i = rindex(fix, '\\')))    /* find start of name */
  119.         if (!(i = rindex(fix, '/')))
  120.             if (!(i = rindex(fix, ':')))
  121.                 i = fix - 1;
  122.     } else i = fix -1;
  123. #else
  124.     if (!(i = rindex(fix, sepchr[0])))
  125.         if (fix[0] != tmpchr[0])
  126.             i = fix - 1;
  127.         else
  128.             i = fix;
  129. #endif
  130.     strcpy(i + 1, hdr->name);    /* replace template with name */
  131.  
  132.     if (note)
  133.         printf("Extracting file: %s\n", fix);
  134.  
  135.     if (warn && !overlay) {
  136.         if (f = fopen(fix, "r")) {    /* see if it exists */
  137.                 fclose(f);
  138.                 printf("WARNING: File %s already exists!", fix);
  139.                 fflush(stdout);
  140.                 while (1) {
  141.                     printf("  Overwrite it (y/n)? ");
  142.                     fflush(stdout);
  143.                     fgets(buf, STRLEN, stdin);
  144.                     *buf = toupper(*buf);
  145.                     if (*buf == 'Y' || *buf == 'N')
  146.                         break;
  147.                 }
  148.                 if (*buf == 'N') {
  149.                     printf("%s not extracted.\n", fix);
  150.                     fseek(arc, hdr->size, 1);
  151.                     return;
  152.                 }
  153.         }
  154.     }
  155. #if    !MTS
  156.     if (!(f = fopen(fix, OPEN_W)))
  157. #else
  158.     {
  159.         fortran         create();
  160.         void        memset();
  161.         char            c_name[256];
  162.         struct crsize {
  163.             short           maxsize, cursize;
  164.         }               c_size;
  165.         char            c_vol[6];
  166.         int             c_type;
  167.         strcpy(c_name, fix);
  168.         strcat(c_name, " ");
  169.         c_size.maxsize = 0;
  170.         c_size.cursize = hdr->length / 4096 + 1;
  171.         memset(c_vol, 0, sizeof(c_vol));
  172.         c_type = 0x100;
  173.         create(c_name, &c_size, c_vol, &c_type);
  174.     }
  175.     if (image) {
  176.         f = fopen(fix, "wb");
  177.     } else
  178.         f = fopen(fix, "w");
  179.     if (!f)
  180. #endif
  181.     {
  182.         if (warn) {
  183.             printf("Cannot create %s\n", fix);
  184.             nerrs++;
  185.         }
  186.         fseek(arc, hdr->size, 1);
  187.         return;
  188.     }
  189.     /* now unpack the file */
  190.  
  191.     unpack(arc, f, hdr);    /* unpack file from archive */
  192.     fclose(f);        /* all done writing to file */
  193. #if    !MTS
  194.     setstamp(fix, hdr->date, hdr->time);    /* use filename for stamp */
  195. #endif
  196. }
  197.