From c28da8841fbeaa8887f3e3659c511247daa5add3 Mon Sep 17 00:00:00 2001 From: Akhmed Dovletov Date: Mon, 20 May 2024 20:54:03 +0200 Subject: [PATCH] Been there done that --- include/pipex.h | 20 +++++++++++--- src/ak_pipe.c | 8 +++--- src/ak_pipeout.c | 10 +++---- src/clear_exit.c | 16 +++++------ src/cmd_exe.c | 60 +++++++++++++++++++++--------------------- src/dr_here.c | 10 +++---- src/seek_and_execute.c | 8 +++--- 7 files changed, 72 insertions(+), 60 deletions(-) diff --git a/include/pipex.h b/include/pipex.h index 9c12f5b..8c6dd37 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/20 23:07:37 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 19:23:08 by akdovlet ### ########.fr */ +/* Updated: 2024/05/20 18:21:05 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ # define PIPEX_H # include "libft.h" -# include "get_next_line.h" # include # include # include @@ -59,6 +58,14 @@ void ak_pipeout(t_data *data, int i); /******************************cmd_exe.c**************************************/ // Will execute the command and return the correct exit code void cmd_exe(t_data *data); +/*if command is provided with a path such as "/usr/bin/cat" +check if it exits, if it's executable and then send it traight to execve*/ +void path_run(char *cmd, t_data *data); +/*If no path is given, find its path with access() and + then send it to nopath_exec()*/ +void nopath_run(char *cmd, t_data *data); +/*Function that checks execution rights and send the command to execve*/ +void nopath_exec(char *cmd, t_data *data); /******************************dr_here.c**************************************/ // Creates a pipe and forks, calls dr_dre which reads @@ -66,6 +73,7 @@ void cmd_exe(t_data *data); void dr_here(t_data *data); // Forgot about Dre void dr_dre(t_data *data, int *fd); +// looks for the delimiter by excluding the '\n' character at the end bool delimiter_cmp(char *s1, char *s2); /********************************env_access.c**********************************/ @@ -77,17 +85,21 @@ char **get_path_from_env(char **env); bool file_access(char *file, int check); /******************************open_infile.c***********************************/ +/* Precise setup. If the infile isn't open to communicate, we will open +/dev/null which is a file that contains EOF, as to not break a command by +sending it an invalid fd. This is to mimic the behavior of bash*/ void infile_setup(t_data *data, char **av); int infile_check(char *file); /*****************************open_outfile.c***********************************/ +/* Open differently in case here_doc */ void open_outfile(t_data *data); /********************************free_exit.c***********************************/ +// Calls clear_all and exits with the right exit code +void clear_all_exit(t_data *data, int exit_code); // Frees everything void clear_all(t_data *data); -// Calls clear_all and exits with the right exit code -void clear_exit(t_data *data, int exit_code); /******************************px_split.c**************************************/ // ft_split with a twist: adds '/' add the end of each word diff --git a/src/ak_pipe.c b/src/ak_pipe.c index f2b8657..92e7222 100644 --- a/src/ak_pipe.c +++ b/src/ak_pipe.c @@ -19,10 +19,10 @@ void ak_pipe(t_data *data, int i) if (data->infile == -1 && i == 0) return ; if (pipe(fd) == -1) - return (perror("pipe"), clear_exit(data, EXIT_FAILURE)); + return (perror("pipe"), clear_all_exit(data, EXIT_FAILURE)); data->pid_array[i] = fork(); if (data->pid_array[i] < 0) - return (perror("fork"), clear_exit(data, EXIT_FAILURE)); + return (perror("fork"), clear_all_exit(data, EXIT_FAILURE)); if (data->pid_array[i] == CHILD) child(fd, data); close(fd[1]); @@ -34,10 +34,10 @@ void child(int fd[2], t_data *data) { close(fd[0]); if (dup2(data->hermes, STDIN_FILENO) == -1) - return (perror("dup2"), clear_exit(data, EXIT_FAILURE)); + return (perror("dup2"), clear_all_exit(data, EXIT_FAILURE)); close(data->hermes); if (dup2(fd[1], STDOUT_FILENO) == -1) - return (perror("dup2"), clear_exit(data, EXIT_FAILURE)); + return (perror("dup2"), clear_all_exit(data, EXIT_FAILURE)); close(fd[1]); cmd_exe(data); } diff --git a/src/ak_pipeout.c b/src/ak_pipeout.c index 9bf7e85..49ea68b 100644 --- a/src/ak_pipeout.c +++ b/src/ak_pipeout.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 03:05:49 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 19:16:14 by akdovlet ### ########.fr */ +/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,10 @@ void ak_pipeout(t_data *data, int i) status = 0; open_outfile(data); if (data->outfile < 0) - return (perror(data->outfile_name), clear_exit(data, EXIT_FAILURE)); + return (perror(data->outfile_name), clear_all_exit(data, EXIT_FAILURE)); data->pid_array[i] = fork(); if (data->pid_array[i] < 0) - return (perror("fork"), clear_exit(data, EXIT_FAILURE)); + return (perror("fork"), clear_all_exit(data, EXIT_FAILURE)); if (data->pid_array[i] == CHILD) child_out(data); close(data->hermes); @@ -40,10 +40,10 @@ void ak_pipeout(t_data *data, int i) void child_out(t_data *data) { if (dup2(data->hermes, STDIN_FILENO) == -1) - clear_exit(data, EXIT_FAILURE); + clear_all_exit(data, EXIT_FAILURE); close(data->hermes); if (dup2(data->outfile, STDOUT_FILENO) == -1) - clear_exit(data, EXIT_FAILURE); + clear_all_exit(data, EXIT_FAILURE); close(data->outfile); cmd_exe(data); } diff --git a/src/clear_exit.c b/src/clear_exit.c index acc7331..b585d28 100644 --- a/src/clear_exit.c +++ b/src/clear_exit.c @@ -5,13 +5,19 @@ /* +:+ +:+ +:+ */ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/07 00:41:28 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 19:14:45 by akdovlet ### ########.fr */ +/* Created: 2024/05/20 18:19:12 by akdovlet #+# #+# */ +/* Updated: 2024/05/20 18:19:20 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" +void clear_all_exit(t_data *data, int exit_code) +{ + clear_all(data); + exit(exit_code); +} + void clear_all(t_data *data) { ft_free(data->path); @@ -27,9 +33,3 @@ void clear_all(t_data *data) if (data->outfile > 0) close(data->outfile); } - -void clear_exit(t_data *data, int exit_code) -{ - clear_all(data); - exit(exit_code); -} diff --git a/src/cmd_exe.c b/src/cmd_exe.c index e06d6ce..4db44f5 100644 --- a/src/cmd_exe.c +++ b/src/cmd_exe.c @@ -6,22 +6,39 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/18 22:39:03 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 19:37:34 by akdovlet ### ########.fr */ +/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -void nopath_exec(char *cmd, t_data *data) +void cmd_exe(t_data *data) { - if (!file_access(cmd, X_OK)) + if (!data->cmd[0]) + { + ft_dprintf(STDERR, ERR_CMD, data->cmd[0], strerror(errno)); + clear_all_exit(data, 127); + } + if (ft_strchr(data->cmd[0], '/')) + path_run(data->cmd[0], data); + else + nopath_run(data->cmd[0], data); +} + +void path_run(char *cmd, t_data *data) +{ + if (!file_access(cmd, F_OK)) { ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); - clear_exit(data, 126); + clear_all_exit(data, 127); + } + else if (!file_access(cmd, X_OK)) + { + ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); + clear_all_exit(data, 126); } execve(cmd, data->cmd, data->env); - ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); - clear_exit(data, 127); + clear_all_exit(data, 127); } void nopath_run(char *cmd, t_data *data) @@ -35,41 +52,24 @@ void nopath_run(char *cmd, t_data *data) { full_path = ft_strjoin(data->path[i], cmd); if (!full_path) - return (clear_exit(data, EXIT_FAILURE)); + return (clear_all_exit(data, EXIT_FAILURE)); if (file_access(full_path, F_OK)) nopath_exec(full_path, data); free(full_path); i++; } ft_dprintf(STDERR, ERR_CMD, data->cmd[0]); - clear_exit(data, 127); + clear_all_exit(data, 127); } -void path_run(char *cmd, t_data *data) +void nopath_exec(char *cmd, t_data *data) { - if (!file_access(cmd, F_OK)) - { - ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); - clear_exit(data, 127); - } - else if (!file_access(cmd, X_OK)) + if (!file_access(cmd, X_OK)) { ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); - clear_exit(data, 126); + clear_all_exit(data, 126); } execve(cmd, data->cmd, data->env); - clear_exit(data, 127); -} - -void cmd_exe(t_data *data) -{ - if (!data->cmd[0]) - { - ft_dprintf(STDERR, ERR_CMD, data->cmd[0], strerror(errno)); - clear_exit(data, 127); - } - if (ft_strchr(data->cmd[0], '/')) - path_run(data->cmd[0], data); - else - nopath_run(data->cmd[0], data); + ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); + clear_all_exit(data, 127); } diff --git a/src/dr_here.c b/src/dr_here.c index 8157d60..7cd3b94 100644 --- a/src/dr_here.c +++ b/src/dr_here.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 00:17:44 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 19:04:32 by akdovlet ### ########.fr */ +/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,10 +19,10 @@ void dr_here(t_data *data) pid_t pid; if (pipe(fd) == -1) - clear_exit(data, EXIT_FAILURE); + clear_all_exit(data, EXIT_FAILURE); pid = fork(); if (pid == -1) - clear_exit(data, EXIT_FAILURE); + clear_all_exit(data, EXIT_FAILURE); if (pid == CHILD) dr_dre(data, fd); close(fd[1]); @@ -41,7 +41,7 @@ void dr_dre(t_data *data, int *fd) write(STDOUT_FILENO, "> ", 2); line = get_next_line(STDIN_FILENO); if (!line) - clear_exit(data, EXIT_FAILURE); + clear_all_exit(data, EXIT_FAILURE); if (delimiter_cmp(data->here_doc_delimiter, line)) { free(line); @@ -51,7 +51,7 @@ void dr_dre(t_data *data, int *fd) free(line); } close(fd[1]); - clear_exit(data, EXIT_SUCCESS); + clear_all_exit(data, EXIT_SUCCESS); } bool delimiter_cmp(char *s1, char *s2) diff --git a/src/seek_and_execute.c b/src/seek_and_execute.c index 08768cd..f62037f 100644 --- a/src/seek_and_execute.c +++ b/src/seek_and_execute.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/06 23:52:25 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 19:05:11 by akdovlet ### ########.fr */ +/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,20 +19,20 @@ void seek_and_execute(t_data *data) i = 0; data->pid_array = malloc(sizeof(pid_t) * data->cmd_count); if (!data->pid_array) - return (clear_exit(data, EXIT_FAILURE)); + return (clear_all_exit(data, EXIT_FAILURE)); if (data->here_doc_delimiter) dr_here(data); while (i < data->cmd_count - 1) { data->cmd = ft_multi_split(data->commands[i], " '"); if (!data->cmd) - return (clear_exit(data, EXIT_FAILURE)); + return (clear_all_exit(data, EXIT_FAILURE)); ak_pipe(data, i); ft_free(data->cmd); i++; } data->cmd = ft_multi_split(data->commands[i], " '"); if (!data->cmd) - return (clear_exit(data, EXIT_FAILURE)); + return (clear_all_exit(data, EXIT_FAILURE)); ak_pipeout(data, i); }