-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiping.c
66 lines (51 loc) · 1.58 KB
/
piping.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "headers.h"
void piping_process(char *sub_sub_string, char *current, char *prevdir, char *original_Command, process_pointer *p, int n, int processth, int pipe_count)
{
p[processth]->ground = 1;
int index = 0;
int command_index = 0;
int command_count = 0;
char **commands = (char **)malloc(1000 * sizeof(char *));
for (int i = 0; i < 1000; i++)
{
commands[i] = (char *)malloc(1000 * sizeof(char));
strcpy(commands[i], "NULL");
}
char *temp = (char *)malloc(4096 * sizeof(char));
int original_stdin = dup(STDIN_FILENO);
int original_stdout = dup(STDOUT_FILENO);
int num_pipes = 0; // handle incorrect use
for (int i = 0; i < strlen(sub_sub_string); i++)
{
if (sub_sub_string[i] == '|')
num_pipes++;
}
int i = 0;
char *token = strtok_r(sub_sub_string, "|", &sub_sub_string);
while (token != NULL)
{
strcpy(commands[i++], token);
token = strtok_r(NULL, "|", &sub_sub_string);
}
int command_no = 0;
int inp[2];
int outp[2];
for (int i = 0; i <= num_pipes; i++)
{
int worked = pipe(inp);
dup2(outp[0], STDIN_FILENO);
if (num_pipes != i)
{
dup2(inp[1], STDOUT_FILENO);
close(inp[1]);
}
if (i == num_pipes)
dup2(original_stdout, STDOUT_FILENO);
find_input(commands[i], current, prevdir);
if(i != num_pipes)
close(outp[0]);
outp[0] = inp[0];
}
dup2(original_stdin, STDIN_FILENO);
dup2(original_stdout, STDOUT_FILENO);
}