-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
172 lines (139 loc) · 3.45 KB
/
Copy pathshell.c
File metadata and controls
172 lines (139 loc) · 3.45 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
#include <unistd.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>
#define BUFSIZE 1024
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_RESET "\x1b[0m"
void throw_err(char* description) {
fprintf(stderr, description);
exit(EXIT_FAILURE);
}
char *builtin_str[] = {
"clear",
"cd",
"help",
"exit"
};
int sh_num_builtins() {
return sizeof(builtin_str) / sizeof(char *);
}
int sh_clear(char **args) {
printf("\033[H\033[2J"); // Moves cursor to the top left corner of the screen and clears it
return 1;
}
void sh_print_logo() {
printf(ANSI_COLOR_GREEN " ____ _ _ _ \n");
printf(ANSI_COLOR_GREEN "/ ___|| |__ ___| | |\n");
printf(ANSI_COLOR_GREEN "\\___ \\| '_ \\ / _ \\ | |\n");
printf(ANSI_COLOR_GREEN " ___) | | | | __/ | |\n");
printf(ANSI_COLOR_GREEN "|____/|_| |_|\\___|_|_|\n" ANSI_COLOR_RESET);
}
int sh_help(char** args) {
sh_clear(args);
sh_print_logo();
printf("Enter commands and their arguments, then press Enter.\n");
printf(ANSI_COLOR_YELLOW "Available built-in commands:\n" ANSI_COLOR_RESET);
for (int i = 0; i < sh_num_builtins(); i++) {
printf(" - " ANSI_COLOR_GREEN "%s\n" ANSI_COLOR_RESET, builtin_str[i]);
}
printf("Any other command will be treated as an external program.\n");
return 1;
}
int sh_cd(char **args) {
if (args[1] == NULL) fprintf(stderr, "cd: expected argument, none provided.");
else if (chdir(args[1])) perror("sh_cd");
return 1;
}
int sh_exit(char** args) {
return 0;
}
int (*builtin_func[]) (char **) = {
&sh_clear,
&sh_cd,
&sh_help,
&sh_exit
};
char* sh_read_line() {
char *line = NULL;
size_t bufsize = 0;
if (getline(&line, &bufsize, stdin) == -1) {
if (feof(stdin)) {
exit(EXIT_SUCCESS);
} else {
perror("READ_LINE_ERROR");
exit(EXIT_FAILURE);
}
}
return line;
}
char** sh_split_line(char* line) {
int bufsize = BUFSIZE;
char **tokens = malloc(bufsize);
if (!tokens) throw_err("SPLIT_LINE_ALLOC_ERROR_1");
char* token = NULL;
char* delim = " \t\n";
token = strtok(line, delim);
for (int i = 0; token != NULL; i++) {
tokens[i] = token;
// Reallocate memory for tokens array if it's not enough
if (i >= bufsize) {
bufsize += BUFSIZE;
tokens = realloc(tokens, bufsize);
if (!tokens) throw_err("SPLIT_LINE_ALLOC_ERROR_2");
}
token = strtok(NULL, delim);
}
return tokens;
}
int sh_launch(char** args) {
pid_t pid, wpid;
int status;
pid = fork();
if (!pid) { // Child
// Expects program name and array of arguments
if (execvp(args[0], args) == -1) {
perror("sh_execute");
}
exit(EXIT_FAILURE);
} else if (pid == -1) {
// Error forking
perror("sh_execute");
} else { // Parent
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSTOPPED(status)); // Run until child is either exited or killed
}
return 1;
}
int sh_execute(char **args) {
if (!args[0]) return 1;
// Execute built-in
for (int i = 0; i < sh_num_builtins(); i++) {
if (!strcmp(args[0], builtin_str[i])) return (*builtin_func[i])(args);
}
// Else run program;
return sh_launch(args);
}
void sh_loop() {
char* line;
char** args;
int status;
do {
printf("> ");
// Read line from terminal
line = sh_read_line();
// Split into arguments
args = sh_split_line(line);
// Execute
status = sh_execute(args);
} while(status);
}
int main() {
sh_help(NULL);
sh_loop();
return 0;
}