-
Notifications
You must be signed in to change notification settings - Fork 0
/
isp.c
500 lines (400 loc) · 13.9 KB
/
isp.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#define CMD_BUFFER 256
#define NUM_BUFFERS 32
#define TOKEN_BUFFER 256
#define READ 0
#define WRITE 1
typedef struct
{
char* name;
int len;
} token;
typedef struct
{
int total_char;
int total_read_call;
int total_write_call;
} pipe_statistics;
void fill(char* _str, char c, int len)
{
for (size_t i = 0; i < len; i++)
_str[i] = c;
}
void replace(char *str, char orig, char rep)
{
char *current = str;
while((current = strchr(current, orig)) != NULL)
{
*current++ = rep;
}
}
int count(char *str, char c)
{
int n = 0;
char *current = str;
while((current = strchr(current, c)) != NULL)
{
n++;
}
return n;
}
void trim(char* _str, char* _new, char _c)
{
_new[0] = 0;
if (_str == NULL || strlen(_str) == 0) return;
int i = 0;
int j = 0;
char prevoius = _c;
char current;
while ((current = _str[i]) != 0)
{
if (current != _c || prevoius != _c)
{
_new[j] = current;
j += 1;
}
i += 1;
prevoius = current;
}
if (j >= 1 && _new[j - 1] == _c)
{
_new[j - 1] = 0;
}
}
int generate_tokens(token* token_buffer, char* _str)
{
if (strlen(_str) == 0) return 0;
int n = 0;
char* split = strtok(_str, " ");
token_buffer[n] = (token) {
split,
strlen(split)
};
while ((split = strtok(NULL, " ")) != NULL)
{
n += 1;
token_buffer[n] = (token) {
split,
strlen(split)
};
}
return n + 1;
}
int main(int argc, char const *argv[])
{
// debug mode print insigts
int debug_mode = 1; // set this 0 to disable debug
char* l1 = " ____ _ _____ _ _ _ ";
char* l2 = " | _ \\ | | / ____| | | | |";
char* l3 = " | |_) | ___ _ __ ___ | |__ __ _ _ __ | (___ | |__ ___| | |";
char* l4 = " | _ < / _ \\| '_ ` _ \\| '_ \\ / _` | '__| \\___ \\| '_ \\ / _ \\ | |";
char* l5 = " | |_) | (_) | | | | | | |_) | (_| | | ____) | | | | __/ | |";
char* l6 = " |____/ \\___/|_| |_| |_|_.__/ \\__,_|_| |_____/|_| |_|\\___|_|_|";
printf("%s\n%s\n%s\n%s\n%s\n%s\n\n", l1, l2, l3, l4, l5, l6);
if (debug_mode)
{
printf("!! Running on debug mode on, this will display the parsed input and time !!\n\n");
}
// define the buffers in stack
char in_buffer[CMD_BUFFER];
char cmd_buffer[CMD_BUFFER];
token token_buffer[TOKEN_BUFFER];
int no_bytes = 64; // expected positive
int mode = 1; // expected 1 (normal) or 2 (tapped)
// check for arguments
if (argc < 3)
{
printf("%d", argc);
printf(" ! Not enough arguments are given! (expected 2, given %d)\n", argc - 1);
printf(" continuing with default values b=%d m=%d\n\n", no_bytes, mode);
}
else
{
int _no_bytes = atoi(argv[1]); // expected positive
int _mode = atoi(argv[2]); // expected 1 (normal) or 2 (tapped)
if (_mode != 1 && _mode != 2)
{
printf(" ! Mode can only be 1 (normal mode) or 2 (tapped mode) (continuing m=%d)\n\n", mode);
}
else
{
mode = _mode;
}
if (_no_bytes <= 0 || _no_bytes > 4096)
{
printf(" ! No of bytes can be positive integers and maximum 4096 (continuing b=%d)\n\n", no_bytes);
}
else
{
no_bytes = _no_bytes;
}
}
while (1)
{
// clean the buffers
fill(in_buffer, 0, CMD_BUFFER);
fill(cmd_buffer, 0, CMD_BUFFER);
fill((char*) token_buffer, 0, CMD_BUFFER);
// write the prompt
printf("bombar-shell$ ");
// get the input and remove the new line and then trim
fgets(in_buffer, CMD_BUFFER - 1, stdin);
replace(in_buffer, '\n', 0);
trim(in_buffer, cmd_buffer, ' ');
if (debug_mode)
printf("> '%s'\n", cmd_buffer); // Comment this out for debug
// generate tokens (alters the cmd_buffer)
int no_tokens = generate_tokens(token_buffer, cmd_buffer);
// if the command is piped or not
int pipe_symbol = -1;
for (size_t i = 0; i < no_tokens; i++)
if (strcmp(token_buffer[i].name, "|") == 0)
pipe_symbol = i;
// if single command is given
if (pipe_symbol == -1)
{
// create arguments
char* args[no_tokens + 1];
for (size_t i = 0; i < no_tokens; i++)
{
args[i] = token_buffer[i].name;
}
args[no_tokens] = 0;
struct timeval begin_time;
gettimeofday(&begin_time, NULL);
pid_t p = fork();
// child's perspective
if (p == 0)
{
int ret = execvp(token_buffer[0].name, args);
if (ret < 0 && debug_mode)
{
printf(" ! Execution failed (exited with status code %d)\n", ret);
}
exit(0);
}
// parent's perspective
if (p != 0)
{
wait(NULL);
if (debug_mode)
{
struct timeval end_time;
gettimeofday(&end_time, NULL);
long time_interval_usec = 1000000 * (end_time.tv_sec - begin_time.tv_sec) + (end_time.tv_usec - begin_time.tv_usec);
// printf("> character-count : %d\n", st.total_char);
// printf("> read-call-count : %d\n", st.total_read_call);
// printf("> write-call-count : %d\n", st.total_write_call);
printf("> total-time-passed: %ld\n", time_interval_usec / 1000);
}
}
}
// if double commands are given and in normal mode
if (pipe_symbol != -1 && mode == 1)
{
int fd[2];
// int fd1[2];
// int fd2[2];
// create arguments
char* arg1[pipe_symbol + 1];
char* arg2[no_tokens - pipe_symbol];
for (size_t i = 0; i < no_tokens; i++)
{
if (i < pipe_symbol)
{
arg1[i] = token_buffer[i].name;
}
else if (i == pipe_symbol)
{
arg1[i] = 0;
}
else if (i > pipe_symbol)
{
arg2[i - pipe_symbol - 1] = token_buffer[i].name;
}
}
arg2[no_tokens - pipe_symbol - 1] = 0;
struct timeval begin_time;
gettimeofday(&begin_time, NULL);
pipe(fd);
// pipe(fd1);
// pipe(fd2);
pid_t p1 = fork();
// Producer's perspective
if (p1 == 0)
{
int dup_std_out = dup(STDOUT_FILENO);
dup2(fd[WRITE], STDOUT_FILENO);
close(fd[READ]);
int ret = execvp(token_buffer[0].name, arg1);
if (ret < 0 && debug_mode)
{
printf(" ! Child 1 execution failed (exited with status code %d)\n", ret);
}
close(fd[WRITE]);
if (debug_mode)
{
int read_byte;
char buffer[no_bytes];
int total_char = 0;
int total_read_call = 0;
while (1)
{
read_byte = read(STDOUT_FILENO, buffer, no_bytes);
if (read_byte <= 0)
break;
total_char += read_byte;
total_read_call += 1;
}
printf("> character-count : %d\n", total_char);
printf("> read-call-count : %d\n", total_read_call);
}
exit(0);
}
pid_t p2 = fork();
// Consumer's perspective
if (p2 == 0)
{
dup2(fd[READ], STDIN_FILENO);
close(fd[WRITE]);
int ret = execvp(token_buffer[pipe_symbol + 1].name, arg2);
if (ret < 0 && debug_mode)
{
printf(" ! Child 2 execution failed (exited with status code %d)\n", ret);
}
close(fd[READ]);
exit(0);
}
// parent's perspective
if (p2 != 0)
{
int parent_std_inn = dup(STDIN_FILENO);
int parent_std_out = dup(STDOUT_FILENO);
close(fd[READ]);
close(fd[WRITE]);
wait(NULL);
wait(NULL);
dup2(STDIN_FILENO, parent_std_inn);
dup2(STDOUT_FILENO, parent_std_out);
if (debug_mode)
{
struct timeval end_time;
gettimeofday(&end_time, NULL);
long time_interval_usec = 1000000 * (end_time.tv_sec - begin_time.tv_sec) + (end_time.tv_usec - begin_time.tv_usec);
// printf("> character-count : %d\n", st.total_char);
// printf("> read-call-count : %d\n", st.total_read_call);
// printf("> write-call-count : %d\n", st.total_write_call);
printf("> total-time-passed: %ld\n", time_interval_usec / 1000);
}
}
}
// if double commands are given and in tapped mode
if (pipe_symbol != -1 && mode == 2)
{
int fd1[2];
int fd2[2];
// create arguments
char* arg1[pipe_symbol + 1];
char* arg2[no_tokens - pipe_symbol];
for (size_t i = 0; i < no_tokens; i++)
{
if (i < pipe_symbol)
{
arg1[i] = token_buffer[i].name;
}
else if (i == pipe_symbol)
{
arg1[i] = 0;
}
else if (i > pipe_symbol)
{
arg2[i - pipe_symbol - 1] = token_buffer[i].name;
}
}
arg2[no_tokens - pipe_symbol - 1] = 0;
pipe(fd1);
pipe(fd2);
struct timeval begin_time;
gettimeofday(&begin_time, NULL);
pid_t p1 = fork();
// Producer's perspective
if (p1 == 0)
{
close(fd2[WRITE]);
close(fd2[READ]);
close(fd1[READ]);
dup2(fd1[WRITE], STDOUT_FILENO);
close(fd1[WRITE]);
int ret = execvp(token_buffer[0].name, arg1);
if (ret < 0 && debug_mode)
{
printf(" ! Child 1 execution failed (exited with status code %d)\n", ret);
}
exit(0);
}
pid_t p2 = fork();
// Consumer's perspective
if (p2 == 0)
{
close(fd1[WRITE]);
close(fd1[READ]);
close(fd2[WRITE]);
dup2(fd2[READ], STDIN_FILENO);
close(fd2[READ]);
int ret = execvp(token_buffer[pipe_symbol + 1].name, arg2);
if (ret < 0 && debug_mode)
{
printf(" ! Child 2 execution failed (exited with status code %d)\n", ret);
}
exit(0);
}
// parent's perspective
if (p2 != 0)
{
close(fd1[WRITE]);
close(fd2[READ]);
int parent_std_inn = dup(STDIN_FILENO);
int parent_std_out = dup(STDOUT_FILENO);
char parent_buffer[no_bytes];
pipe_statistics st = (pipe_statistics) {0, 0, 0};
// read from producer and write to the consumer
while (1)
{
int read_bytes = read(fd1[READ], parent_buffer, no_bytes);
st.total_read_call += 1;
if (read_bytes <= 0)
break;
write(fd2[WRITE], parent_buffer, read_bytes);
st.total_write_call += 1;
st.total_char += read_bytes;
}
close(fd2[WRITE]);
close(fd1[READ]);
wait(NULL);
wait(NULL);
dup2(STDIN_FILENO, parent_std_inn);
dup2(STDOUT_FILENO, parent_std_out);
if (debug_mode)
{
struct timeval end_time;
gettimeofday(&end_time, NULL);
long time_interval_usec = 1000000 * (end_time.tv_sec - begin_time.tv_sec) + (end_time.tv_usec - begin_time.tv_usec);
printf("> character-count : %d\n", st.total_char);
printf("> read-call-count : %d\n", st.total_read_call);
printf("> write-call-count : %d\n", st.total_write_call);
printf("> total-time-passed: %ld\n", time_interval_usec / 1000);
// printf("> total-time-passed-ms: %d\n", (int) ((end_time - begin_time) / CLOCKS_PER_SEC));
}
}
}
}
return 0;
}