diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-09-27 09:42:32 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-27 10:15:42 +0200 |
commit | b009f8522cf2bb6d3254bc52b9a4729f902b33e0 (patch) | |
tree | 3c630c565aebb5096882f00ea6baf10b09fac5de /Libraries | |
parent | 676af444ca80cc9885ba9b1950e5179aeb4fb4fa (diff) | |
download | serenity-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
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibC/stdlib.cpp | 12 |
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); |