/
smash.c
358 lines (328 loc) · 7.84 KB
/
smash.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* @dpalinsk
* smash.c
* Stop Mucking Around SHell
* GET SMASHED!
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <limits.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#define BUFFSIZE 100 // max number of tokens on a line.
char * readline1();
void looper();
char** splitline(char *line);
void exit1();
int check_for_redirect(char ** command);
int redirect(char ** command);
int check_for_pipe(char ** command);
FILE * finp;
FILE * foutp;
char * line;
char ** args;
int arglen;
pid_t child;
int sigflag; //not safe to print in signal handler
//jmp_buf env;
int input_redirect_index;
int output_redirect_index;
int redirect_flag;
int input_redirect_fd;
int output_redirect_fd;
int pipe_flag;
int pipe_index;
char * readline1(){
size_t n =0;
char *line=NULL;
getline(&line, &n, stdin);
return line;
}
char **splitline(char *line){
arglen=0;
int bufsize= BUFFSIZE+1, index = 0;
char **tokens = calloc(bufsize, (sizeof(char *)));
char * token;
const char delim[5] = " \t\r\n";
//const char delim2[5]="<>|&"
/* Possibly implement handling no spaces between chars in delim 2. Maybe strpbrk?*/
if (NULL == tokens){
perror("SMASH: ALLOCATION ERROR! GET SMASHED!");
exit(EXIT_FAILURE);
}
token = strtok(line, delim);
while (token != NULL) {
arglen+=1;
tokens[index]=token;
index++;
if (index >= bufsize){
bufsize+=BUFFSIZE;
tokens=realloc(tokens, bufsize*sizeof(char*));
if (NULL == tokens){
perror("SMASH: ALLOCATION ERROR! GET SMASHED!");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, delim);
}
return tokens;
}
void myinfo(){
pid_t pid = getpid();
pid_t ppid = getppid();
printf("PID: %d\nPPID: %d\n",pid,ppid );
}
void mycd(char * command){
char buff[PATH_MAX + 1];
char * cpath;
char * upath = ""; //user path
upath = command;
cpath = getcwd(buff, PATH_MAX+1);
int len = strlen(cpath) + strlen(upath) + 2 ;
char* newpath = malloc(len * sizeof(char));
newpath = strcpy(newpath, cpath);
newpath = strcat(newpath, "/");
newpath = strcat(newpath,upath);
//printf("%s\n", newpath);
if (chdir(newpath) != 0 ){
perror("SMASH");
}
free(newpath);
}
int SMASHexec(char ** command){
int status;
//printf("Hi\n");
if (0==strcmp("exit", command[0])){
free(line);
free(args);
exit(0);
} else if (0==strcmp("myinfo", command[0])){
myinfo();
} else if (0==strcmp("cd", command[0]) && NULL == args[1]) {
char * HOME="";
HOME = getenv("HOME");
if (NULL != HOME){
chdir(HOME);
} else{
printf("$HOME path corrupt\n");
}
}else if (0 == strcmp("cd", command[0])){
mycd(command[1]);
}else {
if (pipe_flag){
args[pipe_index]=NULL;
//pid_t child2;
int fds[2];
int buffersize = arglen - pipe_index -1;
//printf("%d\n", buffersize );
char ** buffer = calloc(sizeof(char*), buffersize);
for (int i = 1; i <= buffersize; ++i)
{
buffer[i-1] = command[pipe_index + i];
//printf("ABC%s\n", buffer[i-1] );
}
//printf("%s\n", buffer[0] );
pipe(fds);
//child = fork();
if (fork() == 0){
dup2(fds[0], STDIN_FILENO);
//close(fds[1]);
close(fds[0]);
//child2 = fork();
if (fork() == 0){
dup2(fds[1], STDOUT_FILENO);
close(fds[0]);
//close(fds[1]);
//printf("1st %s\n", command[0] );
execvp(command[0], command);
}
wait(NULL);
close(fds[1]);
execvp(buffer[0], buffer);
}
close(fds[1]);
close(fds[0]);
free(buffer);
wait(NULL);
} else {
//printf("B\n");
child = fork();
if (child){
//parent
if (0==strcmp(command[arglen-1], "&")){
char buff[PATH_MAX + 1];
char * cpath;
cpath = getcwd(buff, PATH_MAX+1);
printf("\n< %s SMASH >$ ", cpath);
waitpid(-1, &status, WNOHANG );
} else {
waitpid(child, &status, 0 );
}
} else{
//child
//sleep(5); used for testing of sigint handler
redirect(args);
if (redirect_flag){
if ( redirect_flag == 1 ){
args[input_redirect_index]=NULL;
} else if ( redirect_flag == 2 ){
args[output_redirect_index]=NULL;
} else if ( redirect_flag == 3){
args[output_redirect_index]=NULL;
args[input_redirect_index]=NULL;
}
}
int failure = execvp(command[0], command);
if (-1 == failure){
perror("SMASH");
exit(EXIT_FAILURE);
}
}
}
}
return child;
}
void prompter(){
char * cpath;
char buff[PATH_MAX + 1];
cpath = getcwd(buff, PATH_MAX+1);
printf("\n< %s SMASH >$ ", cpath);
}
void sig_handler(int sig){
signal(sig, sig_handler);
switch (sig) {
case (SIGINT) :
if (0==child){
sigflag=1;
//prompter();
return;
} else{
kill(child, SIGINT);
}
}
return;
}
/* checks for redirect.
return 0 for no redirect
1 for input redirect
2 for output redirect
3 for both
if 1 or 2, set input_red_index or output_red_index respectively
if 3, set both. fu
//tracker keeps track of return;
*/
int check_for_redirect(char ** command){
int tracker=0;
for (int i = 0; i < arglen; ++i){
if ( 0 == strcmp(command[i], "<") ){
input_redirect_index=i;
tracker+=1;
} else if ( 0 == strcmp(command[i], ">")){
output_redirect_index=i;
tracker+=2;
}
}
return(tracker);
}
int redirect(char ** command){
if (0 == redirect_flag){
return 0;
} else if (1 == redirect_flag){
//printf("%s\n", command[input_redirect_index+1]);
if (NULL == (finp = fopen(command[input_redirect_index+1],"r+"))){
perror("Invalid File. Errno ");
exit(errno);
}
int finpfd=fileno(finp);
dup2(finpfd, STDIN_FILENO);
foutp = stdout;
} else if ( 2==redirect_flag ){
if (NULL == (foutp = fopen(command[output_redirect_index+1],"w+"))){
perror("Invalid File. Errno ");
exit(errno);
}
printf("HI");
int foutpfd=fileno(foutp);
dup2(foutpfd, STDOUT_FILENO);
finp = stdin;
} else if (3 == redirect_flag ) {
if (NULL == (foutp = fopen(command[output_redirect_index+1],"w+"))){
perror("Invalid File. Errno ");
exit(errno);
}
if (NULL == (finp = fopen(command[input_redirect_index+1],"r+"))){
perror("Invalid File. Errno ");
exit(errno);
}
int finpfd=fileno(finp);
dup2(finpfd, STDIN_FILENO);
int foutpfd=fileno(foutp);
dup2(foutpfd, STDOUT_FILENO);
}
return 0;
}
int check_for_pipe(char ** command){
int status=0;
for ( int i = 0; i < arglen; ++i){
if (0==strcmp("|", command[i])){
status=1;
pipe_index=i;
}
}
return status;
}
void looper(){
int status=1;
char * cpath;
char buff[PATH_MAX + 1];
sigflag=0;
do {
//setjmp (env);
input_redirect_index=0, output_redirect_index=0;
cpath = getcwd(buff, PATH_MAX+1);
printf("< %s ", cpath);
line = readline("SMASH >$ ");
if (NULL == line){
exit(EOF);
}
add_history(line);
if (strlen(line)>=1){
args = splitline(line);
redirect_flag=check_for_redirect(args);
pipe_flag=check_for_pipe(args);
SMASHexec(args);
//signal(SIGINT, sig_handler);
free(args);
}
//printf("hi!");
//sleep(1);
free(line);
}while (status);
}
//silly little procrastination ascii function.
void intro(){
//ASCII GENERATED BY http://www.network-science.de/ascii/
char * space = " ";
printf("%27sWelcome to\n", space);
printf("%13s _____ __ __ _____ _ _\n", space);
printf("%13s / ____| \\/ | /\\ / ____| | | |\n", space);
printf("%13s| (___ | \\ / | / \\ | (___ | |__| |\n", space);
printf("%13s \\___ \\| |\\/| | / /\\ \\ \\___ \\| __ |\n", space);
printf("%13s ____) | | | |/ ____ \\ ____) | | | |\n", space);
printf("%13s|_____/|_| |_/_/ \\_\\_____/|_| |_|\n\n", space);
printf("%10sThe Stop Mucking Around SHell - GET SMASHED!\n", space);
printf("%13sBy: Derek Palinski\n\n", space);
}
int main (){
signal(SIGINT, sig_handler);
intro();
//myinfo();
looper();
}