-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.c
189 lines (170 loc) · 3.3 KB
/
pipeline.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
/*Concatenates shell commands and redirects
std input and output*/
enum{
N = 10,
};
struct commands{
int stdin;
int stdout;
char **commands;
int pipes[N][2];
};
typedef struct commands commands;
static char*
full_path(char *location, char *command)
{
char *path = malloc (265);
snprintf(path, 265, "%s/%s", location, command);
return path;
}
static char*
find_command(char *command)
{
int N_Paths = 3;
int i;
char *locations[] = {"/bin", "/usr/bin", "/usr/local/bin"};
char *path;
for(i=0; i < N_Paths; i++){
path = full_path(locations[i], command);
if(access(path, X_OK) == 0){
break;
}
}
return path;
}
static void
runprocess(commands *cml, int com_num, int num_commands)
{
char *command;
char *argv[2];
switch(fork()){
case -1:
err(1,"fork");
exit(1);
case 0:
if(com_num == 0){
if(dup2(cml->stdin, 0)<0){
err(1,"dup2-1");
}
if(close(cml->stdin)<0){
err(1,"close");
exit(1);
}
if(num_commands > 1){
if(dup2(cml->pipes[com_num][1], 1)<0){
err(1,"dup2-2");
}
}else{
if(dup2(cml->stdout, 1)<0){
err(1,"dup2-4");
}
if(close(cml->stdout)<0){
err(1,"close");
exit(1);
}
}
}else if(com_num == num_commands-1){
if(dup2(cml->pipes[com_num-1][0], 0)<0){
err(1,"dup2-3");
}
if(dup2(cml->stdout, 1)<0){
err(1,"dup2-4");
}
if(close(cml->stdout)<0){
err(1,"close");
exit(1);
}
}else{
if(dup2(cml->pipes[com_num-1][0], 0)<0){
err(1,"dup2-5");
}
if(dup2(cml->pipes[com_num][1], 1)<0){
err(1,"dup2-6");
}
}
command = find_command(cml->commands[com_num]);
argv[0] = cml->commands[com_num];
argv[1] = NULL;
execv(command, argv);
err(1, "exec %s failed", command);
default:
if(com_num == 0){
if(close(cml->pipes[com_num][1])<0){
err(1,"Parent close");
exit(1);
}
}else if(com_num == num_commands-1){
if(close(cml->pipes[com_num-1][0])<0){
err(1,"Parent close");
}
}else{
if(close(cml->pipes[com_num-1][0])<0){
err(1,"Parent close");
exit(1);
}
if(close(cml->pipes[com_num][1])<0){
err(1,"Parent close");
exit(1);
}
}
}
}
static void
pipelining(commands *cml, int num_commands)
{
int i, fd[2], sts;
for (i=0; i < num_commands-1; i++){
pipe(fd);
cml->pipes[i][0] = fd[0];
cml->pipes[i][1] = fd[1];
}
for (i=0; i < num_commands; i++){
runprocess(cml, i, num_commands);
}
for (i = 0; i < num_commands; i++){
wait(&sts);
}
}
int
main (int argc, char *argv[])
{
int i, fdout, fdin;
char outflag[] = "-o";
char inflag[] = "-i";
char *outfile, *infile;
commands *com_line = (commands*) malloc (sizeof(commands));
argc--;
argv++;
if (argc < 6 || argc > N){
err(1, "No apropriate number of commands");
exit(1);
}else{
for (i=0; i < 5; i++){
if (!strcmp(argv[i], outflag)){
i++;
outfile = argv[i];
fdout = creat(outfile, O_WRONLY);
com_line->stdout = fdout;
}else if (!strcmp(argv[i], inflag)){
i++;
infile = argv[i];
fdin = open(infile, O_RDONLY);
com_line->stdin = fdin;
}
}
argc=argc-4;
argv=argv+4;
com_line->commands = argv;
pipelining(com_line, argc);
}
exit(0);
}