mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-28 15:34:31 +00:00
feat(userspace/libc): implement system() function
Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
parent
b1a107fb65
commit
123d11e4e3
@ -103,7 +103,7 @@ extern "C"
|
||||
long strtol(const char *restrict nptr, char **restrict endptr, int base);
|
||||
long long strtoll(const char *restrict nptr, char **restrict endptr, int base);
|
||||
unsigned long int strtoul(const char *, char **, int);
|
||||
int system(const char *);
|
||||
int system(const char *command);
|
||||
int ttyslot(void);
|
||||
int unlockpt(int);
|
||||
void *valloc(size_t);
|
||||
|
@ -330,7 +330,56 @@ export long long strtoll(const char *restrict nptr, char **restrict endptr, int
|
||||
}
|
||||
|
||||
export unsigned long int strtoul(const char *, char **, int);
|
||||
export int system(const char *);
|
||||
|
||||
export int system(const char *command)
|
||||
{
|
||||
if (command == NULL)
|
||||
return 1;
|
||||
|
||||
pid_t pid;
|
||||
int status;
|
||||
struct sigaction sa, old_int, old_quit;
|
||||
sigset_t new_mask, old_mask;
|
||||
|
||||
sa.sa_handler = SIG_IGN;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
sigaction(SIGINT, &sa, &old_int);
|
||||
sigaction(SIGQUIT, &sa, &old_quit);
|
||||
|
||||
sigemptyset(&new_mask);
|
||||
sigaddset(&new_mask, SIGCHLD);
|
||||
sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
|
||||
|
||||
if ((pid = fork()) == 0)
|
||||
{
|
||||
sigaction(SIGINT, &old_int, NULL);
|
||||
sigaction(SIGQUIT, &old_quit, NULL);
|
||||
sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
||||
execl("/bin/sh", "sh", "-c", command, (char *)0);
|
||||
_exit(127);
|
||||
}
|
||||
|
||||
if (pid == -1)
|
||||
status = -1;
|
||||
else
|
||||
{
|
||||
while (waitpid(pid, &status, 0) == -1)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
status = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sigaction(SIGINT, &old_int, NULL);
|
||||
sigaction(SIGQUIT, &old_quit, NULL);
|
||||
sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
||||
return status;
|
||||
}
|
||||
|
||||
export int ttyslot(void);
|
||||
export int unlockpt(int);
|
||||
export void *valloc(size_t);
|
||||
|
Loading…
x
Reference in New Issue
Block a user