From f4bf733040f2832d730f111a3a689636cf6dfbca Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 8 Feb 2025 19:30:24 +0100 Subject: [PATCH] excutor_absolute_path WIP checks for file existence --- Makefile | 5 ++- inc/executor.h | 20 ++++++++++ inc/minishell.h | 3 +- src/executor/executor_absolute_path.c | 53 +++++++++++++++++++++++++++ src/utils/simple_builtins.c | 41 ++++++++++++++++++++- 5 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 inc/executor.h create mode 100644 src/executor/executor_absolute_path.c diff --git a/Makefile b/Makefile index efc03a2..4530439 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: qmennen +#+ # # +#+ # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/02/05 16:02:39 by whaffman ######## odam.nl # +# Updated: 2025/02/08 17:13:50 by willem ######## odam.nl # # # # **************************************************************************** # @@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a OBJ_PATH = obj -VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils +VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) @@ -40,6 +40,7 @@ ifeq ($(UNAME_S),Linux) endif all: $(NAME) + echo $(SOURCES) $(NAME): $(LIBFT) $(OBJECTS) $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) diff --git a/inc/executor.h b/inc/executor.h new file mode 100644 index 0000000..1415487 --- /dev/null +++ b/inc/executor.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor.h :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/08 17:06:07 by willem #+# #+# */ +/* Updated: 2025/02/08 17:15:24 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef EXECUTER_H +# define EXECUTER_H + +# include "minishell.h" + +char *executor_absolute_path(t_list *env, char *cmd); + +#endif // EXECUTER_H \ No newline at end of file diff --git a/inc/minishell.h b/inc/minishell.h index c347bde..9851194 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:28:59 by whaffman ######## odam.nl */ +/* Updated: 2025/02/08 17:15:32 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ # include "environment.h" # include "prompt.h" # include "tokenizer.h" +# include "executor.h" # include "utils.h" # define TRUE 1 diff --git a/src/executor/executor_absolute_path.c b/src/executor/executor_absolute_path.c new file mode 100644 index 0000000..db09808 --- /dev/null +++ b/src/executor/executor_absolute_path.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_absolute_path.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/08 17:00:24 by willem #+# #+# */ +/* Updated: 2025/02/08 19:27:05 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *executor_absolute_path(t_list *env, char *cmd) +{ + char **path; + char *executable; + int i; + + if (cmd[0] == '/' || cmd[0] == '.') + { + if (access(cmd, F_OK) == 0) + { + executable = ft_strdup(cmd); + if (!executable) + return (NULL); + return (executable); + } + return (NULL); + } + path = ft_split(environment_get(env, "PATH")->value, ':'); + if (!path) + return (NULL); + i = 0; + while (path[i] != NULL) + { + executable = malloc(ft_strlen(path[i]) + ft_strlen(cmd) + 2); + if (!executable) + return (ft_free_arr(path), NULL); + ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1); + ft_strlcat(executable, "/", ft_strlen(path[i]) + 2); + ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2); + if (access(executable, F_OK) == 0) + { + ft_free_arr(path); + return (executable); + } + free(executable); + i++; + } + return (ft_free_arr(path), NULL); +} diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c index b979008..624e74f 100644 --- a/src/utils/simple_builtins.c +++ b/src/utils/simple_builtins.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ -/* Updated: 2025/02/08 14:41:19 by willem ######## odam.nl */ +/* Updated: 2025/02/08 19:24:05 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,8 +53,35 @@ static int cmp_value(t_list *list, char *str) return (FALSE); } +void fork_execve(t_minishell *minishell, char *path, char **argv) +{ + pid_t pid; + int status; + + pid = fork(); + if (pid == 0) + { + execve(path, argv, environment_get_arr(minishell->environment)); + while (*argv != NULL) + printf("%s\n", *argv++); + printf("minishell->: %s: %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + else if (pid < 0) + { + printf("minishell: %s\n", strerror(errno)); + } + else + { + free(path); + waitpid(pid, &status, 0); + } +} + void simple_builtins(t_minishell *minishell) { + char *path; + if (cmp_value(minishell->tokens, "clear")) printf("\033[2J\033[1;1H"); else if (cmp_value(minishell->tokens, "env")) @@ -68,4 +95,16 @@ void simple_builtins(t_minishell *minishell) { builtin_export(minishell); } + else + { + path = executor_absolute_path(minishell->environment, ((t_token *)minishell->tokens->content)->value); + if (path == NULL) + printf("minishell: %s: command not found\n", ((t_token *)minishell->tokens->content)->value); + else + { + printf("found excutable: %s\n", path); + fork_execve(minishell, path, ft_split(((t_token *)minishell->tokens->content)->value, ' ')); + + } + } }