Browse Source

[enh] redirections: use common code for right chevrons.

Moul 4 years ago
parent
commit
7699d47a58
2 changed files with 7 additions and 13 deletions
  1. 6 12
      src/redirect.c
  2. 1 1
      tests.sh

+ 6 - 12
src/redirect.c

@@ -36,8 +36,7 @@ void	parse_redirection(char *str)
 /*
 ** if <: open file and dup it in STDIN, save stdfd
 ** elif <<: run bottow function
-** elif >: open, trunc, save stdfd, dup new fd
-** elif >>: open, append, save stdfd, dup new fd
+** elif > or >>: open, trunc or append, save stdfd, dup new fd in STDIN
 ** if > or >>: dup STDERR or STDOUT in to STDIN
 ** launch pipe
 ** if != <<: close fd, restore stdfd
@@ -57,17 +56,12 @@ void	redirections(char *cmd, char *operator, char *file)
 	}
 	else if (ft_strcmp(operator, "<<") == 0)
 		prompt_redirection(cmd, file);
-	else if (ft_strcmp(operator, ">") == 0 || ft_strcmp(operator, "2>") == 0 \
-			|| ft_strcmp(operator, "1>") == 0)
+	else if (ft_strstr(operator, ">") != NULL)
 	{
-		fd = open(file, O_TRUNC | O_WRONLY | O_CREAT, 0644);
-		std = operator[0] == '2' ? dup(STDERR) : dup(STDOUT);
-		operator[0] == '2' ? dup2(fd, STDERR) : dup2(fd, STDOUT);
-	}
-	else if (ft_strcmp(operator, ">>") == 0 || ft_strcmp(operator, "2>>") == 0 \
-			|| ft_strcmp(operator, "1>>") == 0)
-	{
-		fd = open(file, O_APPEND | O_WRONLY | O_CREAT, 0644);
+		if (ft_strstr(operator, ">>") != NULL)
+			fd = open(file, O_APPEND | O_WRONLY | O_CREAT, 0644);
+		else
+			fd = open(file, O_TRUNC | O_WRONLY | O_CREAT, 0644);
 		std = operator[0] == '2' ? dup(STDERR) : dup(STDOUT);
 		operator[0] == '2' ? dup2(fd, STDERR) : dup2(fd, STDOUT);
 	}

+ 1 - 1
tests.sh

@@ -76,7 +76,7 @@ tests=(
 #"cat << toto\nnrstai\nrsnati\nairnst\ntoto" # double gauche avec autre string de fin
 
 ## Mix
-"ls ; pwd; ls -a | cat -e | wc > $tmpf ; cat $tmpf ; rm $tmpf" # point-virgules, pipes et redirection
+"rm $tmpf ; ls ; pwd; ls -a | cat -e | wc > $tmpf ; cat $tmpf ; rm $tmpf" # point-virgules, pipes et redirection
 "ls -a | cat -e | wc >> $tmpf ; cat $tmpf ; rm $tmpf" # pipes et double redirection
 #"mkdir test ; cd test ; ls -a ; ls | cat | wc -c > fifi ; cat fifi ; rm fifi"
 )