scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 540b7b0aa13418274213617487ed4c7c4d5e64a3
parent bc8e04323b76bf135995d22ecacf9a3cf987901c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 18 May 2022 18:38:54 +0200

libc/posix: Add system()

Diffstat:
Msrc/libc/arch/posix/Makefile | 1+
Asrc/libc/arch/posix/system.c | 34++++++++++++++++++++++++++++++++++
Msrc/libc/objs/amd64-linux.mk | 1+
Msrc/libc/objs/amd64-netbsd.mk | 1+
Msrc/libc/objs/amd64-openbsd.mk | 1+
5 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/src/libc/arch/posix/Makefile b/src/libc/arch/posix/Makefile @@ -13,6 +13,7 @@ OBJS=\ getenv.$O\ raise.$O\ signal.$O\ + system.$O\ time.$O\ all: $(OBJS) diff --git a/src/libc/arch/posix/system.c b/src/libc/arch/posix/system.c @@ -0,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; + } +} diff --git a/src/libc/objs/amd64-linux.mk b/src/libc/objs/amd64-linux.mk @@ -44,5 +44,6 @@ OBJS =\ arch/posix/getenv.$O\ arch/posix/raise.$O\ arch/posix/signal.$O\ + arch/posix/system.$O\ arch/posix/time.$O\ string/strlen.$O\ diff --git a/src/libc/objs/amd64-netbsd.mk b/src/libc/objs/amd64-netbsd.mk @@ -37,5 +37,6 @@ OBJS =\ arch/posix/getenv.$O\ arch/posix/raise.$O\ arch/posix/signal.$O\ + arch/posix/system.$O\ arch/posix/time.$O\ string/strlen.$O\ diff --git a/src/libc/objs/amd64-openbsd.mk b/src/libc/objs/amd64-openbsd.mk @@ -36,5 +36,6 @@ OBJS =\ arch/posix/getenv.$O\ arch/posix/raise.$O\ arch/posix/signal.$O\ + arch/posix/system.$O\ arch/posix/time.$O\ string/strlen.$O\