commit b171d3ab2267f3e5b8eee797d19c4d63a0fb608d
parent 3780d162acf6e42670502a6c668c1ddb6618944f
Author: night0721 <[email protected]>
Date: Fri, 9 Feb 2024 11:27:13 +0000
fix pipes
Diffstat:
2 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/commands.c b/commands.c
@@ -206,21 +206,21 @@ int execute_pipe(char ***args) {
int i = 0;
int in = 0;
- int num_pipes = 0;
- while (args[num_pipes] != NULL) {
- num_pipes++;
+ int num_cmds = 0;
+ while (args[num_cmds] != NULL) {
+ num_cmds++;
}
-
- for (i = 0; i < num_pipes; i++) {
+
+ for (int i = 0; i < num_cmds - 1; i++) {
pipe(pipefd);
if ((pid = fork()) == 0) {
dup2(in, 0); // change input according to the old one
- if (i < num_pipes - 1) {
- dup2(pipefd[1], 1); // change output according to the new one
+ if (i < num_cmds - 1) {
+ dup2(pipefd[1], 1); // change output to the input of the next command
}
close(pipefd[0]);
- execvp(args[i][0], args[i]);
- exit(EXIT_FAILURE);
+ status = execute(args[i]);
+ exit(status);
} else if (pid < 0) {
perror("fork failed");
}
@@ -230,18 +230,21 @@ int execute_pipe(char ***args) {
if ((pid = fork()) == 0) {
dup2(in, 0);
- execvp(args[i][0], args[i]);
- exit(EXIT_FAILURE);
+ status = execute(args[num_cmds - 1]);
+ exit(status);
} else if (pid < 0) {
perror("fork failed");
}
- close(pipefd[0]);
- close(pipefd[1]);
-
- for (i = 0; i <= num_pipes; i++) {
+ close(in);
+ for (int i = 0; i < num_cmds; i++) {
wait(&status);
}
+
+
+
+
+
return 1;
}
diff --git a/rush.c b/rush.c
@@ -390,7 +390,7 @@ char **modifyargs(char **args) {
while (args[num_arg] != NULL) {
num_arg++;
}
- // makes ls and diff have color without user typing it
+ // makes ls and diff and grep have color without user typing it
if (strncmp(args[0], "ls", 2) == 0 || strncmp(args[0], "diff", 4) == 0 || strncmp(args[0], "grep", 4) == 0) {
args[num_arg] = "--color=auto";
num_arg++;