From dbc7bd42046f8ce637b6f80c52c45a950745e072 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 12:15:46 +0900 Subject: [PATCH 01/21] rebase process inside while loop --- src/invoke_cmd/exec_pipeline.c | 106 ++++++++++++++++----------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 9fb79cf..3b2a1dd 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 12:14:57 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)) + { + 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; } } From 7d0f3650ce400539961eead1d497cddf119e24f0 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 12:30:22 +0900 Subject: [PATCH 02/21] add space for other exit status --- inc/execute_cmd.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/execute_cmd.h b/inc/execute_cmd.h index 1e9d0bb..6954eb1 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/28 12:00:45 by dayano ### ########.fr */ +/* Updated: 2025/04/28 12:29:14 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # define PATH_PREFIX_LEN 5 # define EQUAL_LEN 1 + # define CMD_NOT_FOUND_EXIT_STATUS 127 void execute_cmd(t_cmd *cmd, t_minish *minish); From 30a89466b01b988d1d1ea0c55d10743dacd290e5 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 13:57:31 +0900 Subject: [PATCH 03/21] add exit status of cmd failed --- inc/execute_cmd.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/execute_cmd.h b/inc/execute_cmd.h index 6954eb1..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/28 12:29:14 by dayano ### ########.fr */ +/* Updated: 2025/04/28 13:56:33 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ # 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); From c38852aa7d42c973c2e0d8f6fffa071dcdab65a0 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 13:58:59 +0900 Subject: [PATCH 04/21] update execute_cmd --- src/invoke_cmd/execute_cmd.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) 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; } } From 8bdb49739894d60015fa5908d6685dc4db144e43 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 14:13:17 +0900 Subject: [PATCH 05/21] update is_xx --- inc/pipeline_helper.h | 4 ++-- src/invoke_cmd/exec_pipeline.c | 4 ++-- src/invoke_cmd/pipeline_helper.c | 14 ++++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) 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 3b2a1dd..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/28 12:14:57 by dayano ### ########.fr */ +/* Updated: 2025/04/28 14:12:13 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) close(fds1[1]); continue ; } - if (!is_head(cmd)) + if (!is_head(cmd, cmd_head)) { close(STDIN_FILENO); dup2(fds1[0], STDIN_FILENO); 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) From 2070728ade2063188b5131efbfc5fe56b58fe859 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 15:11:12 +0900 Subject: [PATCH 06/21] update redirect_stdout func --- inc/pipeline_helper.h | 3 ++- src/invoke_cmd/pipeline_helper.c | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/inc/pipeline_helper.h b/inc/pipeline_helper.h index 8f26123..d147757 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/28 14:11:40 by dayano ### ########.fr */ +/* Updated: 2025/04/28 14:56:23 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define PIPELINE_HELPER_H # include "cmd.h" +# include # include // pipeline_helper.c diff --git a/src/invoke_cmd/pipeline_helper.c b/src/invoke_cmd/pipeline_helper.c index 0576cc8..58d9b6d 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/28 14:12:08 by dayano ### ########.fr */ +/* Updated: 2025/04/28 15:04:51 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,26 @@ void redirect_stdout(t_cmd *cmd) { - (void)cmd; + int fd; + char *path; + + path = cmd->argv[0]; + close(STDOUT_FILENO); + if (cmd->type == REDIR_OUT) + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (cmd->type == REDIR_APPEND) + fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); + return ; + } + if (fd != 1) + { + dup2(fd, STDOUT_FILENO); + close(fd); + } } bool is_redirect(t_cmd *cmd) From 0ac6a807dd2502c40b5429fdef1eac575b1db6c7 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 15:53:48 +0900 Subject: [PATCH 07/21] add redirect.h --- inc/main.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/main.h b/inc/main.h index d3118a6..f4b9128 100644 --- a/inc/main.h +++ b/inc/main.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */ -/* Updated: 2025/04/28 12:00:56 by dayano ### ########.fr */ +/* Updated: 2025/04/28 15:52:52 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ # include "minish_signal.h" # include "parser.h" # include "pipeline_helper.h" +# include "redirect.h" # include "struct.h" # include "tokenizer.h" # include From 36e2eaeb900bcff6366392e17d2e29d8f3ed36f2 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 21:24:43 +0900 Subject: [PATCH 08/21] update redirect funcs --- Makefile | 7 ++- inc/redirect.h | 22 +++++++ src/invoke_cmd/redirect.c | 119 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 inc/redirect.h create mode 100644 src/invoke_cmd/redirect.c diff --git a/Makefile b/Makefile index fb1cc76..fbe0969 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: ttsubo +#+ +:+ +#+ # +# By: dayano +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/04/03 12:55:20 by ttsubo #+# #+# # -# Updated: 2025/04/26 11:30:48 by ttsubo ### ########.fr # +# Updated: 2025/04/28 16:59:19 by dayano ### ########.fr # # # # **************************************************************************** # @@ -47,7 +47,8 @@ endef # 2.Add the source code when it is added ROOT_SRC = main.c minish_signal.c initialize.c INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \ - execute_cmd_helper.c invoke_command.c pipeline_helper.c + execute_cmd_helper.c invoke_command.c pipeline_helper.c \ + redirect.c TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \ is_quote_closed.c get_token_capa.c is_redirect_validate.c PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c \ diff --git a/inc/redirect.h b/inc/redirect.h new file mode 100644 index 0000000..bf37425 --- /dev/null +++ b/inc/redirect.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/28 15:20:03 by dayano #+# #+# */ +/* Updated: 2025/04/28 15:30:35 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef REDIRECT_H +# define REDIRECT_H + +# include "cmd.h" + +void redirect(t_cmd *cmd); +void redirect_stdout(t_cmd *cmd); +void redirect_stdin(t_cmd *cmd); + +#endif diff --git a/src/invoke_cmd/redirect.c b/src/invoke_cmd/redirect.c new file mode 100644 index 0000000..877cb8a --- /dev/null +++ b/src/invoke_cmd/redirect.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/28 15:19:19 by dayano #+# #+# */ +/* Updated: 2025/04/28 16:57:45 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main.h" + +void redirect_stdout(t_cmd *cmd) +{ + int fd; + char *path; + + path = cmd->argv[0]; + if (cmd->type == REDIR_OUT) + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + else if (cmd->type == REDIR_APPEND) + fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); + else + return ; + if (fd < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); + return ; + } + if (dup2(fd, STDOUT_FILENO) < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); + } + close(fd); +} + +bool is_limiter(int fd, char *limiter) +{ + char *line; + + line = NULL; + line = readline("> "); + if (!line) + { + write(STDOUT_FILENO, "\n", 1); + return (true); + } + if (ft_strcmp(line, limiter) == 0) + { + free(line); + return (true); + } + write(fd, line, ft_strlen(line)); + write(fd, "\n", 1); + free(line); + return (false); +} + +void here_doc(char *limiter) +{ + int pipefd[2]; + + if (pipe(pipefd) < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror("pipe"); + return ; + } + while (!is_limiter(pipefd[1], limiter)) + ; + close(pipefd[1]); + if (dup2(pipefd[0], STDIN_FILENO) < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror("here_doc"); + } + close(pipefd[0]); +} + +void redirect_stdin(t_cmd *cmd) +{ + int fd; + char *path; + + path = cmd->argv[0]; + if (cmd->type == REDIR_HEREDOC) + { + here_doc(cmd->argv[0]); + return ; + } + else if (cmd->type == REDIR_IN) + { + fd = open(path, O_RDONLY); + if (fd < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); + return ; + } + if (dup2(fd, STDIN_FILENO) < 0) + { + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); + } + close(fd); + } +} + +void redirect(t_cmd *cmd) +{ + if (cmd->type == REDIR_IN || cmd->type == REDIR_HEREDOC) + redirect_stdin(cmd); + if (cmd->type == REDIR_OUT || cmd->type == REDIR_APPEND) + redirect_stdout(cmd); +} From f6cc0d776f826b726b46916746a070eede08dcb5 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 21:56:48 +0900 Subject: [PATCH 09/21] fix return value to test execpipe_line function --- inc/invoke_command.h | 3 ++- src/invoke_cmd/invoke_command.c | 6 +++--- src/invoke_cmd/pipeline_helper.c | 28 ++-------------------------- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/inc/invoke_command.h b/inc/invoke_command.h index bba0a58..ce10ffd 100644 --- a/inc/invoke_command.h +++ b/inc/invoke_command.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:21:13 by dayano #+# #+# */ -/* Updated: 2025/04/21 21:00:48 by dayano ### ########.fr */ +/* Updated: 2025/04/28 21:21:30 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,5 +16,6 @@ # include "cmd.h" int invoke_commands(t_cmd *cmd_head, t_minish *minish); +int exec_unit_builtin(t_cmd *cmd_head); #endif diff --git a/src/invoke_cmd/invoke_command.c b/src/invoke_cmd/invoke_command.c index 65d53c6..6375ade 100644 --- a/src/invoke_cmd/invoke_command.c +++ b/src/invoke_cmd/invoke_command.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:19:33 by dayano #+# #+# */ -/* Updated: 2025/04/25 21:05:18 by dayano ### ########.fr */ +/* Updated: 2025/04/28 21:37:53 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ static bool is_unit_builtin(t_cmd *cmd_head) { (void)cmd_head; - return (true); + return (false); } /** @@ -35,7 +35,7 @@ static bool is_unit_builtin(t_cmd *cmd_head) * @param cmd_head * @return int */ -static int exec_unit_builtin(t_cmd *cmd_head) +int exec_unit_builtin(t_cmd *cmd_head) { (void)cmd_head; return (0); diff --git a/src/invoke_cmd/pipeline_helper.c b/src/invoke_cmd/pipeline_helper.c index 58d9b6d..1548f3c 100644 --- a/src/invoke_cmd/pipeline_helper.c +++ b/src/invoke_cmd/pipeline_helper.c @@ -6,36 +6,12 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 14:29:29 by dayano #+# #+# */ -/* Updated: 2025/04/28 15:04:51 by dayano ### ########.fr */ +/* Updated: 2025/04/28 21:07:41 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -void redirect_stdout(t_cmd *cmd) -{ - int fd; - char *path; - - path = cmd->argv[0]; - close(STDOUT_FILENO); - if (cmd->type == REDIR_OUT) - fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (cmd->type == REDIR_APPEND) - fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); - if (fd < 0) - { - ft_putstr_fd("minish: ", STDERR_FILENO); - perror(path); - return ; - } - if (fd != 1) - { - dup2(fd, STDOUT_FILENO); - close(fd); - } -} - bool is_redirect(t_cmd *cmd) { if (cmd->type != REDIR_NONE) @@ -60,5 +36,5 @@ bool is_tail(t_cmd *cmd) bool is_builtin(t_cmd *cmd) { (void)cmd; - return (true); + return (false); } From 9a6b46a3ae188f3669e63e3c52be683f34c43df9 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Mon, 28 Apr 2025 21:57:31 +0900 Subject: [PATCH 10/21] fix execpipe_line --- src/invoke_cmd/exec_pipeline.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 93e4b70..8938d96 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/28 14:12:13 by dayano ### ########.fr */ +/* Updated: 2025/04/28 21:40:44 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,6 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) int fds1[2]; int fds2[2]; - (void)minish; cmd = cmd_head; initialize_fds(fds1, fds2); while (cmd && !is_redirect(cmd)) @@ -62,11 +61,11 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) close(fds1[0]); if (fds1[1] != -1) close(fds1[1]); + cmd = cmd->next; continue ; } if (!is_head(cmd, cmd_head)) { - close(STDIN_FILENO); dup2(fds1[0], STDIN_FILENO); close(fds1[0]); close(fds1[1]); @@ -74,20 +73,21 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) 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); + redirect(cmd->next); } - if (!is_builtin(cmd)) + if (is_builtin(cmd)) { - execute_cmd(cmd, minish); - exit(EXIT_FAILURE); + exec_unit_builtin(cmd); + exit(EXIT_SUCCESS); } - cmd = cmd->next; + execute_cmd(cmd, minish); + cmd->status = EXIT_FAILURE; + exit(EXIT_FAILURE); } } From e5cd850c30df98c4e2473b544a980b0dd55e3837 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 11:49:02 +0900 Subject: [PATCH 11/21] add lib to use waitpid in wait_pipeline func --- inc/exec_pipeline.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/exec_pipeline.h b/inc/exec_pipeline.h index 6793f29..9b50019 100644 --- a/inc/exec_pipeline.h +++ b/inc/exec_pipeline.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:12:27 by dayano #+# #+# */ -/* Updated: 2025/04/24 14:17:43 by dayano ### ########.fr */ +/* Updated: 2025/04/29 11:48:05 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ # define EXEC_PIPELINE_H # include "cmd.h" +# include +# include void exec_pipeline(t_cmd *cmd_head, t_minish *minish); int wait_pipeline(t_cmd *cmd_head); From b1ff58e303189fdca421815f26cadba612801505 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 12:00:52 +0900 Subject: [PATCH 12/21] update wait_pipeline --- src/invoke_cmd/exec_pipeline.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 8938d96..66e6daf 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/28 21:40:44 by dayano ### ########.fr */ +/* Updated: 2025/04/29 11:47:57 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -93,6 +93,26 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) int wait_pipeline(t_cmd *cmd_head) { - (void)cmd_head; - return (0); + int last_status; + int status; + t_cmd *cmd; + + last_status = 0; + cmd = cmd_head; + while (cmd) + { + if (!is_builtin(cmd) && !is_redirect(cmd)) + { + waitpid(cmd->pid, &status, NULL); + if (WIFEXITED(status)) + cmd->status = WEXITSTATUS(status); + else + cmd->status = EXIT_FAILURE; + last_status = cmd->status; + } + if (is_builtin(cmd)) + last_status = cmd->status; + cmd = cmd->next; + } + return (last_status); } From 7f53ead9a064827bf2432bc3f10126b3061c70dc Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 12:07:50 +0900 Subject: [PATCH 13/21] fix exit status --- src/main.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 2ffbdef..419fd4f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */ -/* Updated: 2025/04/28 12:01:09 by dayano ### ########.fr */ +/* Updated: 2025/04/29 12:07:02 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,13 +39,12 @@ static void destroy_minish(t_minish *minish) * @return true * @return false */ -static bool prompt(char *program_name, t_minish *minish, int *status) +static bool prompt(char *program_name, t_minish *minish) { char *line; char **tokens; t_cmd **cmds; - (void)status; line = readline("minish>"); if (!line) return (false); @@ -60,7 +59,7 @@ static bool prompt(char *program_name, t_minish *minish, int *status) return (false); } if (cmds[0]->argc > 0) - *status = invoke_commands(cmds[0], minish); + minish->last_status = invoke_commands(cmds[0], minish); _free_tokens(tokens); free_cmds(cmds, cmds_len(cmds)); free(line); @@ -71,14 +70,13 @@ int main(int argc, char **argv, char **envp) { t_minish *minish; char *program_name; - int exit_status; (void)argc; program_name = argv[0]; minish = initialize(envp); minish_signal(); - while (prompt(program_name, minish, &exit_status)) + while (prompt(program_name, minish)) ; destroy_minish(minish); - return (exit_status); + return (minish->last_status); } From 33173f2386f80b9022a3acca66e1d28627229c80 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 12:52:05 +0900 Subject: [PATCH 14/21] update wait_pipeline --- src/invoke_cmd/exec_pipeline.c | 60 ++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 66e6daf..f1fa505 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/29 11:47:57 by dayano ### ########.fr */ +/* Updated: 2025/04/29 12:51:12 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,27 +91,61 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) } } +/** + * @brief Wait for one command’s process to finish. + * + * This function calls waitpid() for cmd->pid, stores the exit + * or signal status into cmd->status, and returns that status. + * + * @param cmd The command whose child process to wait for. + * @return The exit status or 128+signal number on signal, + or EXIT_FAILURE on error. + */ +static int _get_status(t_cmd *cmd) +{ + int ret; + int status; + + ret = 0; + ret = waitpid(cmd->pid, &status, 0); + if (ret < 0) + { + cmd->status = EXIT_FAILURE; + return (cmd->status); + } + if (WIFEXITED(status)) + cmd->status = WEXITSTATUS(status); + else if (WIFSIGNALED(status)) + cmd->status = 128 + WTERMSIG(status); + else + cmd->status = EXIT_FAILURE; + return (cmd->status); +} + +/** + * @brief Wait for all child processes in the pipeline to finish. + * + * This function walks through each command in the pipeline, + * calls waitpid() for every non-redirect node, stores each + * child’s exit or signal status into cmd->status, and then + * returns the status of the last command executed. + * + * @param cmd_head Pointer to the head of the command list. + * @return The exit status of the last command in the pipeline. + */ int wait_pipeline(t_cmd *cmd_head) { int last_status; - int status; + int ret; t_cmd *cmd; last_status = 0; + ret = 0; cmd = cmd_head; while (cmd) { - if (!is_builtin(cmd) && !is_redirect(cmd)) - { - waitpid(cmd->pid, &status, NULL); - if (WIFEXITED(status)) - cmd->status = WEXITSTATUS(status); - else - cmd->status = EXIT_FAILURE; - last_status = cmd->status; - } - if (is_builtin(cmd)) - last_status = cmd->status; + if (!is_redirect(cmd)) + last_status = _get_status(cmd); cmd = cmd->next; } return (last_status); From 54aba45aa90e08352c790a598a223aab0b12ae0c Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 13:29:40 +0900 Subject: [PATCH 15/21] add print error --- inc/main.h | 4 +++- src/error.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/error.c diff --git a/inc/main.h b/inc/main.h index f4b9128..ddba4a3 100644 --- a/inc/main.h +++ b/inc/main.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */ -/* Updated: 2025/04/28 15:52:52 by dayano ### ########.fr */ +/* Updated: 2025/04/29 13:25:47 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,11 +30,13 @@ # include "redirect.h" # include "struct.h" # include "tokenizer.h" +# include # include # include # include # include # include +# include # include #endif diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..7a5279e --- /dev/null +++ b/src/error.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/29 13:16:23 by dayano #+# #+# */ +/* Updated: 2025/04/29 13:29:14 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main.h" + +/** + * @brief print error + * format: minish: : + * + * @param cmd_or_path + */ +void print_error(const char *cmd_or_path) +{ + char *error_msg; + + error_msg = strerror(errno); + ft_putstr_fd("minish: ", STDERR_FILENO); + ft_putstr_fd(cmd_or_path, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + ft_putendl_fd(error_msg, STDERR_FILENO); +} From 41641bca1779abd031879cc9375e3ca251e5ba54 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 13:34:24 +0900 Subject: [PATCH 16/21] add lib to use waitpid --- inc/main.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/main.h b/inc/main.h index ddba4a3..0ea8a0a 100644 --- a/inc/main.h +++ b/inc/main.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */ -/* Updated: 2025/04/29 13:25:47 by dayano ### ########.fr */ +/* Updated: 2025/04/29 13:31:04 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,8 @@ # include # include # include +# include +# include # include #endif From 6e34ab26bc9db45816fdb0f39a46d67a17252524 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 13:57:37 +0900 Subject: [PATCH 17/21] add error.h --- inc/error.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 inc/error.h diff --git a/inc/error.h b/inc/error.h new file mode 100644 index 0000000..cab3d89 --- /dev/null +++ b/inc/error.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/29 13:48:47 by dayano #+# #+# */ +/* Updated: 2025/04/29 13:49:23 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ERROR_H +# define ERROR_H + +void print_error(const char *cmd_or_path); + +#endif From ed34a05dca876377fc041135e4e32439c992ca15 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 14:36:45 +0900 Subject: [PATCH 18/21] add print_error --- Makefile | 4 ++-- inc/error.h | 4 ++-- inc/main.h | 4 +++- src/error.c | 4 ++-- src/invoke_cmd/exec_pipeline.c | 30 +++++++++++++++--------------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index fbe0969..8daa656 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: dayano +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/04/03 12:55:20 by ttsubo #+# #+# # -# Updated: 2025/04/28 16:59:19 by dayano ### ########.fr # +# Updated: 2025/04/29 14:33:30 by dayano ### ########.fr # # # # **************************************************************************** # @@ -45,7 +45,7 @@ define define_rule endef # 2.Add the source code when it is added -ROOT_SRC = main.c minish_signal.c initialize.c +ROOT_SRC = main.c minish_signal.c initialize.c error.c INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \ execute_cmd_helper.c invoke_command.c pipeline_helper.c \ redirect.c diff --git a/inc/error.h b/inc/error.h index cab3d89..ba2e68d 100644 --- a/inc/error.h +++ b/inc/error.h @@ -6,13 +6,13 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/29 13:48:47 by dayano #+# #+# */ -/* Updated: 2025/04/29 13:49:23 by dayano ### ########.fr */ +/* Updated: 2025/04/29 14:35:12 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef ERROR_H # define ERROR_H -void print_error(const char *cmd_or_path); +void print_error(char *cmd_or_path); #endif diff --git a/inc/main.h b/inc/main.h index 0ea8a0a..86714df 100644 --- a/inc/main.h +++ b/inc/main.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */ -/* Updated: 2025/04/29 13:31:04 by dayano ### ########.fr */ +/* Updated: 2025/04/29 13:55:06 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ # include "create_envp.h" # include "env_utils.h" # include "env_utils_2.h" +# include "error.h" # include "exec_pipeline.h" # include "execute_cmd.h" # include "initialize.h" @@ -39,6 +40,7 @@ # include # include # include +# include # include #endif diff --git a/src/error.c b/src/error.c index 7a5279e..f764645 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/29 13:16:23 by dayano #+# #+# */ -/* Updated: 2025/04/29 13:29:14 by dayano ### ########.fr */ +/* Updated: 2025/04/29 14:33:57 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ * * @param cmd_or_path */ -void print_error(const char *cmd_or_path) +void print_error(char *cmd_or_path) { char *error_msg; diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index f1fa505..2d51651 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/29 12:51:12 by dayano ### ########.fr */ +/* Updated: 2025/04/29 14:05:04 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,14 @@ void initialize_fds(int fds1[2], int fds2[2]) fds2[1] = -1; } +void cleanup_fds(int fds1[2]) +{ + if (fds1[0] != -1) + close(fds1[0]); + if (fds1[1] != -1) + close(fds1[1]); +} + /** * @brief old execute_cmd function * execvp(cmd->argv[0], cmd->argv); @@ -41,26 +49,20 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) { fds1[0] = fds2[0]; fds1[1] = fds2[1]; - if (!is_tail(cmd)) + if (!is_tail(cmd) && pipe(fds2) < 0) { - if (pipe(fds2) < 0) - { - perror("pipe"); - exit(3); - } + print_error(cmd->argv[0]); + exit(EX_OSERR); } cmd->pid = fork(); if (cmd->pid < 0) { - perror("fork"); - exit(3); + print_error(cmd->argv[0]); + exit(EX_OSERR); } if (cmd->pid > 0) { - if (fds1[0] != -1) - close(fds1[0]); - if (fds1[1] != -1) - close(fds1[1]); + cleanup_fds(fds1); cmd = cmd->next; continue ; } @@ -77,9 +79,7 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) close(fds2[1]); } if ((cmd->next != NULL) && is_redirect(cmd->next)) - { redirect(cmd->next); - } if (is_builtin(cmd)) { exec_unit_builtin(cmd); From b1625142e8ea6adc5ccc1094bf5e85f8ff3367c6 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 15:32:53 +0900 Subject: [PATCH 19/21] change pid type: int -> pid_t --- inc/cmd.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/cmd.h b/inc/cmd.h index 4320694..36f6e88 100644 --- a/inc/cmd.h +++ b/inc/cmd.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cmd.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ttsubo +#+ +:+ +#+ */ +/* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/13 13:47:36 by ttsubo #+# #+# */ -/* Updated: 2025/04/14 21:30:15 by ttsubo ### ########.fr */ +/* Updated: 2025/04/29 15:24:31 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,8 +29,8 @@ typedef struct s_cmd char **argv; int capa; int status; - int pid; + pid_t pid; struct s_cmd *next; } t_cmd; -#endif \ No newline at end of file +#endif From 07220a77f4613392ca6e47ee50a8a61f4e8f3a68 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 16:39:26 +0900 Subject: [PATCH 20/21] fix files and codes to be under the norm --- inc/cmd.h | 3 +- inc/exec_pipeline.h | 10 ++- inc/main.h | 2 +- src/invoke_cmd/exec_pipeline.c | 144 ++++++++++++++++++++------------- 4 files changed, 99 insertions(+), 60 deletions(-) diff --git a/inc/cmd.h b/inc/cmd.h index 36f6e88..1f0e6bd 100644 --- a/inc/cmd.h +++ b/inc/cmd.h @@ -6,13 +6,14 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/13 13:47:36 by ttsubo #+# #+# */ -/* Updated: 2025/04/29 15:24:31 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:09:43 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CMD_H # define CMD_H +# include typedef enum e_redir_type { REDIR_NONE, diff --git a/inc/exec_pipeline.h b/inc/exec_pipeline.h index 9b50019..e964098 100644 --- a/inc/exec_pipeline.h +++ b/inc/exec_pipeline.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:12:27 by dayano #+# #+# */ -/* Updated: 2025/04/29 11:48:05 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:11:28 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,12 @@ # define EXEC_PIPELINE_H # include "cmd.h" -# include -# include + +typedef struct s_pipe_io +{ + int prev_fds[2]; + int curr_fds[2]; +} t_pipe_io; void exec_pipeline(t_cmd *cmd_head, t_minish *minish); int wait_pipeline(t_cmd *cmd_head); diff --git a/inc/main.h b/inc/main.h index 86714df..fd50d89 100644 --- a/inc/main.h +++ b/inc/main.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */ -/* Updated: 2025/04/29 13:55:06 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:09:27 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 2d51651..dcebd22 100644 --- a/src/invoke_cmd/exec_pipeline.c +++ b/src/invoke_cmd/exec_pipeline.c @@ -6,18 +6,16 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:14:15 by dayano #+# #+# */ -/* Updated: 2025/04/29 14:05:04 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:36:09 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -void initialize_fds(int fds1[2], int fds2[2]) +void init_fds(int fds[2]) { - fds1[0] = -1; - fds1[1] = -1; - fds2[0] = -1; - fds2[1] = -1; + fds[0] = -1; + fds[1] = -1; } void cleanup_fds(int fds1[2]) @@ -28,66 +26,102 @@ void cleanup_fds(int fds1[2]) close(fds1[1]); } +void swap_fds(int prev_fds[2], int curr_fds[2]) +{ + prev_fds[0] = curr_fds[0]; + prev_fds[1] = curr_fds[1]; +} + +void create_pipe(bool need, int fds[2], t_cmd *cmd) +{ + if (!need) + { + fds[0] = -1; + fds[1] = -1; + return ; + } + if (pipe(fds) < 0) + { + print_error(cmd->argv[0]); + exit(EX_OSERR); + } +} + +void setup_stdin(t_cmd *cmd, t_cmd *cmd_head, int prev_fds[2]) +{ + if (is_head(cmd, cmd_head) || prev_fds[0] < 0) + return ; + if (dup2(prev_fds[0], STDIN_FILENO) < 0) + { + print_error(cmd->argv[0]); + exit(EX_OSERR); + } + close(prev_fds[0]); + close(prev_fds[1]); +} + +void setup_stdout(t_cmd *cmd, int curr_fds[2]) +{ + if (!is_tail(cmd) || curr_fds[1] >= 0) + { + close(curr_fds[0]); + if (dup2(curr_fds[1], STDOUT_FILENO) < 0) + { + print_error(cmd->argv[0]); + exit(EX_OSERR); + } + close(curr_fds[1]); + } +} + +pid_t fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds, + t_minish *minish) +{ + pid_t pid; + + pid = fork(); + if (pid < 0) + { + print_error(cmd->argv[0]); + exit(EX_OSERR); + } + if (pid > 0) + return (pid); + setup_stdin(cmd, cmd_head, pipefds->prev_fds); + setup_stdout(cmd, pipefds->curr_fds); + if ((cmd->next != NULL) && is_redirect(cmd->next)) + redirect(cmd->next); + if (is_builtin(cmd)) + exit(exec_unit_builtin(cmd)); + execute_cmd(cmd, minish); + print_error(cmd->argv[0]); + exit(EXIT_FAILURE); +} + /** - * @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 */ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) { - t_cmd *cmd; - int fds1[2]; - int fds2[2]; + t_cmd *cmd; + t_pipe_io pipefds; + bool need_pipe; cmd = cmd_head; - initialize_fds(fds1, fds2); + init_fds(pipefds.prev_fds); + init_fds(pipefds.curr_fds); while (cmd && !is_redirect(cmd)) { - fds1[0] = fds2[0]; - fds1[1] = fds2[1]; - if (!is_tail(cmd) && pipe(fds2) < 0) - { - print_error(cmd->argv[0]); - exit(EX_OSERR); - } - cmd->pid = fork(); - if (cmd->pid < 0) - { - print_error(cmd->argv[0]); - exit(EX_OSERR); - } - if (cmd->pid > 0) - { - cleanup_fds(fds1); - cmd = cmd->next; - continue ; - } - if (!is_head(cmd, cmd_head)) - { - dup2(fds1[0], STDIN_FILENO); - close(fds1[0]); - close(fds1[1]); - } - if (!is_tail(cmd)) - { - close(fds2[0]); - dup2(fds2[1], STDOUT_FILENO); - close(fds2[1]); - } - if ((cmd->next != NULL) && is_redirect(cmd->next)) - redirect(cmd->next); - if (is_builtin(cmd)) - { - exec_unit_builtin(cmd); - exit(EXIT_SUCCESS); - } - execute_cmd(cmd, minish); - cmd->status = EXIT_FAILURE; - exit(EXIT_FAILURE); + need_pipe = !is_tail(cmd); + create_pipe(need_pipe, pipefds.curr_fds, cmd); + cmd->pid = fork_command(cmd, cmd_head, &pipefds, minish); + cleanup_fds(pipefds.prev_fds); + swap_fds(pipefds.prev_fds, pipefds.curr_fds); + init_fds(pipefds.curr_fds); + cmd = cmd->next; } } From e47a8e6d41e706a4f602c630efff961c893ff371 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Tue, 29 Apr 2025 16:54:49 +0900 Subject: [PATCH 21/21] fix under the norm --- Makefile | 4 +- inc/cmd.h | 3 +- inc/invoke_command.h | 16 ++++- src/invoke_cmd/exec_pipeline.c | 118 ++++----------------------------- src/invoke_cmd/fds.c | 48 ++++++++++++++ src/invoke_cmd/wait_pipeline.c | 73 ++++++++++++++++++++ 6 files changed, 151 insertions(+), 111 deletions(-) create mode 100644 src/invoke_cmd/fds.c create mode 100644 src/invoke_cmd/wait_pipeline.c diff --git a/Makefile b/Makefile index 8daa656..d39a39c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: dayano +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/04/03 12:55:20 by ttsubo #+# #+# # -# Updated: 2025/04/29 14:33:30 by dayano ### ########.fr # +# Updated: 2025/04/29 16:48:58 by dayano ### ########.fr # # # # **************************************************************************** # @@ -48,7 +48,7 @@ endef ROOT_SRC = main.c minish_signal.c initialize.c error.c INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \ execute_cmd_helper.c invoke_command.c pipeline_helper.c \ - redirect.c + redirect.c fds.c wait_pipeline.c TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \ is_quote_closed.c get_token_capa.c is_redirect_validate.c PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c \ diff --git a/inc/cmd.h b/inc/cmd.h index 1f0e6bd..d998913 100644 --- a/inc/cmd.h +++ b/inc/cmd.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/13 13:47:36 by ttsubo #+# #+# */ -/* Updated: 2025/04/29 16:09:43 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:53:33 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define CMD_H # include + typedef enum e_redir_type { REDIR_NONE, diff --git a/inc/invoke_command.h b/inc/invoke_command.h index ce10ffd..6275686 100644 --- a/inc/invoke_command.h +++ b/inc/invoke_command.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:21:13 by dayano #+# #+# */ -/* Updated: 2025/04/28 21:21:30 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:47:04 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,18 @@ # define INVOKE_COMMAND_H # include "cmd.h" +# include -int invoke_commands(t_cmd *cmd_head, t_minish *minish); -int exec_unit_builtin(t_cmd *cmd_head); +int invoke_commands(t_cmd *cmd_head, t_minish *minish); +int exec_unit_builtin(t_cmd *cmd_head); + +// wait_pipeline.c +int wait_pipeline(t_cmd *cmd_head); + +// fds.c +void init_fds(int fds[2]); +void cleanup_fds(int fds1[2]); +void swap_fds(int prev_fds[2], int curr_fds[2]); +void create_pipe(bool need, int fds[2], t_cmd *cmd); #endif diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index dcebd22..f6c1dbd 100644 --- a/src/invoke_cmd/exec_pipeline.c +++ b/src/invoke_cmd/exec_pipeline.c @@ -6,48 +6,13 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:14:15 by dayano #+# #+# */ -/* Updated: 2025/04/29 16:36:09 by dayano ### ########.fr */ +/* Updated: 2025/04/29 16:52:01 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -void init_fds(int fds[2]) -{ - fds[0] = -1; - fds[1] = -1; -} - -void cleanup_fds(int fds1[2]) -{ - if (fds1[0] != -1) - close(fds1[0]); - if (fds1[1] != -1) - close(fds1[1]); -} - -void swap_fds(int prev_fds[2], int curr_fds[2]) -{ - prev_fds[0] = curr_fds[0]; - prev_fds[1] = curr_fds[1]; -} - -void create_pipe(bool need, int fds[2], t_cmd *cmd) -{ - if (!need) - { - fds[0] = -1; - fds[1] = -1; - return ; - } - if (pipe(fds) < 0) - { - print_error(cmd->argv[0]); - exit(EX_OSERR); - } -} - -void setup_stdin(t_cmd *cmd, t_cmd *cmd_head, int prev_fds[2]) +static void _setup_stdin(t_cmd *cmd, t_cmd *cmd_head, int prev_fds[2]) { if (is_head(cmd, cmd_head) || prev_fds[0] < 0) return ; @@ -60,7 +25,7 @@ void setup_stdin(t_cmd *cmd, t_cmd *cmd_head, int prev_fds[2]) close(prev_fds[1]); } -void setup_stdout(t_cmd *cmd, int curr_fds[2]) +static void _setup_stdout(t_cmd *cmd, int curr_fds[2]) { if (!is_tail(cmd) || curr_fds[1] >= 0) { @@ -74,7 +39,7 @@ void setup_stdout(t_cmd *cmd, int curr_fds[2]) } } -pid_t fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds, +static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds, t_minish *minish) { pid_t pid; @@ -87,8 +52,8 @@ pid_t fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds, } if (pid > 0) return (pid); - setup_stdin(cmd, cmd_head, pipefds->prev_fds); - setup_stdout(cmd, pipefds->curr_fds); + _setup_stdin(cmd, cmd_head, pipefds->prev_fds); + _setup_stdout(cmd, pipefds->curr_fds); if ((cmd->next != NULL) && is_redirect(cmd->next)) redirect(cmd->next); if (is_builtin(cmd)) @@ -99,10 +64,13 @@ pid_t fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds, } /** - * @brief + * @brief Execute a pipeline of commands. * - * @param cmd_head - * @param minish + * For each command, create a pipe if needed, fork a child, redirect its + * stdin/stdout through the pipes, and record its PID for waiting. + * + * @param cmd_head First command in the pipeline. + * @param minish Shell context for built-ins and execution. */ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) { @@ -117,70 +85,10 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish) { need_pipe = !is_tail(cmd); create_pipe(need_pipe, pipefds.curr_fds, cmd); - cmd->pid = fork_command(cmd, cmd_head, &pipefds, minish); + cmd->pid = _fork_command(cmd, cmd_head, &pipefds, minish); cleanup_fds(pipefds.prev_fds); swap_fds(pipefds.prev_fds, pipefds.curr_fds); init_fds(pipefds.curr_fds); cmd = cmd->next; } } - -/** - * @brief Wait for one command’s process to finish. - * - * This function calls waitpid() for cmd->pid, stores the exit - * or signal status into cmd->status, and returns that status. - * - * @param cmd The command whose child process to wait for. - * @return The exit status or 128+signal number on signal, - or EXIT_FAILURE on error. - */ -static int _get_status(t_cmd *cmd) -{ - int ret; - int status; - - ret = 0; - ret = waitpid(cmd->pid, &status, 0); - if (ret < 0) - { - cmd->status = EXIT_FAILURE; - return (cmd->status); - } - if (WIFEXITED(status)) - cmd->status = WEXITSTATUS(status); - else if (WIFSIGNALED(status)) - cmd->status = 128 + WTERMSIG(status); - else - cmd->status = EXIT_FAILURE; - return (cmd->status); -} - -/** - * @brief Wait for all child processes in the pipeline to finish. - * - * This function walks through each command in the pipeline, - * calls waitpid() for every non-redirect node, stores each - * child’s exit or signal status into cmd->status, and then - * returns the status of the last command executed. - * - * @param cmd_head Pointer to the head of the command list. - * @return The exit status of the last command in the pipeline. - */ -int wait_pipeline(t_cmd *cmd_head) -{ - int last_status; - int ret; - t_cmd *cmd; - - last_status = 0; - ret = 0; - cmd = cmd_head; - while (cmd) - { - if (!is_redirect(cmd)) - last_status = _get_status(cmd); - cmd = cmd->next; - } - return (last_status); -} diff --git a/src/invoke_cmd/fds.c b/src/invoke_cmd/fds.c new file mode 100644 index 0000000..70dc718 --- /dev/null +++ b/src/invoke_cmd/fds.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/29 16:45:58 by dayano #+# #+# */ +/* Updated: 2025/04/29 16:46:03 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main.h" + +void init_fds(int fds[2]) +{ + fds[0] = -1; + fds[1] = -1; +} + +void cleanup_fds(int fds1[2]) +{ + if (fds1[0] != -1) + close(fds1[0]); + if (fds1[1] != -1) + close(fds1[1]); +} + +void swap_fds(int prev_fds[2], int curr_fds[2]) +{ + prev_fds[0] = curr_fds[0]; + prev_fds[1] = curr_fds[1]; +} + +void create_pipe(bool need, int fds[2], t_cmd *cmd) +{ + if (!need) + { + fds[0] = -1; + fds[1] = -1; + return ; + } + if (pipe(fds) < 0) + { + print_error(cmd->argv[0]); + exit(EX_OSERR); + } +} diff --git a/src/invoke_cmd/wait_pipeline.c b/src/invoke_cmd/wait_pipeline.c new file mode 100644 index 0000000..88c7226 --- /dev/null +++ b/src/invoke_cmd/wait_pipeline.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* wait_pipeline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/29 16:42:14 by dayano #+# #+# */ +/* Updated: 2025/04/29 16:42:50 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main.h" + +/** + * @brief Wait for one command’s process to finish. + * + * This function calls waitpid() for cmd->pid, stores the exit + * or signal status into cmd->status, and returns that status. + * + * @param cmd The command whose child process to wait for. + * @return The exit status or 128+signal number on signal, + or EXIT_FAILURE on error. + */ +static int _get_status(t_cmd *cmd) +{ + int ret; + int status; + + ret = 0; + ret = waitpid(cmd->pid, &status, 0); + if (ret < 0) + { + cmd->status = EXIT_FAILURE; + return (cmd->status); + } + if (WIFEXITED(status)) + cmd->status = WEXITSTATUS(status); + else if (WIFSIGNALED(status)) + cmd->status = 128 + WTERMSIG(status); + else + cmd->status = EXIT_FAILURE; + return (cmd->status); +} + +/** + * @brief Wait for all child processes in the pipeline to finish. + * + * This function walks through each command in the pipeline, + * calls waitpid() for every non-redirect node, stores each + * child’s exit or signal status into cmd->status, and then + * returns the status of the last command executed. + * + * @param cmd_head Pointer to the head of the command list. + * @return The exit status of the last command in the pipeline. + */ +int wait_pipeline(t_cmd *cmd_head) +{ + int last_status; + int ret; + t_cmd *cmd; + + last_status = 0; + ret = 0; + cmd = cmd_head; + while (cmd) + { + if (!is_redirect(cmd)) + last_status = _get_status(cmd); + cmd = cmd->next; + } + return (last_status); +}