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:
7: #ifndef lint
8: static char sccsid[] = "@(#)pclose.c 1.2 (Berkeley) 3/7/86";
9: #endif not lint
10:
11: #include <stdio.h>
12: #include <signal.h>
13: #include <sys/param.h>
14: #include <sys/wait.h>
15:
16: #define tst(a,b) (*mode == 'r'? (b) : (a))
17: #define RDR 0
18: #define WTR 1
19:
20: extern char *malloc();
21:
22: static int *popen_pid;
23: static int nfiles;
24:
25: FILE *
26: mypopen(cmd,mode)
27: char *cmd;
28: char *mode;
29: {
30: int p[2];
31: int myside, hisside, pid;
32:
33: if (nfiles <= 0)
34: nfiles = getdtablesize();
35: if (popen_pid == NULL) {
36: popen_pid = (int *)malloc((unsigned) nfiles * sizeof *popen_pid);
37: if (popen_pid == NULL)
38: return (NULL);
39: for (pid = 0; pid < nfiles; pid++)
40: popen_pid[pid] = -1;
41: }
42: if (pipe(p) < 0)
43: return (NULL);
44: myside = tst(p[WTR], p[RDR]);
45: hisside = tst(p[RDR], p[WTR]);
46: if ((pid = vfork()) == 0) {
47: /* myside and hisside reverse roles in child */
48: (void) close(myside);
49: if (hisside != tst(0, 1)) {
50: (void) dup2(hisside, tst(0, 1));
51: (void) close(hisside);
52: }
53: execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
54: _exit(127);
55: }
56: if (pid == -1) {
57: (void) close(myside);
58: (void) close(hisside);
59: return (NULL);
60: }
61: popen_pid[myside] = pid;
62: (void) close(hisside);
63: return (fdopen(myside, mode));
64: }
65:
66: pabort()
67: {
68: extern int mflag;
69:
70: mflag = 0;
71: }
72:
73: mypclose(ptr)
74: FILE *ptr;
75: {
76: int child, pid, omask, pabort(), (*istat)();
77: union wait status;
78:
79: child = popen_pid[fileno(ptr)];
80: popen_pid[fileno(ptr)] = -1;
81: (void) fclose(ptr);
82: if (child == -1)
83: return (-1);
84: istat = signal(SIGINT, pabort);
85: omask = sigblock(sigmask(SIGQUIT)|sigmask(SIGHUP));
86: while ((pid = wait(&status)) != child && pid != -1)
87: ;
88: (void) sigsetmask(omask);
89: (void) signal(SIGINT, istat);
90: return (pid == -1 ? -1 : 0);
91: }
Defined functions
Defined variables
sccsid
defined in line
8;
never used
Defined macros
RDR
defined in line
17; used 2 times
WTR
defined in line
18; used 2 times
tst
defined in line
16; used 4 times