1: /* @(#)popen.c 2.1 SCCS id keyword */
2: #include <whoami.h>
3: #include <stdio.h>
4: #include <signal.h>
5: #define tst(a,b) (*mode == 'r'? (b) : (a))
6: #define RDR 0
7: #define WTR 1
8: static int popen_pid[20];
9:
10: FILE *
11: popen(cmd,mode)
12: char *cmd;
13: char *mode;
14: {
15: int p[2];
16: register myside, hisside, pid;
17:
18: if(pipe(p) < 0)
19: return NULL;
20: myside = tst(p[WTR], p[RDR]);
21: hisside = tst(p[RDR], p[WTR]);
22: #ifdef VIRUS_VFORK
23: if((pid = vfork()) == 0)
24: #else
25: if((pid = fork()) == 0)
26: #endif
27: {
28: /* myside and hisside reverse roles in child */
29: close(myside);
30: dup2(hisside, tst(0, 1));
31: close(hisside);
32: execl("/bin/sh", "sh", "-c", cmd, 0);
33: _exit(1);
34: }
35: if(pid == -1) {
36: close(myside);
37: close(hisside);
38: return NULL;
39: }
40: popen_pid[myside] = pid;
41: close(hisside);
42: return(fdopen(myside, mode));
43: }
44:
45: pclose(ptr)
46: FILE *ptr;
47: {
48: register f, r, (*hstat)(), (*istat)(), (*qstat)();
49: int status;
50:
51: f = fileno(ptr);
52: fclose(ptr);
53: istat = signal(SIGINT, SIG_IGN);
54: qstat = signal(SIGQUIT, SIG_IGN);
55: hstat = signal(SIGHUP, SIG_IGN);
56: while((r = wait(&status)) != popen_pid[f] && r != -1)
57: ;
58: if(r == -1)
59: status = -1;
60: signal(SIGINT, istat);
61: signal(SIGQUIT, qstat);
62: signal(SIGHUP, hstat);
63: return(status);
64: }
Defined functions
pclose
defined in line
45; used 13 times
popen
defined in line
10; used 27 times
- in /usr/src/cmd/at.c line
68,
139
- in /usr/src/cmd/awk/run.c line
13,
821
- in /usr/src/cmd/find.c line
49-52(2)
- in /usr/src/cmd/mail.c line
408,
447,
480
- in /usr/src/cmd/uucp/uusend.c line
35,
170
- in /usr/src/games/snake/snake.c line
458,
465
- in /usr/src/ucb/berknet/defs.h line
130
- in /usr/src/ucb/delivermail/vax-mail.c line
557,
607,
627
- in /usr/src/ucb/ex/ex3/expreserve.c line
66,
319
- in /usr/src/ucb/msgs/msgs.c line
100,
561
- in /usr/src/ucb/sq.c line
5,
22-23(2)
- in /usr/src/ucb/uusend.c line
22,
110
- in /usr/src/ucb/w.c line
77
Defined variables
Defined macros
RDR
defined in line
6; used 2 times
WTR
defined in line
7; used 2 times
tst
defined in line
5; used 3 times