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".
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-17 13:42:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
2018-06-23 13:57:57 +00:00
|
|
|
#include <unistd.h>
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
#define FAIL 127
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
static const char* exec_tab[] = {
|
|
|
|
"sh", /* argv[0] */
|
|
|
|
"-c", /* argument to the shell */
|
|
|
|
NULL, /* to be filled with user command */
|
|
|
|
NULL /* terminating NULL */
|
|
|
|
};
|
1990-09-27 16:52:07 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
int system(const char* str)
|
1989-05-16 13:13:53 +00:00
|
|
|
{
|
|
|
|
int pid, exitstatus, waitval;
|
|
|
|
int i;
|
|
|
|
|
2018-06-23 13:57:57 +00:00
|
|
|
if ((pid = fork()) < 0)
|
2018-06-21 20:33:47 +00:00
|
|
|
return str ? -1 : 0;
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (pid == 0)
|
|
|
|
{
|
1989-05-16 13:13:53 +00:00
|
|
|
for (i = 3; i <= 20; i++)
|
2018-06-23 13:57:57 +00:00
|
|
|
close(i);
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!str)
|
|
|
|
str = "cd ."; /* just testing for a shell */
|
|
|
|
exec_tab[2] = str; /* fill in command */
|
2018-06-23 13:57:57 +00:00
|
|
|
execve("/bin/sh", (char* const*)exec_tab, (char* const*)environ);
|
1990-09-27 16:52:07 +00:00
|
|
|
/* get here if execve fails ... */
|
2018-06-21 20:33:47 +00:00
|
|
|
_exit(FAIL); /* see manual page */
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|
2018-06-23 13:57:57 +00:00
|
|
|
while ((waitval = wait(&exitstatus)) != pid)
|
2018-06-21 20:33:47 +00:00
|
|
|
{
|
|
|
|
if (waitval == -1)
|
|
|
|
break;
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
if (waitval == -1)
|
|
|
|
{
|
1989-05-16 13:13:53 +00:00
|
|
|
/* no child ??? or maybe interrupted ??? */
|
|
|
|
exitstatus = -1;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
if (exitstatus == FAIL << 8) /* execve() failed */
|
1989-05-16 13:13:53 +00:00
|
|
|
exitstatus = 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
else
|
|
|
|
exitstatus = 1; /* /bin/sh exists */
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|
|
|
|
return exitstatus;
|
|
|
|
}
|