diff --git a/inc/execute_cmd.h b/inc/execute_cmd.h index d57e26d..da773ac 100644 --- a/inc/execute_cmd.h +++ b/inc/execute_cmd.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 21:39:18 by dayano #+# #+# */ -/* Updated: 2025/04/25 20:46:25 by dayano ### ########.fr */ +/* Updated: 2025/04/28 13:56:33 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ # define PATH_PREFIX_LEN 5 # define EQUAL_LEN 1 + +# define CMD_FAILED_EXIT_STATUS 126 # define CMD_NOT_FOUND_EXIT_STATUS 127 void execute_cmd(t_cmd *cmd, t_minish *minish); @@ -25,4 +27,6 @@ char *get_path_line(char **envp); void free_str_array(char **str); void print_cmd_not_found(t_cmd *cmd); +void execute_cmd(t_cmd *cmd, t_minish *minish); + #endif diff --git a/inc/main.h b/inc/main.h index 14f4b7e..d3118a6 100644 --- a/inc/main.h +++ b/inc/main.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ttsubo +#+ +:+ +#+ */ +/* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */ -/* Updated: 2025/04/27 16:28:37 by ttsubo ### ########.fr */ +/* Updated: 2025/04/28 12:00:56 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/inc/pipeline_helper.h b/inc/pipeline_helper.h index 774f3e8..8f26123 100644 --- a/inc/pipeline_helper.h +++ b/inc/pipeline_helper.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 14:32:27 by dayano #+# #+# */ -/* Updated: 2025/04/24 15:02:17 by dayano ### ########.fr */ +/* Updated: 2025/04/28 14:11:40 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ // pipeline_helper.c void redirect_stdout(t_cmd *cmd); bool is_redirect(t_cmd *cmd); -bool is_head(t_cmd *cmd); +bool is_head(t_cmd *cmd, t_cmd *cmd_head); bool is_tail(t_cmd *cmd); bool is_builtin(t_cmd *cmd); diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 9fb79cf..93e4b70 100644 --- a/src/invoke_cmd/exec_pipeline.c +++ b/src/invoke_cmd/exec_pipeline.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:14:15 by dayano #+# #+# */ -/* Updated: 2025/04/25 21:16:09 by dayano ### ########.fr */ +/* Updated: 2025/04/28 14:12:13 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,62 +21,14 @@ void initialize_fds(int fds1[2], int fds2[2]) } /** - * @brief // execvp(cmd->argv[0], cmd->argv); - // fprintf(stderr, "%s: command not found: %s\n", program_name, - // cmd->argv[0]); + * @brief old execute_cmd function + * execvp(cmd->argv[0], cmd->argv); + fprintf(stderr, "%s: command not found: %s\n", program_name, + cmd->argv[0]); * @brief * @param cmd_head * @param minish */ -// note : process in while loop of inside -// fds1[0] = fds2[0]; -// fds1[1] = fds2[1]; -// if (!is_tail(cmd)) -// { -// if (pipe(fds2) < 0) -// { -// perror("pipe"); -// exit(3); -// } -// } -// cmd->pid = fork(); -// if (cmd->pid < 0) -// { -// perror("fork"); -// exit(3); -// } -// if (cmd->pid > 0) -// { -// if (fds1[0] != -1) -// close(fds1[0]); -// if (fds1[1] != -1) -// close(fds1[1]); -// continue ; -// } -// if (!is_head(cmd)) -// { -// close(STDIN_FILENO); -// dup2(fds1[0], STDIN_FILENO); -// close(fds1[0]); -// close(fds1[1]); -// } -// if (!is_tail(cmd)) -// { -// close(fds2[0]); -// close(STDOUT_FILENO); -// dup2(fds2[1], STDOUT_FILENO); -// close(fds2[1]); -// } -// if ((cmd->next != NULL) && is_redirect(cmd->next)) -// { -// redirect_stdout(cmd->next); -// } -// if (!is_builtin(cmd)) -// { -// execute_cmd(cmd, minish); -// exit(EXIT_FAILURE); -// } -// cmd = cmd->next; void exec_pipeline(t_cmd *cmd_head, t_minish *minish) { t_cmd *cmd; @@ -88,6 +40,54 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) initialize_fds(fds1, fds2); while (cmd && !is_redirect(cmd)) { + fds1[0] = fds2[0]; + fds1[1] = fds2[1]; + if (!is_tail(cmd)) + { + if (pipe(fds2) < 0) + { + perror("pipe"); + exit(3); + } + } + cmd->pid = fork(); + if (cmd->pid < 0) + { + perror("fork"); + exit(3); + } + if (cmd->pid > 0) + { + if (fds1[0] != -1) + close(fds1[0]); + if (fds1[1] != -1) + close(fds1[1]); + continue ; + } + if (!is_head(cmd, cmd_head)) + { + close(STDIN_FILENO); + dup2(fds1[0], STDIN_FILENO); + close(fds1[0]); + close(fds1[1]); + } + if (!is_tail(cmd)) + { + close(fds2[0]); + close(STDOUT_FILENO); + dup2(fds2[1], STDOUT_FILENO); + close(fds2[1]); + } + if ((cmd->next != NULL) && is_redirect(cmd->next)) + { + redirect_stdout(cmd->next); + } + if (!is_builtin(cmd)) + { + execute_cmd(cmd, minish); + exit(EXIT_FAILURE); + } + cmd = cmd->next; } } diff --git a/src/invoke_cmd/execute_cmd.c b/src/invoke_cmd/execute_cmd.c index 8d3f4ca..aeac624 100644 --- a/src/invoke_cmd/execute_cmd.c +++ b/src/invoke_cmd/execute_cmd.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 21:25:41 by dayano #+# #+# */ -/* Updated: 2025/04/25 21:17:14 by dayano ### ########.fr */ +/* Updated: 2025/04/28 13:56:43 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,8 @@ char *get_cmd_path(char *cmd, char **envp) char **path_set; char *full_path; + if (!cmd && ft_strlen(cmd) == 0) + return (NULL); if (access(cmd, X_OK) == 0) return (cmd); path_line = get_path_line(envp); @@ -69,19 +71,14 @@ void execute_cmd(t_cmd *cmd, t_minish *minish) print_cmd_not_found(cmd); return ; } - if (!cmd->argv[0] || ft_strlen(cmd->argv[0]) == 0) - { - ft_putstr_fd("Command '' not found\n", STDERR_FILENO); - exit(127); - } if (!is_pathname(cmd->argv[0], envp, &fullpath)) { - ft_putstr_fd("Command '' not found\n", STDERR_FILENO); - exit(127); + print_cmd_not_found(cmd); + return ; } if (execve(fullpath, cmd->argv, envp) < 0) { free(fullpath); - perror("execve"); + cmd->status = CMD_FAILED_EXIT_STATUS; } } diff --git a/src/invoke_cmd/pipeline_helper.c b/src/invoke_cmd/pipeline_helper.c index 2381e68..0576cc8 100644 --- a/src/invoke_cmd/pipeline_helper.c +++ b/src/invoke_cmd/pipeline_helper.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 14:29:29 by dayano #+# #+# */ -/* Updated: 2025/04/24 14:57:00 by dayano ### ########.fr */ +/* Updated: 2025/04/28 14:12:08 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,16 +24,18 @@ bool is_redirect(t_cmd *cmd) return (false); } -bool is_head(t_cmd *cmd) +bool is_head(t_cmd *cmd, t_cmd *cmd_head) { - (void)cmd; - return (true); + if (cmd == cmd_head) + return (true); + return (false); } bool is_tail(t_cmd *cmd) { - (void)cmd; - return (true); + if (cmd->next == NULL || is_redirect(cmd->next)) + return (true); + return (false); } bool is_builtin(t_cmd *cmd) diff --git a/src/invoke_cmd/pipeline_helper_2.c b/src/invoke_cmd/pipeline_helper_2.c deleted file mode 100644 index 72e332f..0000000 --- a/src/invoke_cmd/pipeline_helper_2.c +++ /dev/null @@ -1,64 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* pipeline_helper_2.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/24 14:52:38 by dayano #+# #+# */ -/* Updated: 2025/04/24 14:59:55 by dayano ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "main.h" - -void perror_exit(char *msg) -{ - perror(msg); - exit(EXIT_FAILURE); -} - -void perror_exit_status(char *msg, int status) -{ - perror(msg); - exit(status); -} - -char *get_path_line(char **envp) -{ - int i; - - i = 0; - while (envp[i]) - { - if (ft_strncmp(envp[i], "PATH=", PATH_PREFIX_LEN) == 0) - return (envp[i] + PATH_PREFIX_LEN); - i++; - } - return (NULL); -} - -char *join_path(char *dir, char *cmd) -{ - char *full_path; - int len_dir; - int len_cmd; - - if (dir[ft_strlen(dir) - 1] == '/') - { - full_path = ft_strjoin(dir, cmd); - if (!full_path) - return (NULL); - return (full_path); - } - len_dir = ft_strlen(dir); - len_cmd = ft_strlen(cmd); - full_path = malloc(len_dir + len_cmd + 2); - if (!full_path) - return (NULL); - ft_strlcpy(full_path, dir, len_dir + 1); - full_path[len_dir] = '/'; - full_path[len_dir + 1] = '\0'; - ft_strlcpy(full_path + len_dir + 1, cmd, len_cmd + 1); - return (full_path); -} diff --git a/src/main.c b/src/main.c index a06e97d..2ffbdef 100644 --- a/src/main.c +++ b/src/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ttsubo +#+ +:+ +#+ */ +/* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */ -/* Updated: 2025/04/28 11:55:41 by ttsubo ### ########.fr */ +/* Updated: 2025/04/28 12:01:09 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ static bool prompt(char *program_name, t_minish *minish, int *status) char **tokens; t_cmd **cmds; - (void) status; + (void)status; line = readline("minish>"); if (!line) return (false);