-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
82 lines (65 loc) · 1.63 KB
/
Copy pathshell.c
File metadata and controls
82 lines (65 loc) · 1.63 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_INPUT 1024
#define MAX_ARGS 64
char **parse_input(char *input);
void execute(char **args);
int main() {
char input[MAX_INPUT];
while (1) {
printf("myshell> ");
fflush(stdout);
if (fgets(input, MAX_INPUT, stdin) == NULL) {
printf("\n");
break;
}
input[strcspn(input, "\n")] = 0;
if (strlen(input) == 0) continue;
if (strcmp(input, "exit") == 0) {
printf("Goodbye!\n");
break;
}
char **args = parse_input(input);
if (strcmp(args[0], "cd") == 0) {
if (args[1] == NULL) {
chdir(getenv("HOME"));
} else {
if (chdir(args[1]) != 0) {
perror("cd failed");
}
}
} else {
execute(args);
}
free(args);
}
return 0;
}
char **parse_input(char *input) {
char **args = malloc(MAX_ARGS * sizeof(char *));
char *input_copy = strdup(input);
int i = 0;
char *token = strtok(input_copy, " ");
while (token != NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
return args;
}
void execute(char **args) {
pid_t pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
printf("myshell: command not found: %s\n", args[0]);
exit(1);
}
} else if (pid > 0) {
wait(NULL);
} else {
perror("fork failed");
}
}