-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtushell.c
349 lines (244 loc) · 8.44 KB
/
gtushell.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#define EXIT "exit"
int parse(char *line, char *tokens[]);
void helpPrint();
void checkCommand(char *commandLine);
void execCommands(char *tokens[], int count, char mainPath[]);
int cdir(char *directory);
int chTokenNum(char *tokens[], int tokenCount,char *ch);
void genericFunc(char *command, char *arg[], char *startPath);
char* getPrevCommand(int prevNum,char *prevCommand[],int SumCommand);
int getPreviousNum(char *input);
void toRealloc(char *arg[], int size);
void toMalloc(char *arg[], int size, char *tokens[]);
void sig_handler(int signo);
int main (void) {
char input[MAX_CANON];
char *tokens[1024];
int tokenCount;
char startPath[1024];
char prevCommand[100][1024];
int commandCount = 0;
int i;
getcwd(startPath, 1024);
while(1){
fflush(stdout);
fprintf(stdout, "%s","gtushell>>");
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ndidnt execute\n");
if (fgets(input,MAX_CANON,stdin) == NULL)
continue;
if(strcmp(input,"\0") == 0 || strcmp(input,"\n") == 0)
continue;
checkCommand(input);
//Gecerli gecersiz her komutu kopyaladım
strcpy (prevCommand[commandCount], input);
commandCount++;
//Gecersiz bir sayı girme durumu ve ! kontrolü
if(input[0] == '!' && ( commandCount > getPreviousNum(input) ) ){
tokenCount = parse(prevCommand[commandCount - getPreviousNum(input) - 1 ], tokens);
if (tokenCount < 0) {
fprintf(stderr, "Failed to parse command line\n");
exit(1);
}
execCommands(tokens,tokenCount,startPath);
continue;
}
tokenCount = parse(input,tokens);
execCommands(tokens,tokenCount,startPath);
}
for(i = 0; i < commandCount; i++)
free(prevCommand[i]);
return 0;
}
void sig_handler(int signo)
{
if (signo == SIGINT){
printf("\n Goodbye ... \n");
exit(0);
//kill(getpid(), SIGKILL);
}
}
int getPreviousNum(char *input){
char *ptr = (char *)malloc(strlen(input));
int i;
for(i = 1; i < strlen(input); i++)
ptr[i - 1] = input[i];
ptr[i] = '\0';
int num = atoi(ptr);
free(ptr);
return num;
}
void checkCommand(char *commandLine){
if(commandLine[strlen(commandLine) - 1] == '\n')
commandLine[strlen(commandLine) - 1] = '\0';
if (strcmp(commandLine, EXIT) == 0){
_exit(0);
}
}
//Return token count;
int parse(char *line, char *tokens[]){
int tokenCount = 0;
char *temp;
temp = strtok(line, " \t");
while(temp != NULL){
tokens[tokenCount] = temp;
//fprintf(stdout, "%s\n",tokens[tokenCount]);
tokenCount++;
temp = strtok(NULL," \t");
}
return tokenCount;
}
void genericFunc(char *command, char *arg[], char startPath[]){
char *allCommands[5] = {"pwd","lsf","cat","wc","bunedu"};
int i;
int ret;
char exeCommand[1024];
/*
for(i = 0; i < arg[i] != '\0'; i++)
fprintf(stdout, "Arg [%d] : %s\n", i , arg[i]);
*/
//to check command
for(i = 0; i < 5; i++){
if(strcmp(allCommands[i],command) == 0){
sprintf(exeCommand, "%s/%s",startPath,command);
}
}
if((ret = execvp(exeCommand,arg)) == -1){
fprintf(stderr, "didnt execute \"%s\" command \n",command);
exit(EXIT_FAILURE);
}
}
void execCommands(char *tokens[], int count, char mainPath[]){
//fprintf(stdout, "First token : %s Token count: %d Main path : %s \n",tokens[0],count,mainPath );
char *firstCommand = tokens[0];
char *arg[4];
int ret;
pid_t child_pid;
pid_t pipeID;
int fd[2];
int status;
//fprintf(stdout, "Token count : %d \n",count );
if(strcmp(firstCommand,"help") == 0){
helpPrint();
return;
}
if(strcmp(firstCommand,"cd") == 0){
if((ret = cdir(tokens[1])) == -1)
fprintf(stderr,"Failed to change current working directory to %s\n",tokens[1]);
return;
}
pipe(fd);
child_pid = fork();
if(child_pid == -1){
fprintf(stderr, "didnt exist child with fork %d\n",errno);
return;
}
else if(child_pid == 0){
int pipeStart;
int rdReadStart;
int rdWriteStart;
int countComman = count;
pipeStart = chTokenNum(tokens, count,"|");
rdReadStart = chTokenNum(tokens, count,"<");
rdWriteStart = chTokenNum(tokens, count,">");
if(pipeStart != 0 ){
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
countComman = pipeStart;
}else if(rdReadStart != 0){
fd[0] = open(tokens[rdReadStart + 1], O_RDONLY);
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
countComman = rdReadStart;
}else if(rdWriteStart != 0){
fd[1] = open(tokens[rdWriteStart + 1], O_CREAT | O_WRONLY ,0666 );
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
countComman = rdWriteStart;
}
toMalloc(arg,countComman,tokens);
//fprintf(stdout, "pipe start : %d \n", countComman);
genericFunc(firstCommand,arg,mainPath);
toRealloc(arg,countComman);
exit(1);
}else {
int pipeStart;
if((pipeStart = chTokenNum(tokens, count,"|")) > 0){
pipeID = fork();
if(pipeID == -1){
fprintf(stderr, "didnt exist child with fork %d\n",errno);
return;
}else if(pipeID == 0){
char *arg2[4];
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
toMalloc(arg2,(count - pipeStart - 1),&(tokens[pipeStart + 1]));
// fprintf(stdout, "Right side of pipe : %s\n",tokens[pipeStart + 1]);
firstCommand = tokens[pipeStart + 1];
genericFunc(firstCommand,arg2,mainPath);
toRealloc(arg2,(count - pipeStart - 1));
exit(1);
}else{
close(fd[0]);
close(fd[1]);
waitpid(pipeID, &status, 0);
}
}
waitpid(child_pid,&status,0);
}
}
void toMalloc(char *arg[], int size, char *tokens[]){
int i;
for(i = 0; i < size; i++){
//arg[i] = (char *) malloc(strlen(tokens[i]));
//strcpy(arg[i], tokens[i]);
arg[i] = tokens[i];
}
arg[i] = NULL;
}
void toRealloc(char *arg[], int size){
int i;
// fprintf(stdout, "start size : %d \n",size );
for(i = 0; i < size; i++)
free(arg[i]);
//fprintf(stdout, "end \n" );
}
int chTokenNum(char *tokens[], int tokenCount,char *ch){
int i = 0;
while(i < tokenCount){
if(strcmp(tokens[i],ch) == 0)
return i;
i++;
}
return 0;
}
int cdir(char *directory){
if (chdir(directory) == -1)
return -1; // as error
return 0; // as success
}
void helpPrint()
{
fprintf(stdout,"- lsf command : which will list file type (R for regular file, S for non-regular(special) file), access rights (int the form of rwxr-xr-x, just like actual ls), file size(bytes) and file name of all files (not directories) in the present working directory.\n");
fprintf(stdout,"- pwd command : which will print the present working directory.\n") ;
fprintf(stdout,"- cd command : which will change the present working directory to the location provided as argument.\n");
fprintf(stdout,"- help command : which will print the list of supported commands.\n");
fprintf(stdout,"- cat command : which will print on standard output the contents of the file provided to it as argument or from standard input.\n");
fprintf(stdout,"- wc command : which will print on standard output the number of lines in the file provided to it as argument.\n");
fprintf(stdout,"- bunedu command : Summarize disk usage of the set of FILEs, recursively for directories.\n");
fprintf(stdout,"- exit; which will exit the shell\n");
fflush(STDIN_FILENO); //to clear stdin
}