home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d050 / unixarc.lha / UnixArc / arcrun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-17  |  4.4 KB  |  126 lines

  1. /*  ARC - Archive utility - ARCRUN
  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 "run" a file
  9.          which is stored in an archive.  At present, all we really do
  10.          is (a) extract a temporary file, (b) give its name as a system
  11.          command, and then (c) delete the file.
  12.  
  13.     Language:
  14.          Computer Innovations Optimizing C86
  15. */
  16. #include <stdio.h>
  17. #include "arc.h"
  18.  
  19. #if unix
  20. runarc(num,arg)                        /* run file from archive */
  21. int num;                               /* number of arguments */
  22. char *arg[];                           /* pointers to arguments */
  23. {
  24.     fprintf(stderr, "runarc(): not supported under unix\n");
  25. }
  26. #else
  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 */
  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,"r"))
  108.               abort("Temporary file %s already exists",buf);
  109.     if(!(tmp=fopen(makefnam("$ARCTEMP",hdr->name,buf),"w+")))
  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. #endif
  126.