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.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2022-11-02 07:34:39 +01:00
parent e87d78afa8
commit 27433dabe0
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
3 changed files with 37 additions and 10 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2017,2018,2019,2022 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2018,2022 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
@ -20,14 +20,40 @@
#include <mes/lib.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#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;
}