home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / spawnl.c < prev    next >
Text File  |  1992-02-19  |  2KB  |  102 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 spawnl (int modeflag, char *path, char *arg0, ...)
  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.    char **argptr;
  20.  
  21.    strcpy (exe_name, path);
  22.  
  23.    if (strstr (exe_name, ".exe") == 0 && strstr (exe_name, ".EXE") == 0)
  24.       strcat (exe_name, ".EXE");
  25.  
  26.    /* construct the command args for DosExecPgm */
  27.  
  28.    for (argptr = &arg0, total_length = 1; *argptr; ++argptr)
  29.       total_length += strlen (*argptr) + 1;
  30.  
  31.    arg_strings = (char *)malloc (total_length);
  32.  
  33.    for (argptr = &arg0, ptr = arg_strings; *argptr; ++argptr)
  34.    {
  35.       bcopy (*argptr, ptr, strlen (*argptr)+1);
  36.       ptr += strlen (*argptr);
  37.       if (argptr == &arg0)
  38.          ptr++;
  39.       else
  40.          *ptr++ = ' ';
  41.    }
  42.  
  43.    *ptr = '\0';
  44.  
  45.    switch (modeflag)
  46.    {
  47.       case P_WAIT:    /* Wait for completion */
  48.          rc = Dos32ExecPgm (&ObjNameBuf[0],
  49.                             512,
  50.                             EXEC_SYNC,
  51.                             arg_strings,
  52.                             NULL,
  53.                             &ReturnCodes,
  54.                             exe_name);
  55.  
  56.          if (rc)
  57.          {
  58.             if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  59.             {
  60.                errno = ENOENT;
  61.                return (-1);
  62.             }
  63.  
  64.             errno = EIO;
  65.             return (-1);
  66.          }
  67.  
  68.          return (ReturnCodes.codeResult);
  69.          break;
  70.  
  71.       case P_NOWAIT:  /* Create a child and return */
  72.          rc = Dos32ExecPgm (&ObjNameBuf[0],
  73.                             512,
  74.                             EXEC_ASYNCRESULT,
  75.                             arg_strings,
  76.                             NULL,
  77.                             &ReturnCodes,
  78.                             exe_name);
  79.  
  80.          if (rc)
  81.          {
  82.             if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  83.             {
  84.                errno = ENOENT;
  85.                return (-1);
  86.             }
  87.  
  88.             errno = EIO;
  89.             return (-1);
  90.          }
  91.  
  92.          return (ReturnCodes.codeTerminate);
  93.          break;
  94.  
  95.       default:        /* oops... */
  96.          errno = EINVAL;
  97.          return (-1);
  98.          break;
  99.    }
  100. }
  101.  
  102.