diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-05-13 04:52:55 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-13 04:52:55 +0200 |
commit | 43604bf71a21c31aad68114f8612a9c4180f22ad (patch) | |
tree | 1969cc5d5e4b218007ffc2084d626e319f97d6db | |
parent | 42cf09fdf1741a84cf90088287f58023c99034f4 (diff) | |
download | serenity-43604bf71a21c31aad68114f8612a9c4180f22ad.zip |
LibC+Shell: Make system() actually work.
system() will now fork off a child process and execute the command via
/bin/sh -c. There are probably some things to fix here, but it's a start.
-rw-r--r-- | LibC/stdlib.cpp | 11 | ||||
-rw-r--r-- | Shell/main.cpp | 7 |
2 files changed, 14 insertions, 4 deletions
diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp index 17486f7c25..d85483ce94 100644 --- a/LibC/stdlib.cpp +++ b/LibC/stdlib.cpp @@ -206,7 +206,16 @@ void srandom(unsigned seed) int system(const char* command) { - return execl("/bin/sh", "sh", "-c", command, nullptr); + auto child = fork(); + if (!child) { + int rc = execl("/bin/sh", "sh", "-c", command, nullptr); + if (rc < 0) + perror("execl"); + exit(0); + } + int wstatus; + waitpid(child, &wstatus, 0); + return WEXITSTATUS(wstatus); } char* mktemp(char* pattern) diff --git a/Shell/main.cpp b/Shell/main.cpp index 98009d8c58..a048a3c203 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -413,9 +413,10 @@ int main(int argc, char** argv) endpwent(); } - if (argc > 1 && !strcmp(argv[1], "-c")) { - fprintf(stderr, "FIXME: Implement /bin/sh -c\n"); - return 1; + if (argc > 2 && !strcmp(argv[1], "-c")) { + dbgprintf("sh -c '%s'\n", argv[2]); + run_command(argv[2]); + return 0; } { |