From f4bf733040f2832d730f111a3a689636cf6dfbca Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 8 Feb 2025 19:30:24 +0100 Subject: [PATCH 1/5] 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, ' ')); + + } + } } From 6f21f114293ca7bf105360b124141fc57c3964c5 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sun, 9 Feb 2025 16:04:57 +0100 Subject: [PATCH 2/5] display exit status of the last command --- src/utils/simple_builtins.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c index 624e74f..eabd2c1 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 19:24:05 by willem ######## odam.nl */ +/* Updated: 2025/02/09 11:17:51 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,7 +74,9 @@ void fork_execve(t_minishell *minishell, char *path, char **argv) else { free(path); + ft_free_arr(argv); waitpid(pid, &status, 0); + printf("exit status of pid(%d): %d\n", pid, (((status) & 0xff00) >> 8)); } } From 4c6f5998d4a9cd346b7e8d4aec0d21a0d0a84859 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 8 Feb 2025 19:46:45 +0100 Subject: [PATCH 3/5] print executable path --- inc/minishell.h | 2 +- src/utils/simple_builtins.c | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/inc/minishell.h b/inc/minishell.h index 9851194..7ef896f 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/08 17:15:32 by willem ######## odam.nl */ +/* Updated: 2025/02/09 16:16:32 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c index eabd2c1..3996825 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/09 11:17:51 by willem ######## odam.nl */ +/* Updated: 2025/02/09 16:18:22 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -83,7 +83,7 @@ void fork_execve(t_minishell *minishell, char *path, char **argv) 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")) @@ -94,9 +94,7 @@ void simple_builtins(t_minishell *minishell) exit(EXIT_SUCCESS); } else if (cmp_value(minishell->tokens, "export")) - { builtin_export(minishell); - } else { path = executor_absolute_path(minishell->environment, ((t_token *)minishell->tokens->content)->value); @@ -105,8 +103,8 @@ void simple_builtins(t_minishell *minishell) else { printf("found excutable: %s\n", path); - fork_execve(minishell, path, ft_split(((t_token *)minishell->tokens->content)->value, ' ')); - + free(path); } } + } From e9a1db9cac8d99a36d9a5c66d0916ba59ba5d235 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 8 Feb 2025 19:30:24 +0100 Subject: [PATCH 4/5] excutor_absolute_path WIP checks for file existence --- inc/minishell.h | 2 +- src/utils/simple_builtins.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/minishell.h b/inc/minishell.h index 7ef896f..3dd180d 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/09 16:16:32 by willem ######## odam.nl */ +/* Updated: 2025/02/09 16:18:44 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c index 3996825..e1f7a09 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/09 16:18:22 by willem ######## odam.nl */ +/* Updated: 2025/02/09 16:19:10 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ From 7cad14f636d93120d58f3cfb64b77e5d2bf97265 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sun, 9 Feb 2025 16:04:57 +0100 Subject: [PATCH 5/5] display exit status of the last command --- src/utils/simple_builtins.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c index e1f7a09..60b1948 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/09 16:19:10 by willem ######## odam.nl */ +/* Updated: 2025/02/09 16:20:04 by willem ######## odam.nl */ /* */ /* ************************************************************************** */