-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
707 lines (637 loc) · 28.3 KB
/
cli.c
File metadata and controls
707 lines (637 loc) · 28.3 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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "cli.h"
#include "command_line_utils.h"
#include "doc_utils.h"
#include "main.h"
#include "map.h"
#include "posting_list.h"
#include "retrie.h"
#include "workers.h"
#define MAX_ARG_STR 256
#define deadline_str 8
#define not_found_sig -1
#define search_sig -2
#define maxcount_sig -3
#define mincount_sig -4
#define wc_sig -5
#define exit_sig -6
#define LOCKED 1
#define UNLOCKED 0
int timeout = UNLOCKED;
//node for the head of search_words list
struct word *search_words_head = NULL;
struct word *found_words_head = NULL;
struct word *search_wwords_head = NULL;
struct word *get_word_list(void)
{
return search_words_head;
}
struct word *new_word(int cli_word_length)
{
//Memory allocated is sizeof(struct) + sizeof(word from cli) + sizeof("\0")
struct word *cli_word_current = (struct word*) malloc(sizeof(struct word) + cli_word_length*sizeof(char));
if (cli_word_current == NULL)
{
printf("Not enough memory in heap\n");
exit(EXIT_FAILURE);
}
memset(cli_word_current, 0, (sizeof(struct word) + cli_word_length*sizeof(char)));
cli_word_current->word_next = NULL;
cli_word_current->word_length = cli_word_length;
return cli_word_current;
}
void got_alarm()
{
timeout = LOCKED;
//puts("^^^TIMEOUT^^^");
ualarm(0, 0); //Disable ualarm
}
void command_line_user(int to_worker_fd[], int to_handler_fd[], struct worker_info workers[])
{
char *str = malloc(MAX_ARG_STR*sizeof(char));
memset(str, 0, MAX_ARG_STR);
char *temp_str = malloc(MAX_ARG_STR*sizeof(char));
memset(temp_str, 0, MAX_ARG_STR);
char *d_str = malloc(deadline_str*sizeof(char));
memset(d_str, 0, deadline_str);
char *tokenSpace1, *word;
int cli_word_length, j, fifo_sig, max_freq, min_freq;
double deadline;
unsigned long int bytes_all, fifo_bytes, words_all, fifo_words;
unsigned int lines_all, fifo_lines;
struct word *cli_word, *search_words_temp;
struct text_info *answer_list_head, *answer_list_temp, *answer_list_cur;
siginfo_t temp;
useconds_t sec;
signal(SIGALRM, got_alarm);
cli_word_length = 0;
fifo_sig = 0;
/***************HANDLER CLI***************/
while (1)
{
fgets (str, MAX_ARG_STR,stdin); //Read a line from stdin and store it to str
strcpy(temp_str, str);
tokenSpace1 = strtok(temp_str, " \t\n"); //Get first argument from stdin
if (strcmp(str, "\n") == 0) //When users just types enter, cli continues
continue;
/***************SEARCH QUERY***************/
else if (strcmp(tokenSpace1, "/search") == 0)
{
fifo_sig = search_sig;
deadline = 0;
word = strtok (str, " \t");
while (word != NULL) //Read all words from stdin after /search
{
word = strtok (NULL, " \t\n");
if (word == NULL)
{
break;
}
if (strcmp(word, "-d") == 0)
{
d_str = strtok(NULL, " \t\n");
deadline = atof(d_str);
sec = deadline*1000000;
break;
}
cli_word_length = strlen(word) + 1; //+1 for \0
cli_word = new_word(cli_word_length);
strcpy(cli_word->actual_word, word); //Fill word struct
strcat(cli_word->actual_word, "\0");
add_word_to_list(cli_word); //Create a list with given words in cli
}
/******Writing to FIFO of each worker the complete search query******/
for (j = 0;j < get_arg_w_val(); j++)
{
write(to_worker_fd[j], &fifo_sig, sizeof(int));
search_words_temp = search_words_head;
while (search_words_temp != NULL)
{
write(to_worker_fd[j], &(search_words_temp->word_length), sizeof(int));
write(to_worker_fd[j], &(search_words_temp->actual_word), search_words_temp->word_length);
search_words_temp = search_words_temp->word_next;
}
kill(workers[j].worker_pid, SIGCONT); //After handler wrote to workers FiFo,
} //send that worker the continue signal to begin search operation
if (deadline != 0)
sec = ualarm(sec, 0);
free_word_list(search_words_head); //Free list created by handler for query.
search_words_head = NULL;
/***********Stop here until all workers complete their search queries***********/
for(j = 0; j < get_arg_w_val(); j++)
{
waitpid(workers[j].worker_pid, NULL, WUNTRACED);
kill(workers[j].worker_pid, SIGCONT);
}
/******Reading from FIFO of each worker the results of the search query******/
for (j = 0;j < get_arg_w_val(); j++)
{
while (read(to_handler_fd[j], &fifo_sig, sizeof(int)) > 0)
{
if (fifo_sig == not_found_sig)
{
printf("Not found in %d Child\n", j);
break;
}
if (timeout == UNLOCKED) //If timeout didn't happen read from Fifos and
{ //print to STDOUT
printf("Line %d", fifo_sig);
read(to_handler_fd[j], &fifo_sig, sizeof(int));
char answer_path[fifo_sig];
read(to_handler_fd[j], &answer_path, fifo_sig);
printf(":In directory:%s\n", answer_path);
read(to_handler_fd[j], &fifo_sig, sizeof(int));
char answer_text[fifo_sig];
read(to_handler_fd[j], &answer_text, fifo_sig);
printf("%s\n\n", answer_text);
}
else //If timeout did happen, read from Fifos
{ //to flush them but don't print to STDOUT
read(to_handler_fd[j], &fifo_sig, sizeof(int));
char answer_path[fifo_sig];
read(to_handler_fd[j], &answer_path, fifo_sig);
read(to_handler_fd[j], &fifo_sig, sizeof(int));
char answer_text[fifo_sig];
read(to_handler_fd[j], &answer_text, fifo_sig);
}
}
}
if(timeout == LOCKED)
{
printf("TIMEOUT\n");
ualarm(0, 0);
timeout = UNLOCKED; //reset timeout to default
}
}
/***************MAXCOUNT QUERY***************/
else if (strcmp(tokenSpace1, "/maxcount") == 0)
{
fifo_sig = maxcount_sig;
if ((word = strtok(NULL, " \t\n")) != NULL)
{
cli_word_length = strlen(word) + 1; //+1 for \0
cli_word = new_word(cli_word_length);
strcpy(cli_word->actual_word, word); //Fill word struct
strcat(cli_word->actual_word, "\0");
for (j = 0;j < get_arg_w_val(); j++)
{ //Send maxcount signal to specific worker
write(to_worker_fd[j], &fifo_sig, sizeof(int));
write(to_worker_fd[j], &(cli_word->word_length), sizeof(int));
write(to_worker_fd[j], &(cli_word->actual_word), cli_word->word_length);
kill(workers[j].worker_pid, SIGCONT); //After handler wrote to workers FiFo, send that
} //worker the continue signal to begin maxcount operation
free(cli_word);
answer_list_head = NULL;
for(j = 0; j < get_arg_w_val(); j++)
{
waitpid(workers[j].worker_pid, NULL, WUNTRACED); //Wait for workers
max_freq = 0;
while ((read(to_handler_fd[j], &fifo_sig, sizeof(int))) > 0)
{
if (fifo_sig == not_found_sig)
break;
else
{ //Create a list of winner keywords and paths from workers
if (answer_list_head == NULL)
{
max_freq = fifo_sig;
read(to_handler_fd[j], &(fifo_sig), sizeof(int));
answer_list_head = new_text_node(fifo_sig);
answer_list_head->freq = max_freq;
read(to_handler_fd[j], &(answer_list_head->path_n_text_name),
answer_list_head->path_n_text_chars_counter);
}
else
{
max_freq = fifo_sig;
read(to_handler_fd[j], &(fifo_sig), sizeof(int));
answer_list_temp = new_text_node(fifo_sig);
answer_list_temp->freq = max_freq;
read(to_handler_fd[j], (answer_list_temp->path_n_text_name),
answer_list_temp->path_n_text_chars_counter);
//Append the newly found answer to the end of the answer list
answer_list_cur = answer_list_head;
while (answer_list_cur->text_list_next != NULL)
answer_list_cur = answer_list_cur->text_list_next;
answer_list_cur->text_list_next = answer_list_temp;
}
}
}
}
if (answer_list_head != NULL)
{
handler_search_max(answer_list_head); //Search for maxcount winner
free_text_list(answer_list_head);
}
}
else
{
printf("Wrong Arguments\n");
continue;
}
}
/***************MINCOUNT QUERY***************/
else if (strcmp(tokenSpace1, "/mincount") == 0)
{
fifo_sig = mincount_sig;
if ((word = strtok(NULL, " \t\n")) != NULL)
{
cli_word_length = strlen(word) + 1; //+1 for \0
cli_word = new_word(cli_word_length);
strcpy(cli_word->actual_word, word); //Fill word struct
strcat(cli_word->actual_word, "\0");
for (j = 0;j < get_arg_w_val(); j++)
{ //Send mincount signal to specific worker
write(to_worker_fd[j], &fifo_sig, sizeof(int));
write(to_worker_fd[j], &(cli_word->word_length), sizeof(int));
write(to_worker_fd[j], &(cli_word->actual_word), cli_word->word_length);
kill(workers[j].worker_pid, SIGCONT); //After handler wrote to workers FiFo, send that
} //worker the continue signal to begin maxcount operation
free(cli_word);
answer_list_head = NULL;
for(j = 0; j < get_arg_w_val(); j++)
{
waitpid(workers[j].worker_pid, NULL, WUNTRACED); //Wait for workers
min_freq = 0;
while ((read(to_handler_fd[j], &fifo_sig, sizeof(int))) > 0)
{
if (fifo_sig == not_found_sig)
break;
else
{
if (answer_list_head == NULL) //Create a list of winner keywords and paths from workers
{
min_freq = fifo_sig;
read(to_handler_fd[j], &(fifo_sig), sizeof(int));
answer_list_head = new_text_node(fifo_sig);
answer_list_head->freq = min_freq;
read(to_handler_fd[j], &(answer_list_head->path_n_text_name),
answer_list_head->path_n_text_chars_counter);
}
else
{
min_freq = fifo_sig;
read(to_handler_fd[j], &(fifo_sig), sizeof(int));
answer_list_temp = new_text_node(fifo_sig);
answer_list_temp->freq = min_freq;
read(to_handler_fd[j], (answer_list_temp->path_n_text_name),
answer_list_temp->path_n_text_chars_counter);
//Append the newly found answer to the end of the answer list
answer_list_cur = answer_list_head;
while (answer_list_cur->text_list_next != NULL)
answer_list_cur = answer_list_cur->text_list_next;
answer_list_cur->text_list_next = answer_list_temp;
}
}
}
}
if (answer_list_head != NULL)
{
handler_search_min(answer_list_head); //Search for mincount winner
free_text_list(answer_list_head);
}
}
else
{
printf("Wrong Arguments\n");
continue;
}
}
/***************WC QUERY***************/
else if (strcmp(tokenSpace1, "/wc") == 0)
{
bytes_all = fifo_bytes = 0;
words_all = fifo_words = 0;
lines_all = fifo_lines = 0;
fifo_sig = wc_sig;
for (j = 0;j < get_arg_w_val(); j++)
{
write(to_worker_fd[j], &(fifo_sig), sizeof(int)); //Send wc signal to specific worker
kill(workers[j].worker_pid, SIGCONT); //After handler wrote to workers FiFo, send that
} //worker the continue signal to begin wc operation
for(j = 0; j < get_arg_w_val(); j++)
{
waitpid(workers[j].worker_pid, NULL, WUNTRACED);
}
for(j = 0; j < get_arg_w_val(); j++)
{
read(to_handler_fd[j], &fifo_bytes, sizeof(unsigned long int));
bytes_all += fifo_bytes; //Add up all bytes
read(to_handler_fd[j], &fifo_words, sizeof(unsigned long int));
words_all += fifo_words; //Add up all words
read(to_handler_fd[j], &fifo_lines, sizeof(unsigned int));
lines_all += fifo_lines; //Add up all lines
}
printf("Statistics : total bytes: %lu\ttotal words: %lu\ttotal lines: %u\n", bytes_all, words_all, lines_all);
}
/***************EXIT QUERY***************/
else if (strcmp(tokenSpace1, "/exit") == 0 || strcmp(tokenSpace1, "exit") == 0)
{
fifo_sig = exit_sig;
for (j = 0;j < get_arg_w_val(); j++)
{
write(to_worker_fd[j], &fifo_sig, sizeof(int)); //Send exit signal to specific worker
kill(workers[j].worker_pid, SIGCONT); //Send SIGCONT to specific worker to exit
}
for (j = 0;j < get_arg_w_val(); j++)
{
waitid(P_PID, workers[j].worker_pid, &temp, WEXITED); //Wait for specific worker to exit
close(to_worker_fd[j]); //Close FiFo
close(to_handler_fd[j]); //Close FiFo
}
free(str);
free(temp_str);
//free(d_str);
puts("Exiting");
break;
}
else
{
printf("Wrong Arguments\n"); //Wrong Arguments
continue;
}
}
free(d_str);
}
/***************WORKER CLI***************/
void worker_cli(int worker_fp, char *fifo_name2)
{
char *log_file;
int fifo_sig, handler_fp;
struct word *worker_word;
struct text_info *max_count, *min_count;
struct answer_list_node *answer_list_temp;
struct wc_answer_list_node *worker_wc_statistics;
FILE *log_fp;
time_t query_arrival;
fifo_sig = 0;
if ((handler_fp = open(fifo_name2, O_WRONLY | O_NONBLOCK)) == -1) //NONBLOCK for debugging reasons
{
perror("ERROR: ");
exit(EXIT_FAILURE);
}
log_file = malloc((11 + count_digits(getpid()) + 1)
*sizeof(char));
sprintf(log_file, "%s/Worker_%d",log, getpid());
if ((log_fp = fopen(log_file, "w+")) == NULL)
{
printf("can not open file \n");
exit(EXIT_FAILURE);
}
while(1)
{
kill(getpid(), SIGSTOP);
read(worker_fp, &fifo_sig, sizeof(int)); //First read from worker is one of the defined exit_sig, search_sig, wc_sig
time(&query_arrival); //Get the time of query arrival
/***************SEARCH QUERY***************/
if (fifo_sig == search_sig)
{
while (read(worker_fp, &fifo_sig, sizeof(int)) > 0)
{ //Read words from handler one by one
worker_word = new_word(fifo_sig); //and search them in tries
read(worker_fp, &(worker_word->actual_word), worker_word->word_length);
search_word_n_update_log(text_list_head, worker_word, query_arrival, log_fp);
free(worker_word);
}
answer_list_temp = answer_list_head;
/******Writing to handlers FIFO the results of the search query******/
while (answer_list_temp != NULL)
{
write(handler_fp, &(answer_list_temp->line_id), sizeof(int));
write(handler_fp, &(answer_list_temp->path_chars_counter), sizeof(int));
write(handler_fp, (answer_list_temp->path_name), answer_list_temp->path_chars_counter);
write(handler_fp, &(answer_list_temp->answer_map_node->text_length), sizeof(int));
write(handler_fp, &(answer_list_temp->answer_map_node->text), answer_list_temp->answer_map_node->text_length);
answer_list_temp = answer_list_temp->answer_next;
}
if (answer_list_head == NULL) //Nothing Found
{
fifo_sig = not_found_sig;
write(handler_fp, &fifo_sig, sizeof(int));
}
if (answer_list_head != NULL)
{
free_answer_list(answer_list_head); //Free list of query results.
answer_list_head = NULL;
}
}
/**************MAXCOUNT QUERY**************/
else if (fifo_sig == maxcount_sig)
{
read(worker_fp, &fifo_sig, sizeof(int));
worker_word = new_word(fifo_sig);
read(worker_fp, &(worker_word->actual_word), worker_word->word_length);
max_count = search_max(text_list_head, worker_word);
if(max_count != NULL)
{
fprintf(log_fp, "%.24s : maxcount : %s : %s : (%d)\n",
ctime(&query_arrival),
worker_word->actual_word, /***Update log file***/
max_count->path_n_text_name,
max_count->freq);
write(handler_fp, &(max_count->freq), sizeof(int));
write(handler_fp, &(max_count->path_n_text_chars_counter), sizeof(int));
write(handler_fp, &(max_count->path_n_text_name), max_count->path_n_text_chars_counter);
free(worker_word);
}
if (max_count == NULL)
{
fifo_sig = not_found_sig;
write(handler_fp, &(fifo_sig), sizeof(int));
}
}
/**************MINCOUNT QUERY**************/
else if (fifo_sig == mincount_sig)
{
read(worker_fp, &fifo_sig, sizeof(int));
worker_word = new_word(fifo_sig);
read(worker_fp, &(worker_word->actual_word), worker_word->word_length);
min_count = search_min(text_list_head, worker_word);
if(min_count != NULL)
{
fprintf(log_fp, "%.24s : mincount : %s : %s : (%d)\n",
ctime(&query_arrival),
worker_word->actual_word, /***Update log file***/
min_count->path_n_text_name,
min_count->freq);
write(handler_fp, &(min_count->freq), sizeof(int));
write(handler_fp, &(min_count->path_n_text_chars_counter), sizeof(int));
write(handler_fp, &(min_count->path_n_text_name), min_count->path_n_text_chars_counter);
free(worker_word);
}
if (min_count == NULL)
{
fifo_sig = not_found_sig;
write(handler_fp, &(fifo_sig), sizeof(int));
}
}
/**************WC QUERY**************/
else if (fifo_sig == wc_sig)
{
worker_wc_statistics = malloc(sizeof(struct wc_answer_list_node));
worker_wc_statistics->n_bytes = 0;
worker_wc_statistics->n_lines = 0;
worker_wc_statistics->n_words = 0;
worker_get_wc_statistics(worker_wc_statistics, text_list_head);
fprintf(log_fp, "%.24s : wc : bytes->%lu : total words->%lu : total lines->%u\n",
ctime(&query_arrival),
worker_wc_statistics->n_bytes, /***Update log file***/
worker_wc_statistics->n_words,
worker_wc_statistics->n_lines);
write(handler_fp, &(worker_wc_statistics->n_bytes), sizeof(unsigned long int));
write(handler_fp, &(worker_wc_statistics->n_words), sizeof(unsigned long int));
write(handler_fp, &(worker_wc_statistics->n_lines), sizeof(unsigned int));
free(worker_wc_statistics);
}
/**************EXIT QUERY**************/
else if (fifo_sig == exit_sig)
{
fprintf(log_fp, "%.24s : exit\n", ctime(&query_arrival)); /***Update log file***/
free_text_list(text_list_head);
free(log_file);
fclose(log_fp);
close(worker_fp); //Close FiFo
close(handler_fp); //Close FiFo
break;
//exit(EXIT_SUCCESS);
}
}
}
void free_all(void)
{
printf("Freeing allocated memory\n");
}
void add_word_to_list(struct word* cli_word)
{
struct word *search_words_cur = search_words_head;
if (search_words_head == NULL)
{
search_words_head = cli_word;
return;
}
else
{
while(search_words_cur->word_next != NULL)
search_words_cur = search_words_cur->word_next;
search_words_cur->word_next = cli_word;
search_words_head->words_found++;
}
}
struct word *free_word_list(struct word *incoming_struct)
{
struct word* search_words_tmp;
while (1)
{
search_words_tmp = incoming_struct;
if (incoming_struct->word_next != NULL)
{
incoming_struct = incoming_struct->word_next;
free(search_words_tmp);
}
else
{
free(search_words_tmp);
incoming_struct = NULL;
return incoming_struct;
}
}
}
void free_answer_list(struct answer_list_node *incoming_struct)
{
struct answer_list_node *answer_list_temp;
while (1)
{
answer_list_temp = incoming_struct;
if (incoming_struct->answer_next != NULL)
{
incoming_struct = incoming_struct->answer_next;
free(answer_list_temp);
}
else
{
free(answer_list_temp);
incoming_struct = NULL;
return;
}
}
}
void free_text_list(struct text_info *incoming_struct)
{
struct text_info *text_list_temp;
if (incoming_struct != NULL)
{
while (1)
{
text_list_temp = incoming_struct;
if (incoming_struct->text_list_next != NULL)
{
if (incoming_struct->current_trie_node_root != NULL)
free_trie(incoming_struct->current_trie_node_root);
if (incoming_struct->current_map_node_root != NULL)
free_map_list(incoming_struct->current_map_node_root);
incoming_struct = incoming_struct->text_list_next;
free(text_list_temp);
}
else
{
if (text_list_temp->current_trie_node_root != NULL)
free_trie(text_list_temp->current_trie_node_root);
if (text_list_temp->current_map_node_root != NULL)
free_map_list(text_list_temp->current_map_node_root);
free(text_list_temp);
incoming_struct = NULL;
return;
}
}
}
}
void free_docfile_lines(struct docfile_line_info *array_lines)
{
for (int i = 0; i < get_docfile_lines(); i++)
{
free(array_lines[i].docfile_line);
}
}
void handler_search_max(struct text_info *answer_list_head)
{
struct text_info *answer_list_temp, *answer_list_cur;
answer_list_temp = answer_list_cur = answer_list_head;
while (answer_list_temp != NULL) //Search the list created by answers from workers
{
if (answer_list_temp->freq > answer_list_cur->freq) //If found frequency is greater than a previous one, keep it
answer_list_cur = answer_list_temp;
else if (answer_list_temp->freq == answer_list_cur->freq)
{ //If there is a tie between answers.
if ((strcmp(answer_list_temp->path_n_text_name, answer_list_cur->path_n_text_name)) < 0)
answer_list_cur = answer_list_temp; //Winner is tha alphabetically smaller path
}
answer_list_temp = answer_list_temp->text_list_next;
}
printf("Winner Path: %s with frequency (%d)\n", answer_list_cur->path_n_text_name, answer_list_cur->freq);
}
void handler_search_min(struct text_info *answer_list_head)
{
struct text_info *answer_list_temp, *answer_list_cur;
answer_list_temp = answer_list_cur = answer_list_head;
while (answer_list_temp != NULL) //Search the list created by answers from workers
{
if (answer_list_temp->freq < answer_list_cur->freq) //If found frequency is less than a previous one, keep it
answer_list_cur = answer_list_temp;
else if (answer_list_temp->freq == answer_list_cur->freq)
{ //If there is a tie between answers.
if ((strcmp(answer_list_temp->path_n_text_name, answer_list_cur->path_n_text_name)) < 0)
answer_list_cur = answer_list_temp; //Winner is tha alphabetically smaller path
}
answer_list_temp = answer_list_temp->text_list_next;
}
printf("Winner Path: %s with frequency (%d)\n", answer_list_cur->path_n_text_name, answer_list_cur->freq);
}