#define _POSIX_SOURCE #define _RESEARCH_SOURCE #include #include #include #include #include #include #define MAXFORKS 20 #define NSYSFILE 3 #define tst(a,b) (*mode == 'r'? (b) : (a)) #define RDR 0 #define WTR 1 struct a_fork { short done; short fd; int pid; int status; }; static struct a_fork the_fork[MAXFORKS]; FILE * popen(char *cmd, char *mode) { int p[2]; int myside, hisside, pid; int i, ind; for (ind = 0; ind < MAXFORKS; ind++) if (the_fork[ind].pid == 0) break; if (ind == MAXFORKS) return NULL; if(pipe(p) < 0) return NULL; myside = tst(p[WTR], p[RDR]); hisside = tst(p[RDR], p[WTR]); switch (pid = fork()) { case -1: return NULL; case 0: /* myside and hisside reverse roles in child */ close(myside); dup2(hisside, tst(0, 1)); for (i=NSYSFILE; i