home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / compress / unixarc.lzh / UNIXARC / ARCEXT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-16  |  5.4 KB  |  174 lines

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