From 27433dabe003ea61dde224b25559758619ac4cdf Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Wed, 2 Nov 2022 07:34:39 +0100 Subject: [PATCH] DRAFT lib: Implement system. * lib/stub/system.c: Move to... * lib/stdlib/system.c: ...here. Implement. * build-aux/configure-lib.sh (libc_gnu_SOURCES): Update file name. * include/stdlib.h (system): Add prototype. --- build-aux/configure-lib.sh | 2 +- include/stdlib.h | 3 ++- lib/{stub => stdlib}/system.c | 42 ++++++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 10 deletions(-) rename lib/{stub => stdlib}/system.c (52%) diff --git a/build-aux/configure-lib.sh b/build-aux/configure-lib.sh index 13b06083..6488d2c4 100644 --- a/build-aux/configure-lib.sh +++ b/build-aux/configure-lib.sh @@ -353,6 +353,7 @@ lib/stdlib/atexit.c lib/stdlib/atof.c lib/stdlib/atol.c lib/stdlib/mbstowcs.c +lib/stdlib/system.c lib/string/bcmp.c lib/string/bcopy.c lib/string/bzero.c @@ -406,7 +407,6 @@ lib/stub/strftime.c lib/stub/strsignal.c lib/stub/sys_siglist.c lib/stub/sysconf.c -lib/stub/system.c lib/stub/times.c lib/stub/ttyname.c lib/stub/umask.c diff --git a/include/stdlib.h b/include/stdlib.h index fe9d574d..3fec7e06 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen + * Copyright © 2017,2018,2019,2022 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -59,6 +59,7 @@ long strtol (char const *string, char **tailptr, int base); long long strtoll (char const *string, char **tailptr, int base); unsigned long strtoul (char const *string, char **tailptr, int base); unsigned long long strtoull (char const *string, char **tailptr, int base); +int system (char const *command); #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 diff --git a/lib/stub/system.c b/lib/stdlib/system.c similarity index 52% rename from lib/stub/system.c rename to lib/stdlib/system.c index 49663fb0..34b4546f 100644 --- a/lib/stub/system.c +++ b/lib/stdlib/system.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2018,2022 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -20,14 +20,40 @@ #include #include +#include +#include +#include + +#ifndef SHELL_FILE_NAME +#define SHELL_FILE_NAME "/bin/sh" +#endif +#ifndef SHELL_COMMAND_NAME +#define SHELL_COMMAND_NAME "sh" +#endif int -system (int x) +system (char const *command) { - static int stub = 0; - if (__mes_debug () && !stub) - eputs ("system stub\n"); - stub = 1; - errno = 0; - return 0; + pid_t pid = fork (); + if (pid == -1) + return -1; + else if (pid == 0) + { + // child + char const *argv[4]; + argv[0] = SHELL_COMMAND_NAME; + argv[1] = "-c"; + argv[2] = command; + argv[3] = 0; + execve (SHELL_FILE_NAME, (char *const *) argv, environ); + _exit (127); + } + + // parent + int status; + pid_t child_pid = waitpid (pid, &status, 0); + if (child_pid != pid) + return -1; + + return status; }