home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / OS2ARC_S.ZIP / ARCRUN.C < prev    next >
Text File  |  1987-10-15  |  5KB  |  140 lines

  1. /*  ARC - Archive utility - ARCRUN
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =1.17), created on $tag(
  6. TED_DATE DB =02/03/86) at $tag(
  7. TED_TIME DB =22:59:06))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to "run" a file
  17.          which is stored in an archive.  At present, all we really do
  18.          is (a) extract a temporary file, (b) give its name as a system
  19.          command, and then (c) delete the file.
  20.  
  21.     Language:
  22.          Computer Innovations Optimizing C86
  23. */
  24. #include <stdio.h>
  25. #include "arc.h"
  26.  
  27. runarc(num,arg)                        /* run file from archive */
  28. int num;                               /* number of arguments */
  29. char *arg[];                           /* pointers to arguments */
  30. {
  31.     struct heads hdr;                  /* file header */
  32.     int run;                           /* true to run current file */
  33.     int did[_MAXARG];                  /* true when argument was used */
  34.     int n;                             /* index */
  35.     char *makefnam();                  /* filename fixer */
  36.     char buf[_STRLEN];                 /* filename buffer */
  37.     FILE *fopen();                     /* file opener */
  38.  
  39.     for(n=0; n<num; n++)               /* for each argument */
  40.          did[n] = 0;                   /* reset usage flag */
  41.     rempath(num,arg);                  /* strip off paths */
  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.          {    run = 0;                 /* reset run flag */
  48.               for(n=0; n<num; n++)     /* for each template given */
  49.               {    if(match(hdr.name,makefnam(arg[n],".*",buf)))
  50.                    {    run = 1;       /* turn on run flag */
  51.                         did[n] = 1;    /* turn on usage flag */
  52.                         break;         /* stop looking */
  53.                    }
  54.               }
  55.  
  56.               if(run)                  /* if running this one */
  57.                    runfile(&hdr);      /* then do it */
  58.               else                     /* else just skip it */
  59.                    fseek(arc,hdr.size,1);
  60.          }
  61.     }
  62.  
  63.     else while(readhdr(&hdr,arc))      /* else run all files */
  64.          runfile(&hdr);
  65.  
  66.     closearc(0);                       /* close archive after changes */
  67.  
  68.     if(note)
  69.     {    for(n=0; n<num; n++)          /* report unused args */
  70.          {    if(!did[n])
  71.               {    printf("File not found: %s\n",arg[n]);
  72.                    nerrs++;
  73.               }
  74.          }
  75.     }
  76. }
  77.  
  78. static runfile(hdr)                    /* run a file */
  79. struct heads *hdr;                     /* pointer to header data */
  80. {
  81.     FILE *tmp, *fopen();               /* temporary file */
  82.     char buf[_STRLEN], *makefnam();    /* temp file name, fixer */
  83.     char sys[_STRLEN];                 /* invocation command buffer */
  84.     char *dir, *gcdir();               /* directory stuff */ /*mpl*/
  85.  
  86.     makefnam("$ARCTEMP",hdr->name,buf);
  87.  
  88.     if(!strcmp(buf,"$ARCTEMP.BAS"))
  89.          strcpy(sys,"BASICA $ARCTEMP");
  90.  
  91.     else if(!strcmp(buf,"$ARCTEMP.BAT")
  92.          || !strcmp(buf,"$ARCTEMP.COM")
  93.          || !strcmp(buf,"$ARCTEMP.EXE"))
  94.          strcpy(sys,"$ARCTEMP");
  95.  
  96.     else
  97.     {    if(warn)
  98.          {    printf("File %s is not a .BAS, .BAT, .COM, or .EXE\n",
  99.                    hdr->name);
  100.               nerrs++;
  101.          }
  102.          fseek(arc,hdr->size,1);  /* skip this file */
  103.          return;
  104.     }
  105.  
  106.     if(warn)
  107.          if(tmp=fopen(buf,"rb"))
  108.               abort("Temporary file %s already exists",buf);
  109.     if(!(tmp=fopen(makefnam("$ARCTEMP",hdr->name,buf),"w+b")))
  110.          abort("Unable to create temporary file %s",buf);
  111.  
  112.     if(note)
  113.          printf("Invoking file: %s\n",hdr->name);
  114.  
  115.     dir = gcdir("");                   /* see where we are */
  116.     unpack(arc,tmp,hdr);               /* unpack the entry */
  117.     fclose(tmp);                       /* release the file */
  118.     system(sys);                       /* try to invoke it */
  119.     chdir(dir); free(dir);             /* return to whence we started */
  120.     if(unlink(buf) && warn)
  121.     {    printf("Cannot unsave temporary file %s\n",buf);
  122.          nerrs++;
  123.     }
  124. }
  125.  
  126.  
  127. /*  +-------------------------------------------------------------------+
  128.     |  gcdir()                                |
  129.     |  does getcwd                            |
  130.     +-------------------------------------------------------------------+ */
  131. char *gcdir()
  132. {
  133.     extern char *strdup();
  134.     char curdir[64];
  135.  
  136.     getcwd(curdir,64);
  137.  
  138.     return strdup(curdir);
  139. }
  140.