home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
LIBSRC.ZOO
/
libsrc
/
local
/
spawnlpe.c
< prev
next >
Wrap
Text File
|
1992-03-14
|
4KB
|
155 lines
#define INCL_DOSPROCESS
#define INCL_DOSERRORS
#include <os2.h>
#include <errno.h>
#include <process.h>
#include <ctype.h>
ULONG Dos32ExecPgm() asm ("Dos32ExecPgm");
ULONG Dos32SearchPath() asm ("Dos32SearchPath");
int spawnlpe (int modeflag, char *path, char *arg0, ...)
{
int i;
ULONG rc;
int total_arg_length;
int total_env_length;
char *arg_strings;
char *env_strings;
char *ptr;
RESULTCODES ReturnCodes;
char ObjNameBuf[512];
char exe_name[512];
char *base_name[512];
char **argptr;
/* Try to find the .EXE file */
strcpy ((char *)base_name, path);
rc = Dos32SearchPath (SEARCH_ENVIRONMENT | SEARCH_CUR_DIRECTORY,
(PSZ) "PATH",
(PSZ) base_name,
(PBYTE) exe_name,
512);
if (rc)
{
strcat (base_name, ".EXE");
rc = Dos32SearchPath (SEARCH_ENVIRONMENT | SEARCH_CUR_DIRECTORY,
(PSZ) "PATH",
(PSZ) base_name,
(PBYTE) exe_name,
512);
if (rc)
{
if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
{
errno = ENOENT;
return (-1);
}
errno = EIO;
return (-1);
}
}
/* construct the command args for DosExecPgm */
for (argptr = &arg0, total_arg_length = 1; *argptr; ++argptr)
total_arg_length += strlen (*argptr) + 1;
for (++argptr, total_env_length = 1; *argptr; ++argptr)
total_env_length += strlen (*argptr) + 1;
arg_strings = (char *)malloc (total_arg_length);
env_strings = (char *)malloc (total_env_length);
for (argptr = &arg0, ptr = arg_strings; *argptr; ++argptr)
{
bcopy (*argptr, ptr, strlen (*argptr)+1);
ptr += strlen (*argptr);
if (argptr == &arg0)
ptr++;
else
*ptr++ = ' ';
}
*ptr = '\0';
for (++argptr, ptr = env_strings; *argptr; ++argptr)
{
char *tmpptr = ptr;
bcopy (*argptr, ptr, strlen (*argptr)+1);
while (*tmpptr != '=' && *tmpptr)
{
*tmpptr = toupper (*tmpptr);
++tmpptr;
}
ptr += strlen (*argptr) + 1;
}
*ptr = '\0';
switch (modeflag)
{
case P_WAIT: /* Wait for completion */
rc = Dos32ExecPgm (&ObjNameBuf[0],
512,
EXEC_SYNC,
arg_strings,
env_strings,
&ReturnCodes,
exe_name);
if (rc)
{
if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
{
errno = ENOENT;
return (-1);
}
errno = EIO;
return (-1);
}
return (ReturnCodes.codeResult);
break;
case P_NOWAIT: /* Create a child and return */
rc = Dos32ExecPgm (&ObjNameBuf[0],
512,
EXEC_ASYNCRESULT,
arg_strings,
env_strings,
&ReturnCodes,
exe_name);
if (rc)
{
if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND)
{
errno = ENOENT;
return (-1);
}
errno = EIO;
return (-1);
}
return (ReturnCodes.codeTerminate);
break;
default: /* oops... */
errno = EINVAL;
return (-1);
break;
}
}