-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscode.c
More file actions
492 lines (406 loc) · 12.6 KB
/
oscode.c
File metadata and controls
492 lines (406 loc) · 12.6 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
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
/**
******************************************************************************
* File : oscode.c
* Description : Implementation of a simple shell
* Features : History of the last 10 commands,
background processes, list of jobs in background
output redirection
* Author : Felix Dube 260533620
* Version : 1.0.0
* Date : February 2016
******************************************************************************
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <limits.h>
#include <math.h>
#include "oscode.h"
int main()
{
int status;
// command variable
char *args[20];
int i;
for (i = 0; i < 20; ++i)
{
args[i] = NULL;
}
// background variable
int bg;
// history variables
int historynbr = 1;
int *toBeSaved = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
struct cmd **history = mmap(NULL, sizeof(struct cmd), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
int notInitialized = 1;
while(notInitialized) {
int i;
for (i = 0; i < 10; i++) {
history[i] = (struct cmd*) malloc(sizeof(struct cmd));
}
notInitialized = 0;
}
// job variable
struct job * head = NULL;
printf("\n-----------------------------------------\nWelcome!\nThis is a simple shell. \nEnter 'help' for more info.\n-----------------------------------------\n\n");
while (1) {
int cnt = getcmd("\n>> ", args, &bg);
// int i;
// for (i = 0; i < cnt; i++)
// printf("\nArg[%d] = %s", i, args[i]);
// if (bg)
// printf("\nBackground enabled..\n");
// else
// printf("\nBackground not enabled \n");
printf("\n");
/**********************************/
/******** INTERNAL COMMAND ********/
/**********************************/
// HISTORY
if (cnt != 0 && isnumber(args[0])) {
int nbr = stringtoint(args[0]);
*toBeSaved = 0;
// find the index of the command in the history, if it is there
int index = searchhistory(history, nbr);
if (index == -1){
printf("***ERROR command ID '%s' does not exist\n\n", args[0]);
}
else {
int j;
for (j = 0; j < 20; j++) {
args[j] = history[index]->args[j];
}
//check is it was in backgorund
int bgIndex;
if ((bgIndex = isbg(args)) > 0) {
bg = 1;
args[bgIndex] = NULL;
}
}
}
// if nothing is entered
if(cnt == 0){
// do nothing
}
// HELP
else if (strcmp(args[0], "help") == 0){
printf("This is a simple shell brought to you by Felix Dube\n\nIt keeps the last 10 commands in HISTORY.\nEnter 'history' to see the list of commands in history.\n\nProcess can be run un BACKGROUND using the '&' argument.\nEnter 'jobs' to see the list of process running in background\nEnter 'fg' and the process job number to bring a process in the forground.\n\n");
}
// PRINT HISTORY
else if (strcmp(args[0], "history") == 0) {
printhistory(history);
initargs(args);
}
// PRESENT WORKING DIRECTORY
else if( strcmp(args[0], "pwd") == 0) {
char* cwd;
char buff[PATH_MAX + 1];
cwd = getcwd( buff, PATH_MAX + 1 );
if( cwd != NULL ) {
printf( "My working directory is %s.\n", cwd );
}
addhistory(history, args, historynbr);
historynbr++;
initargs(args);
}
// CHANGE DIRECTORY
else if( strcmp(args[0], "cd") == 0 ){
chdir(args[1]);
addhistory(history, args, historynbr);
historynbr++;
initargs(args);
}
// JOBS
else if( strcmp(args[0], "jobs") == 0) {
printjobs(&head);
addhistory(history, args, historynbr);
historynbr++;
initargs(args);
}
// FOREGROUND
else if( strcmp(args[0], "fg") == 0) {
addhistory(history, args, historynbr);
historynbr++;
if (jobexist(head, stringtoint(args[1]))){
waitpid(stringtoint(args[1]), &status, 0);
}
else{
printf("***ERROR job PID '%s' does not exist\n\n", args[1]);
}
initargs(args);
}
// EXIT
else if ( strcmp(args[0], "exit") == 0){
exit(0);
}
/**********************************/
/******** EXTERNAL COMMAND ********/
/**********************************/
else if (cnt != 0) {
*toBeSaved = 1;
pid_t pid = fork();
/**** PARENT ****/
// the parent process either wait for the child process or not
// depending if the command is executed in background or not
if ( pid != 0 ) {
if (bg) {
pushjob(&head, pid, args);
// save the cmd if it was valid and not already in the history
if(*toBeSaved){
int i = 0;
while(args[i] != NULL){
i++;
}
args[i] = "&";
addhistory(history, args, historynbr);
historynbr++;
}
initargs(args);
}
else {
waitpid(pid, &status, 0);
// save the cmd if it was valid and not already in the history
if(*toBeSaved){
addhistory(history, args, historynbr);
historynbr++;
}
initargs(args);
}
}
/**** CHILD ****/
// the command is exucuted in the child process
else {
if (cnt != 0 && isnumber(args[0])){
exit(0);
}
// change the output of the process when specified (eg. ls > out.txt)
int argNbr;
if ((argNbr = isredirected(args)) > 0){
freopen(args[argNbr+1], "w", stdout);
args[argNbr] = NULL;
args[argNbr+1] = NULL;
}
// execute the command and make sure it is valid
if (execvp(args[0], args) == -1) {
*toBeSaved = 0;
printf("***ERROR invalid command\n");
}
}
}
}
}
/* JOB -------------------------------------------------------------------------*/
void removejob(struct job ** head, int pid){
struct job * current = *head;
struct job * temp_job = NULL;
// if the job to be removed is the head
if (current->pid == pid){
temp_job = *head;
*head = (*head)->next;
free(temp_job);
return;
}
// if not we need to find where is the job
while(current->next != NULL && current->next->pid != pid){
current = current->next;
}
if(current->next != NULL){
temp_job = current->next;
current->next = temp_job->next;
free(temp_job);
}
}
void printjobs(struct job ** head) {
int i = 0;
int status;
struct job * current = *head;
// check if any job need to be printed
if (current == NULL) {
printf("No job in background.\n");
}
else{
printf("Jobs PID\tStatus\t\tJobs\n----------------------------------------------------------\n");
}
while (current != NULL) {
// print pid
printf("%i\t\t", current->pid);
// print status
int pid = current->pid;
waitpid(pid, &status, WNOHANG);
if(waitpid(pid, &status, WNOHANG) == -1){
printf("Done\t\t");
}
else {
printf("Working\t\t");
}
// print cmd
int j = 0;
while(current->args[j] != NULL){
printf("%s\t", current->args[j]);
j++;
}
printf("\n");
// check if the process is terminated
// if it is it is removed from the jobs list
current = current->next;
if(waitpid(pid, &status, WNOHANG) == -1){
removejob(head, pid);
}
}
}
void pushjob(struct job ** head, int pid, char *args[]) {
struct job * new_job;
new_job = malloc(sizeof(struct job));
new_job->pid = pid;
new_job->next = *head;
int j = 0 ;
for (j = 0; j < 20; j++) {
new_job->args[j] = args[j];
}
*head = new_job;
}
int jobexist(struct job * head, int pid){
struct job * current = head;
while(current != NULL){
if (current->pid == pid){
return 1;
}
}
return 0;
}
/* HISTORY -----------------------------------------------------------------------*/
void addhistory(struct cmd *history[], char *args[], int nbr){
// delete the first element of history and add the new command at position 9
if (nbr > 10){
int j;
for (j = 0; j< 9; j++){
history[j] = history[j+1];
}
history[9] = (struct cmd*) malloc(sizeof(struct cmd));
for (j = 0; j < 20; j++) {
history[9]->args[j] = args[j];
}
history[9]->nbr = nbr;
}
// add the new command at the first empty spot in the history
else{
int j;
for (j = 0; j < 20; j++) {
history[nbr -1]->args[j] = args[j];
}
history[nbr -1]->nbr = nbr;
}
return;
}
int searchhistory(struct cmd *history[], int nbr) {
int i;
for (i = 0; i < 10; ++i)
{
if (history[i]->nbr == nbr){
return i;
}
}
return -1;
}
int isnumber (char *string) {
int num;
num = atoi( string );
if (num == 0 && string[0] != '0')
return 0; //false
else
return 1; //true
}
void printhistory(struct cmd *history[]) {
int i = 0;
// check if there is any history to be printed
if (history[0]->nbr == 0) {
printf("Nothing in history.\n");
}
else{
printf("History ID\t\tCommand\n----------------------------------------------------------\n");
}
while (i < 10 && history[i]->nbr != 0) {
printf("%i\t\t\t", history[i]->nbr);
int j = 0;
while(history[i]->args[j] != NULL){
printf("%s\t", history[i]->args[j]);
j++;
}
printf("\n");
i++;
}
}
int stringtoint (char* a) {
int i;
int index = 0;
int add;
for (i = 0; i < strlen(a); i++){
// from the ascci of the char get the number
add = a[i] -48;
// multiply by some power of 10 if required and add to the final number
index += (add)*( pow( 10, (strlen(a) -i -1)));
}
return index;
}
int isbg(char * args[]){
int i = 0;
while(args[i] != NULL){
if (strcmp(args[i], "&") == 0){
return i;
}
i++;
}
return -1;
}
/* COMMAND -----------------------------------------------------------------------*/
void freecmd(char *line) {
free(line);
return;
}
int getcmd(char *prompt, char *args[], int *background) {
int length, i = 0;
char *token, *loc;
char *line = NULL;
size_t linecap = 0;
printf("%s", prompt);
length = getline(&line, &linecap, stdin);
if (length <= 0) {
exit(-1);
}
// Check if background is specified..
if ((loc = index(line, '&')) != NULL) {
*background = 1;
*loc = ' ';
} else
*background = 0;
while ((token = strsep(&line, " \t\n")) != NULL) {
int j;
for (j = 0; j < strlen(token); j++)
if (token[j] <= 32)
token[j] = '\0';
if (strlen(token) > 0)
args[i++] = token;
}
freecmd(line);
return i;
}
void initargs(char *args[]){
int i;
for (i = 0; i < 20; ++i)
{
args[i] = NULL;
}
return;
}
int isredirected(char * args[]){
int i = 0;
while(args[i] != NULL){
if (strcmp(args[i], ">") == 0){
return i;
}
i++;
}
return -1;
}