#include #include #include #include #include #include /* connect player server mapname */ int serverPID=0; int clientPID=0; void sig_chld(int signo) { pid_t pid; int stat; while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0){ stat >>=8; if(pid==serverPID){ fprintf(stderr, "server terminated with %i\n", stat); if(stat) printf("\n\nFailed\n"); else printf("\n\nSuccess!\n"); kill(-getpgrp(), SIGINT); exit(stat); } else fprintf(stderr, "client terminated with %i\n", stat); } return; } int main(int argc, char** argv){ char commandLine[500]; int SInPipes[2]; int SOutPipes[2]; int CInPipes[2]; int COutPipes[2]; int servStat; fd_set rfds; int maxFD; setpgrp(); // set new group so we can kill self pipe(SInPipes); pipe(SOutPipes); pipe(CInPipes); pipe(COutPipes); // launch client if(!(clientPID=fork())){ dup2(CInPipes[0], 0); dup2(COutPipes[1], 1); if(servStat=system(argv[1])<0) fprintf(stderr, "unable to exec player\n"); exit(servStat>>8); } if(!(serverPID=fork())){ dup2(SInPipes[0], 0); dup2(SOutPipes[1], 1); snprintf(commandLine, 499, "%s %s", argv[2], argv[3]); if((servStat=system(commandLine))<0){ fprintf(stderr, "unable to exec server\n"); exit(1); } exit(servStat>>8); } signal(SIGCHLD, sig_chld); FD_ZERO(&rfds); maxFD=COutPipes[0]; if(SOutPipes[0]>maxFD) maxFD=SOutPipes[0]; maxFD++; while(1){ FD_SET(COutPipes[0],&rfds); FD_SET(SOutPipes[0],&rfds); select(maxFD, &rfds, NULL, NULL, NULL); if(FD_ISSET(COutPipes[0], &rfds)){ char c=0; printf (">>>>>>>>>>>>>> "); while(c!='\n'){ if(read(COutPipes[0], &c, 1)<1) break; fwrite(&c, 1,1, stdout); write(SInPipes[1], &c, 1); } } if(FD_ISSET(SOutPipes[0], &rfds)){ char c=0; printf ("<<<<<< "); while(c!='\n'){ if(read(SOutPipes[0], &c, 1)<1) break; fwrite(&c, 1,1, stdout); write(CInPipes[1], &c, 1); } } } printf ("*****\n"); }