Repository navigation
Expand file tree
/
Copy pathsystem.c
More file actions
60 lines (53 loc) · 1.4 KB
/
Copy pathsystem.c
File metadata and controls
60 lines (53 loc) · 1.4 KB
1
2
3
4
5
6
7
8
<
7C86
div data-line-number="9" class="react-line-number react-code-text" style="padding-right:16px">910
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
extern pid_t _fork(void);
extern pid_t _wait(int *);
extern void _exit(int);
extern void _execve(const char *path, const char ** argv, const char ** envp);
extern int _close(int);
#define FAIL 127
extern const char ***_penviron;
static const char *exec_tab[] = {
"sh", /* argv[0] */
"-c", /* argument to the shell */
NULL, /* to be filled with user command */
NULL /* terminating NULL */
};
int
system(const char *str)
{
int pid, exitstatus, waitval;
int i;
if ((pid = _fork()) < 0) return str ? -1 : 0;
if (pid == 0) {
for (i = 3; i <= OPEN_MAX; i++)
_close(i);
if (!str) str = "cd ."; /* just testing for a shell */
exec_tab[2] = str; /* fill in command */
_execve("/bin/sh", exec_tab, *_penviron);
/* get here if execve fails ... */
_exit(FAIL); /* see manual page */
}
while ((waitval = _wait(&exitstatus)) != pid) {
if (waitval == -1) break;
}
if (waitval == -1) {
/* no child ??? or maybe interrupted ??? */
exitstatus = -1;
}
if (!str) {
if (exitstatus == FAIL << 8) /* execve() failed */
exitstatus = 0;
else exitstatus = 1; /* /bin/sh exists */
}
return exitstatus;
}