home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 157.lha / Arc_Src / arcext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-27  |  4.5 KB  |  132 lines

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