-
Notifications
You must be signed in to change notification settings - Fork 1
/
My_Shell.c
62 lines (47 loc) · 1.55 KB
/
My_Shell.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
#include <stdio.h>
#include <stdlib.h>
#include "./lib/Lib.c"
#define MAX_COMMAND_LENGTH 1024
// Main function
int main(){
char command[MAX_COMMAND_LENGTH];
char arg1[MAX_COMMAND_LENGTH], arg2[MAX_COMMAND_LENGTH];
while (1){
// printf("NerdyGamer611@Desktop:~/> ");
display_prompt();
fgets(command, sizeof(command), stdin);
command[strcspn(command, "\n")] = 0; // Remove newline character
if (strcmp(command, "pwd") == 0){
display_pwd();
}
else if (strcmp(command, "exit") == 0){
printf("\033[35m\nExiting my_shell...\n\033[0m");
sleep(1);
break;
}
else if (strcmp(command, "help") == 0) {
display_commands();
}
else if (sscanf(command, "mv %s %s", arg1, arg2) == 2) {
move_file(arg1, arg2);
}
else if (strncmp(command, "grep ", 5) == 0){
sscanf(command, "grep %s %s", arg1, arg2);
search_in_file(arg1, arg2);
}
else if (strncmp(command, "split ", 6) == 0){
sscanf(command, "split %s %s", arg1, arg2);
int num_chunks = atoi(arg2);
if (num_chunks <= 0){
printf("\033[31mError: Number of chunks must be a positive integer.\n\033[0m");
}
else{
split_file(arg1, num_chunks);
}
}
else{
printf("\033[31mError: Unrecognized command:\033[0m '%s'\n", command);
}
}
return EXIT_SUCCESS;
}