-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset2.c
154 lines (134 loc) · 3.86 KB
/
set2.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
#include "shell.h"
/* split line according to '<' into an array of strings */
char **split_input_redirection(char *line);
/* split line according to '>' into an array of strings */
char **split_output_redirection(char *line);
/* check if line has '|' */
int is_pipe(char *line){
if(strstr(line, "|") != NULL){
return 1;
}
return 0;
}
/* check if line has '<' */
int is_input(char *line){
if(strstr(line,"<")!= NULL){
return 1;
}
return 0;
}
/* check if line has '>' */
int is_output(char *line){
if(strstr(line,">")!=NULL){
return 1;
}
return 0;
}
/* split line according to '|' into an array of strings */
char **split_pipe(char *line){
char **tokens = malloc(BUFSIZE * sizeof(char*));
char *token;
token = strtok(line, "|");
int i = 0;
while(token != NULL) {
tokens[i] = token;
i++;
token = strtok(NULL, "|");
}
tokens[i] = NULL;
return tokens;
}
/* remove any extra space from the string */
void removeSpaces(char *str)
{
int c = 0;
for (int i = 0; str[i]; i++) {
if (str[i] != ' ') {
str[c++] = str[i];
}
}
//make sure to end with NULL
str[c] = '\0';
}
/* split line according to '>' into an array of strings */
char **split_output_redirection(char *line){
char **tokens = malloc(BUFSIZE * sizeof(char*));
char *token;
token = strtok(line, ">\n");
int i = 0;
while(token != NULL) {
if(i == 1){
removeSpaces(token);
}
tokens[i] = token;
i++;
token = strtok(NULL, ">\n");
}
tokens[i] = NULL;
return tokens;
}
/* split line according to '<' into an array of strings */
char **split_input_redirection(char *line){
char **tokens = malloc(BUFSIZE * sizeof(char*));
char *token;
token = strtok(line, "<\n");
int i = 0;
while(token != NULL) {
if(i == 1){
removeSpaces(token);
}
tokens[i] = token;
i++;
token = strtok(NULL, "<\n");
}
tokens[i] = NULL;
return tokens;
}
/* create the pipe using pipe() and use dup2() to channel
input and output.*/
int create_pipe(char *line) {
pid_t childpid1;
int status = 0;
char **args = split_pipe(line);
int fd[2];
int store = 0;
int i = 0;
while(args[i]!=NULL){
pipe(fd);
childpid1 = fork();
if(childpid1 >= 0){
if(childpid1 == 0){
if(args[i+1] == NULL && is_output(args[i])) { // check if we have an output file
char **inp = split_output_redirection(args[i]);
freopen(inp[1],"w+",stdout); //open the file in stdout, and print to that file
free(inp);
}
if(is_input(args[i]) && i == 0 ){ // check if we have an input file
char **inp = split_input_redirection(args[i]);
freopen(inp[1],"r",stdin); //open the file in stdin, and take input from it
free(inp);
}else{
dup2(store, STDIN_FILENO); // Otherwise take input from previous comand or stdin
}
if(args[i+1] != NULL) {
dup2(fd[1],STDOUT_FILENO); // write to fd[1]
}
close(fd[0]); //unused
status = exec_cmd(split_input(args[i])); //execute the command by creating new process
exit(status);
}
}else{
perror("fork");
free(args);
return (-1);
}
waitpid(childpid1, &status, 0); // wait for child to finish
close(fd[1]); //unused
store = fd[0]; // Used by child, send input for next command
i++;
}
close(fd[0]);
close(fd[1]);
free(args);
return 1;
}