home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ARC521-2.ZIP / ARCRUN.C < prev    next >
C/C++ Source or Header  |  1989-12-30  |  4KB  |  152 lines

  1. /*
  2.  * $Header: arcrun.c,v 1.4 88/07/31 18:52:50 hyc Exp $
  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. int    readhdr(), match(), unpack();
  26. static    void    runfile();
  27. char    *strcat();
  28.  
  29. void
  30. runarc(num, arg)        /* run file from archive */
  31.     int             num;    /* number of arguments */
  32.     char           *arg[];    /* pointers to arguments */
  33. {
  34.     struct heads    hdr;    /* file header */
  35.     char           *makefnam();    /* filename fixer */
  36.     char            buf[STRLEN];    /* filename buffer */
  37.     FILE           *fopen();/* file opener */
  38.     char           *dummy[2];
  39.  
  40.     dummy[0]="dummy";
  41.     dummy[1]=NULL;
  42.     rempath(num, arg);    /* strip off paths */
  43.  
  44.     openarc(0);        /* open archive for reading */
  45.  
  46.     if (num) {        /* if files were named */
  47.         while (readhdr(&hdr, arc)) {    /* while more files to check */
  48.             if (match(hdr.name, makefnam(arg[0], ".*", buf)))
  49.                 runfile(&hdr, num, arg);
  50.             else
  51.                 fseek(arc, hdr.size, 1);
  52.         }
  53.     } else
  54.         while (readhdr(&hdr, arc))    /* else run all files */
  55.             runfile(&hdr, 1, dummy);
  56.  
  57.     closearc(0);        /* close archive after changes */
  58. }
  59.  
  60. static  void
  61. runfile(hdr, num, arg)        /* run a file */
  62.     struct heads   *hdr;    /* pointer to header data */
  63.     int             num;    /* number of arguments */
  64.     char           *arg[];    /* pointers to arguments */
  65. {
  66.     FILE           *tmp, *fopen();    /* temporary file */
  67.     char           *dir, *gcdir();    /* directory stuff */
  68.     char            buf[STRLEN], *makefnam();    /* temp file name, fixer */
  69. #if    DOS
  70.     char        nbuf[64], *i, *rindex();
  71. #endif
  72. #if    !GEMDOS
  73.     int             n;    /* index */
  74.     char            sys[STRLEN];    /* invocation command buffer */
  75. #endif
  76.  
  77.     /* makefnam("$ARCTEMP",hdr->name,buf); */
  78. #if    UNIX
  79.     sprintf(buf, "%s.RUN", arctemp);
  80.     strcpy(sys, buf);
  81. #else
  82.     strcpy(nbuf, arctemp);
  83.     makefnam(nbuf,hdr->name,buf);
  84.     i = rindex(buf,'.');
  85. #endif
  86. #if    MSDOS
  87.     if (!strcmp(i, ".BAS")) {
  88.         strcpy(sys, "GWBASIC ");
  89.         strcat(sys, buf);
  90.     }
  91.     else if (!strcmp(i, ".BAT")
  92.          || !strcmp(i, ".COM")
  93.          || !strcmp(i, ".EXE"))
  94.         strcpy(sys, buf);
  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. #endif
  106. #if    GEMDOS
  107.       if (strcmp(i, ".PRG")
  108.               && strcmp(i, ".TTP")
  109.               && strcmp(i, ".TOS"))
  110.       {
  111.               if (warn) {
  112.                       printf("File %s is not a .PRG, .TOS, or .TTP\n",
  113.                               hdr->name);
  114.                       nerrs++;
  115.               }
  116.               fseek(arc, hdr->size, 1);       /* skip this file */
  117.               return;
  118.       }
  119. #endif
  120.  
  121.     if (warn)
  122.         if (tmp = fopen(buf, "r"))
  123.             abort("Temporary file %s already exists", buf);
  124.     if (!(tmp = fopen(buf, OPEN_W)))
  125.         abort("Unable to create temporary file %s", buf);
  126.  
  127.     if (note)
  128.         printf("Invoking file: %s\n", hdr->name);
  129.  
  130.     dir = gcdir("");    /* see where we are */
  131.     unpack(arc, tmp, hdr);    /* unpack the entry */
  132.         fclose(tmp);            /* release the file */
  133. #ifndef MSDOS
  134.         chmod(buf, 0700);      /* make it executable */
  135. #endif
  136. #if    GEMDOS
  137.     execve(buf, arg, NULL);
  138. #else
  139.     for (n = 1; n < num; n++) {    /* add command line arguments */
  140.         strcat(sys, " ");
  141.         strcat(sys, arg[n]);
  142.     }
  143.     system(buf);        /* try to invoke it */
  144. #endif
  145.     chdir(dir);
  146.     free(dir);        /* return to whence we started */
  147.     if (unlink(buf) && warn) {
  148.         printf("Cannot unsave temporary file %s\n", buf);
  149.         nerrs++;
  150.     }
  151. }
  152.