From 951b043dc87acba1ae7cf4461661eaaa7831a559 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 26 Feb 2024 16:38:14 +0100 Subject: [PATCH] working on file redirections --- srcs/exec.c | 59 +++++++++++++++++++++++++++++++++++++----------- srcs/minishell.h | 4 +++- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/srcs/exec.c b/srcs/exec.c index cc4b570..d69680e 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/02/26 14:52:54 by marde-vr ### ########.fr */ +/* Updated: 2024/02/26 16:33:16 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -143,14 +143,31 @@ int is_fd_open(int fd) // debug void redirect_input(t_msh *msh, int i) { - if (dup2(msh->fds[i - 1][0], 0) < 0) - ft_exit(msh, 1); + if (msh->in_type != PIPE) + { + if (dup2(msh->in_fd, 0) < 0) + ft_exit(msh, 1); + close(msh->in_fd); + } + else + { + if (dup2(msh->fds[i - 1][0], 0) < 0) + ft_exit(msh, 1); + } } void redirect_output(t_msh *msh, int i) { - if (dup2(msh->fds[i][1], 1) < 0) - ft_exit(msh, 1); + if (msh->out_type != PIPE) + { + if (dup2(msh->out_fd, 1) < 0) + ft_exit(msh, 1); + } + else + { + if (dup2(msh->fds[i][1], 1) < 0) + ft_exit(msh, 1); + } } void pipe_child(t_msh *msh, char **cmd_args, int i) @@ -165,8 +182,6 @@ void pipe_child(t_msh *msh, char **cmd_args, int i) //ft_printf_fd(2, "redirecting output\n"); redirect_output(msh, i); } - - if (i != 0) { if (msh->fds[i - 1][0] > 2) @@ -178,8 +193,6 @@ void pipe_child(t_msh *msh, char **cmd_args, int i) close(msh->fds[i][0]); if (msh->fds[i][1] > 2) close(msh->fds[i][1]); - - if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd") || !ft_strcmp(msh->cmds->token, "alias") || !ft_strcmp(msh->cmds->token, "unalias") @@ -269,18 +282,34 @@ void remove_command_from_msh(t_msh *msh) t_cmd *cmd_tmp; cmd_cur = msh->cmds; + msh->out_type = ARG; while (cmd_cur && cmd_cur->next) { - if (cmd_cur->type != ARG) + //if (cmd_cur->type != ARG) + while (cmd_cur->type != ARG) { + //remove cmd if not PIPE + if (cmd_cur->type == PIPE) + { + cmd_tmp = cmd_cur; + cmd_cur = cmd_cur->next; + msh->in_type = cmd_tmp->type; + free(cmd_tmp->token); + free(cmd_tmp); + msh->cmds = cmd_cur; + return ; + } cmd_tmp = cmd_cur; cmd_cur = cmd_cur->next; - msh->in_type = cmd_tmp->type; + msh->out_type = cmd_cur->type; free(cmd_tmp->token); free(cmd_tmp); msh->cmds = cmd_cur; - return ; } + if (msh->out_type == RED_O) + msh->out_fd = open(cmd_cur->token, O_CREAT | O_RDWR | O_TRUNC, 0644); + if (msh->out_type == RED_O_APP) + msh->out_fd = open(cmd_cur->token, O_CREAT | O_RDWR | O_APPEND, 0644); cmd_tmp = cmd_cur; cmd_cur = cmd_cur->next; free(cmd_tmp->token); @@ -299,8 +328,11 @@ void get_out_type(t_msh *msh) if (!cur_cmd->type) msh->out_type = ARG; else + { + //cur_cmd->token msh->out_type = cur_cmd->type; - //ft_printf_fd(2, "%d\n", msh->out_type); + } + ft_printf_fd(2, "type: %d\n", msh->out_type); } void exec_command(t_msh *msh) @@ -318,6 +350,7 @@ void exec_command(t_msh *msh) ft_exit(msh, 1); while (i < cmd_count) { + ft_printf_fd(2, "%s\n", msh->cmds->token); msh->fds[i] = ft_calloc(2, sizeof(int *)); if (!msh->fds[i]) ft_exit(msh, 1); diff --git a/srcs/minishell.h b/srcs/minishell.h index dc54ecd..8436486 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/02/25 13:10:20 by marde-vr ### ########.fr */ +/* Updated: 2024/02/26 15:59:49 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,8 @@ typedef struct s_msh int *pids; enum e_token_type in_type; enum e_token_type out_type; + int in_fd; + int out_fd; } t_msh; extern int g_return_code;