-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test2 2.c
600 lines (551 loc) · 16.5 KB
/
Test2 2.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>
#include <time.h>
#include <fcntl.h>
#define BUFSIZE 1000
#define INPBUF 100
#define ARGMAX 10
#define GREEN "\x1b[92m"
#define BLUE "\x1b[94m"
#define DEF "\x1B[0m"
#define CYAN "\x1b[96m"
// colour guide - https://github.com/shiena/ansicolor/blob/master/README.md
/*
* Usage - make clean ; clear ; make ; ./Cshell
*/
/* implement shell functions
*/
/* Instructions to support :
* cd <dir> - Done cd
* pwd - Done pwd
* mkdir <dir> - Done mkdir
* rmdir <dir> - Done rmdir
* ls (support ls -l) - Done ls & -l
* cp <file1> <file2> - Done cp
* exit - Done exit
* execute any other command like ./a.out - Done exec
* support background execution - & - Done &
* redirect input output >, < - Done
* a.out | b.out - must support a| b| c - Done
* Additional - clear
* Additional - screenfetch
* Additional - up down keys, history- ?
* Additional - tab keys autocompletiton
*/
struct _instr
{
char * argval[INPBUF];
int argcount;
};
typedef struct _instr instruction;
char *input,*input1;
//char input[1000];
int exitflag = 0;
int filepid,fd[2];
char cwd[BUFSIZE];
char* argval[ARGMAX]; // our local argc, argv
int argcount = 0,inBackground = 0;
int externalIn=0,externalOut=0;
char inputfile[INPBUF],outputfile[INPBUF];
void screenfetch();
void about();
void getInput();
int function_exit();
void function_pwd(char*, int);
void function_cd(char*);
void function_mkdir(char*);
void function_rmdir(char*);
void function_clear();
void nameFile(struct dirent*, char*);
void function_ls();
void function_lsl();
void function_cp(char*, char*);
void executable();
void pipe_dup(int, instruction*);
void run_process(int, int, instruction*);
int main(int argc, char* argv[])
{
signal(SIGINT,strSignal);
int i;
int pipe1 = pipe(fd);
function_clear();
screenfetch();
function_pwd(cwd,0);
while(exitflag==0)
{
externalIn = 0; externalOut = 0;inBackground = 0;
printf("%s%s ~> ",DEF,cwd ); //print user prompt
// scanf("%s",input); - fails due to spaces, tabs
getInput();
if(strcmp(argval[0],"exit")==0 || strcmp(argval[0],"z")==0)
{
function_exit();
}
else if(strcmp(argval[0],"screenfetch")==0 && !inBackground)
{
screenfetch();
}
else if(strcmp(argval[0],"about")==0 && !inBackground)
{
about();
}
else if(strcmp(argval[0],"pwd")==0 && !inBackground)
{
function_pwd(cwd,1);
}
else if(strcmp(argval[0],"cd")==0 && !inBackground)
{
char* path = argval[1];
function_cd(path);
}
else if(strcmp(argval[0],"mkdir")==0 && !inBackground)
{
char* foldername = argval[1];
function_mkdir(foldername);
}
else if(strcmp(argval[0],"rmdir")==0 && !inBackground)
{
char* foldername = argval[1];
function_rmdir(foldername);
}
else if(strcmp(argval[0],"clear")==0 && !inBackground)
{
function_clear();
}
else if(strcmp(argval[0],"ls")==0 && !inBackground)
{
char* optional = argval[1];
if(strcmp(optional,"-l")==0 && !inBackground)
{
function_lsl();
}
else function_ls();
}
else if(strcmp(argval[0],"cp")==0 && !inBackground)
{
char* file1 = argval[1];
char* file2 = argval[2];
if(argcount > 2 && strlen(file1) > 0 && strlen(file2) > 0)
{
function_cp(file1,file2);
}
else
{
printf("+--- Error in cp : insufficient parameters\n");
}
}
else
{
executable();
}
}
}
/*get input containing spaces and tabs and store it in argval*/
void getInput()
{
fflush(stdout); // clear all previous buffers if any
input = NULL;
ssize_t buf = 0;
getline(&input,&buf,stdin);
// Copy into another string if we need to run special executables
input1 = (char *)malloc(strlen(input) * sizeof(char));
strncpy(input1,input,strlen(input));
argcount = 0;inBackground = 0;
while((argval[argcount] = strsep(&input, " \t\n")) != NULL && argcount < ARGMAX-1)
{
// do not consider "" as a parameter
if(sizeof(argval[argcount])==0)
{
free(argval[argcount]);
}
else argcount++;
if(strcmp(argval[argcount-1],"&")==0)
{
inBackground = 1; //run in inBackground
return;
}
}
free(input);
}
// Next 2 functions are called by executable() */
/* use execvp to run the command, check path, and handle erors*/
void runprocess(char * cli, char* args[], int count)
{
/*int i;
printf("open file now\n");
printf("cli : %s\n",cli );
for(i = 0; i < count; i++ )
{
printf("arg %d %s \n",i,args[i] );
}
printf("\n" );
*/
int ret = execvp(cli, args);
char* pathm;
pathm = getenv("PATH");// check for all paths , executing non./
char path[1000];
strcpy(path, pathm);
strcat(path,":");
strcat(path,cwd);
char * cmd = strtok(path, ":\r\n");
while(cmd!=NULL)
{
char loc_sort[1000];
strcpy(loc_sort, cmd);
strcat(loc_sort, "/");
strcat(loc_sort, cli);
printf("execvp : %s\n",loc_sort );
ret = execvp(loc_sort, args);
if(ret==-1)
{
perror("+--- Error in running executable ");
exit(0);
}
cmd = strtok(NULL, ":\r\n");
}
}
/* create pipes if required and send proper formatted commands to run_process */
void pipe_dup(int n, instruction* command)
{
int in = 0,fd[2], i;
int pid, status,pipest;
if(externalIn)
{
in = open(inputfile, O_RDONLY); // open the file
if(in < 0)
{
perror("+--- Error in executable : input file ");
}
}
//printf("%d %d\n",command[0].argcount,n );
// enters only in case of pipes
for (i = 1; i < n; i++)
{
/*printf("Input ");
int k;
for(k=0;k<command[0].argcount;k++)
{
printf("%s ",command[0].argval[k]);
}*/
//printf("i is in loop : %d %s\n",i-1,command[i-1].argval[0]);
pipe (fd);// fd[0] => fd[1] i.e, r=>w
int id = fork();
if (id==0)
{
if (in!=0)
{
dup2(in, 0);
close(in);
}
if (fd[1]!=1)
{
dup2(fd[1], 1);
close(fd[1]);
}
//printf("pipe_dup send %s to runprocess\n",command[i].argval[0]);
runprocess(command[i-1].argval[0], command[i-1].argval,command[i-1].argcount);
exit(0);
}
else wait(&pipest);
close(fd[1]);
in = fd[0]; // store input for next child, it there is one
}
i--; // for final case
// keep a copy of current file descriptor
if(in != 0)
{
dup2(in, 0);
}
if(externalOut)
{
int ofd = open(outputfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
dup2(ofd, 1);
}
// printf("cli sent from pipe_dup : %s\n",command[i].argval[0]);
runprocess(command[i].argval[0], command[i].argval, command[i].argcount);
}
/* executables like ./a.out */
void executable()
{
instruction command[INPBUF];
int i=0,j=1,status;
char* curr = strsep(&input1," \t\n");// need to do all over again
// since we need to identify distinct commands
command[0].argval[0] = curr;
while(curr!=NULL)
{
curr = strsep(&input1, " \t\n");
if(curr==NULL)
{
command[i].argval[j++] = curr;
}
else if(strcmp(curr,"|")==0)
{
command[i].argval[j++] = NULL;
command[i].argcount = j;
j = 0;i++;// move to the next instruction
}
else if(strcmp(curr,"<")==0)
{
externalIn = 1;
curr = strsep(&input1, " \t\n");
strcpy(inputfile, curr);
}
else if(strcmp(curr,">")==0)
{
externalOut = 1;
curr = strsep(&input1, " \t\n");
strcpy(outputfile, curr);
}
else if(strcmp(curr, "&")==0)
{
inBackground = 1;
}
else
{
command[i].argval[j++] = curr;
}
}
command[i].argval[j++] = NULL; // handle last command separately
command[i].argcount = j;
i++;
// parent process waits for execution and then reads from terminl
filepid = fork();
if(filepid == 0)
{
pipe_dup(i, command);
}
else
{
if(inBackground==0)
{
waitpid(filepid,&status, 0);
}
else
{
printf("+--- Process running in inBackground. PID:%d\n",filepid);
}
}
filepid = 0;
free(input1);
}
/* copy one file to another */
void function_cp(char* file1, char* file2)
{
FILE *f1,*f2;
struct stat t1,t2;
f1 = fopen(file1,"r");
if(f1 == NULL)
{
perror("+--- Error in cp file1 ");
return;
}
f2 = fopen(file2,"r");// if file exists
if(f2)
{
// file2 exists
// file1 must be more recently updated
stat(file1, &t1);
stat(file2, &t2);
if(difftime(t1.st_mtime,t2.st_mtime) < 0)
{
printf("+--- Error in cp : %s is more recently updated than %s\n",file2,file1);
fclose(f1);
fclose(f2);
return;
}
}
f2 = fopen(file2,"ab+"); // create the file if it doesn't exist
fclose(f2);
f2 = fopen(file2,"w+");
if(f2 == NULL)
{
perror("Error in cp file2 ");
fclose(f1);
return;
}
//if(access(file2,W_OK)!=0 || access(file1,R_OK)!=0 || access(file2,F_OK)!=0)
if(open(file2,O_WRONLY)<0 || open(file1,O_RDONLY)<0)
{
perror("Error in cp access ");
return;
}
char cp;
while((cp=getc(f1))!=EOF)
{
putc(cp,f2);
}
fclose(f1);
fclose(f2);
}
/* Just a fancy name printing function*/
void nameFile(struct dirent* name,char* followup)
{
if(name->d_type == DT_REG) // regular file
{
printf("%s%s%s",BLUE, name->d_name, followup);
}
else if(name->d_type == DT_DIR) // a directory
{
printf("%s%s/%s",GREEN, name->d_name, followup);
}
else // unknown file types
{
printf("%s%s%s",CYAN, name->d_name, followup);
}
}
/*ls -l lists date permissions etc*/
void function_lsl()
{
int i=0,total=0;
char timer[14];
struct dirent **listr;
struct stat details;
int listn = scandir(".",&listr,0,alphasort);
if(listn > 0)
{
printf("%s+--- Total %d objects in this directory\n",CYAN,listn-2);
for ( i = 0; i < listn; i++)
{
if(strcmp(listr[i]->d_name,".")==0 || strcmp(listr[i]->d_name,"..")==0)
{
continue;
}
else if(stat(listr[i]->d_name,&details)==0)
{
// example - -rwxrwxr-x 1 user user 8872 Jan 26 10:19 a.out*
// owner permissions - group permissions - other permissions
// links associated - owner name - group name
// file size (bytes) - time modified - name
total += details.st_blocks; // block size
// owner permissions - group permissions - other permissions
printf("%s%1s",DEF,(S_ISDIR(details.st_mode)) ? "d" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IRUSR) ? "r" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IWUSR) ? "w" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IXUSR) ? "x" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IRGRP) ? "r" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IWGRP) ? "w" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IXGRP) ? "x" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IROTH) ? "r" : "-");
printf("%s%1s",DEF,(details.st_mode & S_IWOTH) ? "w" : "-");
printf("%s%1s ",DEF,(details.st_mode & S_IXOTH) ? "x" : "-");
// links associated - owner name - group name
printf("%2ld ",(unsigned long)(details.st_nlink));
printf("%s ",(getpwuid(details.st_uid))->pw_name);
printf("%s ",(getgrgid(details.st_gid))->gr_name);
// file size (bytes) - time modified - name
printf("%5lld ",(unsigned long long)details.st_size);
strftime (timer,14,"%h %d %H:%M",localtime(&details.st_mtime));
printf("%s ",timer);
nameFile(listr[i],"\n");
}
}
printf("%s+--- Total %d object contents\n",CYAN,total/2);
}
else
{
printf("+--- Empty directory\n" );
}
}
/* list cwd contents*/
void function_ls()
{
int i=0;
struct dirent **listr;
int listn = scandir(".", &listr, 0, alphasort);
if (listn >= 0)
{
printf("%s+--- Total %d objects in this directory\n",CYAN,listn-2);
for(i = 0; i < listn; i++ )
{
if(strcmp(listr[i]->d_name,".")==0 || strcmp(listr[i]->d_name,"..")==0)
{
continue;
}
else nameFile(listr[i]," ");
if(i%8==0) printf("\n");
}
printf("\n");
}
else
{
perror ("+--- Error in ls ");
}
}
/* clear the screen*/
void function_clear()
{
const char* blank = "\e[1;1H\e[2J";
write(STDOUT_FILENO,blank,12);
}
/* remove folder */
void function_rmdir(char* name)
{
int statrm = rmdir(name);
if(statrm==-1)
{
perror("+--- Error in rmdir ");
}
}
/* Make folder */
void function_mkdir(char* name)
{
int stat = mkdir(name, 0777);// all appropriate permissions
if(stat==-1)
{
perror("+--- Error in mkdir ");
}
}
/*change directory functionality*/
void function_cd(char* path)
{
int ret = chdir(path);
if(ret==0) // path could be changed if cd successful
{
function_pwd(cwd,0);
}
else perror("+--- Error in cd ");
}
/*Implement basic exit*/
int function_exit()
{
exitflag = 1;
return 0; // return 0 to parent process in run.c
}
/* Implement pwd function in shell - 1 prints, 0 stores*/
void function_pwd(char* cwdstr,int command)
{
char temp[BUFSIZE];
char* path=getcwd(temp, sizeof(temp));
if(path != NULL)
{
strcpy(cwdstr,temp);
if(command==1) // check if pwd is to be printed
{
printf("%s\n",cwdstr);
}
}
else perror("+--- Error in getcwd() : ");
}
/* mimic screenfetch like logo functionality from ubuntu*/
void screenfetch()
{
char* welcomestr = "\n ./+o+-\n yyyyy- -yyyyyy+\n ://+//////-yyyyyyo\n .++ .:/++++++/-.+sss/`\n .:++o: /++++++++/:--:/-\n o:+o+:++.`..```.-/oo+++++/\n .:+o:+o/. `+sssoo+/\n .++/+:+oo+o:` /sssooo.\n /+++//+:`oo+o /::--:.\n \\+/+o+++`o++o ++////.\n .++.o+++oo+:` /dddhhh.\n .+.o+oo:. `oddhhhh+\n \\+.++o+o``-````.:ohdhhhhh+\n `:o+++ `ohhhhhhhhyo++os:\n .o:`.syhhhhhhh/.oo++o`\n /osyyyyyyo++ooo+++/\n ````` +oo+++o\\: CShell\n `oo++. Made by Kaustubh Hiware\n\n";
printf("%s",welcomestr);
}
/* about this shell*/
void about()
{
// Source - http://ascii.co.uk/art/seashell
char* descr = " _.-''|''-._\n .-' | `-.\n .'\\ | /`.\n .' \\ | / `. Cshell\n \\ \\ | / / Made by @kaustubhhiware\n `\\ \\ | / /'\n `\\ \\ | / /'\n `\\ \\ | / /'\n _.-`\\ \\ | / /'-._ \n {_____`\\\\|//'_____}\n `-'\n\n";
printf("%s",descr);
}