/*
 *   Copyright (C) 1988-2006 Lucent Technologies Inc. and others.
 *   All Rights Reserved.
 */

/*	cscope - interactive C symbol cross-reference
 *
 *	process execution functions
 */

#include "global.h"

#ifdef pyr
#undef pyr
#include <varargs.h>
#define pyr
#else
#ifdef STDARG
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#endif
#if defined(__BORLANDC__) || defined(FOR_WIN32)
#include <io.h>
#include <process.h>
#endif

#ifndef _NFILE
#define _NFILE	FOPEN_MAX
#endif

#if !BSD
#define getdtablesize()	_NFILE
#endif	

#define MAXARGS	100	/* maximum number of arguments to executed command */

pid_t	childpid;	/* child's process ID */

#if !defined(__BORLANDC__) && !defined(FOR_WIN32)
static	SIGTYPE	(*oldsigquit)();	/* old value of quit signal */
#endif

#ifdef SIGTSTP
static	SIGTYPE	(*oldsigtstp)();	/* old value of terminal stop signal */
#endif

/* execute forks and executes a program or shell script, waits for it to
 * finish, and returns its exit code.
 */

#ifdef STDARG
execute(char *path, ...)
#else
/*VARARGS0*/
execute(va_alist)	/* note: "exec" is already defined on u370 */
va_dcl
#endif
{
	va_list	ap;
	char	*args[MAXARGS + 1];
	int	exitcode;
	register int	i;
	char	msg[MSGLEN + 1];
#ifndef STDARG
	char	*path;
#endif
#if !defined(__BORLANDC__) && !defined(FOR_WIN32)
	pid_t	p;
	pid_t	myfork();
#endif
	/* fork and exec the program or shell script */
#ifdef STDARG
	va_start(ap, path);
#else
	va_start(ap);
#endif
#if !defined(__BORLANDC__) && !defined(FOR_WIN32)
	exitcurses();
	if ((p = myfork()) == 0) {
		
		/* close all files except stdin, stdout, and stderr */
		for (i = 3; i < getdtablesize() && close(i) == 0; ++i) {
			;
		}
#endif
		/* execute the program or shell script */
#ifndef STDARG
		path = va_arg(ap, char *);
#endif
		for (i = 0; i < MAXARGS && (args[i] = va_arg(ap, char *)) != NULL; ++i) {
			;
		}
		args[i] = NULL;			/* in case MAXARGS reached */
		args[0] = mybasename(args[0]);
#if defined(__BORLANDC__) || defined(FOR_WIN32)
		if ((exitcode = spawnvp(P_WAIT, path, args)) != 0) {
#else
		(void) execvp(path, args);	/* returns only on failure */
#endif
		(void) sprintf(msg, "\n%s: cannot execute %s", argv0, path);
		(void) perror(msg);	/* display the reason */
		askforreturn();		/* wait until the user sees the message */
#if defined(__BORLANDC__) || defined(FOR_WIN32)
		}
#else
		exit(1);		/* exit the child */
	}
	else {
		exitcode = join(p);	/* parent */
	}
	va_end(ap);
	if (noacttimeout) {
		(void) fprintf(stderr, "%s: no activity time out--exiting\n",argv0);
		myexit(SIGALRM);
	}
#endif
	entercurses();
	return(exitcode);
}
#if !defined(__BORLANDC__) && !defined(FOR_WIN32)

/* myfork acts like fork but also handles signals */

pid_t
myfork() 
{
	pid_t	p;		/* process number */

#ifdef SIGTSTP	/* curses catches the terminal stop signal */
	oldsigtstp = signal(SIGTSTP, SIG_DFL);
#endif
	/* the parent ignores the interrupt and quit signals */
	if ((p = fork()) > 0) {
		childpid = p;
		oldsigquit = signal(SIGQUIT, SIG_IGN);
	}
	/* so they can be used to stop the child */
	else if (p == 0) {
		(void) signal(SIGINT, SIG_DFL);
		(void) signal(SIGQUIT, SIG_DFL);
		(void) signal(SIGHUP, SIG_DFL);	/* restore hangup default */
	}
	/* check for fork failure */
	if (p == -1) {
		myperror("Cannot fork");
	}
	return p;
}

/* join is the compliment of fork */

join(p) 
pid_t	p; 
{
	int	status;  
	pid_t	w;

	/* wait for the correct child to exit */
	do {
		w = wait(&status);
	} while (p != -1 && w != p);
	childpid = 0;

	/* restore signal handling */
	(void) signal(SIGQUIT, oldsigquit);
#ifdef SIGTSTP
	(void) signal(SIGTSTP, oldsigtstp);
#endif
	/* return the child's exit code */
	return(status >> 8);
}
#endif  /* not __BORLANDC__ && not FOR_WIN32 */
