summaryrefslogtreecommitdiff
path: root/src/stdlib/system.c
blob: 612a957042f31f06a51edd4ce0e8e5545ed2fd15 (plain)
1
2
3
4
5
6
7
8
9
10
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
61
62
63
64
65
66
67
68
69
70
#include <stdlib.h>

#if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE
# include "errno.h"
# include "sys/types.h"
# include "unistd.h"
# include "sys/wait.h"
# define USE_FORK
#endif

/** execute a command **/

int system(const char * string)
{
	#if defined USE_FORK
	pid_t pid;
	int status;

	if (string == NULL) {
		return 0;
	}

	/* ignore SIGINT and SIGQUIT */
	/* block SIGCHLD */

	pid = fork();
	if (pid < 0) {
		/* errno comes from fork() */
		return -1;
	}

	if (pid == 0) {
		/* restore signal handlers */
		execl("/bin/sh", "sh", "-c", string, (char *)0);
		_exit(127);
	}
	
	if (waitpid(pid, &status, 0) == -1) {
		errno = ECHILD;
		status = -1;
	}

	/* restore signal handlers */
	if (WIFSIGNALED(status)) {
		
	}

	return status;

	#else
	(void)string;
	return 1;
	#endif
}

/***
runs the command ARGUMENT(string) using the host environment's command
processor.

Specifying CONSTANT(NULL) for ARGUMENT(string) tests whether a command
processor is available.
***/

/*
IMPLEMENTATION(How the command processor is invoked)
IMPLEMENTATION(The return value when ARGUMENT(string) is not CONSTANT(NULL))
RETURN(NONZERO, If ARGUMENT(string) is CONSTANT(NULL), a command processor is available)
RETURN(0, If ARGUMENT(string) is CONSTANT(NULL), a command processor is not available)
STDC(1)
*/