diff --git a/include/include.h b/include/include.h index 92e9074..2109a54 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/25 18:04:46 by mcolonna ### ########.fr */ +/* Updated: 2024/04/26 12:27:17 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,8 @@ t_command parse_command(const t_memclass mc, const char *command); ///// EXECUTE COMMAND ///// // Execute the command given. Return the exit status. -int execute_command(t_memclass mc, t_command command, char *const envp[]); +int execute_command(t_memclass mc, t_command command, + char *const envp[]); ///// ERROR ///// @@ -78,6 +79,6 @@ char **get_path(char *const envp[]); // Search the program in PATH. // Returns the path of the program to use, or NULL if there is none. const char *search_path( - const t_memclass mc, const char **path, const char *prog); + const t_memclass mc, const char **path, const char *prog); #endif diff --git a/minishell b/minishell deleted file mode 100755 index 5638437..0000000 Binary files a/minishell and /dev/null differ diff --git a/src/exec_command.c b/src/exec_command.c index a9b46c7..1af5a6b 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/04/25 18:14:03 by mcolonna ### ########.fr */ +/* Updated: 2024/04/26 12:26:56 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,30 +28,37 @@ static void free_tab(char **tab) // TODO works with only one call (no pipe). // TODO is t_call.argv ended by NULL? -static void fork_exec(const char *path, t_command command, char *const envp[]) +static void fork_exec(const char *path, t_call call, const int inout[2], + char *const envp[]) { - if (command.input_fd != 0) - dup2(command.input_fd, 0); - if (command.output_fd != 1) - dup2(command.output_fd, 1); - execve(path, command.calls[0].argv, envp); + dup2(inout[0], 0); + dup2(inout[1], 1); + execve(path, call.argv, envp); } -// TODO works with only one call (no pipe). -int execute_command(t_memclass mc, t_command command, char *const envp[]) +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 *program_path; - program_path = search_path(mc, (const char **)path, command.calls[0].program); + program_path = search_path(mc, (const char **)path, call.program); if (!program_path) return (127); pid = fork(); if (pid < 0) return (1); if (pid == 0) - fork_exec(program_path, command, envp); + fork_exec(program_path, call, inout, envp); free_tab(path); return (0); } + +// TODO works with only one call (no pipe). +int execute_command(t_memclass mc, t_command command, char *const envp[]) +{ + const int inout[] = {0, 1}; + + return (execute_call(mc, command.calls[0], inout, envp)); +} diff --git a/src/main.c b/src/main.c index d8f80a7..0b5483d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/04/25 18:14:53 by mcolonna ### ########.fr */ +/* Updated: 2024/04/26 12:24:58 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,10 +46,10 @@ static void print_hi(void) int main(const int argc, const char *argv[], char *const envp[]) { char **const path = get_path(envp); - t_memclass mc; - const char *command_str; - t_command command; - int errorstatus; + t_memclass mc; + const char *command_str; + t_command command; + int errorstatus; (void)argc; (void)argv; diff --git a/src/path.c b/src/path.c index fde3885..8283163 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/04/25 18:03:49 by mcolonna ### ########.fr */ +/* Updated: 2024/04/26 12:24:41 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,8 @@ char **get_path(char *const envp[]) { int i; - char **re; - char *path; + char **re; + char *path; i = 0; while (ft_strncmp(envp[i], "PATH=", 5) != 0)