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

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