summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-09-27 09:42:32 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-09-27 10:15:42 +0200
commitb009f8522cf2bb6d3254bc52b9a4729f902b33e0 (patch)
tree3c630c565aebb5096882f00ea6baf10b09fac5de
parent676af444ca80cc9885ba9b1950e5179aeb4fb4fa (diff)
downloadserenity-b009f8522cf2bb6d3254bc52b9a4729f902b33e0.zip
LibC: Make system() behave according to POSIX
- system(nullptr) returns non-zero to indicate the presence of a shell - Failure to fork() returns -1 - Failure to exec() in the child returns 127
-rw-r--r--Libraries/LibC/stdlib.cpp12
1 files changed, 9 insertions, 3 deletions
diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp
index 89cafd8e8c..dcade0b816 100644
--- a/Libraries/LibC/stdlib.cpp
+++ b/Libraries/LibC/stdlib.cpp
@@ -263,12 +263,18 @@ void srandom(unsigned seed)
int system(const char* command)
{
+ if (!command)
+ return 1;
+
auto child = fork();
+ if (child < 0)
+ return -1;
+
if (!child) {
int rc = execl("/bin/sh", "sh", "-c", command, nullptr);
- if (rc < 0)
- perror("execl");
- exit(0);
+ ASSERT(rc < 0);
+ perror("execl");
+ exit(127);
}
int wstatus;
waitpid(child, &wstatus, 0);