1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
33 /* cscope - interactive C symbol cross-reference
35 * process execution functions
42 #include <sys/types.h> /* pid_t */
46 #if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
52 static char const rcsid[] = "$Id: exec.c,v 1.12 2009/04/10 13:39:23 broeker Exp $";
54 static sighandler_t oldsigquit; /* old value of quit signal */
55 static sighandler_t oldsighup; /* old value of hangup signal */
56 static sighandler_t oldsigtstp; /* old value of SIGTSTP */
58 #ifndef __MSDOS__ /* none of these is needed, there */
59 static int join(pid_t p);
60 static int myexecvp(char *a, char **args);
61 static pid_t myfork(void);
64 /* execute forks and executes a program or shell script, waits for it to
65 * finish, and returns its exit code.
70 execute(char *a, ...) /* note: "exec" is already defined on u370 */
73 int exitcode = -1; /* initialize, to avoid warning */
77 /* fork and exec the program or shell script */
78 endwin(); /* restore the terminal modes */
82 for (p = 0; (argv[p] = va_arg(ap, char *)) != 0; p++)
85 /* HBB 20010313: in MSDOG, everything is completely different.
86 * No fork()/exec()/wait(), but rather a single libc call: */
87 exitcode = spawnvp(P_WAIT, a, argv);
89 if ((p = myfork()) == 0) {
90 myexecvp(a, argv); /* child */
93 exitcode = join(p); /* parent */
97 /* the menu and scrollbar may be changed by the command executed */
98 #if UNIXPC || !TERMINFO
99 # ifndef __DJGPP__ /* leave CRLF handling as is */
102 raw(); /* endwin() turns off cbreak mode so restore it */
106 drawscrollbar(topline, nextline);
111 #ifndef __MSDOS__ /* None of the following functions is used there */
113 /* myexecvp is an interface to the execvp system call to
114 * modify argv[0] to reference the last component of its path-name.
117 myexecvp(char *a, char **args)
119 char msg[MSGLEN + 1];
121 /* modify argv[0] to reference the last component of its path name */
122 args[0] = mybasename(args[0]);
124 /* execute the program or shell script */
125 execvp(a, args); /* returns only on failure */
126 snprintf(msg, sizeof(msg), "\nCannot exec %s", a);
127 perror(msg); /* display the reason */
128 askforreturn(); /* wait until the user sees the message */
129 myexit(1); /* exit the child */
133 /* myfork acts like fork but also handles signals */
138 pid_t p; /* process number */
142 /* the parent ignores the interrupt, quit, and hangup signals */
144 oldsigquit = signal(SIGQUIT, SIG_IGN);
145 oldsighup = signal(SIGHUP, SIG_IGN);
147 oldsigtstp = signal(SIGTSTP, SIG_DFL);
150 /* so they can be used to stop the child */
152 signal(SIGINT, SIG_DFL);
153 signal(SIGQUIT, SIG_DFL);
154 signal(SIGHUP, SIG_DFL);
156 signal(SIGTSTP, SIG_DFL);
159 /* check for fork failure */
161 myperror("Cannot fork");
166 /* join is the compliment of fork */
174 /* wait for the correct child to exit */
177 } while (p != -1 && w != p);
179 /* restore signal handling */
180 signal(SIGQUIT, oldsigquit);
181 signal(SIGHUP, oldsighup);
183 signal(SIGTSTP, oldsigtstp);
186 /* return the child's exit code */