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