-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
217 lines (169 loc) · 4.52 KB
/
main.c
File metadata and controls
217 lines (169 loc) · 4.52 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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
void print_error() {
char error_message[30] = "An error has occurred\n";
write(STDERR_FILENO, error_message, strlen(error_message));
}
int main(int argc, char *argv[]) {
printf(" \n");
printf(" ▄▄▄▄ ▄▄ ▄▄ ▄▄ ▄▄ ▄▄ \n");
printf("██▀▀▀ ██▄██ ██ ██ ██ \n");
printf("▀████ ██ ██ ██ ██▄▄▄ ██▄▄▄ \n");
printf(" \n");
printf(" \n");
FILE *input;
int interactive;
char *line = NULL;
size_t len = 0;
ssize_t nread;
char *paths[100];
int path_count;
paths[0] = "/bin";
path_count = 1;
pid_t pids[100];
int pid_count = 0;
if (argc == 1) {
interactive = 1;
} else if (argc == 2) {
interactive = 0; // batch mode
} else {
print_error();
exit(1);
}
input = stdin;
if (argc == 2) {
input = fopen(argv[1], "r");
if (input == NULL) {
print_error();
exit(1);
}
}
// random emoji
const char *emojis[] = {"🌊", "❄️", "😌", "🌿"};
srand(time(NULL));
int emoji_count = sizeof(emojis) / sizeof(emojis[0]);
int index = rand() % emoji_count;
while (1) {
if (interactive) {
printf("%s chill> ", emojis[index]);
fflush(stdout);
}
nread = getline(&line, &len, input);
if (nread == -1) {
exit(0);
}
line[strcspn(line, "\n")] = '\0'; // delete white space
if (strcmp(line, "exit") == 0) { // compare
exit(0);
}
char *saveptr1, *saveptr2;
pid_count = 0;
// spilt by &
char *cmd_str = strtok_r(line, "&", &saveptr1);
while (cmd_str != NULL) {
char *args[100];
int i = 0;
char *token = strtok_r(cmd_str, " \t", &saveptr2);
while (token != NULL) {
args[i++] = token;
token = strtok_r(NULL, " \t", &saveptr2);
}
args[i] = NULL;
// cd
if (args[0] != NULL && strcmp(args[0], "cd") == 0) {
int args_count = 0;
for (int j = 1; args[j] != NULL; j++)
args_count++;
if (args_count > 1) {
print_error();
continue; // skip to nextt command
}
if (args[1] == NULL || strcmp(args[1], "~") == 0) {
args[1] = getenv("HOME");
}
if (chdir(args[1]) != 0) {
print_error();
}
cmd_str = strtok_r(NULL, "&", &saveptr1);
continue;
}
// path
if (args[0] != NULL && strcmp(args[0], "path") == 0) {
if (args[1] == NULL) {
path_count = 0;
paths[0] = NULL;
continue;
}
path_count = 0;
for (int j = 1; args[j] != NULL; j++) {
paths[path_count++] = strdup(args[j]);
}
// path testing
for (int i = 0; i < path_count; i++)
printf("%s\n", paths[i]);
cmd_str = strtok_r(NULL, "&", &saveptr1);
continue;
}
if (path_count == 0) {
print_error();
continue;
}
// redirection
char *redirect_file = NULL;
for (int j = 1; args[j] != NULL; j++) {
if (strcmp(args[j], ">") == 0) {
if (args[j + 1] == NULL || args[j + 2] != NULL) {
print_error();
redirect_file = NULL;
} else {
args[j] = NULL;
redirect_file = args[j + 1];
}
break;
}
}
pid_t pid = fork();
if (pid < 0) {
print_error();
}
if (pid == 0) {
if (redirect_file != NULL) {
int fd = open(redirect_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
print_error();
}
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
// search path and exec
char full_path[1024];
for (int j = 0; j < path_count; j++) {
snprintf(full_path, sizeof(full_path), "%s/%s", paths[j], args[0]);
if (access(full_path, X_OK) == 0) {
execv(full_path, args);
print_error();
exit(1);
}
}
print_error();
exit(1);
} else {
pids[pid_count++] = pid;
}
cmd_str = strtok_r(NULL, "&", &saveptr1);
}
for (int i = 0; i < pid_count; i++) {
waitpid(pids[i], NULL, 0);
}
}
if (argc == 2) {
fclose(input);
}
return 0;
}