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