-
Notifications
You must be signed in to change notification settings - Fork 1
/
skerl.c
489 lines (447 loc) · 9.97 KB
/
skerl.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
/* ---- Skerl Shell ---- */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pwd.h>
#define SKERL_TOKEN_BUFFERSIZE 64
#define SKERL_TOKEN_DELIMITER " "
char cwd[1024];
int usage = 0; // counter variable to store the number of external commands used
char *builtin_command[] = {
"cd", "pwd", "help", "globalusage", "averageusage"};
/* Internal shell commands */
int skerl_cd(char **internal_command);
int skerl_pwd(char **internal_command);
int skerl_help(char **internal_command);
int skerl_globalusage(char **internal_command);
int skerl_averageusage(char **internal_command);
int (*execute_builtin_command[])(char **) = {
&skerl_cd,
&skerl_pwd,
&skerl_help,
&skerl_globalusage,
&skerl_averageusage};
int skerl_total_builtin_command()
{
return sizeof(builtin_command) / sizeof(char *);
}
char **skerl_split_command(char *input_command, int *count)
{
int buffer_size = SKERL_TOKEN_BUFFERSIZE, position = 0;
char **tokens = malloc(buffer_size * sizeof(char *));
char *token, **tokens_backup;
if (!tokens)
{
fprintf(stderr, "Skerl: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(input_command, SKERL_TOKEN_DELIMITER);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= buffer_size)
{
buffer_size += SKERL_TOKEN_BUFFERSIZE;
tokens_backup = tokens;
tokens = realloc(tokens, buffer_size * sizeof(char *));
if (!tokens)
{
free(tokens_backup);
fprintf(stderr, "Skerl: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, SKERL_TOKEN_DELIMITER);
}
tokens[position] = NULL;
*count = position;
return tokens;
}
int skerl_execute_external_command(char **single_command, int background)
{
pid_t pid;
int status;
usage++; // increment usage
pid = fork();
if (pid == 0)
{
if (execvp(single_command[0], single_command) == -1)
{
perror("skerl");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
perror("skerl");
}
else
{
if (!background)
{
do
{
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
else
{
printf("child process with pid [%d] is running as background process\n", pid);
}
}
return 1;
}
/* Implementation of internal command (cd) */
int skerl_cd(char **internal_command)
{
char *home_dir;
struct passwd *pwd;
pwd = getpwuid(getuid());
home_dir = pwd->pw_dir;
if (internal_command[1] == NULL)
{
chdir(home_dir);
}
else if ((strcmp(internal_command[1], "~") == 0) || (strcmp(internal_command[1], "~/") == 0))
{
chdir(home_dir);
}
else if (chdir(internal_command[1]) < 0)
printf("skerl: cd: %s: No such file or directory\n", internal_command[1]);
}
/* Implementation of internal command (help) */
int skerl_help(char **internal_command)
{
int i;
printf("type program names and arguments, and hit enter");
printf("\nyou can try some builtin commands:\n");
for (i = 0; i < skerl_total_builtin_command(); i++)
{
printf(" %s\n", builtin_command[i]);
}
printf("Use the man command for information on other commands");
}
/* Implementation of internal command (pwd) */
int skerl_pwd(char **internal_command)
{
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
printf("%s\n", cwd);
}
else
{
perror("getcwd() error");
}
}
int skerl_globalusage(char **internal_command)
{
FILE *fptr;
// To get home directory of current user
char *home_dir;
struct passwd *pwd;
pwd = getpwuid(getuid());
home_dir = pwd->pw_dir;
fptr = fopen(strcat(home_dir, "/.usage.log"), "r");
if (fptr == NULL)
{
perror("skerl: ~/.usage_log not found");
}
else
{
int total = 0, value;
while (fscanf(fptr, "%d", &value) != EOF)
{
total += value;
}
printf("skerl usage: %d commands :: current session %d commands\n", total, usage);
}
fclose(fptr);
}
int skerl_averageusage(char **internal_command)
{
FILE *fptr;
// To get home directory of current user
char *home_dir;
struct passwd *pwd;
pwd = getpwuid(getuid());
home_dir = pwd->pw_dir;
fptr = fopen(strcat(home_dir, "/.usage.log"), "r");
if (fptr == NULL)
{
perror("skerl: ~/.usage_log not found");
}
else
{
int total = 0, count = 0, value;
while (fscanf(fptr, "%d", &value) != EOF)
{
total += value;
count++;
}
float avg_usage = total / count;
float per_utilization = usage / avg_usage * 100;
printf("skerl average usage: %f commands :: current session %f\% \n", avg_usage, per_utilization);
}
fclose(fptr);
}
int skerl_execute(char **single_command, int background)
{
int i;
if (single_command[0] == NULL)
{
return 1;
}
//check if there are internal commands
for (i = 0; i < skerl_total_builtin_command(); i++)
{
if (strcmp(single_command[0], builtin_command[i]) == 0)
{
return (*execute_builtin_command[i])(single_command);
}
}
return skerl_execute_external_command(single_command, background);
}
/* parses the commanfd and tokenizes the input string */
void skerl_parse_input_command(char *input_command)
{
// an array of single cmds / tokens are obtained
int token_count;
char **command = skerl_split_command(input_command, &token_count);
// here we implement the logic to check for redirection
int type = 0;
for (int i = 1; i < token_count; i++)
{
if (strcmp(command[i], "&") == 0)
{
// run as background process
type = 1;
command[i] = NULL;
skerl_execute(command,1);
}
else if ((strcmp(command[i], ">") == 0) || (strcmp(command[i], ">>") == 0))
{
// output redirection
// stdout -> file
type = 1;
int saved_stdout = dup(1);
int output_file;
if (strcmp(command[i], ">") == 0)
{
FILE *fptr = fopen(command[i + 1], "w");
fclose(fptr);
output_file = open(command[i + 1], O_WRONLY);
}
else
{
FILE *fptr = fopen(command[i + 1], "a");
fclose(fptr);
output_file = open(command[i + 1], O_WRONLY | O_APPEND);
}
int redirect_stream = dup2(output_file, 1);
command[i] = NULL;
skerl_execute(command,0);
dup2(saved_stdout, 1);
close(saved_stdout);
}
else if (strcmp(command[i], "<") == 0)
{
// input redirection
// file -> stdin
type = 1;
int input_file = open(command[i + 1], O_RDONLY);
if (input_file > 0)
{
int saved_stdin = dup(0);
int redirect_stream = dup2(input_file, 0);
command[i] = NULL;
skerl_execute(command,0);
dup2(saved_stdin, 0);
close(saved_stdin);
}
else
{
perror(strcat(command[i + 1], " not found"));
}
}
else if (strcmp(command[i], "|") == 0)
{
// pipes
type = 1;
// stdout -> file
int saved_stdout = dup(1);
int output_file;
FILE *fptr = fopen("temp", "w");
fclose(fptr);
output_file = open("temp", O_WRONLY);
dup2(output_file, 1);
command[i] = NULL;
skerl_execute(command,0);
dup2(saved_stdout, 1);
close(saved_stdout);
// file -> stdin
int input_file = open("temp", O_RDONLY);
int saved_stdin = dup(0);
dup2(input_file, 0);
char **cmd = &command[i + 1];
skerl_execute(cmd,0);
dup2(saved_stdin, 0);
close(saved_stdin);
remove("temp");
}
}
if (type == 0)
{
skerl_execute(command,0);
}
}
void skerl_prompt()
{
char prompt_msg[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
strcpy(prompt_msg, "skerl:~");
strcat(prompt_msg, cwd);
strcat(prompt_msg, "$ ");
printf("%s", prompt_msg);
}
else
perror("getcwd error..");
}
int display_history_file()
{
FILE *fptr;
fptr = fopen("history.txt", "r");
int count = 0;
if (fptr != NULL)
{
char s[100];
while (fgets(s, sizeof(s), fptr) != NULL)
{
printf("#%d - %s", count + 1, s);
count = count + 1;
}
}
if (count == 0)
return 0;
else
return 1;
fclose(fptr);
}
/* writes cmds into history file */
void write_history(char *command)
{
FILE *fptr;
fptr = fopen("history.txt", "a");
if (fptr != NULL)
{
fprintf(fptr, "%s\n", command);
}
fclose(fptr);
}
/* returns the cmd from history corresponding to the history number */
char *return_history(int history_number)
{
FILE *fptr;
char s[100];
char *c;
fptr = fopen("history.txt", "r");
int count = 0;
if (fptr != NULL)
{
while (fgets(s, sizeof(s), fptr) != NULL)
{
count = count + 1;
if (count == history_number)
{
fclose(fptr);
strcpy(c, s);
return c;
}
}
}
fclose(fptr);
return NULL;
}
void write_usage_log()
{
FILE *fptr;
// To get home directory of current user
char *home_dir;
struct passwd *pwd;
pwd = getpwuid(getuid());
home_dir = pwd->pw_dir;
fptr = fopen(strcat(home_dir, "/.usage.log"), "a");
fprintf(fptr, "%d\n", usage);
fclose(fptr);
}
void sigint_handler(int sig_num)
{
signal(SIGINT, sigint_handler);
fflush(stdout);
fflush(stdin);
}
int main(int argv, char **argc)
{
char new_line_checker[2] = {"\n"};
char *input_command = NULL; // Stores the shell inputs
ssize_t input_command_buffer = 0;
int history_choice;
int flag = 0;
int enter_history = 0;
signal(SIGINT, sigint_handler);
while (1)
{
enter_history = 0;
skerl_prompt();
getline(&input_command, &input_command_buffer, stdin); // accept input
if (strcmp(input_command, new_line_checker) == 0)
{
continue;
}
input_command[strlen(input_command) - 1] = '\0';
if (strcmp(input_command, "exit") == 0) // terminate shell on exit cmd
{
printf("exiting skerl shell\n");
write_history(input_command);
write_usage_log();
flag = 1;
exit(0);
}
if (strcmp(input_command, "history") == 0)
{
int count = display_history_file();
if (count == 0)
{
printf("No recent history\n");
continue;
}
else
{
printf("\nEnter the history number : ");
scanf("%d", &history_choice);
input_command = return_history(history_choice);
if (input_command == NULL)
{
printf("wrong input\n");
continue;
}
write_history(input_command);
input_command[strlen(input_command) - 1] = '\0';
enter_history = 1;
}
}
if (enter_history == 0)
{
write_history(input_command);
}
skerl_parse_input_command(input_command); // handle and parse internal and external cmds
input_command = NULL;
}
return EXIT_SUCCESS;
}