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
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]
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * cscope - interactive C symbol cross-reference
36 * process execution functions
40 #include <sys/types.h>
48 #define getdtablesize() _NFILE
50 #define MAXARGS 100 /* maximum number of arguments to executed command */
52 pid_t childpid
; /* child's process ID */
54 static SIGTYPE (*oldsigquit
)(); /* old value of quit signal */
55 static SIGTYPE (*oldsigtstp
)(); /* old value of terminal stop signal */
57 static pid_t
myfork(void);
58 static int join(pid_t p
);
61 * execute forks and executes a program or shell script, waits for it to
62 * finish, and returns its exit code.
67 execute(char *path
, ...)
70 char *args
[MAXARGS
+ 1];
76 /* fork and exec the program or shell script */
78 if ((p
= myfork()) == 0) {
80 /* close all files except stdin, stdout, and stderr */
81 for (i
= 3; i
< getdtablesize() && close(i
) == 0; ++i
) {
84 /* execute the program or shell script */
86 for (i
= 0; i
< MAXARGS
&&
87 (args
[i
] = va_arg(ap
, char *)) != NULL
; ++i
) {
90 args
[i
] = NULL
; /* in case MAXARGS reached */
91 args
[0] = basename(args
[0]);
92 (void) execvp(path
, args
); /* returns only on failure */
93 (void) sprintf(msg
, "\ncscope: cannot execute %s", path
);
94 (void) perror(msg
); /* display the reason */
95 askforreturn(); /* wait until the user sees the message */
96 exit(1); /* exit the child */
98 exitcode
= join(p
); /* parent */
101 (void) fprintf(stderr
,
102 "cscope: no activity time out--exiting\n");
109 /* myfork acts like fork but also handles signals */
114 pid_t p
; /* process number */
116 oldsigtstp
= signal(SIGTSTP
, SIG_DFL
);
117 /* the parent ignores the interrupt and quit signals */
118 if ((p
= fork()) > 0) {
120 oldsigquit
= signal(SIGQUIT
, SIG_IGN
);
122 /* so they can be used to stop the child */
124 (void) signal(SIGINT
, SIG_DFL
);
125 (void) signal(SIGQUIT
, SIG_DFL
);
126 (void) signal(SIGHUP
, SIG_DFL
); /* restore hangup default */
128 /* check for fork failure */
130 myperror("Cannot fork");
135 /* join is the compliment of fork */
143 /* wait for the correct child to exit */
146 } while (p
!= -1 && w
!= p
);
149 /* restore signal handling */
150 (void) signal(SIGQUIT
, oldsigquit
);
151 (void) signal(SIGTSTP
, oldsigtstp
);
152 /* return the child's exit code */
153 return (status
>> 8);