int socket;...int f=fork();if (f>0){ dup2(0,socket); dup2(1,socket); dup2(2,socket); exec("...");}else if(f<0){ ...}// parent continue...
int process(char* command, char *argv[], char *envp[], int &stdin, int &stdout, int &stderr);// Linux:#ifdef UNIX#include <unistd.h>int process(char* command, char *argv[], char *envp[], int &stdin, int &stdout, int &stderr){int pid=fork();if(pid<=0) return errno;if(pid==0){ dup2(0,stdin); dup2(1,stdout); dup2(2,stderr); execve(command,argv,envp); char *err="Cannot do exec.\n"; write(stderr,err,sizeof(err)); return errno;} // pid >0 - this is parentreturn 0; // do nithing? =)}#endif//Windows#ifdef WIN32int process(char* command, char *argv[], char *envp[], int &stdin, int &stdout, int &stderr){ char buffer[1024]; strcpy(buffer,command); STARTUPINFO siStartInfo; PROCESS_INFORMATION pi; ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.hStdError = (HANDLE)(&stderr); siStartInfo.hStdOutput = (HANDLE)(&stdout); siStartInfo.hStdInput = (HANDLE)(&stdin); siStartInfo.dwFlags |= STARTF_USESTDHANDLES; // BOOL SetStdHandle(STD_INPUT_HANDLE,(HANDLE) stdin); if(CreateProcess(NULL,buffer,NULL,NULL, FALSE, // means that handles will be inherits... dont know if we need it. 0,envp,NULL,&siStartInfo,&pi)==TRUE){ return 0;}else{ return -1;}}#endif