home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / spawn.c < prev    next >
C/C++ Source or Header  |  1992-10-18  |  4KB  |  163 lines

  1. /*
  2.    89/03/02: ERS: added the "mode" argument for MS-DOS/Unix
  3.    compatibility. Added prototypes for GNU C.
  4.  
  5.    fixed spawnve: the original didn't pass the command tail
  6.    correctly, nor did it handle errno right. Also, this version
  7.    passes args in the environment as well as the command line,
  8.    using the ARGV= mechanism supported by crt0.s
  9.  
  10.    Written 89/01/10 by ERS. Original version Copyright (c) 1988 by
  11.    Memorial University of Newfoundland. This version is based upon
  12.    that original, but is substantially different. (In fact, there
  13.    probably isn't a single line of the original left.)
  14.  
  15.    Adapted 90/06/25 by ERS to MiNT.
  16. */
  17.  
  18. #include    <stdarg.h>
  19. #include    <process.h>
  20. #include    <param.h>
  21. #include    <errno.h>
  22. #include    <osbind.h>
  23. #include    <mintbind.h>
  24. #include    <stdlib.h>
  25. #include    <time.h>
  26. #include    <string.h>
  27. #include    <unistd.h>
  28. #include    "lib.h"
  29.  
  30. #define TOS_ARGS 126
  31.  
  32. extern char **environ;
  33. extern int __mint;
  34.  
  35. static    char    cmd[TOS_ARGS+1];
  36.  
  37. int
  38. _spawnve(mode, _path, argv, envp)
  39.     int    mode;
  40.     char    *_path;
  41.     char    **argv;
  42.     char    **envp;
  43. {
  44.     extern void _exit();
  45.     static char    path[MAXPATHLEN];
  46.     size_t        cmlen;
  47.     size_t        enlen = 0;
  48.     char        *p;
  49.     char        *s, *t;
  50.     char        *env;
  51.     long        rval;
  52.  
  53.     if (mode != P_WAIT && mode != P_OVERLAY && mode != P_NOWAIT) {
  54.         errno = EINVAL;
  55.         return -1;
  56.     }
  57.     (void)_unx2dos(_path, path);    /* convert filename, if necessary */
  58.     if (!envp)
  59.         envp = environ;
  60.  
  61. /* count up space needed for environment */
  62.     for(cmlen = 0; argv[cmlen]; cmlen++)
  63.         enlen += strlen(argv[cmlen]) + 1;
  64.     for(cmlen = 0; envp[cmlen]; cmlen++)
  65.         enlen += strlen(envp[cmlen]) + 1;
  66.     enlen += 32;    /* filler for stuff like ARGV= and zeros */
  67.  
  68.     if ((env = (char *)Malloc((long)enlen)) == NULL) {
  69.         errno = ENOMEM;
  70.         return -1;
  71.     }
  72.     s = env;
  73.     while ((p = *envp) != 0) {
  74. /*
  75.  * NOTE: in main.c, we converted the PATH environment variable into
  76.  * POSIX form. Here, we convert back into gulam form. Note that the
  77.  * new variable will be shorter than the old, so space is not a problem.
  78.  */
  79.         if (!strncmp(p, "PATH=", 5)) {
  80.             strncpy(s, p, 5); s += 5; p += 5;
  81.             while (*p) {
  82.                 if (!strncmp(p, "/dev/", 5) && p[5]) {
  83.                     *s++ = p[5];
  84.                     *s++ = ':';
  85.                     p += 6;
  86.                 } else if (*p == ':') {
  87.                     *s++ = ','; p++;
  88.                 } else if (*p == '/') {
  89.                     *s++ = '\\'; p++;
  90.                 } else {
  91.                     *s++ = *p++;
  92.                 }
  93.             }
  94.         } else {
  95.             while(*p)
  96.                 *s++ = *p++;
  97.         }
  98.         *s++ = '\0';
  99.         envp++;
  100.     }
  101.     
  102.     strcpy(s, "ARGV=");
  103.     s += 6; /* s+=sizeof("ARGV=") */
  104.  
  105. /* copy argv[0] first (because it doesn't go into the command line */
  106.     if (argv && *argv) {
  107.         for (p = *argv; *p; )
  108.             *s++ = *p++;
  109.         *s++ = '\0';
  110.     }
  111.  
  112.     bzero(t = cmd, sizeof(cmd));
  113.  
  114. /* s points at the environment's copy of the args */
  115. /* t points at the command line copy to be put in the basepage */
  116.  
  117.         cmlen = 0;
  118.     if (argv && *argv) {
  119.         t++;
  120.         while (*++argv) {
  121.             p = *argv;
  122.             while (*p) {
  123.                               if (cmlen < TOS_ARGS) {
  124.                                       *t++ = *p; cmlen++;
  125.                               }
  126.                 *s++ = *p++;
  127.             }
  128.                         if (cmlen < TOS_ARGS && *(argv+1)) {
  129.                                 *t++ = ' '; cmlen++;
  130.                         }
  131.             *s++ = '\0';
  132.         }
  133. /*                *cmd = (char) cmlen;  NOT ANY MORE */
  134.     }
  135.  
  136.     /* tie off environment */
  137.     *s++ = '\0';
  138.     *s = '\0';
  139.  
  140.     /* signal Extended Argument Passing */
  141.     *cmd = 0x7f;
  142.  
  143. /* MiNT and MicroRTX support background processes with Pexec(100,...) */
  144. /* MiNT supports overlays with Pexec(200,...) */
  145.  
  146.     if (mode == P_NOWAIT) cmlen = 100;
  147.     else if (mode == P_OVERLAY && __mint) cmlen = 200;
  148.     else cmlen = 0;
  149.  
  150.     rval = Pexec((int)cmlen, path, cmd, env);
  151.  
  152.     if (rval < 0)
  153.     {
  154.         errno = (int) -rval;
  155.         rval = -1;
  156.     }
  157.     else if (mode == P_OVERLAY)
  158.     /* note that we get here only if MiNT is not active! */
  159.         _exit((int)rval);
  160.     (void)Mfree(env);
  161.     return (int) rval;
  162. }
  163.