xref: /illumos-gate/usr/src/tools/cscope-fast/mypopen.c (revision dd72704bd9e794056c558153663c739e2012d721)
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) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1999 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <libgen.h>
38 #include "global.h"	/* pid_t, SIGTYPE, shell, and basename() */
39 
40 #define	tst(a, b) (*mode == 'r'? (b) : (a))
41 #define	RDR	0
42 #define	WTR	1
43 
44 static pid_t popen_pid[20];
45 static SIGTYPE (*tstat)();
46 
47 FILE *
48 mypopen(char *cmd, char *mode)
49 {
50 	int	p[2];
51 	pid_t *poptr;
52 	int myside, yourside;
53 	pid_t pid;
54 
55 	if (pipe(p) < 0)
56 		return (NULL);
57 	myside = tst(p[WTR], p[RDR]);
58 	yourside = tst(p[RDR], p[WTR]);
59 	if ((pid = fork()) > 0) {
60 		tstat = signal(SIGTSTP, SIG_DFL);
61 	} else if (pid == 0) {
62 		/* myside and yourside reverse roles in child */
63 		int	stdio;
64 
65 		/* close all pipes from other popen's */
66 		for (poptr = popen_pid; poptr < popen_pid+20; poptr++) {
67 			if (*poptr)
68 				(void) close(poptr - popen_pid);
69 		}
70 		stdio = tst(0, 1);
71 		(void) close(myside);
72 		(void) close(stdio);
73 		(void) fcntl(yourside, F_DUPFD, stdio);
74 		(void) close(yourside);
75 		(void) execlp(shell, basename(shell), "-c", cmd, 0);
76 		_exit(1);
77 	}
78 	if (pid == -1)
79 		return (NULL);
80 	popen_pid[myside] = pid;
81 	(void) close(yourside);
82 	return (fdopen(myside, mode));
83 }
84 
85 int
86 mypclose(FILE *ptr)
87 {
88 	int f;
89 	pid_t r;
90 	int status;
91 	SIGTYPE (*hstat)(), (*istat)(), (*qstat)();
92 
93 	f = fileno(ptr);
94 	(void) fclose(ptr);
95 	istat = signal(SIGINT, SIG_IGN);
96 	qstat = signal(SIGQUIT, SIG_IGN);
97 	hstat = signal(SIGHUP, SIG_IGN);
98 	while ((r = wait(&status)) != popen_pid[f] && r != -1) {
99 	}
100 	if (r == -1)
101 		status = -1;
102 	(void) signal(SIGINT, istat);
103 	(void) signal(SIGQUIT, qstat);
104 	(void) signal(SIGHUP, hstat);
105 	(void) signal(SIGTSTP, tstat);
106 	/* mark this pipe closed */
107 	popen_pid[f] = 0;
108 	return (status);
109 }
110