xref: /haiku/src/system/libroot/posix/unistd/system.cpp (revision 020cbad9d40235a2c50a81a42d69912a5ff8fbc4)
1 /*
2 ** Copyright 2004, Ingo Weinhold, bonefish@cs.tu-berlin.de. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 */
5 
6 
7 #include <image.h>
8 
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <errno.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 	status_t returnValue;
30 	status_t error;
31 	do {
32 		error = wait_for_thread(thread, &returnValue);
33 	} while (error == B_INTERRUPTED);
34 
35 	if (error != B_OK) {
36 		errno = error;
37 		return -1;
38 	}
39 	return returnValue;
40 }
41