Browse Source

[enh] handle double redirections on a specialized f().

Moul 2 years ago
parent
commit
715df09b95
3 changed files with 35 additions and 1 deletions
  1. 29 0
      src/pipe_exec.c
  2. 5 1
      src/pipe_parsing.c
  3. 1 0
      src/sh.h

+ 29 - 0
src/pipe_exec.c

@@ -83,6 +83,35 @@ void	inside_pipe(t_e *e, t_pipe *p, t_pipe_redir *t)
 }
 
 /*
+** Special function to handle double redirections of fd and execution fork
+** fork, open left and right redirections
+** execute cmd, close fd, father wait for end of son fork
+*/
+
+void	launch_double_redirections(t_e *e, t_pipe *p, t_pipe_redir **t)
+{
+	int		fd;
+
+	if (ft_strcmp(t[0]->operator, "<<") == 0)
+	{
+		prompt_redirection(e, p, t[0]);
+		return ;
+	}
+	else if (fork() == 0)
+	{
+		fd = open(t[0]->otxt, O_RDONLY);
+		close(0);
+		dup(fd);
+		open_right_redirections(e, p, t[1]);
+		manage_cmd_2(e, cmd_splitting(e, t[0]->itxt, ' '));
+		close(STDOUT);
+		close(fd);
+		exit(0);
+	}
+	wait(NULL);
+}
+
+/*
 ** check every cmd, every cote type
 ** if block starts and ends with same cote:
 ** cut first and last char

+ 5 - 1
src/pipe_parsing.c

@@ -42,7 +42,11 @@ void	pipe_init_2(t_e *e, t_pipe *p, char *all_cmd)
 	pipe_parsing(p, e);
 	t = (t_pipe_redir**)malloc(sizeof(t_pipe_redir*) * (p->nbr + 2));
 	fill_pipe_redir_data(e, p, t);
-	launch_pipes(e, p, t);
+	if (ft_strchr(t[0]->operator, '<') != NULL && t[1] != NULL && \
+			ft_strchr(t[1]->operator, '>') != NULL)
+		launch_double_redirections(e, p, t);
+	else
+		launch_pipes(e, p, t);
 }
 
 /*

+ 1 - 0
src/sh.h

@@ -54,6 +54,7 @@ void	parse_and(char *str, t_pipe_redir *t, int file_pos);
 void	launch_pipes(t_e *e, t_pipe *p, t_pipe_redir **t);
 void	handle_multi_piping(t_e *e, t_pipe *p, t_pipe_redir *t);
 void	inside_pipe(t_e *e, t_pipe *p, t_pipe_redir *t);
+void	launch_double_redirections(t_e *e, t_pipe *p, t_pipe_redir **t);
 void	remove_cotes(char **cmd);
 /*
 ** redirect.c