/* various routines to run child processes Kees Lemmens 1993 */

#define MINT

#include <stdio.h>
#include <process.h>
#include <string.h>
#include <osbind.h>	/* Pexec */

char *ux2dos(char *string)	/* convert / to \ */
{	char *tmp;
	while((tmp=strchr(string,'/')) != NULL)
		*tmp='\\';
	return string;
}

/* NON standard, but very convenient ! */

int sspawnve(int mode,char *cmd, char *argstring, char **envp)
{	long status;
	char TosArgs[128] = "x";
	int imode;
	
#ifdef DEBUG
	mode = P_WAIT;
#endif

	/* changing process group is not necessary, because we don't need
	   to exit the parent process. This is the responsibility of
	   the parent.
	*/
	ux2dos(cmd);
	ux2dos(argstring);

	strncat(TosArgs,argstring,sizeof(TosArgs) - 1);
	*TosArgs = strlen(argstring);	/* first byte is strlen */

	switch(mode)
	{	case P_WAIT    : imode=  0; break;
		case P_NOWAIT  : imode=100; break;
		case P_OVERLAY : imode=200; break;
	}
	
	status = Pexec(imode, cmd, TosArgs, (void *)envp);

	return (status < 0L ? -1 : 0);
}

int spawnve(int mode,char *cmd, char **argv, char **envp)
{	static char CmdString[127];
	char *ptr = CmdString;
	int  spaceleft = (int)sizeof(CmdString) - 1;
	
	while(argv && *argv && spaceleft > 0 )
	{	int len = (int)strlen(*argv);
		strncpy(ptr,*argv,spaceleft);
		ptr += len;
		*ptr++ = ' ';	/* space is separator */
		spaceleft -= len + 1;	/* also space counts ! */
		argv++;		
	}
	return sspawnve(mode, cmd, CmdString, envp);
}

int spawnv(int mode,char *cmd, char **argv)
{	return spawnve(mode, cmd, argv, NULL);
}