xref: /original-bsd/lib/libc/gen/popen.c (revision 0bdd3421)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software written by Ken Arnold and
6  * published in UNIX Review, Vol. 6, No. 8.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 05/03/95";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/param.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 
19 #include <signal.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <paths.h>
26 
27 static struct pid {
28 	struct pid *next;
29 	FILE *fp;
30 	pid_t pid;
31 } *pidlist;
32 
33 FILE *
popen(command,type)34 popen(command, type)
35 	const char *command, *type;
36 {
37 	struct pid *cur;
38 	FILE *iop;
39 	int pdes[2], pid, twoway;
40 
41 	if (strchr(type, '+')) {
42 		twoway = 1;
43 		type = "r+";
44 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
45 			return (NULL);
46 	} else  {
47 		twoway = 0;
48 		if (*type != 'r' && *type != 'w' || type[1] ||
49 		    (pipe(pdes) < 0))
50 			return (NULL);
51 	}
52 
53 	if ((cur = malloc(sizeof(struct pid))) == NULL)
54 		return (NULL);
55 
56 	switch (pid = vfork()) {
57 	case -1:			/* Error. */
58 		(void)close(pdes[0]);
59 		(void)close(pdes[1]);
60 		(void)free(cur);
61 		return (NULL);
62 		/* NOTREACHED */
63 	case 0:				/* Child. */
64 		if (*type == 'r') {
65 			if (pdes[1] != STDOUT_FILENO) {
66 				(void)dup2(pdes[1], STDOUT_FILENO);
67 				(void)close(pdes[1]);
68 				pdes[1] = STDOUT_FILENO;
69 			}
70 			(void) close(pdes[0]);
71 			if (twoway && (pdes[1] != STDIN_FILENO))
72 				(void)dup2(pdes[1], STDIN_FILENO);
73 		} else {
74 			if (pdes[0] != STDIN_FILENO) {
75 				(void)dup2(pdes[0], STDIN_FILENO);
76 				(void)close(pdes[0]);
77 			}
78 			(void)close(pdes[1]);
79 		}
80 		execl(_PATH_BSHELL, "sh", "-c", command, NULL);
81 		_exit(127);
82 		/* NOTREACHED */
83 	}
84 
85 	/* Parent; assume fdopen can't fail. */
86 	if (*type == 'r') {
87 		iop = fdopen(pdes[0], type);
88 		(void)close(pdes[1]);
89 	} else {
90 		iop = fdopen(pdes[1], type);
91 		(void)close(pdes[0]);
92 	}
93 
94 	/* Link into list of file descriptors. */
95 	cur->fp = iop;
96 	cur->pid =  pid;
97 	cur->next = pidlist;
98 	pidlist = cur;
99 
100 	return (iop);
101 }
102 
103 /*
104  * pclose --
105  *	Pclose returns -1 if stream is not associated with a `popened' command,
106  *	if already `pclosed', or waitpid returns an error.
107  */
108 int
pclose(iop)109 pclose(iop)
110 	FILE *iop;
111 {
112 	register struct pid *cur, *last;
113 	int omask;
114 	int pstat;
115 	pid_t pid;
116 
117 	/* Find the appropriate file pointer. */
118 	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
119 		if (cur->fp == iop)
120 			break;
121 	if (cur == NULL)
122 		return (-1);
123 
124 	(void)fclose(iop);
125 
126 	do {
127 		pid = waitpid(cur->pid, &pstat, 0);
128 	} while (pid == -1 && errno == EINTR);
129 
130 	/* Remove the entry from the linked list. */
131 	if (last == NULL)
132 		pidlist = cur->next;
133 	else
134 		last->next = cur->next;
135 	free(cur);
136 
137 	return (pid == -1 ? -1 : pstat);
138 }
139