-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
604 lines (536 loc) · 13.6 KB
/
myshell.c
File metadata and controls
604 lines (536 loc) · 13.6 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
/************************************
* GLOBAL VARIABLES
************************************/
char *tokens[255]; // array of strings, tokens
int token_count = 0; // number of tokens
char *terminal_name = "mysh";
char *current_dir;
int exit_status = 0;
int current_in; // stdin = 0
int current_out; // stdout = 1
/************************************
* ALL FUNCTIONS
************************************/
// Create tokens from an input line
void tokenize(char *line)
{
// Reset tokens array
memset(tokens, 0, 255);
// Reset tokens number to 0
token_count = 0;
// Remove line break char
int line_len = strlen(line);
char token[255];
int token_len = 0;
int quote_mode = 0;
int prvi_znak = 0;
for (int i=0; i<line_len; i++)
{
char znak = line[i];
int char_empty = isspace(znak);
// Check for only white-space chars
if (prvi_znak == 0 && char_empty)
continue;
else
prvi_znak = 1;
// Begin quote_mode "...
if (znak == '"' && quote_mode == 0)
{
quote_mode = 1;
continue;
}
// Turn OFF quote_mode
else if (znak == '"' && quote_mode == 1)
{
quote_mode = 0;
i++;
}
// Symbol is not a white-space char
if ((char_empty == 0 || quote_mode == 1) && znak != '"')
{
token[token_len] = znak;
token_len++;
}
// symbol = white-space, we are not in "..."
// or
// end quote mode ..."
if (char_empty != 0 && quote_mode == 0 ||
znak == '"' && quote_mode == 0)
{
// Null terminate token so we can print it
token[token_len] = '\0';
tokens[token_count] = strdup(token);
token_count++;
memset(token, 0, 255);
token_len = 0;
}
}
}
void print_tokens()
{
for (int i=0; i<token_count; i++)
{
printf("%s ", tokens[i]);
}
printf("\n");
fflush(stdout);
}
void action_name()
{
// New name was given
if (token_count > 1)
terminal_name = strdup(tokens[1]);
else
printf("%s\n", terminal_name);
fflush(stdout);
exit_status = 0;
}
void action_help()
{
printf(
" ------------------ HELP --------------------\n\n"
" SIMPLE COMMANDS:\n"
" - name\n"
" - help\n"
" - status\n"
" - exit \n"
" - print\n"
" - echo\n"
" - pid\n"
" - ppid\n"
"\n"
" DIRECTORY MANIPULATION COMMANDS:\n"
" - dirchange\n"
" - dirwhere\n"
" - dirmake\n"
" - dirremove\n"
" - dirlist\n"
"\n"
" FILE MANIPULATION COMMANDS:\n"
" - linkhard\n"
" - linksoft\n"
" - linkread\n"
" - linklist\n"
" - unlink\n"
" - rename\n"
" - cpcat\n"
"\n"
" PIPELINE:\n"
" Example:\n"
" - pipes \"cat /etc/passwd\" \"cut -d: -f7\" \"sort\" \"uniq -c\"\n\n"
"\n"
" --------------------------------------------\n"
);
fflush(stdout);
exit_status = 0;
}
void action_status()
{
printf("%d\n", exit_status);
fflush(stdout);
exit_status = 0;
}
void action_print()
{
for (int i=1; i<token_count; i++)
{
if (i == token_count -1)
printf("%s", tokens[i]);
else
printf("%s ", tokens[i]);
fflush(stdout);
}
exit_status = 0;
}
void action_echo()
{
fflush(stdout);
for (int i=1; i<token_count; i++)
{
if (i == token_count -1)
printf("%s", tokens[i]);
else
printf("%s ", tokens[i]);
}
printf("\n");
fflush(stdout);
exit_status = 0;
}
void action_dirchange()
{
// We use default path if path was not given
char *path = "/";
if (token_count > 1)
path = tokens[1];
// chdir returns 0 on success
int error = chdir(path);
// There was no error
if (error == 0)
{
// memset(current_dir, 0, 255);
getcwd(current_dir, 255);
// printf("%d\n", error);
}
else
{
exit_status = error;
perror("dirchange");
}
}
// int mkdir(char *pathname, mode_t mode);
void action_dirmake()
{
// If mkdir fails it returns -1
if (mkdir(tokens[1], S_IRWXU) == -1)
{
exit_status = errno;
perror("dirmake");
}
}
// int rmdir(char *path);
void action_dirremove()
{
// If rmdir fails it returns -1
if (rmdir(tokens[1]) == -1)
{
exit_status = errno;
perror("dirremove");
}
}
void action_dirlist()
{
// If directory is not given, we
// use current working directory
char *dirname = ".";
DIR *dirp;
struct dirent *entry;
// Directory is given
if (token_count > 1)
dirname = tokens[1];
dirp = opendir(dirname);
if (dirp == NULL)
{
exit_status = errno;
perror("dirlist");
}
else
{
// readdir() returns a pointer to a structure
// representing the directory entry
while(entry = readdir(dirp))
{
if(entry->d_name[0])
printf("%s ", entry->d_name);
}
printf("\n");
fflush(stdout);
closedir(dirp);
}
}
// int rename(char *old_filename, char *new_filename)
void action_rename()
{
// if rename fails it returns -1
if (rename(tokens[1], tokens[2]) == -1)
{
exit_status = errno;
perror("rename");
}
}
// int link(char *oldpath, char *newpath);
void action_linkhard()
{
if (link(tokens[1], tokens[2]) == -1)
{
exit_status = errno;
perror("linkhard");
}
}
// int symlink(char *path1, char *path2);
void action_linksoft()
{
if (symlink(tokens[1], tokens[2]) == -1)
{
exit_status = errno;
perror("linksoft");
}
}
// int symlink(char *path1, char *path2);
void action_linkread()
{
char buf[255];
if (readlink(tokens[1], buf, 255) == -1)
{
exit_status = errno;
perror("linkread");
}
else
{
printf("%s\n", buf);
fflush(stdout);
}
}
void action_linklist()
{
// If directory is not given, we
// use current working directory
char *dirname = ".";
DIR *dirp;
struct dirent *entry;
struct stat fileStat;
int inode_file;
int fd = open(tokens[1], O_RDONLY);
if (fd == -1)
{
exit_status = errno;
perror("linklist");
}
if (fstat(fd, &fileStat) < 0)
{
exit_status = errno;
perror("linklist");
}
inode_file = fileStat.st_ino;
dirp = opendir(dirname);
if (dirp == NULL){
exit_status = errno;
perror("linklist");
}
else
{
// readdir() returns a pointer to a structure
// representing the directory entry
while(entry = readdir(dirp))
{
if ((int)entry->d_ino == (int)inode_file)
printf("%s ", entry->d_name);
}
printf("\n");
fflush(stdout);
closedir(dirp);
close(fd);
}
}
// int unlink(const char *path);
void action_unlink()
{
if (unlink(tokens[1]) == -1)
{
exit_status = errno;
perror("unlink");
}
}
void cpcat(int fd, int out)
{
char buf[255];
int bytes_read, bytes_write, err, nbytes = sizeof(buf);
while (1)
{
bytes_read = read(fd, buf, nbytes);
if (bytes_read == -1)
{
exit_status = errno;
perror("cpcat");
}
// Is empty
if (bytes_read == 0)
break;
err = write(out, &buf, bytes_read);
if (err == -1)
{
exit_status = errno;
perror("cpcat");
}
}
}
void action_cpcat()
{
int in = 0;
int out = 1;
// stdin to stdout
if (token_count == 1)
{
in = 0;
out = 1;
}
// file to stdout, write file
else if (token_count == 2)
{
in = open(tokens[1], O_RDONLY);
if (in == -1)
{
exit_status = errno;
perror("cpcat");
}
}
// stdin to file or fileA to fileB
else if (token_count == 3)
{
if (tokens[1][0] == '-')
in = 0;
else
{
in = open(tokens[1], O_RDONLY);
if (in == -1)
{
exit_status = errno;
perror("cpcat");
}
}
out = open(tokens[2], O_CREAT|O_WRONLY|O_TRUNC,S_IWRITE|S_IREAD);
if(out == -1)
{
exit_status = errno;
perror("cpcat");
}
}
cpcat(in, out);
}
void zunanji_ukaz()
{
int background = 0;
// Execution in background &
if (tokens[token_count-1][0] == '&')
{
background = 1;
token_count--;
tokens[token_count] = '\0';
// Copy without & in the end
memcpy(&tokens, &tokens, sizeof(tokens)-sizeof(tokens[token_count]));
}
pid_t pid = fork();
if (pid < 0)
exit(0);
// CHILD, execute command and exit
if (pid == 0)
{
tokens[token_count] = NULL;
execvp(tokens[0], tokens);
exit(0);
}
// PARENT, wait if process is not in background
if (pid > 0 && !background)
{
int status;
waitpid(pid, &status, 0);
}
}
// Remove zombies
void remove_zombie(int signum) {
int status;
waitpid(-1, &status, WNOHANG);
}
void preusmeritev()
{
fflush(stdout);
// Redirect stdout % who > names : Redirect standard output to a file named names
if (tokens[token_count-1][0] == '>')
{
char name[strlen(tokens[token_count-1])];
strcpy(name, tokens[token_count-1]+1);
name[strlen(name)] = '\0';
token_count--;
tokens[token_count] = '\0';
memcpy(&tokens, &tokens, sizeof(tokens)-sizeof(tokens[token_count]));
int fdout = open(name, O_CREAT|O_WRONLY,S_IWRITE|S_IREAD);
if (fdout == -1)
exit_status = errno;
dup2(fdout, STDOUT_FILENO);
close(fdout);
}
// Redirect stdin
if (tokens[token_count-1][0] == '<')
{
char name[255];
strncpy(name, tokens[token_count-1] + 1, strlen(tokens[token_count-1]));
token_count--;
int fd = open(name, O_RDONLY);
if (fd == -1)
exit_status = errno;
dup2(fd, STDIN_FILENO);
close(fd);
}
}
/************************************
* DRIVER FUNCTION
************************************/
// Determine action and call given function
void driver_function()
{
char *action = strdup(tokens[0]);
if (strcmp(action, "name") == 0) action_name();
else if (strcmp(action, "help") == 0) action_help();
else if (strcmp(action, "status") == 0) action_status();
else if (strcmp(action, "exit") == 0) exit(atoi(tokens[1]));
else if (strcmp(action, "print") == 0) action_print();
else if (strcmp(action, "echo") == 0) action_echo();
else if (strcmp(action, "pid") == 0) printf("%d\n", getpid());
else if (strcmp(action, "ppid") == 0) printf("%d\n", getppid());
else if (strcmp(action, "dirchange") == 0) action_dirchange();
else if (strcmp(action, "dirwhere") == 0) printf("%s\n", current_dir);
else if (strcmp(action, "dirmake") == 0) action_dirmake();
else if (strcmp(action, "dirremove") == 0) action_dirremove();
else if (strcmp(action, "dirlist") == 0) action_dirlist();
else if (strcmp(action, "rename") == 0) action_rename();
else if (strcmp(action, "linkhard") == 0) action_linkhard();
else if (strcmp(action, "linksoft") == 0) action_linksoft();
else if (strcmp(action, "linkread") == 0) action_linkread();
else if (strcmp(action, "linklist") == 0) action_linklist();
else if (strcmp(action, "unlink") == 0) action_unlink();
else if (strcmp(action, "cpcat") == 0) action_cpcat();
// Shell Builtin commands
else zunanji_ukaz();
}
/************************************
* MAIN
************************************/
int main(int argc, char* args[])
{
signal(SIGCHLD, remove_zombie);
current_dir = malloc(sizeof(255));
getcwd(current_dir, 255);
int is_terminal = isatty(0);
char line[255];
if(is_terminal)
{
printf("%s> ", terminal_name);
fflush(stdout);
}
// ctrl+d or ctrl+z or ctrl+c to terminate
while(fgets (line, 255, stdin))
{
tokenize(line);
current_in = dup(STDIN_FILENO);
current_out = dup(STDOUT_FILENO);
if (token_count > 0 && tokens[0][0] != '#')
{
// Determine redirections, depending on < or >
preusmeritev();
driver_function();
fflush(stdout);
// Restore descriptors
dup2(current_in, STDIN_FILENO);
dup2(current_out, STDOUT_FILENO);
}
if(is_terminal)
{
printf("%s> ", terminal_name);
fflush(stdout);
}
fflush(stdout);
memset(line, 0, 255);
}
free(current_dir);
return 0;
}