-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyshell.c
611 lines (568 loc) · 13.8 KB
/
myshell.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
601
602
603
604
605
606
607
608
609
610
611
/**
* myshell interface program
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <dirent.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sched.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
#define MAX_PATH_NO 20
#define MAX_PAT_LENGTH 200
#define MAX_BOOKMARK 10
#define MAX_TASK 20
int parseCommand(char inputBuffer[],
char *args[],
int *background,
int *argumentCount,
int *bookmark);
void printPATH();
void codeSearch(const char *name, int isRecursive, const char *searchFor);
char *trimwhitespace(char *str);
int removeModule();
int insertCron(char* hour, char* min, char* exe, char* file);
int removeCron(char* hour, char* min);
int printCron();
char* paths[MAX_PATH_NO];
char* bookmarks[MAX_BOOKMARK] = {NULL};
int pathLenght;
struct muzik_task
{
char m[3];
char h[3];
char filePath[MAX_PAT_LENGTH];
};
int main(void)
{
char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /* command line (of 80) has max of 40 arguments */
pid_t pid; /* process id of the child process */
int status; /* result from execv system call*/
int shouldrun = 1;
int i, j, upper;
char* pathEV = getenv("PATH");
char* token;
char path[MAX_PAT_LENGTH];
char cwd[MAX_PAT_LENGTH];
int argumentCount;
int bookmark = -1;
int currentPID = -1;
const char* user = getenv("USER");
using_history();
register HIST_ENTRY **histList;
rl_readline_name = "myshell> ";
/* get the first token */
token = strtok(pathEV, ":");
paths[1] = token;
pathLenght = 2;
/* walk through other tokens */
while( token != NULL )
{
token = strtok(NULL, ":");
if (token != NULL)
{
paths[pathLenght] = token;
pathLenght++;
}
}
// get current saved crontabs
system("/usr/bin/crontab -l > mycron");
while (shouldrun)
{ /* Program terminates normally inside setup */
background = 0;
int result;
memset(cwd,'\0',sizeof(cwd));
getcwd(cwd,MAX_PAT_LENGTH);
paths[0] = cwd;
do
{
shouldrun = parseCommand(inputBuffer,
args,
&background,
&argumentCount,
&bookmark); /* get next command */
} while (!args[0]);
if (strncmp(args[0], "exit", 4) == 0)
{
shouldrun = 0; /* Exiting from myshell*/
// removeModule();
}
if (shouldrun)
{
//-----------------------------------------------------------------
// get contents of path file
if (strcmp(args[0],"$PATH") == 0)
{
printPATH();
}
// cd command implementation
else if (strcmp(args[0],"cd") == 0)
{
if (chdir(args[1]) != 0 && args[1] != NULL)
{
printf("No such directory: %s\n", args[1]);
}
}
else if (strncmp(args[0],"history",7) == 0)
{
histList = history_list();
j = 0;
while (histList[j] != NULL)
{
printf("%s\n", histList[j]->line);
j++;
}
}
else if (strcmp(args[0],"bookmark") == 0 && argumentCount > 1)
{
// list bookmarks
if (strcmp(args[1],"-l") == 0)
{
for (int k = 0; k < MAX_BOOKMARK; k++)
{
if (bookmarks[k] == NULL)
{
break;
}
printf("%d: %s\n",k, bookmarks[k]);
}
}
// execute at index
else if (strcmp(args[1],"-i") == 0 && argumentCount > 2)
{
j = atoi(args[2]);
if (j >= 0 && j < MAX_BOOKMARK && bookmarks[j] != NULL)
{
bookmark = j;
}
}
// delete at index
else if (strcmp(args[1],"-d") == 0 && argumentCount > 2)
{
j = atoi(args[2]);
if (j >= 0 && j < MAX_BOOKMARK)
{
bookmarks[j] = NULL;
}
}
// inser bookmark
else
{
for (int k = 0; k < MAX_BOOKMARK; k++)
{
if (bookmarks[k] == NULL)
{
bookmarks[k] = args[1];
break;
}
}
// printf("Bookmarks is full, can't insert.\n");
}
}
else if (strcmp(args[0],"codesearch") == 0)
{
if (args[1] != NULL)
{
if (strcmp(args[1],"-r") == 0 && args[2] != NULL)
{
codeSearch(".",1,args[2]);
}
else
{
codeSearch(".",0,args[1]);
}
}
}
else if (strcmp(args[0],"muzik") == 0)
{
if (args[1] != NULL)
{
if (strcmp(args[1],"-l") == 0)
{
printCron();
}
else if (strcmp(args[1],"-r") == 0 && args[2] != NULL)
{
token = strtok(args[2],".");
if (token != NULL && strlen(token) < 3)
{
char m[3];
char h[3];
j = 0;
strcpy(h,token);
token = strtok(NULL,".");
strcpy(m,token);
removeCron(h,m);
}
}
else if (argumentCount > 2)
{
char *h,*m;
h = strtok(args[1],".");
if (h != NULL && strlen(h) < 3)
{
m = strtok(NULL,".");
insertCron(h,m,"/usr/bin/play",args[2]);
}
}
pid = fork();
if (pid < 0)
{
}
else if (pid == 0)
{
char* dum[3] = {"/usr/bin/crontab","./mycron",NULL};
execv(dum[0],dum);
}
else
{
waitpid(pid);
}
}
}
else if (strcmp(args[0],"processInfo") == 0)
{
if (argumentCount > 2)
{ // 2337
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0)
{
if (currentPID != atoi(args[1]))
{
if (currentPID != -1)
{
removeModule();
}
char dum1[50] = "processID=";
char dum2[50] = "processPrio=";
strcat(dum1,args[1]);
strcat(dum2,args[2]);
args[0] = "/usr/bin/sudo";
args[1] = "insmod";
args[2] = "processInfo.ko";
args[3] = dum1;
args[4] = dum2;
args[5] = NULL;
execv(args[0], args);
}
exit(0);
}
else
{
waitpid(pid);
currentPID = atoi(args[1]);
}
}
}
else
{
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0)
{
for (int pathIndex=0;pathIndex<pathLenght;pathIndex++)
{
setpgid(0,0);
memset(path,'\0',sizeof(path));
strcpy(path,paths[pathIndex]);
strncat(path,"/",1);
strncat(path,args[0],MAX_LINE);
execv(path, args);
}
exit(0);
}
else
{
// wait or background
if (!background)
{
waitpid(pid);
}
background = 0;
}
}
}
}
if (currentPID != -1)
{
removeModule();
}
return 0;
}
/**
* The parseCommand function below will not return any value, but it will just: read
* in the next command line; separate it into distinct arguments (using blanks as
* delimiters), and set the args array entries to point to the beginning of what
* will become null-terminated, C-style strings.
*/
int parseCommand(char inputBuffer[],
char *args[],
int *background,
int *argumentCount,
int *bookmark)
{
int length, /* # of characters in the command line */
ct; /* index of where to place the next parameter into args[] */
char* token;
char* quoteToken;
char* arguments;
ct = 0;
/* read what the user enters on the command line */
if (*bookmark == -1)
{
do
{
inputBuffer = readline("myshell> ");
} while (!inputBuffer[0] || inputBuffer[0] == '\n'); /* swallow newline characters */
add_history(inputBuffer);
}
else
{
strcpy(inputBuffer, bookmarks[*bookmark]);
*bookmark = -1;
}
length = strlen(inputBuffer);
// get rid off &
if (inputBuffer[length-1] == '&')
{
*background = 1;
inputBuffer[length-1] = '\0';
}
if (length == 0)
{
exit(0); /* ^d was entered, end of user command stream */
}
/**
* the <control><d> signal interrupted the read system call
* if the process is in the read() system call, read returns -1
* However, if this occurs, errno is set to EINTR. We can check this value
* and disregard the -1 value
*/
if ( (length < 0) && (errno != EINTR) )
{
perror("error reading the command");
exit(-1); /* terminate with error code of -1 */
}
arguments = strtok(inputBuffer,"\"");
quoteToken = strtok(NULL,"\"");
token = strtok(arguments, " ");
ct = 0;
/* walk through other tokens */
while( token != NULL )
{
args[ct] = token;
ct++;
token = strtok(NULL, " ");
}
if (quoteToken != NULL)
{
args[ct] = quoteToken;
ct++;
}
*argumentCount = ct;
args[ct] = NULL; /* just in case the input line was > 80 */
return 1;
} /* end of parseCommand routine */
void printPATH()
{
for (int i=0;i<pathLenght;i++)
{
printf( "%s\n", paths[i] );
}
}
void codeSearch(const char *currentDir, int isRecursive, const char *searchFor)
{
DIR *dir;
struct dirent *entry;
char* token;
char dummy[MAX_PAT_LENGTH];
char path[1024];
FILE* fp;
char line[MAX_LINE];
int lineNumber, lineStart = *line;
if (!(dir = opendir(currentDir)))
return;
if (!(entry = readdir(dir)))
return;
do
{
int len = snprintf(path, sizeof(path)-1, "%s/%s", currentDir, entry->d_name);
path[len] = 0;
lineNumber = 1;
// printf("%s\n", path);
// if its a dir
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") == 0 || // ignore "."
strcmp(entry->d_name, "..") == 0 || // ignore ".."
strcmp(entry->d_name, ".git") == 0) // ignore ".git"
continue;
if (isRecursive)
{
codeSearch(path, isRecursive, searchFor);
}
}
// if its a file
else
{
strcpy(dummy,entry->d_name);
token = strtok(dummy,".");
if (token != NULL)
{
token = strtok(NULL,".");
if (token != NULL)
{
if (strcmp(token,"c") == 0 ||
strcmp(token,"C") == 0 ||
strcmp(token,"h") == 0 ||
strcmp(token,"H") == 0 ||
strcmp(token,"cpp") == 0 ||
strcmp(token,"c++") == 0)
{
// printf("%s\n", token);
fp = fopen(path,"r");
while(fgets(line,sizeof(line),fp))
{
if (strstr(line,searchFor) != NULL)
{
printf("%s -> %d: %s", path, lineNumber, trimwhitespace(line));
*line = lineStart;
}
lineNumber++;
}
fclose(fp);
}
}
}
}
} while (entry = readdir(dir));
closedir(dir);
}
int isIndent(char c)
{
if (c == ' ' || c == '\t')
return 1;
return 0;
}
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while(isIndent((unsigned char)*str)) str++;
// All spaces?
if(*str == 0)
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isIndent((unsigned char)*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
int removeModule()
{
int pid = fork();
char* args[4] = {"/usr/bin/sudo","rmmod","processInfo",NULL};
if (pid < 0)
{
printf("Fork Failed");
return 1;
}
else if (pid == 0)
{
execv(args[0],args);
}
else
{
waitpid(pid);
}
return 0;
}
int insertCron(char* hour, char* min, char* exe, char* file)
{
FILE *fp1, *fp2;
char h[20], m[20], d[20], mo[20], w[20], e[MAX_PAT_LENGTH], f[MAX_PAT_LENGTH];
fp1 = fopen("mycron","r");
fp2 = fopen("mycronnew","wr+");
if (fp1 && fp2)
{
while(1)
{
if(fscanf(fp1, "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f) == EOF)
{
break;
}
fprintf(fp2, "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f);
}
fprintf(fp2, "%s %s %s %s %s %s %s\n",min, hour, "*", "*", "*", exe, file);
// printf("%s %s %s %s\n", min, hour, exe, file);
fscanf(fp2, "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f);
// printf("%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f);
fclose(fp1);
fclose(fp2);
remove("mycron");
rename("mycronnew","mycron");
}
return 0;
}
int removeCron(char* hour, char* min)
{
FILE *fp1, *fp2;
char h[20], m[20], d[20], mo[20], w[20], e[MAX_PAT_LENGTH], f[MAX_PAT_LENGTH];
fp1 = fopen("mycron","r");
fp2 = fopen("mycronnew","w+");
if (fp1 && fp2)
{
while(1)
{
if(fscanf(fp1, "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f) == EOF)
{
break;
}
if (strcmp(m,min) != 0 || strcmp(h,hour) != 0)
{
fprintf(fp2, "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f);
}
}
fclose(fp1);
fclose(fp2);
remove("mycron");
rename("mycronnew","mycron");
}
return 0;
}
int printCron()
{
FILE *fp1;
char h[20], m[20], d[20], mo[20], w[20], e[MAX_PAT_LENGTH], f[MAX_PAT_LENGTH];
fp1 = fopen("mycron","rw+");
if (fp1)
{
while(1)
{
if(fscanf(fp1, "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f) == EOF)
{
break;
}
printf( "%s %s %s %s %s %s %s\n",m, h, d, mo, w, e, f);
}
fclose(fp1);
}
return 0;
}