libc: Add system()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-08 12:18:52 +02:00
parent 95a93a7f66
commit eb3fb04734
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 30 additions and 1 deletions

View File

@ -100,7 +100,8 @@ extern "C"
/* Exit the program abnormally, without performing any registered cleanup actions. */
__noreturn void _Exit(int status);
int system(const char*);
/* Execute a shell command. */
int system(const char* cmd);
/* Get the value of an environment variable. */
char* getenv(const char* key);

View File

@ -6,6 +6,9 @@
#include <bits/waitpid.h>
#include <sys/types.h>
#define WIFEXITED(ret) ((ret) | 0)
#define WEXITSTATUS(ret) ((ret)&0xff)
#ifdef __cplusplus
extern "C"
{

View File

@ -6,6 +6,7 @@
#include <luna/Utf8.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
template <typename ArgT, typename ResultT> static inline ResultT __generic_div(ArgT a, ArgT b)
@ -175,4 +176,28 @@ extern "C"
{
free_impl(ptr);
}
int system(const char* cmd)
{
if (!cmd)
{
// FIXME: Check if there is a shell available in the system.
errno = ENOTSUP;
return -1;
}
pid_t child = fork();
if (child == 0)
{
execl("/bin/sh", "sh", "-c", cmd, NULL);
_Exit(127);
}
if (child < 0) return -1;
int status;
waitpid(child, &status, 0);
return status;
}
}