This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathash.c
More file actions
204 lines (171 loc) · 4.78 KB
/
ash.c
File metadata and controls
204 lines (171 loc) · 4.78 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define ASH_BUFSIZE 1024
#define ASH_TOKEN_BUFSIZE 64
#define ASH_TOKEN_DELIM " \t\r\n\a"
int ash_cd(char **args);
int ash_exit(char **args);
int ash_pwd(char **args);
char *builtin_str[] = {
"cd",
"exit",
"pwd",
};
int (*builtin_func[]) (char **) = {
&ash_cd,
&ash_exit,
&ash_pwd,
};
int ash_num_builtins()
{
return sizeof(builtin_str) / sizeof(char *);
}
int ash_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "ash: unexpected argument\n");
} else {
if (chdir(args[1]) != 0)
perror("ash");
}
return 1;
}
int ash_pwd(char **args)
{
char cwd[ASH_TOKEN_BUFSIZE];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
} else {
perror("ash");
}
return 1;
}
int ash_exit(char **args)
{
return 0;
}
char* ash_read_line(void)
{
int bufsize = ASH_BUFSIZE;
int pos = 0;
int c;
//allocate primary buffer size to read users input
char *buffer = malloc(sizeof(char) * bufsize);
if (!buffer) {
fprintf(stderr, "ash: memory allocation error\n");
exit(EXIT_FAILURE);
}
while (true) {
c = getchar();
if (c == '\n') {
buffer[pos] = '\0';
return buffer;
} else if (c == EOF) {
buffer[pos] = EOF;
return buffer;
} else {
buffer[pos] = c;
}
pos++;
// if memory is not enough for input
if (pos >= bufsize) {
bufsize += ASH_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "ash: memory reallocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
char** ash_parse_line(char* line)
{
int bufsize = ASH_TOKEN_BUFSIZE;
int pos = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;
if (!tokens) {
fprintf(stderr, "ash: memory allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, ASH_TOKEN_DELIM);
if (*token == EOF) {
tokens[pos] = "exit";
return tokens;
}
while (token != NULL) {
tokens[pos] = token;
pos++;
if (pos >= bufsize) {
bufsize += ASH_TOKEN_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
fprintf(stderr, "ash: memory allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, ASH_TOKEN_DELIM);
}
tokens[pos] = NULL;
return tokens;
}
int ash_run(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
// It's a child
if (pid == 0) {
// trying to execute command
if (execvp(args[0], args) == -1)
perror("ash");
exit(EXIT_FAILURE);
} else if (pid < 0) {
// forking goes wrong
perror("ash");
} else {
// it's parent
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return wpid;
}
int ash_exec(char **args)
{
// input is empty
if (args[0] == NULL)
return 1;
// check, if input from builtins
for (int i = 0; i < ash_num_builtins(); i++) {
if (!strcmp(args[0], builtin_str[i])) {
return (*builtin_func[i])(args);
}
}
return ash_run(args);
}
void ash_loop(void)
{
char *line;
char **parsed;
int status = 1;
do {
// TODO replace with customizable input char
printf("> ");
line = ash_read_line();
parsed = ash_parse_line(line);
status = ash_exec(parsed);
free(line);
free(parsed);
} while (status);
}
int main (int argc, char *argv[])
{
// main shell loop
ash_loop();
return 0;
}