This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrgm-1.c
447 lines (352 loc) · 11 KB
/
Prgm-1.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
/*
Lab 4: 2019-19-Aug
History of commands
Relevant topics/commands: files, pipes, fork, malloc, free and inter-process communication.
Specification (To be implemented and shown in lab)
Extend the program written for Lab task 2 to read multiple file names from the command prompt and to be able to read interactively from the user.
Each file contains a list of commands, one per each line, with variable number of arguments.
Write a C program to read each command and its arguments and execute them. Once the program doesn't have any more lines to read from a file, it will process the next file and so on.
For this lab, you may assume that no files have overlapping commands.
Once there are no more files to read, the program will move to an interactive mode and start reading user inputs.
You are to implement the following two features.
In the interactive mode, the program should prompt user for inputs as follows: "Please enter your command:"
The following commands should be supported:
1. When the user types command "HISTORY BRIEF", the program should list out all the commands (no need for file names) that were executed so far without the respective arguments.
The display should show one command per line.
2. When the user types command "HISTORY FULL", the program should list all the commands so far with their respective arguments.
Each command should have an INDEX number associated with it and it should be displayed as well.
3. If the user types "EXEC <COMMAND_NAME>", your program should execute that command with its arguments.
4. When if the user types "EXEC <COMMAND_INDEX_NUMBER>", your program should execute that command with its arguments.
5. The program should exit when the user types a special command "STOP".
6. The program should handle following border conditions:
a. At least one file name should be supplied when the program starts.
b. For unrecognized commands (not in history), the program should type appropriate message.
c. Avoid memory leaks and segmentation faults. Don't allocate memory where not required and as far as possible free memory.
*/
/*
Note: There should be a `\n`(newline, not literally) at the end of the file:
FILE_NAME
ls -al
ping
whoami
END
for the program to work
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
int exec_file(char *fileName){
// fileName
FILE *file_ptr;
file_ptr = fopen(fileName, "r");
if (file_ptr == NULL){
printf("\nFile Not Found!\nExiting\n");
perror("fopen - bad file");
return -1;
}
// Count the number of commands in the file
int count_lines = 0;
char ch;
const char EOL = '\n';
ch = getc(file_ptr);
while (ch != EOF)
{
if (ch == EOL)
{
count_lines = count_lines + 1;
}
ch = getc(file_ptr);
}
fseek(file_ptr, 0, SEEK_SET);
// Allocate memory for the first read
char **buffer = (char **)malloc(count_lines*sizeof(char *));
for(int i = 0; i < count_lines; i++){
buffer[i] = (char *)malloc(100*sizeof(char));
}
// Allocate memory for the tokens
char ***token = (char ***)malloc(count_lines*sizeof(char **));
for(int i = 0; i < count_lines; i++){
token[i] = (char **)malloc(20*sizeof(char *));
for(int j = 0; j < 20; j++){
token[i][j] = (char *)malloc(20*sizeof(char));
}
}
// Read the file
int i = 0;
ch = getc(file_ptr);
while(ch != EOF){
if(i != 0){
ch = fgetc(file_ptr);
}
// Save the line
int p = 0;
while (ch != EOL)
{
buffer[i][p++] = ch;
ch = fgetc(file_ptr);
}
// Tokenize the line
char* tok = strtok(buffer[i], " ");
int pos = 0;
while (tok != NULL) {
token[i][pos++] = tok;
tok = strtok(NULL, " ");
}
while (pos != 20){
token[i][pos++] = NULL;
}
i++;
ch = fgetc(file_ptr);
// Break if end of file is reached
if(ch == EOF){
break;
}
else{
fseek(file_ptr, -1, SEEK_CUR);
}
}
// Begin Executing
for (int i = 0; i < count_lines; ++i){
pid_t child_pid = fork();
if (child_pid == 0){
execvp(token[i][0], token[i]);
return 0;
} else
wait(NULL);
}
return 0;
}
int exec_process(char *readBuffer){
/* Util function to execute a process */
char readBuffer_Copy[strlen(readBuffer)];
strcpy(readBuffer_Copy, readBuffer);
/* 1. Count number of words required as well as max word length */
int wordCount = 0;
char *tempWord = strtok(readBuffer, " \n");
while (tempWord != NULL){
++wordCount;
tempWord = strtok(NULL, " \n");
}
// Make Arguments
char *args[wordCount + 1];
char *word = strtok(readBuffer_Copy, " \n");
int i = 0;
// Copy tokens to args
while (word != NULL){
args[i++] = word;
word = strtok(NULL, " \n");
}
args[i] = NULL;
/* 2. Execute le command via un fork */
pid_t execPid;
execPid = fork();
if (execPid == -1){
perror("Fork creation in execute_process failed!\nExiting\n");
return -1;
}
if (execPid == 0){
/* This is the child process */
execvp(args[0], args);
exit(errno);
} else {
/* This is the parent process */
int returnStatus;
waitpid(execPid, &returnStatus, 0);
}
return 0;
}
int disp_history(int mode){
/*
Mode:
0 - HISTORY BRIEF
1 - HISTORY FULL
*/
FILE *file_ptr;
file_ptr = fopen("~prgm-1-history.TEMP", "r");
if (file_ptr == NULL){
printf("\nError:\tdisp_history\nFile:\t~prgm-1-history.TEMP not found\nReturning -1");
perror("Fopen - Could not open file");
return -1;
}
char *line = NULL;
size_t len = 0;
ssize_t nread;
int lineCount = 0;
char **lines = (char **)malloc(20 * sizeof(char *));
for (int i = 0; i < 20; ++i)
lines[i] = (char *)malloc(40 * sizeof(char));
line = (char *)malloc(40 * sizeof(char));
while ((nread = getline(&line, &len, file_ptr)) != -1) {
if (nread >= 40){
for (int i = 0; i < 20; ++i)
free(lines[i]);
free(lines);
printf("\nError:\tDisp_History\nLine has more than 40 characters while reading ~prgm-1-history.TEMP\nReturning -1\n");
return -1;
}
strncpy(lines[lineCount++], line, nread);
}
free(line);
fclose(file_ptr);
if (mode == 0){
// HISTORY BRIEF
for (int i = 0; i < lineCount; ++i){
printf("%d. ", i);
for (int j = 0; j < 40; ++j){
if (lines[i][j] == ' ')
break;
if (lines[i][j] == '\n')
break;
printf("%c", lines[i][j]);
}
printf("\n");
}
} else if (mode == 1){
// HISTORY FULL
for (int i = 0; i < lineCount + 1; ++i){
if (strncmp(lines[i], "\n", strlen("\n")) == 0)
break;
printf("%d. %s", i, lines[i]);
}
}
for (int i = 0; i < 20; ++i)
free(lines[i]);
free(lines);
return -1;
}
int clean_up(){
remove("~prgm-1-history.TEMP");
return 0;
}
int main(int argc, char *argv[]){
// CLI
// argv[0] ./a.out
// argv[1-n] filename[1-n].txt
printf("Starting Lab4...\n");
printf("Question:\n\tWrite a program to execute a set of files given through CLI. Then, the ");
printf("program enters a interactive mode.\n\n");
if (argc < 2){
printf("Error:\tMain\nMore than 1 argumnet is required. ./a.out FILENAME[0] ... FILENAME[n-1]\nExiting:\t-1\n");
return -1;
}
/* Step 1 - Read each file iteratively */
for (int i = 1; i < argc; ++i){
printf("Reading File:\t%s\n", argv[i]);
if (exec_file(argv[i]) == -1){
printf("Error:\tMain\nexec_file:\treturned an error\nExiting:\t-1");
return -1;
}
}
/* Step 2 - Go to interactive mode */
int signal_sendPipe[2]; // This is a pipe for sending the user input from the parent
int signal_ackPipe[2]; // This is a pipe to acknowledge from the child
char quit_word[][10] = {
"STOP"
};
int loopProcess = 1; // The entire program loop is controlled by this variable
/* Pipe_return_status */
int signal_sendPipe_rs = pipe(signal_sendPipe);
int signal_ackPipe_rs = pipe(signal_ackPipe);
if (signal_sendPipe_rs == -1){
perror("signal_sendPipe creation failed!\nExiting\n");
return -1;
}
if (signal_ackPipe_rs == -1){
perror("signal_ackPipe creation failed!\nExiting\n");
return -1;
}
pid_t pid = fork();
if (pid == -1){
perror("Fork creation failed!\nExiting\n");
return -1;
}
FILE *history_file_ptr;
history_file_ptr = fopen("~prgm-1-history.TEMP", "w");
fclose(history_file_ptr);
// Begin Loop
while (loopProcess){
if (pid == 0){
/* This is the Child Process */
raise(SIGSTOP); // Wait for Parent to give signal to continue
printf("\nCHILD HAS STARTED");
char readBuffer[1024];
close(signal_sendPipe[1]); // Close writing
close(signal_ackPipe[0]); // Close reading
if (read(signal_sendPipe[0], readBuffer, sizeof(readBuffer)) == -1){
perror("Child Process:\tReading (readBuffer) from Pipeline in Child failed!\nReturning -1\n");
return -1;
}
// printf("Child Process:\tRead:\t%s\n", readBuffer);
char send_acknowledge[10];
strcpy(send_acknowledge, "continue");
for (int i = 0; i < sizeof(quit_word)/10; ++i){
if (strncmp(quit_word[i], readBuffer, strlen(quit_word[i])) == 0){
strcpy(send_acknowledge, "quit");
loopProcess = 0;
}
}
// Send Acknowledge
if (write(signal_ackPipe[1], send_acknowledge, sizeof(send_acknowledge)) == -1){
perror("Child Process:\tWriting (send_acknowledge: success) to Pipeline in Child failed!\nBreaking\n");
return -1;
}
// printf("\nSending:\t%s", send_acknowledge);
// strncmp evaluates to 0 if it's true.
if (strncmp(send_acknowledge, "quit", 4) != 0){
// Begin custom checks
if (strncmp(readBuffer, "HISTORY BRIEF", strlen("HISTORY BRIEF")) == 0){
disp_history(0);
} else if (strncmp(readBuffer, "HISTORY FULL", strlen("HISTORY FULL")) == 0){
disp_history(1);
} else if (strncmp(readBuffer, "EXEC", strlen("EXEC")) == 0){
// exec_commad(readBuffer);
} else{
readBuffer[strlen(readBuffer)] = '\0';
FILE *history_file_ptr;
history_file_ptr = fopen("~prgm-1-history.TEMP", "a");
fprintf(history_file_ptr, "%s", readBuffer);
fclose(history_file_ptr);
if (exec_process(readBuffer) == -1){
perror("Child Process:\texecute_process returned -1.\nReturning -1\n");
return -1;
}
}
}
} else {
char writeBuffer[1024];
/* This is the Parent Process */
waitpid(pid, NULL, WUNTRACED);
printf("PARENT HAS STARTED");
close(signal_sendPipe[0]); // Close reading
close(signal_ackPipe[1]); // Close writing
// printf("\nParent Process:\tType Command:\t");
printf("\nType Command:\t");
fgets(writeBuffer, 1024, stdin);
if (write(signal_sendPipe[1], writeBuffer, sizeof(writeBuffer)) == -1){
perror("Parent Process:\tWriting (writeBuffer) to Pipeline in Parent failed!\nBreaking\n");
kill(pid, SIGKILL);
clean_up();
return -1;
}
kill(pid, SIGCONT);
// Read Acknowledge
char acknowledge[10];
if (read(signal_ackPipe[0], acknowledge, sizeof(acknowledge)) == -1){
perror("Parent Process:\tReading (acknowledge) from Pipeline in Parent failed!\nBreaking\n");
kill(pid, SIGKILL);
clean_up();
return -1;
}
if (strncmp(acknowledge, "quit", 4) == 0){
printf("Parent Process:\tAcknowledge QUIT received!\n");
kill(pid, SIGKILL);
loopProcess = 0;
}
}
}
clean_up();
return 0;
}