From b009f8522cf2bb6d3254bc52b9a4729f902b33e0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 27 Sep 2019 09:42:32 +0200 Subject: [PATCH] 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 --- Libraries/LibC/stdlib.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp index 89cafd8e8c6..dcade0b816b 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);