Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / mailx / popen.c
blobfbdba5985bf999dd722a41a83627d89638a9a51f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * University Copyright- Copyright (c) 1982, 1986, 1988
28 * The Regents of the University of California
29 * All Rights Reserved
31 * University Acknowledgment- Portions of this document are derived from
32 * software developed by the University of California, Berkeley, and its
33 * contributors.
36 #pragma ident "%Z%%M% %I% %E% SMI"
39 * mailx -- a modified version of a University of California at Berkeley
40 * mail program
42 * npopen() and npclose()
44 * stolen from C library, modified to use SHELL variable
48 #include <stdio.h>
49 #include <signal.h>
50 #include <fcntl.h>
52 #define tst(a,b) (*mode == 'r'? (b) : (a))
53 #define RDR 0
54 #define WTR 1
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];
73 FILE *
74 npopen(char *cmd, char *mode)
76 int p[2];
77 register pid_t pid;
78 register int myside, yourside;
79 char *Shell, *value(char *);
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;
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);
104 if(pid == (pid_t)-1)
105 return(NULL);
106 popen_pid[myside] = pid;
107 (void) close(yourside);
108 return(fdopen(myside, mode));
112 npclose(FILE *ptr)
114 register int f;
115 register pid_t r;
116 int status;
117 void (*istat)(int), (*qstat)(int);
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)
125 if(r == (pid_t)-1)
126 status = -1;
127 (void) sigset(SIGINT, istat);
128 (void) sigset(SIGQUIT, qstat);
129 return(status);