-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
255 lines (246 loc) · 6.71 KB
/
Copy pathshell.c
File metadata and controls
255 lines (246 loc) · 6.71 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <linux/limits.h>
#include <sys/resource.h>
#define LOW8BIT(x) ((x) & 0xFF)
int status;
char * io_tokens[10];
int redirector(int type, char *iotoken){
char * filename;
int fd;
int offset;
int mode;
int orig;
switch(type)
{
case 1:
{
offset = 1;
mode = O_RDONLY;
orig = STDIN_FILENO;
break;
}
case 2:
{
offset = 1;
mode = O_WRONLY | O_CREAT | O_TRUNC;
orig = STDOUT_FILENO;
break;
}
case 3:
{
offset = 2;
mode = O_WRONLY | O_CREAT | O_TRUNC;
orig = STDOUT_FILENO;
break;
}
case 4:
{
offset = 3;
mode = O_WRONLY | O_CREAT | O_APPEND;
orig = STDOUT_FILENO;
break;
}
case 5:
{
offset = 4;
mode = O_WRONLY | O_CREAT | O_APPEND;
orig = STDERR_FILENO;
break;
}
default:
return -1;
}
if((fd = open(iotoken + offset, mode, 0666)) < 0){
fprintf(stderr, "Error no such file or directory : %s \n", iotoken);
return -1;
}
if(dup2(fd, orig) < 0){
fprintf(stderr, "Error with redirection: %s\n", strerror(errno));
return -1;
}
close(fd);
return 0;
}
int lex(char * token){
switch(token[0]){
case '<':
return 1;
break;
case '>':
if(token[1] == '>'){
return 4;
} else {
return 2;
}
break;
case '2':
if(token[1] == '>' && token[2] == '>'){
return 5;
} else if(token[1] == '>'){
return 3;
} else {
return 0;
}
break;
default:
return 0;
}
}
int exitcode(int status){
if(WIFEXITED(status)){
return WEXITSTATUS(status);
} else if(WIFSIGNALED(status)){
return LOW8BIT(status);
}
return 1;
}
char ** tokenize(char * line, int len){
char * token;
char ** tokens = malloc(100 * sizeof(char *));
token = strtok(line, " \t");
int pos_io = 0;
int pos = 0;
while(token != NULL){
int type = lex(token);
if(type != 0){
io_tokens[pos_io++] = token;
} else {
tokens[pos++] = token;
}
token = strtok(NULL, " \t");
}
tokens[pos] = NULL;
return tokens;
}
void execute(char ** tokens){
struct rusage usage;
struct timeval current_time;
struct timeval end;
gettimeofday(¤t_time, NULL);
int pid = fork();
if(pid < 0){
fprintf(stderr, "Error with forking process: %s\n", strerror(errno));
return;
}
if(pid == 0){
int i = 0;
while(io_tokens[i]){
if(redirector(lex(io_tokens[i]), io_tokens[i]) != 0){
exit(1);
}
i++;
}
if(execvp(tokens[0], tokens) == -1){
fprintf(stderr, "Error:");
if(errno == ENOENT){
fprintf(stderr, "command not found: %s\n", tokens[0]);
} else {
fprintf(stderr, " %s\n", strerror(errno));
}
exit(127);
}
}
if(wait3(&status, 0, &usage) < 0){
fprintf(stderr, "Error with waiting for child process stat. : %s", strerror(errno));
}
gettimeofday(&end, NULL);
fprintf(stderr, "Child process %d ", pid );
if(WIFEXITED(status)){
fprintf(stderr, "exited normally with return %d \n", WEXITSTATUS(status));
} else if(WIFSIGNALED(status)){
if(WCOREDUMP(status)){
fprintf(stderr, "exited with signal %d", WTERMSIG(status));
fprintf(stderr, "(segmentation fault) \n");
} else {
fprintf(stderr, "exited with signal %d \n", WTERMSIG(status));
}
}
fprintf(stderr, "Real: %.3fs ", (double)(end.tv_sec - current_time.tv_sec) + (double)(end.tv_usec - current_time.tv_usec)/1000000);
fprintf(stderr, "User: %ld.%.03lds ", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec/1000);
fprintf(stderr, "Sys: %ld.%.03lds \n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec/1000);
for(int i = 0; i < 10; i++){
io_tokens[i] = NULL;
}
}
int main(int argc, char * argv[]){
FILE *fp;
char *line = NULL;
char **tokens;
size_t len = 0;
if(argc == 1){
fp = stdin;
} else {
fp = fopen(argv[1], "r");
if(fp == NULL){
fprintf(stderr, "Error: %s : %s \n", strerror(errno), argv[1]);
exit(1);
}
}
while(1){
if(fp == stdin){
//We should personalize the name of our shell haha :)
printf("ZASH> ");
fflush(stdout);
}
if(getline(&line, &len, fp) != -1){
if(line[0] == '#' || line[0] == '\n'){
continue;
}
line[strlen(line) - 1] = '\0';
tokens = tokenize(line, len);
if(strcmp(tokens[0], "cd") == 0){
int change = 0;
if(tokens[1] == NULL){
change = chdir(getenv("HOME"));
} else {
change = chdir(tokens[1]);
}
if(change != 0){
fprintf(stderr, "Error: %s : %s \n", strerror(errno), tokens[1]);
}
free(tokens);
continue;
} else if(strcmp(tokens[0], "exit") == 0){
if(tokens[1] != NULL){
if(fp != stdin){
fclose(fp);
}
exit(atoi(tokens[1]));
} else {
if(fp != stdin){
fclose(fp);
}
exit(exitcode(status));
}
} else if(strcmp(tokens[0], "pwd") == 0){
char curr [PATH_MAX];
memset(curr, PATH_MAX, 0);
if(!getcwd(curr, PATH_MAX)){
perror("unable to get access to current dir");
continue;
}
printf("%s\n", curr);
free(tokens);
continue;
}
execute(tokens);
free(tokens);
} else {
if(fp != stdin){
fclose(fp);
}
free(line);
exit(exitcode(status));
}
}
return 0;
}