-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminsh.c
201 lines (178 loc) · 4.25 KB
/
minsh.c
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
/*
* Minimal Shell Project
*
*/
/*** includes ***/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <wait.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include "minsh.h"
/*** defines ***/
#define TRUE 1
#define BUFSIZE 1024
#define TOK_BUF 64
/*** shell builtin functions implementations. ***/
int total_number_of_builtins() {
return sizeof(builtin_str) / sizeof(char *);
}
/* cd function is needed to be builtin function, because every process has a working directory in linux, cannot change working directory from outside of a process.*/
int func_cd(char **args)
{
if (args[1] == NULL) { fprintf(stderr, "expected argument for cd \n"); }
else
{
if (chdir(args[1]) != 0) { perror("minsh"); }
}
return 1;
}
int func_help()
{
int i;
printf("minsh help test \n");
printf("builtin functions : ");
for (i = 0; i < total_number_of_builtins(); i++) {
printf(" %s\n", builtin_str[i]);
}
return 1;
}
int func_exit()
{
exit(EXIT_SUCCESS);
}
void handle_error(const char *prompt)
{
perror(prompt);
exit(EXIT_FAILURE);
}
/*** initialization ***/
int main()
{
main_loop();
}
int execute_loop(char **args)
{
int i;
if (args[0] == NULL) { return 1; }
for (i = 0; i < total_number_of_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}
return exec_cmd(args);
}
/*main shell loop*/
void main_loop()
{
char *line;
char **args;
while(TRUE)
{
printf("minsh -> ");
line = read_line();
/*debug*/
//printf("line is %s \n",line);
args = split_line(line);
execute_loop(args);
free(line);
free(args);
}
}
/*splits line by whitespace*/
char** split_line(char* line)
{
int pos = 0;
int bufsize = TOK_BUF; //64 bytes
char *tok;
char **toks;
tok = strtok(line, " ");
toks = malloc(bufsize * sizeof(char *));
while(tok != NULL)
{
/*debug*/
//printf("%s\n", to);
toks[pos] = tok;
pos++;
/*handle memory allocation problem*/
if(pos >= bufsize)
{
/*double the size of the buffer if pos is more than buf*/
bufsize += TOK_BUF;
toks = realloc(toks, bufsize *sizeof(char*));
if(!toks)
{
fprintf(stderr, "minsh : alloc error ! \n");
exit(EXIT_FAILURE);
}
}
tok = strtok(NULL, " ");
}
toks[pos] = NULL;
return toks;
}
/*read user input char by char until eof or newline*/
char* read_line()
{
int bufsize = BUFSIZE;
char *line = (char*)malloc(sizeof(char*) * BUFSIZE);
int i = 0;
char c;
while(TRUE)
{
c = getchar();
if(c == EOF || c == '\n') { line[i] = '\0'; return line; }
else
{
if(i >= bufsize)
{
bufsize += BUFSIZE;
line = realloc(line, bufsize * sizeof(char*));
if(!line)
{
fprintf(stderr, "minsh : alloc error !\n");
exit(EXIT_FAILURE);
}
}
line[i] = c;
}
i++;
}
}
/*execute splitted input*/
int exec_cmd(char **args)
{
/*create a new process, execute arguments comes from split line with new process, wait until it finishes, then terminate the process*/
pid_t child_ps, wait;
int waitstatus;
switch (child_ps = fork())
{
case -1:
perror("cannot create child process");
exit(EXIT_FAILURE);
case 0:
/*code to be executed by child*/
if (execvp(args[0], args) == -1)
{
handle_error("execvp err ");
}
break;
case 1:
/*code to be executed by parent*/
wait = waitpid(child_ps, &waitstatus, WUNTRACED | WCONTINUED);
if(wait == -1) { perror("waitpid"); exit(EXIT_FAILURE); }
do
{
if(WIFEXITED(waitstatus)) {printf("exited\n");}
else if(WIFSIGNALED(waitstatus)) {puts("signaled");}
}while(!WIFEXITED(waitstatus) && !WIFSIGNALED(waitstatus));
}
/*indicates successful execution.*/
return 1;
}