1989-05-16 13:13:53 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
|
1990-11-22 13:59:54 +00:00
|
|
|
#if defined(_POSIX_SOURCE)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
1989-05-16 13:13:53 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
1990-01-22 13:00:13 +00:00
|
|
|
extern int _fork(void);
|
|
|
|
extern int _wait(int *);
|
1989-05-16 13:13:53 +00:00
|
|
|
extern void _exit(int);
|
1990-09-27 16:52:07 +00:00
|
|
|
extern void _execve(const char *path, const char ** argv, const char ** envp);
|
1990-01-22 13:00:13 +00:00
|
|
|
extern void _close(int);
|
1989-05-16 13:13:53 +00:00
|
|
|
|
|
|
|
#define FAIL 127
|
|
|
|
|
1990-09-27 16:52:07 +00:00
|
|
|
extern const char **_penvp;
|
|
|
|
static const char *exec_tab[] = {
|
|
|
|
"sh", /* argv[0] */
|
|
|
|
"-c", /* argument to the shell */
|
|
|
|
NULL, /* to be filled with user command */
|
|
|
|
NULL /* terminating NULL */
|
|
|
|
};
|
|
|
|
|
1989-05-16 13:13:53 +00:00
|
|
|
int
|
|
|
|
system(const char *str)
|
|
|
|
{
|
|
|
|
int pid, exitstatus, waitval;
|
|
|
|
int i;
|
|
|
|
|
1990-01-22 13:00:13 +00:00
|
|
|
if ((pid = _fork()) < 0) return str ? -1 : 0;
|
1989-05-16 13:13:53 +00:00
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
for (i = 3; i <= 20; i++)
|
1990-01-22 13:00:13 +00:00
|
|
|
_close(i);
|
1989-05-16 13:13:53 +00:00
|
|
|
if (!str) str = "cd ."; /* just testing for a shell */
|
1990-09-27 16:52:07 +00:00
|
|
|
exec_tab[2] = str; /* fill in command */
|
|
|
|
_execve("/bin/sh", exec_tab, _penvp);
|
|
|
|
/* get here if execve fails ... */
|
1989-05-16 13:13:53 +00:00
|
|
|
_exit(FAIL); /* see manual page */
|
|
|
|
}
|
1990-01-22 13:00:13 +00:00
|
|
|
while ((waitval = _wait(&exitstatus)) != pid) {
|
1989-05-16 13:13:53 +00:00
|
|
|
if (waitval == -1) break;
|
|
|
|
}
|
|
|
|
if (waitval == -1) {
|
|
|
|
/* no child ??? or maybe interrupted ??? */
|
|
|
|
exitstatus = -1;
|
|
|
|
}
|
|
|
|
if (!str) {
|
1990-09-27 16:52:07 +00:00
|
|
|
if (exitstatus == FAIL << 8) /* execve() failed */
|
1989-05-16 13:13:53 +00:00
|
|
|
exitstatus = 0;
|
|
|
|
else exitstatus = 1; /* /bin/sh exists */
|
|
|
|
}
|
|
|
|
return exitstatus;
|
|
|
|
}
|