ref: 540b7b0aa13418274213617487ed4c7c4d5e64a3
parent: bc8e04323b76bf135995d22ecacf9a3cf987901c
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed May 18 14:38:54 EDT 2022
libc/posix: Add system()
--- a/src/libc/arch/posix/Makefile
+++ b/src/libc/arch/posix/Makefile
@@ -13,6 +13,7 @@
getenv.$O\
raise.$O\
signal.$O\
+ system.$O\
time.$O\
all: $(OBJS)
--- /dev/null
+++ b/src/libc/arch/posix/system.c
@@ -1,0 +1,34 @@
+#include <sys.h>
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include "../../syscall.h"
+
+#define SHELL "/bin/sh"
+
+#undef system
+
+extern char **_environ;
+
+int
+system(const char *cmd)
+{
+ int st;
+ pid_t pid;
+
+ if (!cmd)
+ return _access(SHELL, X_OK);
+
+ switch ((pid = _fork())) {
+ case -1:
+ return -1;
+ case 0:
+ _execve(SHELL, (char*[]) {"sh", "-c", cmd, NULL}, _environ);
+ _exit(127);
+ default:
+ while (_waitpid(pid, &st, 0) < 0 && errno == EINTR)
+ ;
+ return st;
+ }
+}
--- a/src/libc/objs/amd64-linux.mk
+++ b/src/libc/objs/amd64-linux.mk
@@ -44,5 +44,6 @@
arch/posix/getenv.$O\
arch/posix/raise.$O\
arch/posix/signal.$O\
+ arch/posix/system.$O\
arch/posix/time.$O\
string/strlen.$O\
--- a/src/libc/objs/amd64-netbsd.mk
+++ b/src/libc/objs/amd64-netbsd.mk
@@ -37,5 +37,6 @@
arch/posix/getenv.$O\
arch/posix/raise.$O\
arch/posix/signal.$O\
+ arch/posix/system.$O\
arch/posix/time.$O\
string/strlen.$O\
--- a/src/libc/objs/amd64-openbsd.mk
+++ b/src/libc/objs/amd64-openbsd.mk
@@ -36,5 +36,6 @@
arch/posix/getenv.$O\
arch/posix/raise.$O\
arch/posix/signal.$O\
+ arch/posix/system.$O\
arch/posix/time.$O\
string/strlen.$O\