Commit | Line | Data |
---|---|---|
761330fe DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
bbbe213d | 7 | #ifndef lint |
95f51977 | 8 | static char *sccsid = "@(#)popen.c 5.2 (Berkeley) 6/21/85"; |
761330fe | 9 | #endif not lint |
bbbe213d | 10 | |
e343f97a KS |
11 | #include <stdio.h> |
12 | #include <signal.h> | |
ea394d88 | 13 | #include <errno.h> |
e343f97a KS |
14 | #define tst(a,b) (*mode == 'r'? (b) : (a)) |
15 | #define RDR 0 | |
16 | #define WTR 1 | |
17 | static int popen_pid[20]; | |
ea394d88 | 18 | |
56d6aa4a | 19 | #ifndef VMUNIX |
4bc721c3 SL |
20 | #define vfork fork |
21 | #endif VMUNIX | |
d3c349a9 CS |
22 | #ifndef SIGRETRO |
23 | #define sigchild() | |
24 | #endif | |
e343f97a KS |
25 | |
26 | FILE * | |
27 | popen(cmd,mode) | |
28 | char *cmd; | |
29 | char *mode; | |
30 | { | |
31 | int p[2]; | |
32 | register myside, hisside, pid; | |
33 | ||
34 | if(pipe(p) < 0) | |
35 | return NULL; | |
36 | myside = tst(p[WTR], p[RDR]); | |
37 | hisside = tst(p[RDR], p[WTR]); | |
38 | if((pid = vfork()) == 0) { | |
39 | /* myside and hisside reverse roles in child */ | |
cb9e16fc | 40 | sigchild(); |
e343f97a KS |
41 | close(myside); |
42 | dup2(hisside, tst(0, 1)); | |
43 | close(hisside); | |
44 | execl("/bin/csh", "sh", "-c", cmd, 0); | |
45 | _exit(1); | |
46 | } | |
47 | if(pid == -1) | |
48 | return NULL; | |
49 | popen_pid[myside] = pid; | |
50 | close(hisside); | |
51 | return(fdopen(myside, mode)); | |
52 | } | |
53 | ||
54 | pclose(ptr) | |
55 | FILE *ptr; | |
56 | { | |
39c234be | 57 | register f, r; |
4bc721c3 | 58 | int status, omask; |
ea394d88 | 59 | extern int errno; |
e343f97a KS |
60 | |
61 | f = fileno(ptr); | |
62 | fclose(ptr); | |
ea394d88 | 63 | # ifdef VMUNIX |
56d6aa4a | 64 | omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); |
ea394d88 KS |
65 | # endif VMUNIX |
66 | while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR) | |
e343f97a KS |
67 | ; |
68 | if(r == -1) | |
69 | status = -1; | |
ea394d88 | 70 | # ifdef VMUNIX |
4bc721c3 | 71 | sigsetmask(omask); |
ea394d88 | 72 | # endif VMUNIX |
e343f97a KS |
73 | return(status); |
74 | } |