. use library function to parse memory string
[minix3.git] / lib / ansi / system.c
blob100e0cde4d3f1942d0d6d26009d7e0c3cb1fff27
1 /*
2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
4 */
5 /* $Header$ */
7 #if defined(_POSIX_SOURCE)
8 #include <sys/types.h>
9 #endif
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <limits.h>
14 extern pid_t _fork(void);
15 extern pid_t _wait(int *);
16 extern void _exit(int);
17 extern void _execve(const char *path, const char ** argv, const char ** envp);
18 extern int _close(int);
20 #define FAIL 127
22 extern const char ***_penviron;
23 static const char *exec_tab[] = {
24 "sh", /* argv[0] */
25 "-c", /* argument to the shell */
26 NULL, /* to be filled with user command */
27 NULL /* terminating NULL */
30 int
31 system(const char *str)
33 int pid, exitstatus, waitval;
34 int i;
36 if ((pid = _fork()) < 0) return str ? -1 : 0;
38 if (pid == 0) {
39 for (i = 3; i <= OPEN_MAX; i++)
40 _close(i);
41 if (!str) str = "cd ."; /* just testing for a shell */
42 exec_tab[2] = str; /* fill in command */
43 _execve("/bin/sh", exec_tab, *_penviron);
44 /* get here if execve fails ... */
45 _exit(FAIL); /* see manual page */
47 while ((waitval = _wait(&exitstatus)) != pid) {
48 if (waitval == -1) break;
50 if (waitval == -1) {
51 /* no child ??? or maybe interrupted ??? */
52 exitstatus = -1;
54 if (!str) {
55 if (exitstatus == FAIL << 8) /* execve() failed */
56 exitstatus = 0;
57 else exitstatus = 1; /* /bin/sh exists */
59 return exitstatus;