xref: /titanic_51/usr/src/cmd/mailx/popen.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * University Copyright- Copyright (c) 1982, 1986, 1988
28  * The Regents of the University of California
29  * All Rights Reserved
30  *
31  * University Acknowledgment- Portions of this document are derived from
32  * software developed by the University of California, Berkeley, and its
33  * contributors.
34  */
35 
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 /*
39  * mailx -- a modified version of a University of California at Berkeley
40  *	mail program
41  *
42  * npopen() and npclose()
43  *
44  * stolen from C library, modified to use SHELL variable
45  */
46 
47 
48 #include <stdio.h>
49 #include <signal.h>
50 #include <fcntl.h>
51 
52 #define	tst(a,b) (*mode == 'r'? (b) : (a))
53 #define	RDR	0
54 #define	WTR	1
55 
56 #ifdef VMUNIX
57 #define	sigset	signal
58 #else
59 extern void (*sigset())();
60 #endif
61 #ifdef preSVr4
62 extern FILE *fdopen();
63 extern int execlp(), fork(), pipe(), close(), fcntl();
64 #ifndef sun
65 typedef int pid_t;
66 #endif
67 #else
68 # include <unistd.h>
69 # include <wait.h>
70 #endif
71 static pid_t popen_pid[20];
72 
73 FILE *
74 npopen(char *cmd, char *mode)
75 {
76 	int	p[2];
77 	register pid_t pid;
78 	register int myside, yourside;
79 	char *Shell, *value(char *);
80 
81 	if ((Shell = value("SHELL")) == NULL || *Shell=='\0')
82 #ifdef preSVr4
83 		Shell = "/bin/sh";
84 #else
85 		Shell = "/usr/bin/sh";
86 #endif
87 	if(pipe(p) < 0)
88 		return(NULL);
89 	myside = tst(p[WTR], p[RDR]);
90 	yourside = tst(p[RDR], p[WTR]);
91 	if((pid = fork()) == 0) {
92 		/* myside and yourside reverse roles in child */
93 		int	stdio;
94 
95 		stdio = tst(0, 1);
96 		(void) close(myside);
97 		(void) close(stdio);
98 		(void) fcntl(yourside, 0, stdio);
99 		(void) close(yourside);
100 		(void) execlp(Shell, Shell, "-c", cmd, (char *)0);
101 		perror(Shell);
102 		_exit(1);
103 	}
104 	if(pid == (pid_t)-1)
105 		return(NULL);
106 	popen_pid[myside] = pid;
107 	(void) close(yourside);
108 	return(fdopen(myside, mode));
109 }
110 
111 int
112 npclose(FILE *ptr)
113 {
114 	register int f;
115 	register pid_t r;
116 	int status;
117 	void (*istat)(int), (*qstat)(int);
118 
119 	f = fileno(ptr);
120 	(void) fclose(ptr);
121 	istat = sigset(SIGINT, SIG_IGN);
122 	qstat = sigset(SIGQUIT, SIG_IGN);
123 	while((r = wait(&status)) != popen_pid[f] && r != (pid_t)-1)
124 		;
125 	if(r == (pid_t)-1)
126 		status = -1;
127 	(void) sigset(SIGINT, istat);
128 	(void) sigset(SIGQUIT, qstat);
129 	return(status);
130 }
131