forked from Martell0x1/PosPosShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
193 lines (135 loc) · 3.24 KB
/
Copy pathexecutor.c
File metadata and controls
193 lines (135 loc) · 3.24 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
#include "shell.h"
void execute_pipe(char *input) {
char input_cpy[MAX_INPUT];
strcpy(input_cpy, input);
char *pipe_pos = strchr(input_cpy, '|');
if (pipe_pos == NULL) return;
*pipe_pos = '\0';
char *cmd1 = input_cpy;
char *cmd2 = pipe_pos + 1;
while (*cmd2 == ' ') cmd2++;
char *args1[MAX_ARGS];
char *args2[MAX_ARGS];
parse_args(cmd1, args1);
parse_args(cmd2, args2);
int fd[2];
if(pipe(fd) == -1){
perror("pipe failed");
return;
}
pid_t p1 = fork();
if (p1 < 0){
perror("fork failed");
return;
}
if (p1 == 0) {
if(dup2(fd[1], STDOUT_FILENO) == -1){
perror("dup2 failed");
exit(EXIT_FAILURE);
}
close(fd[0]);
close(fd[1]);
execvp(args1[0], args1);
perror("command 1 failed");
exit(EXIT_FAILURE);
}
pid_t p2 = fork();
if (p2 < 0){
perror("fork failed");
return;
}
if (p2 == 0) {
if(dup2(fd[0], STDIN_FILENO) == -1){
perror("dup2 failed");
exit(EXIT_FAILURE);
}
close(fd[1]);
close(fd[0]);
execvp(args2[0], args2);
perror("command 2 failed");
exit(EXIT_FAILURE);
}
close(fd[0]);
close(fd[1]);
waitpid(p1, NULL, 0);
waitpid(p2, NULL, 0);
}
void execute_a_fork_family(char ** args , int background){
pid_t pid = fork();
if(pid < 0){
perror("fork failed");
return;
}
if(pid == 0){
int i = 0;
while (args[i] != NULL) {
if (strcmp(args[i], ">") == 0) {
if (args[i+1] == NULL){
fprintf(stderr, "syntax error: no output file\n");
exit(EXIT_FAILURE);
}
int fd = open(args[i+1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0){
perror("output file error");
exit(EXIT_FAILURE);
}
if(dup2(fd, STDOUT_FILENO) < 0){
perror("dup2 failed");
exit(EXIT_FAILURE);
}
close(fd);
args[i] = NULL;
}
else if (strcmp(args[i], "<") == 0) {
if (args[i+1] == NULL){
fprintf(stderr, "syntax error: no input file\n");
exit(EXIT_FAILURE);
}
int fd = open(args[i+1], O_RDONLY);
if (fd < 0){
perror("input file error");
exit(EXIT_FAILURE);
}
if(dup2(fd, STDIN_FILENO) < 0){
perror("dup2 failed");
exit(EXIT_FAILURE);
}
close(fd);
args[i] = NULL;
}
i++;
}
execvp(args[0], args);
fprintf(stderr, "%s: command not found\n", args[0]);
exit(EXIT_FAILURE);
}
else {
if(!background){
waitpid(pid, NULL, 0);
} else {
printf("Background PID: %d\n", pid);
}
}
}
void execute_command(char *input) {
if(input == NULL || strlen(input) == 0)
return;
if (strchr(input, '|') != NULL) {
execute_pipe(input);
return;
}
char* args[MAX_ARGS];
int arg_count = parse_args(input, args);
if(arg_count == 0)
return;
if(handle_commands(arg_count, args))
return;
int background = 0;
while(arg_count > 0 && args[arg_count -1] != NULL
&& strcmp(args[arg_count -1], "&")==0){
background = 1;
args[arg_count -1] = NULL;
arg_count--;
}
execute_a_fork_family(args, background);
}