-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
97 lines (97 loc) · 2.18 KB
/
pipe.c
File metadata and controls
97 lines (97 loc) · 2.18 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "headers.h"
#include "pipe.h"
#include "main.h"
int get_tot_pipes(char *input)
{
int no = 0, i, n = strlen(input);
for (i = 0; i < n; i++)
{
if (input[i] == '|')
{
no++;
}
}
return no;
}
int get_pipe_args(char *input)
{
char *p = strtok(input, "|");
no = 0;
while (p != NULL)
{
strcpy(pipe_args[no], p);
no++;
p = strtok(NULL, "|");
}
return no;
}
int piping(char *command)
{
int tot2 = get_tot_pipes(command);
int tot = get_pipe_args(command);
char *ptr = (char *)malloc(1000 * sizeof(char));
char *ptr2 = (char *)malloc(1000 * sizeof(char));
if (tot2 + 1 != tot)
{
printf("\x1B[1;31mParsing error in pipe !!! \n\x1B[1;0m");
prestat = 'f';
return 1;
}
else if (tot2 + 1 == tot)
{
for (int i = 0; i < tot; i++)
{
strcpy(ptr, pipe_args[i]);
ptr2 = strtok(ptr, " \t");
if (ptr2 == NULL)
{
printf("\x1B[1;31mParsing error in pipe !!! \n\x1B[1;0m");
prestat = 'f';
return 1;
}
}
}
int type = 0, pipe_arr[2], fd = 0, status;
pid_t npid;
for (int j = 0; j < tot; j++)
{
prestat = 's';
pipe(pipe_arr);
npid = fork();
if (npid == 0)
{
dup2(fd, 0);
if (j + 1 < tot)
{
dup2(pipe_arr[1], 1);
}
close(pipe_arr[0]);
strcpy(colonsep[0], pipe_args[j]);
call_command(1, 1);
if (prestat == 'f')
{
exit(1);
}
else if (prestat == 's')
{
exit(0);
}
}
else if (npid == -1)
{
perror("\x1B[1;31mError while forking \x1B[1;0m");
prestat = 'f';
return 1;
}
else
{
waitpid(npid, &status, 0);
if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0))
{
prestat = 'f';
}
close(pipe_arr[1]);
fd = pipe_arr[0];
}
}
}