home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / spawnlp.c < prev    next >
Text File  |  1992-03-14  |  3KB  |  132 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. ULONG Dos32SearchPath() asm ("Dos32SearchPath");
  9.  
  10. int spawnlp (int modeflag, char *path, char *arg0, ...)
  11. {
  12.    int i;
  13.    ULONG rc;
  14.    int total_length;
  15.    char *arg_strings;
  16.    char *ptr;
  17.    RESULTCODES ReturnCodes;
  18.    char ObjNameBuf[512];
  19.    char exe_name[512];
  20.    char **argptr;
  21.    char *base_name[512];
  22.  
  23.    /* Try to find the .EXE file */
  24.  
  25.    strcpy ((char *)base_name, path);
  26.  
  27.    rc = Dos32SearchPath (SEARCH_ENVIRONMENT | SEARCH_CUR_DIRECTORY,
  28.                                          (PSZ) "PATH",
  29.                                          (PSZ) base_name,
  30.                                          (PBYTE) exe_name,
  31.                                          512);
  32.  
  33.    if (rc)
  34.    {
  35.       strcat (base_name, ".EXE");
  36.  
  37.       rc = Dos32SearchPath (SEARCH_ENVIRONMENT | SEARCH_CUR_DIRECTORY,
  38.                                             (PSZ) "PATH",
  39.                                             (PSZ) base_name,
  40.                                             (PBYTE) exe_name,
  41.                                             512);
  42.  
  43.       if (rc)
  44.       {
  45.          if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  46.          {
  47.             errno = ENOENT;
  48.             return (-1);
  49.          }
  50.  
  51.          errno = EIO;
  52.          return (-1);
  53.       }
  54.    }
  55.  
  56.    /* construct the command args for DosExecPgm */
  57.  
  58.    for (argptr = &arg0, total_length = 1; *argptr; ++argptr)
  59.       total_length += strlen (*argptr) + 1;
  60.  
  61.    arg_strings = (char *)malloc (total_length);
  62.  
  63.    for (argptr = &arg0, ptr = arg_strings; *argptr; ++argptr)
  64.    {
  65.       bcopy (*argptr, ptr, strlen (*argptr)+1);
  66.       ptr += strlen (*argptr);
  67.       if (argptr == &arg0)
  68.          ptr++;
  69.       else
  70.          *ptr++ = ' ';
  71.    }
  72.  
  73.    *ptr = '\0';
  74.  
  75.    switch (modeflag)
  76.    {
  77.       case P_WAIT:    /* Wait for completion */
  78.          rc = Dos32ExecPgm (&ObjNameBuf[0],
  79.                             512,
  80.                             EXEC_SYNC,
  81.                             arg_strings,
  82.                             NULL,
  83.                             &ReturnCodes,
  84.                             exe_name);
  85.  
  86.          if (rc)
  87.          {
  88.             if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  89.             {
  90.                errno = ENOENT;
  91.                return (-1);
  92.             }
  93.  
  94.             errno = EIO;
  95.             return (-1);
  96.          }
  97.  
  98.          return (ReturnCodes.codeResult);
  99.          break;
  100.  
  101.       case P_NOWAIT:  /* Create a child and return */
  102.          rc = Dos32ExecPgm (&ObjNameBuf[0],
  103.                             512,
  104.                             EXEC_ASYNCRESULT,
  105.                             arg_strings,
  106.                             NULL,
  107.                             &ReturnCodes,
  108.                             exe_name);
  109.  
  110.          if (rc)
  111.          {
  112.             if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
  113.             {
  114.                errno = ENOENT;
  115.                return (-1);
  116.             }
  117.  
  118.             errno = EIO;
  119.             return (-1);
  120.          }
  121.  
  122.          return (ReturnCodes.codeTerminate);
  123.          break;
  124.  
  125.       default:        /* oops... */
  126.          errno = EINVAL;
  127.          return (-1);
  128.          break;
  129.    }
  130. }
  131.  
  132.