Browse Source

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
Andreas Kling 5 năm trước cách đây
mục cha
commit
b009f8522c
1 tập tin đã thay đổi với 9 bổ sung3 xóa
  1. 9 3
      Libraries/LibC/stdlib.cpp

+ 9 - 3
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);