home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / spawnv.c < prev    next >
Text File  |  1992-04-05  |  3KB  |  107 lines

  1. #define INCL_DOSPROCESS
  2. #define INCL_DOSERRORS
  3. #include <os2.h>
  4. #include <errno.h>
  5. #include <process.h>
  6.  
  7. ULONG Dos32ExecPgm() asm ("Dos32ExecPgm");
  8.  
  9. int spawnv (int modeflag, char *path, char *argv[])
  10. {
  11.    int i;
  12.    ULONG rc;
  13.    int total_length;
  14.    char *arg_strings;
  15.    char *ptr;
  16.    RESULTCODES ReturnCodes;
  17.    char ObjNameBuf[512];
  18.    char exe_name[512];
  19.  
  20.    strcpy (exe_name, path);
  21.  
  22.    if (strstr (exe_name, ".exe") == 0 && strstr (exe_name, ".EXE") == 0)
  23.       strcat (exe_name, ".EXE");
  24.  
  25.    /* construct the command args for DosExecPgm */
  26.  
  27.    if (argv) {
  28.       for (i = 0, total_length = 1; argv[i]; ++i)
  29.          total_length += strlen (argv[i]) + 1;
  30.  
  31.       arg_strings = (char *)malloc (total_length);
  32.  
  33.       for (i = 0, ptr = arg_strings; argv[i]; ++i)
  34.       {
  35.          bcopy (argv[i], ptr, strlen (argv[i])+1);
  36.          ptr += strlen (argv[i]);
  37.          if (i == 0)
  38.             ptr++;
  39.          else
  40.             *ptr++ = ' ';
  41.       }
  42.  
  43.       *ptr = '\0';
  44.    } else {
  45.       arg_strings = (char *) malloc (2);
  46.       arg_strings[0] = '\0';
  47.       arg_strings[1] = '\0';
  48.    }
  49.  
  50.    switch (modeflag)
  51.    {
  52.       case P_WAIT:    /* Wait for completion */
  53.          rc = Dos32ExecPgm (&ObjNameBuf[0],
  54.                             512,
  55.                             EXEC_SYNC,
  56.                             arg_strings,
  57.                             NULL,
  58.                             &ReturnCodes,
  59.                             exe_name);
  60.  
  61.          if (rc)
  62.          {
  63.             if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  64.             {
  65.                errno = ENOENT;
  66.                return (-1);
  67.             }
  68.  
  69.             errno = EIO;
  70.             return (-1);
  71.          }
  72.  
  73.          return (ReturnCodes.codeResult);
  74.          break;
  75.  
  76.       case P_NOWAIT:  /* Create a child and return */
  77.          rc = Dos32ExecPgm (&ObjNameBuf[0],
  78.                             512,
  79.                             EXEC_ASYNCRESULT,
  80.                             arg_strings,
  81.                             NULL,
  82.                             &ReturnCodes,
  83.                             exe_name);
  84.  
  85.          if (rc)
  86.          {
  87.             if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  88.             {
  89.                errno = ENOENT;
  90.                return (-1);
  91.             }
  92.  
  93.             errno = EIO;
  94.             return (-1);
  95.          }
  96.  
  97.          return (ReturnCodes.codeTerminate);
  98.          break;
  99.  
  100.       default:        /* oops... */
  101.          errno = EINVAL;
  102.          return (-1);
  103.          break;
  104.    }
  105. }
  106.  
  107.