From aa1ea490d0d4a09833cd5c2d59f215be8535a840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 7 Jul 2004 12:28:22 +0000 Subject: [PATCH] Implemented _exit(). Added empty implementations of fork() and all the exec*() variants; the latter currently all call execve() and may pass the correct parameters (not tested). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8339 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/unistd/Jamfile | 3 + src/kernel/libroot/posix/unistd/_exit.c | 23 ++++ src/kernel/libroot/posix/unistd/exec.c | 154 ++++++++++++++++++++++++ src/kernel/libroot/posix/unistd/fork.c | 22 ++++ 4 files changed, 202 insertions(+) create mode 100644 src/kernel/libroot/posix/unistd/_exit.c create mode 100644 src/kernel/libroot/posix/unistd/exec.c create mode 100644 src/kernel/libroot/posix/unistd/fork.c diff --git a/src/kernel/libroot/posix/unistd/Jamfile b/src/kernel/libroot/posix/unistd/Jamfile index 987306f8fa..fe482500ea 100644 --- a/src/kernel/libroot/posix/unistd/Jamfile +++ b/src/kernel/libroot/posix/unistd/Jamfile @@ -7,7 +7,10 @@ KernelMergeObject posix_unistd.o : <$(SOURCE_GRIST)>conf.c <$(SOURCE_GRIST)>directory.c <$(SOURCE_GRIST)>dup.c + <$(SOURCE_GRIST)>exec.c + <$(SOURCE_GRIST)>_exit.c <$(SOURCE_GRIST)>fcntl.c + <$(SOURCE_GRIST)>fork.c <$(SOURCE_GRIST)>getopt.c <$(SOURCE_GRIST)>hostname.c <$(SOURCE_GRIST)>ioctl.c diff --git a/src/kernel/libroot/posix/unistd/_exit.c b/src/kernel/libroot/posix/unistd/_exit.c new file mode 100644 index 0000000000..4d3f249dcc --- /dev/null +++ b/src/kernel/libroot/posix/unistd/_exit.c @@ -0,0 +1,23 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. +*/ + + +#include +#include + + +extern void (*_IO_cleanup)(void); + + +void +_exit(int status) +{ + // close all open files + _IO_cleanup(); + + // exit with status code + _kern_exit(status); +} + diff --git a/src/kernel/libroot/posix/unistd/exec.c b/src/kernel/libroot/posix/unistd/exec.c new file mode 100644 index 0000000000..4fa8cfc2e7 --- /dev/null +++ b/src/kernel/libroot/posix/unistd/exec.c @@ -0,0 +1,154 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. +*/ + + +#include + +#include +#include +#include +#include +#include + + +static int +count_arguments(va_list list, const char *arg, char ***_env) +{ + int count = 0; + + while (arg != NULL) { + count++; + arg = va_arg(list, const char *); + } + + if (_env) + *_env = va_arg(list, char **); + + return count; +} + + +static void +copy_arguments(va_list list, const char **args, const char *arg) +{ + int count = 0; + + while (arg != NULL) { + arg = va_arg(list, const char *); + args[count++] = arg; + } + + args[count] = NULL; + // terminate list +} + + +// #pragma mark - + + +int +execve(const char *path, char * const argv[], char * const envp[]) +{ + // ToDo: implement me for real! + fprintf(stderr, "execve(): NOT IMPLEMENTED\n"); + return -1; +} + + +int +execv(const char *path, char * const *argv) +{ + return execve(path, argv, environ); +} + + +int +execvp(const char *file, char * const *argv) +{ + // ToDo: do the "p" thing + return execve(file, argv, environ); +} + + +int +execl(const char *path, const char *arg, ...) +{ + const char **args; + va_list list; + int count; + + // count arguments + + va_start(list, arg); + count = count_arguments(list, arg, NULL); + va_end(list); + + // copy arguments + + args = alloca((count + 1) * sizeof(char *)); + va_start(list, arg); + copy_arguments(list, args, arg); + va_end(list); + + return execve(path, (char * const *)args, environ); +} + + +int +execlp(const char *file, const char *arg, ...) +{ + const char **args; + va_list list; + int count; + + // count arguments + + va_start(list, arg); + count = count_arguments(list, arg, NULL); + va_end(list); + + // copy arguments + + args = alloca((count + 1) * sizeof(char *)); + va_start(list, arg); + copy_arguments(list, args, arg); + va_end(list); + + return execvp(file, (char * const *)args); +} + + +int +execle(const char *path, const char *arg, ... /*, char **env */) +{ + const char **args; + char **env; + va_list list; + int count; + + // count arguments + + va_start(list, arg); + count = count_arguments(list, arg, &env); + va_end(list); + + // copy arguments + + args = alloca((count + 1) * sizeof(char *)); + va_start(list, arg); + copy_arguments(list, args, arg); + va_end(list); + + return execve(path, (char * const *)args, env); +} + + +int +exect(const char *path, char * const *argv) +{ + // ToDo: is this any different? + return execv(path, argv); +} + diff --git a/src/kernel/libroot/posix/unistd/fork.c b/src/kernel/libroot/posix/unistd/fork.c new file mode 100644 index 0000000000..33e8aca451 --- /dev/null +++ b/src/kernel/libroot/posix/unistd/fork.c @@ -0,0 +1,22 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. +*/ + + +#include + +#include +#include +#include + + +pid_t +fork(void) +{ + // ToDo: implement me + // ToDo: atfork() + fprintf(stderr, "fork(): NOT IMPLEMENTED\n"); + return -1; +} +