-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.c
More file actions
293 lines (267 loc) · 7.1 KB
/
Copy pathbash.c
File metadata and controls
293 lines (267 loc) · 7.1 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#define true 1
#define false 0
char** arguments(char* command){
int argc = 0;
char** argv = calloc(1, sizeof(char*));
char* buffer = calloc(64, sizeof(char));
//Ensure that everything is not NULL
if(!argv || !buffer){
perror("ERROR: calloc failed");
free(buffer);
free(argv);
return NULL;
}
char c;
do {
c = *(command++);
if(isspace(c) || !c) {
if(strlen(buffer)){
#ifdef DEBUG_MODE
fprintf(stderr, "Writing to args: \"%s\"\n", buffer);
#endif
argv[argc] = calloc(strlen(buffer)+1, sizeof(char));
if(!argv[argc]){
perror("ERROR: calloc failed");
for(int i = 0; i < argc; i++) free(argv[argc]);
free(argv);
free(buffer);
return NULL;
}
#ifdef DEBUG_MODE
fprintf(stderr, " -> Calloc'd an entry in argv of size %ld\n", strlen(buffer)+1);
#endif
strcpy(argv[argc], buffer);
#ifdef DEBUG_MODE
fprintf(stderr, " -> Copied string from buffer to entry\n");
#endif
argc++;
argv = realloc(argv, (argc + 1) * sizeof(char*));
if(!argv) {
perror("ERROR: realloc failed");
free(buffer);
return NULL;
}
#ifdef DEBUG_MODE
fprintf(stderr, " -> Realloc'd argv to be one size larger\n");
#endif
argv[argc] = NULL;
}
for(int i = 0; i < 64; i++) buffer[i] = '\0';
} else buffer[strlen(buffer)] = c;
} while(c);
free(buffer);
#ifdef DEBUG_MODE
for(int i = 0; argv[i]; i++)
fprintf(stderr, "Argv[%d]: \"%s\"\n", i, argv[i]);
#endif
return argv;
}
int get_path(char* command, char* path) {
char* mypath_env = getenv("MYPATH");
if(!mypath_env) mypath_env = "/bin#.";
int i = 0;
int j = 0;
#ifdef DEBUG_MODE
fprintf(stderr, "Finding the executable for \"%s\"\n", command);
#endif
if(command[0] == '/') {
strcpy(path, command);
return true;
}
do {
if(mypath_env[i] == '\0' || mypath_env[i] == '#') {
path[j] = '/';
struct stat statbuf;
strcpy(path + j + 1, command);
#ifdef DEBUG_MODE
fprintf(stderr, " -> Trying \"%s\"\n", path);
#endif
if(lstat(path, &statbuf) == 0){
#ifdef DEBUG_MODE
fprintf(stderr, " -> Found \"%s\"\n", path);
#endif
return true;
}
j = 0;
for(int k = 0; k < strlen(path); k++) path[k] = '\0';
} else {
path[j] = mypath_env[i];
j++;
}
i++;
} while(i <= strlen(mypath_env));
return false;
}
int run_command(char** args, int infile, int outfile, int background, int pfd[2]){
char* path = calloc(64, sizeof(char));
if(!get_path(args[0], path)){
fprintf(stderr, "ERROR: command \"%s\" not found\n", args[0]);
free(path);
return 0;
}
#ifdef DEBUG_MODE
fprintf(stderr, "Forking...\n");
#endif
pid_t child_pid = fork();
if(child_pid == -1){
perror("ERROR: fork failed");
free(path);
return -1;
} else if(!child_pid) {
dup2(infile, 0);
if(infile == 0 && pfd) close(pfd[0]);
dup2(outfile, 1);
if(outfile == 1 && pfd) close(pfd[1]);
#ifdef DEBUG_MODE
fprintf(stderr, "Input comes from file descriptor %d\n", infile);
fprintf(stderr, "Output goes to file descriptor %d\n", outfile);
fprintf(stderr, "Running %s:\n", path);
#endif
execv(path, args);
//Error on failure to run command
free(path);
fprintf(stderr, "ERROR: ");
perror(args[0]);
return -1;
} else {
free(path);
if(pfd && infile == pfd[0]) close(infile);
if(pfd && outfile == pfd[1]) close(outfile);
if(background)
printf("[running background process \"%s\"]\n", args[0]);
else {
#ifdef DEBUG_MODE
fprintf(stderr, "Waiting for process \"%s\" to complete\n", args[0]);
#endif
waitpid(child_pid, NULL, 0);
}
#ifdef DEBUG_MODE
fprintf(stderr, "Process %d terminated\n", child_pid);
#endif
}
return 0;
}
int main() {
setvbuf(stdout, NULL, _IONBF, 0);
char* command_line = calloc(1024, sizeof(char));
int running_processes = 0;
int exit_code = -1;
char* current_directory = calloc(256, sizeof(char));
while(true) {
int* pipefd = NULL;
int background_processing = false;
// Write out the terminal prompt.
getcwd(current_directory, 256);
printf("%s$ ", current_directory);
//Get the terminal command
fgets(command_line, 1024, stdin);
//Parse the terminal command
char** argv = arguments(command_line);
char** argv2 = NULL;
if(!argv) {
exit_code = EXIT_FAILURE;
goto cleanup;
}
//Ignore if the user passes in an empty string
if(!argv[0]) goto cleanup;
//If the command is "exit", exit
if(!strcmp(argv[0], "exit")) {
printf("bye\n");
exit_code = EXIT_SUCCESS;
goto cleanup;
}
//If the command is "cd", we are changing the directory
if(!strcmp(argv[0], "cd")) {
char* new_directory = argv[1] ? argv[1] : getenv("HOME");
#ifdef DEBUG_MODE
fprintf(stderr, "Attempting to change directory to %s\n", new_directory);
#endif
if(chdir(new_directory) == -1)
perror("ERROR: Could not switch directory");
goto cleanup;
}
//Check for piping and background processing
for(char** check = argv; *check; check++) {
if(!strcmp(*check, "|")) {
#ifdef DEBUG_MODE
fprintf(stderr, "Found a pipe character in argv[%ld]\n", check-argv);
#endif
pipefd = calloc(2, sizeof(int));
if(pipe(pipefd) == -1) {
perror("ERROR: pipe failed");
goto cleanup;
}
#ifdef DEBUG_MODE
fprintf(stderr, "-> Freeing up pipe string\n");
#endif
free(*check);
*check++ = NULL;
argv2 = check;
if(!*argv2){
fprintf(stderr, "ERROR: bad syntax\n");
free(pipefd);
goto cleanup;
}
}
if(!strcmp(*check, "&")) {
if(check[1] != NULL) {
fprintf(stderr, "ERROR: bad syntax\n");
goto cleanup;
}
free(check[0]);
check[0] = NULL;
background_processing = true;
}
}
#ifdef DEBUG_MODE
fprintf(stderr, "Ready to run: %s", command_line);
#endif
//Execute the command and wait for it to finish
int failure = 0;
if(argv2) {
failure += run_command(argv, 0, pipefd[1], background_processing, pipefd);
failure += run_command(argv2, pipefd[0], 1, background_processing, pipefd);
if(background_processing) running_processes += 2 - failure;
} else {
failure = run_command(argv, 0, 1, background_processing, pipefd);
if(background_processing) running_processes += 1 - failure;
}
if(failure) exit_code = EXIT_FAILURE;
cleanup:
//Clear up any running processes
if(running_processes) {
int *exit_status = calloc(1, sizeof(int));
int terminated_pid = waitpid(-1, exit_status, WNOHANG);
while(terminated_pid){
if(terminated_pid == -1) {
perror("ERROR: waitpid failed");
break;
}
printf("[process %d terminated with exit status %d]\n", terminated_pid, WEXITSTATUS(*exit_status));
if(!--running_processes) break;
terminated_pid = waitpid(-1, exit_status, WNOHANG);
}
free(exit_status);
}
#ifdef DEBUG_MODE
fprintf(stderr, "There are still %d running processes\n", running_processes);
#endif
//Free up the argument list
for(char** entry = argv; *entry; entry++) free(*entry);
if(argv2) for(char** entry = argv2; *entry; entry++) free(*entry);
free(argv);
free(pipefd);
if(exit_code >= 0) break;
}
//Free up everything used
free(current_directory);
free(command_line);
return exit_code;
}