xref: /haiku/src/system/libroot/posix/unistd/system.cpp (revision 24159a0c7d6d6dcba9f2a0c1a7c08d2c8167f21b)
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 = wait_for_thread(thread, &returnValue);
31 	if (error != B_OK) {
32 		errno = error;
33 		return -1;
34 	}
35 	return returnValue;
36 }
37