-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
205 lines (182 loc) · 6.25 KB
/
Copy pathpipe.c
File metadata and controls
205 lines (182 loc) · 6.25 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
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
#include "headers.h"
#include "tools.h"
int pipeCommand(char *cmd) {
char *commands[MAX_COMMANDS]; // Array to store individual commands
int num_commands = 0;
// return error if there is nothing to the left or to the right of a pipe
if (cmd[0] == '|' || cmd[strlen(cmd) - 1] == '|') {
printError("ERROR: Invalid use of pipe\n");
return 1;
}
// Tokenize the input command by pipe symbol '|'
char *token = strtok(cmd, "|");
while (token != NULL) {
trim(token);
commands[num_commands++] = token;
token = strtok(NULL, "|");
}
if (num_commands < 2) {
// No pipes found, return 0
return 0;
}
// Create an array of pipes
int pipes[num_commands - 1][2];
for (int i = 0; i < num_commands - 1; i++) {
if (pipe(pipes[i]) == -1) {
printError("pipe");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < num_commands; i++) {
pid_t pid = fork();
if (pid == -1) {
printError("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
if (i > 0) {
// Redirect input from the previous pipe
dup2(pipes[i - 1][0], STDIN_FILENO);
close(pipes[i - 1][0]);
close(pipes[i - 1][1]);
}
if (i < num_commands - 1) {
// Redirect output to the next pipe
dup2(pipes[i][1], STDOUT_FILENO);
close(pipes[i][0]);
close(pipes[i][1]);
}
Command *cmd = parseCommand(commands[i], " \t");
if (execvp(cmd->argv[0], cmd->argv) == -1) {
printError("ERROR: '%s' is not a valid command\n", cmd->argv[0]);
exit(EXIT_FAILURE);
}
} else {
// Parent process
if (i > 0) {
// Close the previous pipe's read and write ends
close(pipes[i - 1][0]);
close(pipes[i - 1][1]);
}
waitpid(pid, NULL, 0);
}
}
// Close all remaining pipe ends in the parent process
for (int i = 0; i < num_commands - 1; i++) {
close(pipes[i][0]);
close(pipes[i][1]);
}
return 1;
}
int ioPipe(char *cmd) {
char *commands[MAX_COMMANDS]; // Array to store individual commands
int num_commands = 0;
int input_fd = STDIN_FILENO; // Default input is stdin
int output_fd = STDOUT_FILENO; // Default output is stdout
int append_flag = 0; // Flag for append mode
// Check for input redirection
char *input_file = strchr(cmd, '<');
if (input_file != NULL) {
*input_file = '\0'; // Split the command at the '<' character
input_file = strtok(input_file + 1, " \t"); // Get the input file name
input_fd = open(input_file, O_RDONLY);
if (input_fd == -1) {
printError("open");
return 1;
}
}
// Check for output redirection (including append mode)
char *output_file = strstr(cmd, ">>");
if (output_file != NULL) {
*output_file = '\0'; // Split the command at ">>"
output_file = strtok(output_file + 2, " \t"); // Get the output file name
output_fd = open(output_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
if (output_fd == -1) {
printError("open");
return 1;
}
append_flag = 1; // Set the append flag
} else {
output_file = strchr(cmd, '>');
if (output_file != NULL) {
*output_file = '\0'; // Split the command at the '>' character
output_file = strtok(output_file + 1, " \t"); // Get the output file name
output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (output_fd == -1) {
printError("open");
return 1;
}
}
}
// Tokenize the input command by pipe symbol '|'
char *token = strtok(cmd, "|");
while (token != NULL) {
trim(token);
commands[num_commands++] = token;
token = strtok(NULL, "|");
}
if (num_commands < 2) {
// No pipes found, return 0
return 0;
}
// Create an array of pipes
int pipes[num_commands - 1][2];
for (int i = 0; i < num_commands - 1; i++) {
if (pipe(pipes[i]) == -1) {
printError("pipe");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < num_commands; i++) {
pid_t pid = fork();
if (pid == -1) {
printError("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
if (i > 0) {
// Redirect input from the previous pipe
dup2(pipes[i - 1][0], STDIN_FILENO);
close(pipes[i - 1][0]);
close(pipes[i - 1][1]);
} else {
// Redirect input from the specified file (if any)
dup2(input_fd, STDIN_FILENO);
close(input_fd);
}
if (i < num_commands - 1) {
// Redirect output to the next pipe
dup2(pipes[i][1], STDOUT_FILENO);
close(pipes[i][0]);
close(pipes[i][1]);
} else {
// Redirect output to the specified file (if any)
if (append_flag) {
dup2(output_fd, STDOUT_FILENO);
} else {
dup2(output_fd, STDOUT_FILENO);
close(output_fd);
}
}
Command *cmd = parseCommand(commands[i], " \t");
if (execvp(cmd->argv[0], cmd->argv) == -1) {
printError("ERROR: '%s' is not a valid command\n", cmd->argv[0]);
exit(EXIT_FAILURE);
}
} else {
// Parent process
if (i > 0) {
// Close the previous pipe's read and write ends
close(pipes[i - 1][0]);
close(pipes[i - 1][1]);
}
waitpid(pid, NULL, 0);
}
}
// Close all remaining pipe ends in the parent process
for (int i = 0; i < num_commands - 1; i++) {
close(pipes[i][0]);
close(pipes[i][1]);
}
return 1;
}