xref: /haiku/src/system/libroot/posix/unistd/system.cpp (revision adb0d19d561947362090081e81d90dde59142026)
1 /*
2  * Copyright 2004-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <image.h>
7 
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12 
13 
14 extern "C" int
15 system(const char *command)
16 {
17 	if (!command)
18 		return 1;
19 
20 	const char *argv[] = { "/bin/sh", "-c", command, NULL };
21 	int argc = 3;
22 
23 	thread_id thread = load_image(argc, argv, (const char **)environ);
24 	if (thread < 0) {
25 		errno = thread;
26 		return -1;
27 	}
28 
29 	resume_thread(thread);
30 
31 	int exitStatus;
32 	pid_t result;
33 	while ((result = waitpid(thread, &exitStatus, 0)) < 0
34 		&& errno == B_INTERRUPTED) {
35 		// waitpid() was interrupted by a signal, retry...
36 	}
37 
38 	if (result < 0)
39 		return -1;
40 
41 	return exitStatus;
42 }
43