Repository navigation
Expand file tree
/
Copy pathchain.c
More file actions
84 lines (78 loc) · 2.22 KB
/
Copy pathchain.c
File metadata and controls
84 lines (78 loc) · 2.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
#include "header.h"
//args is array of string where each string is input token get from parsing
// args = echo "hello" < rm.txt ==> args[0] = echo args[1] = hello args[2] = < args[3] = rm.txt
//args[4]= null
int contains_chain(char **args) {
int i = 0;
while (args[i] != NULL) {
if (strcmp(args[i], "&&") == 0 || strcmp(args[i], "||") == 0 || strcmp(args[i], ";") == 0 ) {
return 1;
}
i++;
}
return 0;
}
void handle_chain(char ** args) {
char **args1 = args;
char **args2 = NULL;
pid_t pid;
int flag; // 1 for ; 2 for && 3 for ||
int status;
while (*args != NULL) {
if (strcmp(*args, ";") == 0) {
*args = NULL; // setting current token (;) to NULL
args++;
args2 = args;
flag =1;
break;
}
else if (strcmp(*args, "&&") == 0) {
*args = NULL; // setting current token (;) to NULL
args++;
args2 = args;
flag =2;
break;
}
else if (strcmp(*args, "||") == 0) {
*args = NULL; // setting current token (;) to NULL
args++;
args2 = args;
flag =3;
break;
}
args++;// move on to next input token
}
// The above while loop ensures args1 has left half of || (or) and args2 has right half
pid = fork();
if (pid == -1) {
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0) { // Child process
if (execvp(args1[0], args1) == -1) {// executing the left half of ;
perror("execvp failed");
exit(EXIT_FAILURE);
}
}
else { // Parent process
wait(&status); // Wait for child process to finish
if(flag == 1){
if (execvp(args2[0], args2) == -1) {// executing the second half of |
perror("execvp failed");
exit(EXIT_FAILURE);
}
}
else if(flag ==2 && status == EXIT_SUCCESS){
if (execvp(args2[0], args2) == -1) {// executing the second half of |
perror("execvp failed");
exit(EXIT_FAILURE);
}
}
else if(flag == 3){
if (execvp(args2[0], args2) == -1) {// executing the second half of |
perror("execvp failed");
exit(EXIT_FAILURE);
}
}
}
}