home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / COMPRESS / ARC520S.ZIP / ARCEXT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-24  |  4.9 KB  |  139 lines

  1. /*  ARC - Archive utility - ARCEXT
  2.  
  3.     Version 2.19, created on 10/24/86 at 14:53:32
  4.  
  5. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  6.  
  7.     By:  Thom Henderson
  8.  
  9.     Description:
  10.          This file contains the routines used to extract files from
  11.          an archive.
  12.  
  13.     Language:
  14.          Computer Innovations Optimizing C86
  15. */
  16. #include <stdio.h>
  17. #include "arc.h"
  18.  
  19. extarc(num,arg,prt)                    /* extract files from archive */
  20. int num;                               /* number of arguments */
  21. char *arg[];                           /* pointers to arguments */
  22. int prt;                               /* true if printing */
  23. {
  24.     struct heads hdr;                  /* file header */
  25.     int save;                          /* true to save current file */
  26.     int did[25];                  /* true when argument was used */
  27.     char *i, *rindex();                /* string index */
  28.     char **name, *alloc();             /* name pointer list, allocator */
  29.     int n;                             /* index */
  30.  
  31.     name = alloc(num*sizeof(char *));  /* get storage for name pointers */
  32.  
  33.     for(n=0; n<num; n++)               /* for each argument */
  34.     {    did[n] = 0;                   /* reset usage flag */
  35.          if(!(i=rindex(arg[n],'\\')))  /* find start of name */
  36.               if(!(i=rindex(arg[n],'/')))
  37.                    if(!(i=rindex(arg[n],':')))
  38.                         i = arg[n]-1;
  39.          name[n] = i+1;
  40.     }
  41.  
  42.  
  43.     openarc(0);                        /* open archive for reading */
  44.  
  45.     if(num)                            /* if files were named */
  46.     {    while(readhdr(&hdr,arc))      /* while more files to check */
  47.          {    save = 0;                /* reset save flag */
  48.               for(n=0; n<num; n++)     /* for each template given */
  49.               {    if(match(hdr.name,name[n]))
  50.                    {    save = 1;      /* turn on save flag */
  51.                         did[n] = 1;    /* turn on usage flag */
  52.                         break;         /* stop looking */
  53.                    }
  54.               }
  55.  
  56.               if(save)                 /* extract if desired, else skip */
  57.                    extfile(&hdr,arg[n],prt);
  58.               else fseek(arc,hdr.size,1);
  59.          }
  60.     }
  61.  
  62.     else while(readhdr(&hdr,arc))      /* else extract all files */
  63.          extfile(&hdr,"",prt);
  64.  
  65.     closearc(0);                       /* close archive after reading */
  66.  
  67.     if(note)
  68.     {    for(n=0; n<num; n++)          /* report unused args */
  69.          {    if(!did[n])
  70.               {    printf("File not found: %s\n",arg[n]);
  71.                    nerrs++;
  72.               }
  73.          }
  74.     }
  75.  
  76.     free(name);
  77. }
  78.  
  79. static extfile(hdr,path,prt)           /* extract a file */
  80. struct heads *hdr;                     /* pointer to header data */
  81. char *path;                            /* pointer to path name */
  82. int prt;                               /* true if printing */
  83. {
  84.     FILE *f, *fopen();                 /* extracted file, opener */
  85.     char buf[100];                 /* input buffer */
  86.     char fix[100];                 /* fixed name buffer */
  87.     char *i, *rindex();                /* string index */
  88.  
  89.     if(prt)                            /* printing is much easier */
  90.     {    unpack(arc,stdout,hdr);       /* unpack file from archive */
  91.          printf("\f");                 /* eject the form */
  92.          return;                       /* see? I told you! */
  93.     }
  94.  
  95.     strcpy(fix,path);                  /* note path name template */
  96.     if(!(i=rindex(fix,'\\')))          /* find start of name */
  97.          if(!(i=rindex(fix,'/')))
  98.               if(!(i=rindex(fix,':')))
  99.                    i = fix-1;
  100.     strcpy(i+1,hdr->name);             /* replace template with name */
  101.  
  102.     if(note)
  103.          printf("Extracting file: %s\n",fix);
  104.  
  105.     if(warn && !overlay)
  106.     {    if(f=fopen(fix,"rb"))        /* see if it exists */
  107.          {    fclose(f);
  108.               printf("WARNING: File %s already exists!",fix);
  109.               while(1)
  110.               {    printf("  Overwrite it (y/n)? ");
  111.                    fgets(buf,100,stdin);
  112.                    *buf = toupper(*buf);
  113.                    if(*buf=='Y' || *buf=='N')
  114.                         break;
  115.               }
  116.               if(*buf=='N')
  117.               {    printf("%s not extracted.\n",fix);
  118.                    fseek(arc,hdr->size,1);
  119.                    return;
  120.               }
  121.          }
  122.     }
  123.  
  124.     if(!(f=fopen(fix,"wb")))
  125.     {    if(warn)
  126.          {    printf("Cannot create %s\n",fix);
  127.               nerrs++;
  128.          }
  129.          fseek(arc,hdr->size,1);
  130.          return;
  131.     }
  132.  
  133.     /* now unpack the file */
  134.  
  135.     unpack(arc,f,hdr);                 /* unpack file from archive */
  136.     setstamp(f,hdr->date,hdr->time);   /* set the proper date/time stamp */
  137.     fclose(f);                         /* all done writing to file */
  138. }
  139.