home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 65 / arcext.c next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  5.9 KB  |  183 lines

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