csope/source/exec.c

169 lines
4.8 KiB
C
Raw Normal View History

2023-07-27 14:04:50 -04:00
/*===========================================================================
Copyright (c) 1998-2000, The Santa Cruz Operation
2023-07-27 14:04:50 -04:00
All rights reserved.
2023-07-27 14:04:50 -04:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
*Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
*Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
*Neither name of The Santa Cruz Operation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
2023-07-27 14:04:50 -04:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
2023-07-27 14:04:50 -04:00
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2023-08-04 15:19:25 -04:00
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
2023-07-27 14:04:50 -04:00
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
2023-07-27 14:04:50 -04:00
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
2023-07-27 14:04:50 -04:00
=========================================================================*/
2023-08-04 13:49:03 -04:00
/* cscope - interactive C symbol cross-reference
2023-07-27 14:04:50 -04:00
*
2023-08-04 13:49:03 -04:00
* process execution functions
2023-07-27 14:04:50 -04:00
*/
#include <unistd.h>
#include "global.h"
#include <stdarg.h>
#include <sys/wait.h>
2023-08-13 05:21:52 -04:00
#include <sys/types.h> /* pid_t */
2023-07-27 14:04:50 -04:00
#ifdef __DJGPP__
2023-08-13 05:21:52 -04:00
# include <process.h>
2023-07-27 14:04:50 -04:00
#endif
#include <ncurses.h>
2023-08-13 05:21:52 -04:00
static sighandler_t oldsigquit; /* old value of quit signal */
static sighandler_t oldsighup; /* old value of hangup signal */
static sighandler_t oldsigtstp; /* old value of SIGTSTP */
2023-07-27 14:04:50 -04:00
2023-08-13 05:21:52 -04:00
#ifndef __MSDOS__ /* none of these is needed, there */
static int join(pid_t p);
static int myexecvp(char *a, char **args);
static pid_t myfork(void);
2023-07-27 14:04:50 -04:00
#endif
/* execute forks and executes a program or shell script, waits for it to
* finish, and returns its exit code.
*/
/*VARARGS1*/
2023-08-13 05:21:52 -04:00
int execute(char *a, ...) /* NOTE: "exec" is already defined on u370 */
2023-07-27 14:04:50 -04:00
{
2023-08-13 05:21:52 -04:00
va_list ap;
int exitcode = -1;
char *argv[BUFSIZ];
pid_t p;
2023-08-04 13:49:03 -04:00
2023-08-13 05:21:52 -04:00
/* fork and exec the program or shell script */
endwin(); /* restore the terminal modes */
mousecleanup();
fflush(stdout);
va_start(ap, a);
2023-08-06 07:09:48 -04:00
2023-08-13 05:21:52 -04:00
for(p = 0; (argv[p] = va_arg(ap, char *)) != 0; p++) { }
2023-08-06 07:09:48 -04:00
2023-07-27 14:04:50 -04:00
#ifdef __MSDOS__
2023-08-13 05:21:52 -04:00
/* HBB 20010313: in MSDOG, everything is completely different.
* No fork()/exec()/wait(), but rather a single libc call: */
exitcode = spawnvp(P_WAIT, a, argv);
2023-07-27 14:04:50 -04:00
#else
2023-08-13 05:21:52 -04:00
if((p = myfork()) == 0) {
myexecvp(a, argv); /* child */
} else {
exitcode = join(p); /* parent */
}
2023-07-27 14:04:50 -04:00
#endif /* MSDOS */
2023-08-06 07:09:48 -04:00
entercurses();
2023-08-13 05:21:52 -04:00
va_end(ap);
return (exitcode);
2023-07-27 14:04:50 -04:00
}
#ifndef __MSDOS__ /* None of the following functions is used there */
/* myexecvp is an interface to the execvp system call to
* modify argv[0] to reference the last component of its path-name.
*/
2023-08-13 05:21:52 -04:00
static int myexecvp(char *a, char **args) {
char msg[MSGLEN + 1];
/* modify argv[0] to reference the last component of its path name */
2023-09-25 17:15:52 -04:00
args[0] = (char *)basename(args[0]);
2023-08-13 05:21:52 -04:00
/* execute the program or shell script */
execvp(a, args); /* returns only on failure */
snprintf(msg, sizeof(msg), "\nCannot exec %s", a);
perror(msg); /* display the reason */
askforreturn(); /* wait until the user sees the message */
myexit(1); /* exit the child */
/* NOTREACHED */
return 0;
2023-07-27 14:04:50 -04:00
}
/* myfork acts like fork but also handles signals */
2023-08-13 05:21:52 -04:00
static pid_t myfork(void) {
pid_t p; /* process number */
p = fork();
/* the parent ignores the interrupt, quit, and hangup signals */
if(p > 0) {
oldsigquit = signal(SIGQUIT, SIG_IGN);
oldsighup = signal(SIGHUP, SIG_IGN);
# ifdef SIGTSTP
oldsigtstp = signal(SIGTSTP, SIG_DFL);
# endif
}
/* so they can be used to stop the child */
else if(p == 0) {
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGHUP, SIG_DFL);
# ifdef SIGTSTP
signal(SIGTSTP, SIG_DFL);
# endif
}
/* check for fork failure */
if(p == -1) { myperror("Cannot fork"); }
return p;
2023-07-27 14:04:50 -04:00
}
/* join is the compliment of fork */
2023-08-13 05:21:52 -04:00
static int join(pid_t p) {
int status = -1;
pid_t w;
/* wait for the correct child to exit */
do {
w = wait(&status);
} while(p != -1 && w != p);
/* restore signal handling */
signal(SIGQUIT, oldsigquit);
signal(SIGHUP, oldsighup);
# ifdef SIGTSTP
signal(SIGTSTP, oldsigtstp);
# endif
2023-07-27 14:04:50 -04:00
2023-08-13 05:21:52 -04:00
/* return the child's exit code */
return (status >> 8);
2023-07-27 14:04:50 -04:00
}
#endif /* !MSDOS */