fix pipes
This commit is contained in:
parent
3780d162ac
commit
b171d3ab22
2 changed files with 19 additions and 16 deletions
33
commands.c
33
commands.c
|
@ -206,21 +206,21 @@ int execute_pipe(char ***args) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int in = 0;
|
int in = 0;
|
||||||
|
|
||||||
int num_pipes = 0;
|
int num_cmds = 0;
|
||||||
while (args[num_pipes] != NULL) {
|
while (args[num_cmds] != NULL) {
|
||||||
num_pipes++;
|
num_cmds++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < num_pipes; i++) {
|
for (int i = 0; i < num_cmds - 1; i++) {
|
||||||
pipe(pipefd);
|
pipe(pipefd);
|
||||||
if ((pid = fork()) == 0) {
|
if ((pid = fork()) == 0) {
|
||||||
dup2(in, 0); // change input according to the old one
|
dup2(in, 0); // change input according to the old one
|
||||||
if (i < num_pipes - 1) {
|
if (i < num_cmds - 1) {
|
||||||
dup2(pipefd[1], 1); // change output according to the new one
|
dup2(pipefd[1], 1); // change output to the input of the next command
|
||||||
}
|
}
|
||||||
close(pipefd[0]);
|
close(pipefd[0]);
|
||||||
execvp(args[i][0], args[i]);
|
status = execute(args[i]);
|
||||||
exit(EXIT_FAILURE);
|
exit(status);
|
||||||
} else if (pid < 0) {
|
} else if (pid < 0) {
|
||||||
perror("fork failed");
|
perror("fork failed");
|
||||||
}
|
}
|
||||||
|
@ -230,18 +230,21 @@ int execute_pipe(char ***args) {
|
||||||
|
|
||||||
if ((pid = fork()) == 0) {
|
if ((pid = fork()) == 0) {
|
||||||
dup2(in, 0);
|
dup2(in, 0);
|
||||||
execvp(args[i][0], args[i]);
|
status = execute(args[num_cmds - 1]);
|
||||||
exit(EXIT_FAILURE);
|
exit(status);
|
||||||
} else if (pid < 0) {
|
} else if (pid < 0) {
|
||||||
perror("fork failed");
|
perror("fork failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
close(pipefd[0]);
|
close(in);
|
||||||
close(pipefd[1]);
|
for (int i = 0; i < num_cmds; i++) {
|
||||||
|
|
||||||
for (i = 0; i <= num_pipes; i++) {
|
|
||||||
wait(&status);
|
wait(&status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
2
rush.c
2
rush.c
|
@ -390,7 +390,7 @@ char **modifyargs(char **args) {
|
||||||
while (args[num_arg] != NULL) {
|
while (args[num_arg] != NULL) {
|
||||||
num_arg++;
|
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) {
|
if (strncmp(args[0], "ls", 2) == 0 || strncmp(args[0], "diff", 4) == 0 || strncmp(args[0], "grep", 4) == 0) {
|
||||||
args[num_arg] = "--color=auto";
|
args[num_arg] = "--color=auto";
|
||||||
num_arg++;
|
num_arg++;
|
||||||
|
|
Loading…
Reference in a new issue