-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
152 lines (135 loc) · 3.4 KB
/
shell.c
File metadata and controls
152 lines (135 loc) · 3.4 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#define CMD_BUFSIZE 1024
void cmd_loop(void);
char *cmd_read_line();
char **cmd_split_line(char *);
int cmd_execute(char **);
int cmd_launch(char **);
int main(int argc, char **argv) {
cmd_loop();
return 0;
}
void cmd_loop(void) {
char *line;
char **args;
int status;
printf("\nThis shell is created for educational purposes.\n");
printf("It demonstrates basic shell functionalities:\n\n");
printf(" - Reading user commands\n");
printf(" - Parsing command arguments\n");
printf(" - Executing external programs\n");
printf(" - Currently working commands:\n");
printf(" * cd : change directory\n");
printf(" * exit : terminate the shell\n");
printf(" - Process creation using fork()\n");
printf(" - Program execution using execvp()\n");
printf(" - Parent-child synchronization using waitpid()\n");
printf("========================================\n\n");
do {
printf("=> ");
line = cmd_read_line();
args = cmd_split_line(line);
status = cmd_execute(args);
free(line);
free(args);
} while (status);
}
// traditional method is used. But getline() also can be used.
char *cmd_read_line() {
int bufsize = CMD_BUFSIZE;
int position = 0;
char *buffer = malloc(sizeof(char) * bufsize);
int c;
if (!buffer) {
fprintf(stderr, "memory allocation error\n");
exit(1);
}
while (1) {
c = getchar();
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
if (position >= bufsize) {
bufsize += CMD_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "memory allocation error\n");
exit(1);
}
}
}
}
// initial token buffer size, later reallocated. 64 words can be stored a
// maximum.
#define CMD_TOKEN_BUFSIZE 64
// delimiters " " - space, \t - tab, \r - move cursor to start, \n - new line,
// \a - alert
#define CMD_TOKEN_DELIM " \t\r\n\a"
char **cmd_split_line(char *line) {
int bufsize = CMD_TOKEN_BUFSIZE, position = 0;
char **tokens = malloc(sizeof(char *) * bufsize);
char *token;
if (!tokens) {
fprintf(stderr, "memory allocation error\n");
exit(1);
}
token = strtok(line, CMD_TOKEN_DELIM);
while (token != NULL) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += CMD_TOKEN_BUFSIZE;
tokens = realloc(tokens, sizeof(char *) * bufsize);
if (!tokens) {
fprintf(stderr, "memory allocation error\n");
exit(1);
}
}
token = strtok(NULL, CMD_TOKEN_DELIM);
}
tokens[position] = NULL;
return tokens;
}
int cmd_launch(char **args) {
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("cmd");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
perror("cmd");
} else {
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int cmd_execute(char **args) {
if (args[0] == NULL) {
return 1;
}
if (strcmp(args[0], "exit") == 0) {
return 0;
}
if (strcmp(args[0], "cd") == 0) {
if (args[1]) {
chdir(args[1]);
} else {
fprintf(stderr, "cmd: expected argument to \"cd\"\n");
return 1;
}
}
return cmd_launch(args);
}