-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiping.c
More file actions
164 lines (161 loc) · 5.22 KB
/
piping.c
File metadata and controls
164 lines (161 loc) · 5.22 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "headers.h"
#include "main.h"
#include "takeinput.h"
#include "prompt.h"
#include "systemcommand.h"
#include "io.h"
#include "history.h"
#include "pinfo.h"
#include "signalhandler.h"
void piping(char **argument, int no_of_arg)
{
int pipes[50];
int read = 0;
for (int i = 0; i < 50; i++)
pipes[i] = 0;
int total_pipes = 0;
for (int i = 0; i < no_of_arg; i++)
{
if (strcmp(argument[i], "|") == 0)
{
pipes[total_pipes] = i;
total_pipes++;
}
}
for (int i = 0; i <= total_pipes; i++)
{
int flag_io = 0, size_of_args1 = 0;
// parsing commands
char **args1 = (char **)malloc(256);
if (i == 0)
{
for (int j = 0; j < pipes[0]; j++)
{
args1[j] = (char *)malloc(256 * sizeof(char));
strcpy(args1[j], argument[j]);
if (strcmp(argument[j], "<") == 0 || strcmp(argument[j], ">") == 0 || strcmp(argument[j], ">>") == 0)
{
flag_io = 1;
}
}
args1[pipes[0]] = (char *)malloc(256 * sizeof(char));
args1[pipes[0]] = 0;
size_of_args1 = pipes[0];
// for (int j = 0; j < pipes[0]; j++)
// {
// printf("%s ", args1[j]);
// }
// printf("\n");
}
else if (i > 0 && i != total_pipes)
{
for (int j = pipes[i - 1] + 1; j < pipes[i]; j++)
{
args1[j - pipes[i - 1] - 1] = (char *)malloc(256 * sizeof(char));
strcpy(args1[j - pipes[i - 1] - 1], argument[j]);
if (strcmp(argument[j], "<") == 0 || strcmp(argument[j], ">") == 0 || strcmp(argument[j], ">>") == 0)
{
flag_io = 1;
}
}
args1[pipes[i] - pipes[i - 1] - 1] = (char *)malloc(256 * sizeof(char));
args1[pipes[i] - pipes[i - 1] - 1] = 0;
size_of_args1 = pipes[i] - pipes[i - 1] - 1;
// for (int j = 0; j < pipes[i] - pipes[i - 1] - 1; j++)
// {
// printf("%s ", args1[j]);
// }
// printf("\n");
}
else
{
for (int j = pipes[i - 1] + 1; j < no_of_arg; j++)
{
args1[j - pipes[i - 1] - 1] = (char *)malloc(256 * sizeof(char));
strcpy(args1[j - pipes[i - 1] - 1], argument[j]);
if (strcmp(argument[j], "<") == 0 || strcmp(argument[j], ">") == 0 || strcmp(argument[j], ">>") == 0)
{
flag_io = 1;
}
}
args1[no_of_arg - pipes[i - 1] - 1] = (char *)malloc(256 * sizeof(char));
args1[no_of_arg - pipes[i - 1] - 1] = 0;
size_of_args1 = no_of_arg - pipes[i - 1] - 1;
}
// for (int j = 0; j < size_of_args1; j++)
// {
// printf("%s ", args1[j]);
// }
// printf("\n");
// starting the real pipe thing
int fd[2];
if (pipe(fd) < 0)
{
perror("Unable to make pipe : ");
}
else
{
int pid = fork();
if (pid < 0)
{
perror("Unable to fork : ");
}
else if (pid == 0)
{
// printf("%d %d\n", fd[0], fd[1]);
// fprintf(stderr, "read %d\n", read);
// fprintf(stderr, "fd[1] %d\n", fd[1]);
// fprintf(stderr, "fd[0] %d\n", fd[0]);
// //ctrlz and ctrlc
// signal(SIGINT, SIG_DFL);
// signal(SIGTSTP, SIG_DFL);
if (dup2(read, STDIN_FILENO) < 0)
{
perror("Unable to make file descriptor : ");
}
if (i != total_pipes)
{
int t = dup2(fd[1], STDOUT_FILENO);
if (t < 0)
{
perror("Unable to make file descritor");
}
}
close(fd[0]);
if (flag_io == 1)
{
io(args1, size_of_args1);
}
else if (strcmp(args1[0], "history") == 0)
{
history(size_of_args1, args1);
}
else if (strcmp(args1[0], "pinfo") == 0)
{
pinfo(args1, size_of_args1);
}
else
{
if (execvp(args1[0], args1) < 0)
{
perror("Execute : ");
exit(0);
}
}
exit(1);
}
else
{
ongoing_process = pid;
wait(NULL);
ongoing_process = 0;
// fprintf(stderr, "Wtf??\n");
close(fd[1]);
read = fd[0];
// fprintf(stderr, "fd[1] %d\n", fd[1]);
// fprintf(stderr, "fd[0] %d\n", fd[0]);
}
}
free(args1);
}
}