From 795ad81851cef811f1bd32f40cf02e81a4b8e281 Mon Sep 17 00:00:00 2001 From: kinou-p Date: Thu, 10 Mar 2022 11:50:14 +0100 Subject: [PATCH] simple redirection work and double output --- includes/minishell.h | 3 ++- srcs/main.c | 4 ++-- srcs/pipe/pipe.c | 10 ++++++---- srcs/set_redirection/redirection.c | 17 ++++++++++++++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 5b41f9e..8ea81ab 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/09 22:33:49 by apommier #+# #+# */ -/* Updated: 2022/03/10 00:17:00 by apommier ### ########.fr */ +/* Updated: 2022/03/10 11:39:08 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,5 +73,6 @@ void exit_shell(t_cmd *cmd); //redirection.c set redirection and input good char *set_redirection(t_s_cmd *cmd, char *line); +char next_space(char *str, int i); #endif \ No newline at end of file diff --git a/srcs/main.c b/srcs/main.c index e6d0881..1df680d 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ -/* Updated: 2022/03/10 00:16:05 by apommier ### ########.fr */ +/* Updated: 2022/03/10 11:07:08 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ void print_prompt(char **path) free(input); exit_shell(cmd); } - if (ft_strlen(input)) + if (ft_strlen(input) && next_space(input, 0)) { add_history(input); cmd = set_cmd(input, path); diff --git a/srcs/pipe/pipe.c b/srcs/pipe/pipe.c index 56f2742..ab0bdb5 100644 --- a/srcs/pipe/pipe.c +++ b/srcs/pipe/pipe.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/07 11:13:32 by apommier #+# #+# */ -/* Updated: 2022/03/09 20:43:42 by apommier ### ########.fr */ +/* Updated: 2022/03/10 11:38:53 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,8 @@ void execute(t_cmd *cmd, char **env) i = 0; if (cmd->current_s_cmd->infile)//set the initial input - fdin = open(cmd->current_s_cmd->infile, O_RDONLY); + fdin = open(cmd->current_s_cmd->infile, O_APPEND); + //fdin = open(cmd->current_s_cmd->infile, O_RDONLY); //else if (cmd->infile) // fdin = open(cmd->infile, O_RDONLY); else @@ -34,7 +35,8 @@ void execute(t_cmd *cmd, char **env) //if (i) // cmd->current_s_cmd++; if (i != 0 && cmd->current_s_cmd->infile) - fdin = open(cmd->current_s_cmd->infile, O_RDONLY); + fdin = open(cmd->current_s_cmd->infile, O_CREAT | O_APPEND); + //fdin = open(cmd->current_s_cmd->infile, O_RDONLY); //redirect input dup2(fdin, 0); close(fdin); @@ -44,7 +46,7 @@ void execute(t_cmd *cmd, char **env) { // Last simple command if (cmd->current_s_cmd->outfile) - fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); + fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666); else if(cmd->outfile) fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); else// Use default output diff --git a/srcs/set_redirection/redirection.c b/srcs/set_redirection/redirection.c index 953d108..58a9565 100644 --- a/srcs/set_redirection/redirection.c +++ b/srcs/set_redirection/redirection.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/09 15:18:58 by apommier #+# #+# */ -/* Updated: 2022/03/10 02:40:46 by apommier ### ########.fr */ +/* Updated: 2022/03/10 11:47:21 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -156,6 +156,17 @@ char *ft_output(char *line, t_s_cmd *cmd, int index) return (line); } +void set_file(char *file) +{ + int fd; + + fd = open(file, O_CREAT | O_TRUNC); + if (fd) + close(fd); + if (fd == -1) + error_redirect(); +} + char *set_redirection(t_s_cmd *cmd, char *line) { int i; @@ -167,11 +178,15 @@ char *set_redirection(t_s_cmd *cmd, char *line) if(line[i] == '<') { line = ft_input(line, cmd, i); + //if (cmd->in_type == 1) + // set_file(cmd->infile); i = 0; } else if(line[i] == '>') { line = ft_output(line, cmd, i); + if (cmd->in_type == 0) + set_file(cmd->outfile); i = 0; } else