From: Alan Cox Date: Sat, 19 Nov 2016 11:42:53 +0000 (+0000) Subject: execl: 6502 support X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=e883a108db3610d1c7e8da5149f52fefe4325051;p=FUZIX.git execl: 6502 support --- diff --git a/Library/libs/execl.c b/Library/libs/execl.c index b033002d..e0733133 100644 --- a/Library/libs/execl.c +++ b/Library/libs/execl.c @@ -1,36 +1,36 @@ -/* execl.c - * - * function(s) - * execl - load and run a program - */ - -#include -#include -#include -#include -#include +/* execl.c + * + * function(s) + * execl - load and run a program + */ + +#include +#include +#include +#include +#include #include - -/* Find file in pathes: - * 1. /name or ./name or ../name is already qualified names - * 2. else search in all pathes described in env var PATH (if this - * var is not exist, _PATH_DEFPATH is used) - * 3. else search in current directory - * 4. else return NULL (execve() interpretes NULL as non existent file!) - */ -const char *_findPath(const char *path) + +/* Find file in pathes: + * 1. /name or ./name or ../name is already qualified names + * 2. else search in all pathes described in env var PATH (if this + * var is not exist, _PATH_DEFPATH is used) + * 3. else search in current directory + * 4. else return NULL (execve() interpretes NULL as non existent file!) + */ +const char *_findPath(const char *path) { - char *p; + char *p; const char *envp; static char name[PATHLEN + 1]; if (*path == '/' || /* qualified name */ *path == '.') return path; - - /* search for pathes list */ + + /* search for pathes list */ if ((envp = getenv("PATH")) == NULL) envp = _PATH_DEFPATH; - - /* lookup all pathes */ + + /* lookup all pathes */ while (*envp) { p = name; while (*envp && (*p = *envp++) != ':') { @@ -50,32 +50,70 @@ const char *_findPath(const char *path) if (access(path, 0) == 0) /* file exist in current dir */ return name; return NULL; -} - +} + #ifndef __CC65__ - + /* FIXME: The 6502 calling sequence means these need a different implementation */ -int execl(const char *pathP, const char *arg0, ...) +int execl(const char *pathP, const char *arg0, ...) { return execve(pathP, &arg0, environ); } - -int execlp(const char *pathP, const char *arg0, ...) + +int execlp(const char *pathP, const char *arg0, ...) { return execve(_findPath(pathP), &arg0, environ); } #else -int execl(const char *pathP, const char *arg0, ...) + +/* This gets to be overexciting with the backward argument handling */ +#include + +int execl(const char *pathP, const char *arg0, ...) { - /* BUG: this should be ENOSYS, but Fuzix doesn't have that yet */ - errno = EINVAL; + va_list ptr; + const char *arg[32]; + const char **p = arg; + + va_start(ptr, arg0); + + *p++ = arg0; + + while (p != &arg[32]) { + *p = va_arg(ptr, const char *); + if (*p++ == NULL) { + va_end(ptr); + return execve(pathP, (void *) arg, + (void *) environ); + } + } + va_end(ptr); + errno = E2BIG; return -1; } - -int execlp(const char *pathP, const char *arg0, ...) + + +int execlp(const char *pathP, const char *arg0, ...) { - /* BUG: this should be ENOSYS, but Fuzix doesn't have that yet */ - errno = EINVAL; + va_list ptr; + const char *arg[32]; + const char **p = arg; + + va_start(ptr, arg0); + + *p++ = arg0; + + while (p != &arg[32]) { + *p = va_arg(ptr, const char *); + if (*p++ == NULL) { + va_end(ptr); + return execve(_findPath(pathP), (void *) arg, + (void *) environ); + } + } + va_end(ptr); + errno = E2BIG; return -1; } + #endif