home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8707 / 66 / arcrun.c < prev    next >
C/C++ Source or Header  |  1990-07-13  |  5KB  |  145 lines

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