diff --git a/libs/libc/include/stdlib.h b/libs/libc/include/stdlib.h index ab7dd870..6a04991f 100644 --- a/libs/libc/include/stdlib.h +++ b/libs/libc/include/stdlib.h @@ -107,6 +107,9 @@ extern "C" /* Returns the result of dividing a by b. */ lldiv_t lldiv(long long a, long long b); + /* Runs a shell command. */ + int system(const char* command); + void qsort(void*, size_t, size_t, int (*)(const void*, const void*)); // Not implemented. size_t mbstowcs(wchar_t* dest, const char* src, diff --git a/libs/libc/src/stdlib.cpp b/libs/libc/src/stdlib.cpp index fbe7a3ba..026c3fc3 100644 --- a/libs/libc/src/stdlib.cpp +++ b/libs/libc/src/stdlib.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include template static T string_to_integer_type(const char* str) @@ -117,6 +118,22 @@ extern "C" return common_div(a, b); } + int system(const char* command) + { + pid_t child = fork(); + if (child < 0) return -1; + if (child == 0) + { + char* argv[] = {const_cast("/bin/sh"), const_cast("-c"), const_cast(command), + nullptr}; // FIXME: This is very verbose. + execv(argv[0], argv); + exit(127); + } + int status; + waitpid(child, &status, 0); + return status; + } + void qsort(void*, size_t, size_t, int (*)(const void*, const void*)) { NOT_IMPLEMENTED("qsort");