home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c019 / 1.ddi / ARC521_C / ARCRUN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-01  |  3.7 KB  |  149 lines

  1. /*
  2.  * $Header: arcrun.c,v 1.3 88/06/01 19:57:16 hyc Locked $
  3.  */
  4.  
  5. /*
  6.  * ARC - Archive utility - ARCRUN
  7.  * 
  8.  * Version 1.20, created on 03/24/86 at 19:34:31
  9.  * 
  10.  * (C) COPYRIGHT 1985,85 by System Enhancement Associates; ALL RIGHTS RESERVED
  11.  * 
  12.  * By:  Thom Henderson
  13.  * 
  14.  * Description: This file contains the routines used to "run" a file which is
  15.  * stored in an archive.  At present, all we really do is (a) extract a
  16.  * temporary file, (b) give its name as a system command, and then (c) delete
  17.  * the file.
  18.  * 
  19.  * Language: Computer Innovations Optimizing C86
  20.  */
  21. #include <stdio.h>
  22. #include "arc.h"
  23.  
  24. void    rempath(), openarc(), closearc(), abort();
  25. char    *strcat();
  26.  
  27. void
  28. runarc(num, arg)        /* run file from archive */
  29.     int             num;    /* number of arguments */
  30.     char           *arg[];    /* pointers to arguments */
  31. {
  32.     struct heads    hdr;    /* file header */
  33.     char           *makefnam();    /* filename fixer */
  34.     char            buf[STRLEN];    /* filename buffer */
  35.     FILE           *fopen();/* file opener */
  36.     int             runfile();
  37.     char           *dummy[2];
  38.  
  39.     dummy[0]="dummy";
  40.     dummy[1]=NULL;
  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.             if (match(hdr.name, makefnam(arg[0], ".*", buf)))
  48.                 runfile(&hdr, num, arg);
  49.             else
  50.                 fseek(arc, hdr.size, 1);
  51.         }
  52.     } else
  53.         while (readhdr(&hdr, arc))    /* else run all files */
  54.             runfile(&hdr, 1, dummy);
  55.  
  56.     closearc(0);        /* close archive after changes */
  57. }
  58.  
  59. static          int
  60. runfile(hdr, num, arg)        /* run a file */
  61.     struct heads   *hdr;    /* pointer to header data */
  62.     int             num;    /* number of arguments */
  63.     char           *arg[];    /* pointers to arguments */
  64. {
  65.     FILE           *tmp, *fopen();    /* temporary file */
  66.     char           *dir, *gcdir();    /* directory stuff */
  67.     char            buf[STRLEN], *makefnam();    /* temp file name, fixer */
  68. #if    DOS
  69.     char        nbuf[64], *i, *rindex();
  70. #endif
  71. #if    !GEMDOS
  72.     int             n;    /* index */
  73.     char            sys[STRLEN];    /* invocation command buffer */
  74. #endif
  75.  
  76.     /* makefnam("$ARCTEMP",hdr->name,buf); */
  77. #if    UNIX
  78.     sprintf(buf, "%s.RUN", arctemp);
  79.     strcpy(sys, buf);
  80. #else
  81.     strcpy(nbuf, arctemp);
  82.     makefnam(nbuf,hdr->name,buf);
  83.     i = rindex(buf,'.');
  84. #endif
  85. #if    MSDOS
  86.     if (!strcmp(i, ".BAS")) {
  87.         strcpy(sys, "BASICA ");
  88.         strcat(sys, buf);
  89.     }
  90.     else if (!strcmp(i, ".BAT")
  91.          || !strcmp(i, ".COM")
  92.          || !strcmp(i, ".EXE"))
  93.         strcpy(sys, buf);
  94.  
  95.     else {
  96.         if (warn) {
  97.             printf("File %s is not a .BAS, .BAT, .COM, or .EXE\n",
  98.                    hdr->name);
  99.             nerrs++;
  100.         }
  101.         fseek(arc, hdr->size, 1);    /* skip this file */
  102.         return;
  103.     }
  104. #endif
  105. #if    GEMDOS
  106.       if (strcmp(i, ".PRG")
  107.               && strcmp(i, ".TTP")
  108.               && strcmp(i, ".TOS"))
  109.       {
  110.               if (warn) {
  111.                       printf("File %s is not a .PRG, .TOS, or .TTP\n",
  112.                               hdr->name);
  113.                       nerrs++;
  114.               }
  115.               fseek(arc, hdr->size, 1);       /* skip this file */
  116.               return;
  117.       }
  118. #endif
  119.  
  120.     if (warn)
  121.         if (tmp = fopen(buf, "r"))
  122.             abort("Temporary file %s already exists", buf);
  123.     if (!(tmp = fopen(buf, "wb")))
  124.         abort("Unable to create temporary file %s", buf);
  125.  
  126.     if (note)
  127.         printf("Invoking file: %s\n", hdr->name);
  128.  
  129.     dir = gcdir("");    /* see where we are */
  130.     unpack(arc, tmp, hdr);    /* unpack the entry */
  131.     fclose(tmp);        /* release the file */
  132.     chmod(buf, "700");    /* make it executable */
  133. #if    GEMDOS
  134.     execve(buf, arg, NULL);
  135. #else
  136.     for (n = 1; n < num; n++) {    /* add command line arguments */
  137.         strcat(sys, " ");
  138.         strcat(sys, arg[n]);
  139.     }
  140.     system(buf);        /* try to invoke it */
  141. #endif
  142.     chdir(dir);
  143.     free(dir);        /* return to whence we started */
  144.     if (unlink(buf) && warn) {
  145.         printf("Cannot unsave temporary file %s\n", buf);
  146.         nerrs++;
  147.     }
  148. }
  149.