-
Notifications
You must be signed in to change notification settings - Fork 0
/
piping.c
242 lines (209 loc) · 5.2 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "headers.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
void execute_command_pipe(char **list, char *home_dir, int cnt, char *cmd);
int cmd_cnt_in_pipe = 0;
int piping(char* cmd, char *home_dir)
{
int std_in_saved = dup(0);
int std_out_saved = dup(1);
//std_in_saved = dup(STDIN_FILENO);
//printf("hi2\n");
//counting no. of pipes
int pipe_cnt = 0;
for (int i = 0; i < strlen(cmd); i++)
{
if (cmd[i] == '|')
pipe_cnt++;
}
if (pipe_cnt == 0)
return -1;
//strtok of piped command and solve tokens
char *pipe_cmd_arr[100];
//printf("hi3\n");
pipe_cmd_arr[0] = strtok(cmd, "|"); //separating the commands
long long int i = 0, j = 0;
while (pipe_cmd_arr[i] != NULL)
{
i++;
pipe_cmd_arr[i] = strtok(NULL, "|");
}
int fd[2];
int pipe_input = 0;
pid_t pid;
for (int j = 0; pipe_cmd_arr[j] != NULL; j++)
{
if (j == 0)
{
char input_copy[1024];
strcpy(input_copy, pipe_cmd_arr[j]);
char *input_file = strstr(input_copy, "<");
if (input_file != NULL)
{
char *input_file_name;
input_file_name = strtok(input_file + 1, " \t\r\n\v\f");
int fd_input = open(input_file_name, O_RDONLY);
dup2(fd_input, 0);
char **list;
cmd_cnt_in_pipe = 0;
list = extract_command(pipe_cmd_arr[j], &cmd_cnt_in_pipe);
pipe(fd);
pid = fork();
if (pid == 0)
{
//dup2(pipe_input , 0);
if (pipe_cmd_arr[j + 1] != NULL)
dup2(fd[1], 1);
close(fd[0]);
//printf("executing command %s in child\n", pipe_cmd_arr[j]);
execute_command_pipe(list, home_dir, cmd_cnt_in_pipe, pipe_cmd_arr[j]);
exit(2);
}
else
{
wait(NULL);
close(fd[1]);
pipe_input = fd[0];
}
continue;
}
}
else if (pipe_cmd_arr[j + 1] == NULL)
{
char output_copy_1[1024]; //to check append
char output_copy_2[1024]; //to check normal
strcpy(output_copy_1, pipe_cmd_arr[j]);
strcpy(output_copy_2, pipe_cmd_arr[j]);
char *output_file_append = strstr(output_copy_1, ">>");
char *output_file_normal = strstr(output_copy_2, ">");
if (output_file_append != NULL && output_file_normal != NULL)
{
//printf("hello 123\n");
char *output_file_name;
output_file_name = strtok(output_file_append + 2, " \t\r\n\v\f");
int fd_output = open(output_file_name, O_APPEND | O_WRONLY | O_CREAT, 0644);
//dup2(fd_output, 0);
char **list;
cmd_cnt_in_pipe = 0;
list = extract_command(pipe_cmd_arr[j], &cmd_cnt_in_pipe);
pipe(fd);
pid = fork();
if (pid == 0)
{
dup2(pipe_input , 0);
//if (pipe_cmd_arr[j + 1] != NULL)
// dup2(fd[1], 1);
dup2(fd_output, 1);
close(fd[0]);
//printf("executing command %s in child\n", pipe_cmd_arr[j]);
execute_command_pipe(list, home_dir, cmd_cnt_in_pipe, pipe_cmd_arr[j]);
exit(2);
}
else
{
wait(NULL);
close(fd[1]);
pipe_input = fd[0];
}
continue;
}
else if (output_file_append == NULL && output_file_normal != NULL)
{
char *output_file_name;
output_file_name = strtok(output_file_normal + 1, " \t\r\n\v\f");
int fd_output = open(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
//dup2(fd_output, 0);
char **list;
cmd_cnt_in_pipe = 0;
list = extract_command(pipe_cmd_arr[j], &cmd_cnt_in_pipe);
pipe(fd);
pid = fork();
if (pid == 0)
{
dup2(pipe_input , 0);
//if (pipe_cmd_arr[j + 1] != NULL)
// dup2(fd[1], 1);
dup2(fd_output, 1);
close(fd[0]);
//printf("executing command %s in child\n", pipe_cmd_arr[j]);
execute_command_pipe(list, home_dir, cmd_cnt_in_pipe, pipe_cmd_arr[j]);
exit(2);
}
else
{
wait(NULL);
close(fd[1]);
pipe_input = fd[0];
}
continue;
}
}
//all the other cases
char **list;
cmd_cnt_in_pipe = 0;
list = extract_command(pipe_cmd_arr[j], &cmd_cnt_in_pipe);
pipe(fd);
pid = fork();
if (pid == 0)
{
dup2(pipe_input , 0);
if (pipe_cmd_arr[j + 1] != NULL)
dup2(fd[1], 1);
close(fd[0]);
//printf("executing command %s in child\n", pipe_cmd_arr[j]);
execute_command_pipe(list, home_dir, cmd_cnt_in_pipe, pipe_cmd_arr[j]);
exit(2);
}
else
{
wait(NULL);
close(fd[1]);
pipe_input = fd[0];
}
}
dup2(std_in_saved, STDIN_FILENO);
dup2(std_out_saved, STDOUT_FILENO);
return 1;
}
void execute_command_pipe(char **list, char *home_dir, int cnt, char *cmd)
{
if (strcmp(list[0], "cd") == 0)
{
if (list[2] != NULL)
{
//cd can handle only two arguments and hence if list[2]!=NULL, too many arguments
printf("cd: too many arguments\n");
}
else
{
if (cd_call(list, cmd, home_dir) == -1)
printf("cd: no such file or directory: %s\n", list[1]);
}
}
else if (strcmp(list[0], "pwd") == 0)
pwd();
else if (strcmp(list[0], "echo") == 0)
echo_func(list);
else if (strcmp(list[0], "ls") == 0)
{
//printf("cnt is %d\n", cnt );
//printf("home_dir %s\n", home_dir );
ls_call(list, cnt - 1, home_dir);
}
else if (strcmp(list[0], "pinfo") == 0)
{
pinfo_call(cnt - 1, list, home_dir);
}
else if (strcmp(list[0], "history") == 0)
{
if (list[1] == NULL)
print_hist(10);
else
print_hist(atoi(list[1]));
}
else
foreground(list);
}