From d5ed22276d65a0ef3d9bbe586ff4a2bbfd779714 Mon Sep 17 00:00:00 2001 From: "Nathan P. Reiner" Date: Fri, 16 Dec 2022 10:47:35 +0100 Subject: add env path execution --- smash/.gitignore | 1 + smash/Makefile | 6 +++++- smash/exec.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) (limited to 'smash') diff --git a/smash/.gitignore b/smash/.gitignore index f6ccf57..be6c11f 100644 --- a/smash/.gitignore +++ b/smash/.gitignore @@ -1 +1,2 @@ smash +test diff --git a/smash/Makefile b/smash/Makefile index 7012bdd..0f62c63 100644 --- a/smash/Makefile +++ b/smash/Makefile @@ -12,5 +12,9 @@ unit_test_builtin: gcc builtin.c ../lib/cstr/cstr.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DBUILTIN_UNIT_TEST -g ./test +unit_test_exec: + gcc exec.c builtin.c parser.c ../lib/cstr/cstr.c ../lib/io/io.c ../lib/env/env.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DEXEC_UNIT_TEST -g + ./test + all: - gcc main.c exec.c parser.c builtin.c ../lib/io/io.c ../lib/avl_tree/avl_tree.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o smash -DVARIABLES_UNIT_TEST -g + gcc main.c exec.c parser.c builtin.c ../lib/env/env.c ../lib/io/io.c ../lib/avl_tree/avl_tree.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o smash -DVARIABLES_UNIT_TEST -g diff --git a/smash/exec.c b/smash/exec.c index cd18ca4..11620a5 100644 --- a/smash/exec.c +++ b/smash/exec.c @@ -9,6 +9,8 @@ #include "../lib/sys/exit.h" #include "../lib/cstr/cstr.h" #include "../lib/io/io.h" +#include "../lib/env/env.h" +#include "../lib/malloc/malloc.h" #include "parser.h" #include "builtin.h" @@ -22,11 +24,43 @@ void print_call(char **argv) } } +void __execenv(const char **argv) +{ + const char *path = getenv("PATH"); + const char *end; + char *filename; + + while (*path) { + end = path; + + while (*end && *end != ':') ++end; + + filename = malloc((end - path) + cstr_length(argv[0]) + 2); + + for (int i = 0; i < end - path; ++i) + filename[i] = path[i]; + + filename[end - path] = '/'; + + for (int i = 0; i < cstr_length(argv[0]) + 1; ++i) + filename[(end - path) + i + 1] = argv[0][i]; + + execve(filename, argv, getenvp()); + free(filename); + + path = end; + + if (*path == ':') + ++path; + } + + execve(argv[0], argv, getenvp()); +} + void exec(char *line) { expression_list_t *exps = new_expression_from_line(line); expression_list_t *exp = exps; - char **envp = { 0 }; int pid; int pipefd[2][2]; @@ -57,7 +91,7 @@ void exec(char *line) close(pipefd[1][PIPE_OUT]); close(pipefd[1][PIPE_IN]); - execve(argv[0], argv, envp); + __execenv(argv); wstdf("command not found: %s\n", argv[0]); exit(-1); } @@ -80,3 +114,11 @@ void exec(char *line) free_expression(exps); } +#ifdef EXEC_UNIT_TEST + +int main() { + char *argv[] = { "ls" }; + __execenv(argv); +} + +#endif -- cgit v1.2.3-70-g09d2