Repository navigation
Expand file tree
/
Copy pathshell.c
More file actions
296 lines (239 loc) · 6.75 KB
/
Copy pathshell.c
File metadata and controls
296 lines (239 loc) · 6.75 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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <dirent.h>
#include <sys/stat.h>
#include <limits.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
char *find_executeable(const char *name) {
char *path = getenv("PATH");
if (!path) return NULL;
char *path_copy = strdup(path);
char *dir = strtok(path_copy, ":");
while (dir != NULL) {
char full_path[PATH_MAX];
snprintf(full_path, sizeof(full_path), "%s/%s", dir, name);
struct stat st;
if (stat(full_path, &st) == 0 &&
S_ISREG(st.st_mode) &&
(st.st_mode & S_IXUSR)) {
free(path_copy);
return strdup(full_path);
}
dir = strtok(NULL, ":");
}
free(path_copy);
return NULL;
}
char **parse_command(char *line, int *argc) {
char **argv = malloc(64 * sizeof(char *));
if (!argv) {
perror("malloc");
exit(EXIT_FAILURE);
}
char token[1024];
int tlen = 0;
bool in_single = false;
bool in_double = false;
*argc = 0;
for (int i = 0;; i++) {
char c = line[i];
if (c == '\0') {
if (tlen > 0) {
token[tlen] = '\0';
argv[(*argc)++] = strdup(token);
}
break;
}
if ((!in_single && !in_double && c == '\\') || (in_double && c == '\\')) {
i++;
c = line[i];
if (c == '\0') break;
token[tlen++] = c;
continue;
}
if (!in_single && !in_double && (c == ' ' || c == '\t')) {
if (tlen > 0) {
token[tlen] = '\0';
argv[(*argc)++] = strdup(token);
tlen = 0;
}
continue;
}
if (!in_double && c == '\'') {
in_single = !in_single;
continue;
}
if (!in_single && c == '"') {
in_double = !in_double;
continue;
}
token[tlen++] = c;
}
argv[*argc] = NULL;
return argv;
}
bool execute_builtin(int argc, char **argv)
{
if (strcmp(argv[0], "exit") == 0) {
exit(0);
}
if (strcmp(argv[0], "cd") == 0) {
const char *path;
if (argc < 2) {
path = getenv("HOME");
if (!path) {
fprintf(stderr, "cd: HOME not set\n");
return true;
}
} else {
if (strcmp(argv[1], "~") == 0) {
path = getenv("HOME");
if (!path) {
fprintf(stderr, "cd: HOME not set\n");
return true;
}
} else {
path = argv[1];
}
}
if (chdir(path) != 0) {
switch (errno) {
case ENOENT:
fprintf(stderr, "cd: %s: No such file or directory\n", path);
break;
case EACCES:
fprintf(stderr, "cd: %s: Permission denied\n", path);
break;
default:
perror("cd");
}
}
return true;
}
if (strcmp(argv[0], "pwd") == 0) {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("%s\n", path);
} else {
perror("pwd");
}
return true;
}
if (strcmp(argv[0], "echo") == 0) {
for (int i = 1; i < argc; i++) {
printf("%s", argv[i]);
if (i != argc - 1)
printf(" ");
}
printf("\n");
return true;
}
if (strcmp(argv[0], "type") == 0) {
if (argc < 2) {
printf("type: missing argument\n");
return true;
}
const char *builtins[] = {
"exit",
"echo",
"type",
"pwd",
"cd"
};
size_t builtinLen = sizeof(builtins) / sizeof(builtins[0]);
for (size_t i = 0; i < builtinLen; i++) {
if (strcmp(argv[1], builtins[i]) == 0) {
printf("%s is a shell builtin\n", argv[1]);
return true;
}
}
char *exe = find_executeable(argv[1]);
if (exe) {
printf("%s\n", exe);
free(exe);
} else {
printf("%s: not found\n", argv[1]);
}
return true;
}
return false;
}
void execute_external(char **argv)
{
pid_t pid = fork();
if (pid == 0) {
execvp(argv[0], argv);
fprintf(stderr, "%s: command not found\n", argv[0]);
exit(EXIT_FAILURE);
} else if (pid > 0) {
waitpid(pid, NULL, 0);
} else {
perror("fork");
}
}
int main(void) {
setbuf(stdout, NULL);
while (1) {
printf("$ ");
char line[1024];
if (!fgets(line, sizeof(line), stdin))
break;
line[strcspn(line, "\n")] = '\0';
int argc;
char **argv = parse_command(line, &argc);
char *redirect_file = NULL;
int redirect_target = -1;
bool append = false;
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], ">") == 0
|| strcmp(argv[i], "1>") == 0
|| strcmp(argv[i], "2>") == 0
|| strcmp(argv[i], ">>") == 0
|| strcmp(argv[i], "1>>") == 0
|| strcmp(argv[i], "2>>") == 0) {
if (strcmp(argv[i], ">") == 0 || strcmp(argv[i], "1>") == 0) redirect_target = STDOUT_FILENO;
if (strcmp(argv[i], "2>") == 0) redirect_target = STDERR_FILENO;
if (strcmp(argv[i], "2>>") == 0) {
redirect_target = STDERR_FILENO;
append = true;
}
if (strcmp(argv[i], ">>") == 0 || strcmp(argv[i], "1>>") == 0) {
redirect_target = STDOUT_FILENO;
append = true;
}
redirect_file = argv[i + 1];
argv[i] = NULL;
argc = i;
break;
}
}
if (argc == 0) {
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
continue;
}
int saved_fd = -1;
if (redirect_file) {
saved_fd = dup(redirect_target);
int flags = append ? O_WRONLY | O_CREAT | O_APPEND
: O_WRONLY | O_CREAT | O_TRUNC;
int redirect_fd = open(redirect_file, flags, 0644);
dup2(redirect_fd, redirect_target);
close(redirect_fd);
}
if (!execute_builtin(argc, argv))
execute_external(argv);
if (redirect_file) {
dup2(saved_fd, redirect_target);
close(saved_fd);
}
free(argv);
}
return 0;
}