From 74564daa521a329581d01fc5b0e93d80d395fe7e Mon Sep 17 00:00:00 2001 From: mcolonna Date: Wed, 1 May 2024 16:28:34 +0200 Subject: [PATCH] libtfise everything --- include/include.h | 4 ++-- src/exec_command.c | 47 ++++++++++++++++++---------------------------- src/path.c | 11 +++++------ 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/include/include.h b/include/include.h index 678e891..13f6b22 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/04/30 15:00:48 by mcolonna ### ########.fr */ +/* Updated: 2024/05/01 17:52:52 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,7 +79,7 @@ void fatal_error_msg(const char *msg); // Get the PATH values. // Return a list of strings ended by NULL. -char **get_path(char *const envp[]); +const char **get_path(const t_memclass mc, char *const envp[]); // Search the program in $PATH. // Returns the path of the program to use, or NULL if there is none. diff --git a/src/exec_command.c b/src/exec_command.c index 6ae9086..e9640da 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,54 +6,43 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/04/30 13:58:04 by mcolonna ### ########.fr */ +/* Updated: 2024/05/01 17:51:49 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" -// Free every element of tab, then tab itself. -static void free_tab(char **tab) -{ - int i; - - i = 0; - while (tab[i] != NULL) - { - free(tab[i]); - i++; - } - free(tab); -} - -static void fork_exec(const char *path, t_call call, const int inout[2], - char *const envp[]) -{ - dup2(inout[0], 0); - dup2(inout[1], 1); - execve(path, call.argv, envp); -} - +// Execute a program with specific stdin and stdout: +// - inout[0] is stdin +// - inout[0] is stdout +// If call.program doesn't start with "./" or "/", find the program in $PATH. +// (TODO) +// If the program wasn't to found in $PATH, or the fork didn't work, +// write the error (TODO) and return the error status. static int execute_call(t_memclass mc, t_call call, const int inout[2], char *const envp[]) { - char **const path = get_path(envp); - pid_t pid; - const char *program_path; + const char **path = get_path(mc, envp); + pid_t pid; + const char *program_path; - program_path = search_path(mc, (const char **)path, call.program); + program_path = search_path(mc, path, call.program); if (!program_path) return (127); pid = fork(); if (pid < 0) return (1); if (pid == 0) - fork_exec(program_path, call, inout, envp); - free_tab(path); + { + dup2(inout[0], 0); + dup2(inout[1], 1); + execve(program_path, call.argv, envp); + } return (0); } // TODO works with only one call (no pipe). +// Execute a command, return the error status. int execute_command(t_memclass mc, t_command command, char *const envp[]) { const int inout[] = {command.input_fd, command.output_fd}; diff --git a/src/path.c b/src/path.c index be44a0e..78f0660 100644 --- a/src/path.c +++ b/src/path.c @@ -6,24 +6,23 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/04/29 15:54:40 by mcolonna ### ########.fr */ +/* Updated: 2024/05/01 17:53:38 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" -char **get_path(char *const envp[]) +const char **get_path(const t_memclass mc, char *const envp[]) { int i; - char **re; + const char **re; char *path; i = 0; while (ft_strncmp(envp[i], "PATH=", 5) != 0) i++; - path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5); - re = ft_split(path, ':'); - free(path); + path = envp[i] + 5; + re = (const char **)str_split(fatal_error, mc, path, ":"); return (re); }