| 1 | #include <stdio.h> |
| 2 | #include <signal.h> |
| 3 | #include <errno.h> |
| 4 | #define tst(a,b) (*mode == 'r'? (b) : (a)) |
| 5 | #define RDR 0 |
| 6 | #define WTR 1 |
| 7 | static int popen_pid[20]; |
| 8 | static char *sccsid = "@(#)popen.c 1.4 %G%"; |
| 9 | |
| 10 | # ifndef VMUNIX |
| 11 | # define vfork fork |
| 12 | # endif VMUNIX |
| 13 | |
| 14 | FILE * |
| 15 | popen(cmd,mode) |
| 16 | char *cmd; |
| 17 | char *mode; |
| 18 | { |
| 19 | int p[2]; |
| 20 | register myside, hisside, pid; |
| 21 | |
| 22 | if(pipe(p) < 0) |
| 23 | return NULL; |
| 24 | myside = tst(p[WTR], p[RDR]); |
| 25 | hisside = tst(p[RDR], p[WTR]); |
| 26 | if((pid = vfork()) == 0) { |
| 27 | /* myside and hisside reverse roles in child */ |
| 28 | sigchild(); |
| 29 | close(myside); |
| 30 | dup2(hisside, tst(0, 1)); |
| 31 | close(hisside); |
| 32 | execl("/bin/csh", "sh", "-c", cmd, 0); |
| 33 | _exit(1); |
| 34 | } |
| 35 | if(pid == -1) |
| 36 | return NULL; |
| 37 | popen_pid[myside] = pid; |
| 38 | close(hisside); |
| 39 | return(fdopen(myside, mode)); |
| 40 | } |
| 41 | |
| 42 | pclose(ptr) |
| 43 | FILE *ptr; |
| 44 | { |
| 45 | register f, r; |
| 46 | int status; |
| 47 | extern int errno; |
| 48 | |
| 49 | f = fileno(ptr); |
| 50 | fclose(ptr); |
| 51 | # ifdef VMUNIX |
| 52 | sighold(SIGINT); |
| 53 | sighold(SIGQUIT); |
| 54 | sighold(SIGHUP); |
| 55 | # endif VMUNIX |
| 56 | while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR) |
| 57 | ; |
| 58 | if(r == -1) |
| 59 | status = -1; |
| 60 | # ifdef VMUNIX |
| 61 | sigrelse(SIGINT); |
| 62 | sigrelse(SIGQUIT); |
| 63 | sigrelse(SIGHUP); |
| 64 | # endif VMUNIX |
| 65 | return(status); |
| 66 | } |