libc: Add system()

This commit is contained in:
apio 2022-10-30 19:43:37 +01:00
parent 29c59abf7d
commit 0d443385e9
2 changed files with 20 additions and 0 deletions

View File

@ -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,

View File

@ -3,6 +3,7 @@
#include <luna/syscall.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
template <typename T> static T string_to_integer_type(const char* str)
@ -117,6 +118,22 @@ extern "C"
return common_div<long long, lldiv_t>(a, b);
}
int system(const char* command)
{
pid_t child = fork();
if (child < 0) return -1;
if (child == 0)
{
char* argv[] = {const_cast<char*>("/bin/sh"), const_cast<char*>("-c"), const_cast<char*>(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");