fix pipes

This commit is contained in:
Night Kaly 2024-02-09 11:27:13 +00:00
parent 3780d162ac
commit b171d3ab22
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
2 changed files with 19 additions and 16 deletions

View file

@ -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;
}

2
rush.c
View file

@ -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++;