-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_executor.c
More file actions
70 lines (60 loc) · 1.7 KB
/
Copy pathcmd_executor.c
File metadata and controls
70 lines (60 loc) · 1.7 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
char line[1024];
char *args[64];
while (1) {
// 1. Display the prompt
printf("cmd> ");
fflush(stdout);
// 2. Read input from the user
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
// Remove the newline character (\n) from the end of the string
line[strcspn(line, "\n")] = '\0';
// Handle empty line input (if user just presses Enter)
if (strlen(line) == 0) {
continue;
}
// 3. Tokenize the input string by spaces
int i = 0;
args[i] = strtok(line, " ");
while (args[i] != NULL) {
i++;
args[i] = strtok(NULL, " ");
}
// 4. Handle built-in commands
if (strcmp(args[0], "exit") == 0) {
break;
}
else if (strcmp(args[0], "pwd") == 0) {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
}
continue;
}
// 5. Execute external commands by creating a child process
pid_t pid = fork();
if (pid < 0) {
// Fork failed error
printf("fork failed\n");
}
else if (pid == 0) {
// Child process: Execute the command
if (execvp(args[0], args) < 0) {
printf("command not found: %s\n", args[0]);
exit(1);
}
}
else {
// Parent process: Wait for child to prevent zombie processes
wait(NULL);
}
}
return 0;
}