HuntnGather – Diff between revs 37 and 38

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 37 Rev 38
Line 3... Line 3...
3 /////////////////////////////////////////////////////////////////////////// 3 ///////////////////////////////////////////////////////////////////////////
Line 4... Line 4...
4   4  
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
-   7 #include <string.h>
7 #include <string.h> 8 #include <math.h>
8 #if !defined ___AmigaOS___ 9 #if !defined ___AmigaOS___
9 #include <dirent.h> 10 #include <dirent.h>
10 #include <sys/stat.h> 11 #include <sys/stat.h>
11 #include <signal.h> 12 #include <signal.h>
Line 84... Line 85...
84 #else 85 #else
85 FILE *fp; 86 FILE *fp;
86 #endif 87 #endif
87 char **database; 88 char **database;
88 dbEntry *entry; 89 dbEntry *entry;
89 char *line = NULL; 90 dbLine *line;
90 char *rem; 91 char *rem;
91 int count; 92 int count;
92 int i; 93 int i;
Line 93... Line 94...
93   94  
Line 124... Line 125...
124 continue; 125 continue;
125 } 126 }
126 #endif 127 #endif
127 if((entry = CreateDatabaseEntry(line)) == NULL) { 128 if((entry = CreateDatabaseEntry(line)) == NULL) {
128 fprintf(stderr, "Unable to create database entry.\n"); 129 fprintf(stderr, "Unable to create database entry.\n");
-   130  
-   131 free(line->string);
129 free(line); 132 free(line);
-   133 line = NULL;
-   134  
130 #if defined ___AsyncIO___ 135 #if defined ___AsyncIO___
131 CloseAsync(fp); 136 CloseAsync(fp);
132 #else 137 #else
133 fclose(fp); 138 fclose(fp);
134 #endif 139 #endif
135 return; 140 return;
136 } 141 }
Line 137... Line 142...
137   142  
138 if((database[count] = malloc((strlen(entry->name) + strlen(entry->path) + 1 + 1) * sizeof(*database[count]))) == NULL) { 143 if((database[count] = malloc((strlen(entry->name) + strlen(entry->path) + 1 + 1) * sizeof(*database[count]))) == NULL) {
-   144 fprintf(stderr, "Memory allocation failure.\n");
-   145  
139 fprintf(stderr, "Memory allocation failure.\n"); 146 // Free database entry.
140 free(entry->name); 147 free(entry->name);
141 free(entry->path); 148 free(entry->path);
-   149 free(entry);
-   150 entry = NULL;
-   151  
-   152 // Free the line.
142 free(entry); 153 free(line->string);
-   154 free(line);
-   155 line = NULL;
143 free(line); 156  
144 #if defined ___AsyncIO___ 157 #if defined ___AsyncIO___
145 CloseAsync(fp); 158 CloseAsync(fp);
146 #else 159 #else
147 fclose(fp); 160 fclose(fp);
Line 154... Line 167...
154   167  
155 // Free the database entry. 168 // Free the database entry.
156 free(entry->name); 169 free(entry->name);
157 free(entry->path); 170 free(entry->path);
-   171 free(entry);
Line -... Line 172...
-   172 entry = NULL;
-   173  
158 free(entry); 174 // Free the line.
159   -  
160 free(line); -  
161 } 175 free(line->string);
162   -  
163 if(line != NULL) { 176 free(line);
Line 164... Line 177...
164 free(line); 177 line = NULL;
165 } 178 }
166   179  
Line 218... Line 231...
218 fprintf(fp, "%s\n", database[i]); 231 fprintf(fp, "%s\n", database[i]);
219 #endif 232 #endif
Line 220... Line 233...
220   233  
221 if(rem != NULL) { 234 if(rem != NULL) {
-   235 free(rem);
222 free(rem); 236 rem = NULL;
Line 223... Line 237...
223 } 237 }
224   238  
225 rem = malloc((strlen(database[i]) + 1) * sizeof(*rem)); 239 rem = malloc((strlen(database[i]) + 1) * sizeof(*rem));
Line 226... Line 240...
226 sprintf(rem, "%s", database[i]); 240 sprintf(rem, "%s", database[i]);
227 } 241 }
-   242  
228   243 if(rem != NULL) {
Line 229... Line 244...
229 if(rem != NULL) { 244 free(rem);
230 free(rem); 245 rem = NULL;
231 } 246 }
Line 241... Line 256...
241 } 256 }
Line 242... Line 257...
242   257  
243 // Free up database. 258 // Free up database.
244 for(i = 0; i < count; ++i) { 259 for(i = 0; i < count; ++i) {
-   260 free(database[i]);
245 free(database[i]); 261 database[i] = NULL;
Line 246... Line 262...
246 } 262 }
247   263  
Line 248... Line 264...
248 free(database); 264 free(database);
249 } 265 }
250   266  
251 /* 267 /*
252 * 268 *
253 * Updates a database file "dbFile". 269 * Updates a database file "dbFile".
254 */ 270 */
255 dbStats *CollectFiles(char *dbFile, char **paths, int count) { 271 dbStats *CollectFiles(char *dbFile, VECTOR *paths) {
256 #if defined ___AsyncIO___ 272 #if defined ___AsyncIO___
257 struct AsyncFile *fp; 273 struct AsyncFile *fp;
Line 262... Line 278...
262 struct FileInfoBlock *FIB; 278 struct FileInfoBlock *FIB;
263 BPTR lock; 279 BPTR lock;
264 #else 280 #else
265 DIR *dir; 281 DIR *dir;
266 struct dirent *entry; 282 struct dirent *entry;
267 struct stat dirStat; -  
268 #endif 283 #endif
269 stringStack *stack; 284 stringStack *stack;
270 dbStats *stats = NULL; 285 dbStats *stats = NULL;
271 int i; 286 int i;
272 char *path; 287 char *path;
Line 300... Line 315...
300 stats->files = 0; 315 stats->files = 0;
301 stats->lines = 0; 316 stats->lines = 0;
302 stats->size = 0; 317 stats->size = 0;
Line 303... Line 318...
303   318  
304 // Push the first path onto the stack. 319 // Push the first path onto the stack.
305 stack = stringStackCreate((unsigned int)count); 320 stack = stringStackCreate((unsigned int)paths->length);
306 for(i = 0; PROGRAM_RUN && i < count; ++i) { 321 for(i = 0; PROGRAM_RUN && i < paths->length; ++i) {
307 stringStackPush(stack, paths[i]); 322 stringStackPush(stack, paths->array[i]);
Line 308... Line 323...
308 } 323 }
309   324  
310 while(PROGRAM_RUN && !stringStackIsEmpty(stack)) { 325 while(PROGRAM_RUN && !stringStackIsEmpty(stack)) {
Line 321... Line 336...
321   336  
322 #if defined ___AmigaOS___ 337 #if defined ___AmigaOS___
323 if((lock = Lock(path, ACCESS_READ)) == NULL) { 338 if((lock = Lock(path, ACCESS_READ)) == NULL) {
324 fprintf(stderr, "Could not lock path '%s' for reading.\n", path); 339 fprintf(stderr, "Could not lock path '%s' for reading.\n", path);
-   340 free(path);
325 free(path); 341 path = NULL;
326 continue; 342 continue;
Line 327... Line 343...
327 } 343 }
328   344  
329 if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) { 345 if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) {
330 fprintf(stderr, "File information block for path '%s' could not be allocated.\n", path); 346 fprintf(stderr, "File information block for path '%s' could not be allocated.\n", path);
-   347 UnLock(lock);
331 UnLock(lock); 348 free(path);
332 free(path); 349 path = NULL;
Line 333... Line 350...
333 continue; 350 continue;
334 } 351 }
335   352  
-   353 if(Examine(lock, FIB) == FALSE) {
336 if(Examine(lock, FIB) == FALSE) { 354 fprintf(stderr, "Path '%s' could not be examined.\n", path);
337 fprintf(stderr, "Path '%s' could not be examined.\n", path); 355 FreeDosObject(DOS_FIB, FIB);
-   356 FIB = NULL;
338 FreeDosObject(DOS_FIB, FIB); 357 UnLock(lock);
339 UnLock(lock); 358 free(path);
Line 340... Line 359...
340 free(path); 359 path = NULL;
341 continue; 360 continue;
Line 350... Line 369...
350 #else 369 #else
Line 351... Line 370...
351   370  
352 if((dir = opendir(path)) == NULL) { 371 if((dir = opendir(path)) == NULL) {
353 fprintf(stderr, "Directory '%s' could not be opened.\n", path); 372 fprintf(stderr, "Directory '%s' could not be opened.\n", path);
-   373 free(path);
354 free(path); 374 path = NULL;
355 continue; 375 continue;
Line 356... Line 376...
356 } 376 }
357   377  
Line 366... Line 386...
366 if((sub = malloc(strlen(path) + strlen(entry->d_name) + 1)) == NULL) { 386 if((sub = malloc(strlen(path) + strlen(entry->d_name) + 1)) == NULL) {
367 #endif 387 #endif
368 fprintf(stderr, "Memory allocation failure.\n"); 388 fprintf(stderr, "Memory allocation failure.\n");
369 #if defined ___AmigaOS___ 389 #if defined ___AmigaOS___
370 FreeDosObject(DOS_FIB, FIB); 390 FreeDosObject(DOS_FIB, FIB);
-   391 FIB = NULL;
371 UnLock(lock); 392 UnLock(lock);
372 #else 393 #else
373 closedir(dir); 394 closedir(dir);
374 #endif 395 #endif
-   396  
375 free(path); 397 free(path);
-   398 path = NULL;
-   399  
376 stringStackDestroy(stack); 400 stringStackDestroy(stack);
377 #if defined ___AsyncIO___ 401 #if defined ___AsyncIO___
378 CloseAsync(fp); 402 CloseAsync(fp);
379 #else 403 #else
380 fclose(fp); 404 fclose(fp);
Line 394... Line 418...
394 if((sub = malloc(strlen(path) + strlen(entry->d_name) + 1 + 1)) == NULL) { 418 if((sub = malloc(strlen(path) + strlen(entry->d_name) + 1 + 1)) == NULL) {
395 #endif 419 #endif
396 fprintf(stderr, "Memory allocation failure.\n"); 420 fprintf(stderr, "Memory allocation failure.\n");
397 #if defined ___AmigaOS___ 421 #if defined ___AmigaOS___
398 FreeDosObject(DOS_FIB, FIB); 422 FreeDosObject(DOS_FIB, FIB);
-   423 FIB = NULL;
399 UnLock(lock); 424 UnLock(lock);
400 #else 425 #else
401 closedir(dir); 426 closedir(dir);
402 #endif 427 #endif
-   428  
403 free(path); 429 free(path);
-   430 path = NULL;
-   431  
404 stringStackDestroy(stack); 432 stringStackDestroy(stack);
405 #if defined ___AsyncIO___ 433 #if defined ___AsyncIO___
406 CloseAsync(fp); 434 CloseAsync(fp);
407 #else 435 #else
408 fclose(fp); 436 fclose(fp);
Line 418... Line 446...
418 } 446 }
Line 419... Line 447...
419   447  
420 switch(GetFsType(sub)) { 448 switch(GetFsType(sub)) {
421 case UNKNOWN: 449 case UNKNOWN:
-   450 free(sub);
422 free(sub); 451 sub = NULL;
423 continue; 452 continue;
424 case REGULAR: 453 case REGULAR:
Line 425... Line 454...
425 ++stats->files; 454 ++stats->files;
426   455  
427 if(PROGRAM_VERBOSE) { 456 if(PROGRAM_VERBOSE) {
428 fprintf(stdout, 457 fprintf(stdout,
429 "Gathered %d directories and %d files.\r", 458 "Gathered '%d' directories and '%d' files.\r",
430 stats->dirs, 459 stats->dirs,
431 stats->files); 460 stats->files);
432 } 461 }
Line 436... Line 465...
436   465  
Line 437... Line 466...
437 ++stats->dirs; 466 ++stats->dirs;
438   467  
439 if(PROGRAM_VERBOSE) { 468 if(PROGRAM_VERBOSE) {
440 fprintf(stdout, 469 fprintf(stdout,
441 "Gathered %d directories and %d files.\r", 470 "Gathered '%d' directories and '%d' files.\r",
442 stats->dirs, 471 stats->dirs,
Line 443... Line 472...
443 stats->files); 472 stats->files);
-   473 }
444 } 474  
445   475 free(sub);
Line 446... Line 476...
446 free(sub); 476 sub = NULL;
447 continue; 477 continue;
Line 481... Line 511...
481 #endif 511 #endif
Line 482... Line 512...
482   512  
Line 483... Line 513...
483 ++stats->lines; 513 ++stats->lines;
-   514  
484   515 free(sub);
Line 485... Line 516...
485 free(sub); 516 sub = NULL;
486 } 517 }
-   518  
487   519 #if defined ___AmigaOS___
488 #if defined ___AmigaOS___ 520 FreeDosObject(DOS_FIB, FIB);
489 FreeDosObject(DOS_FIB, FIB); 521 FIB = NULL;
490 UnLock(lock); 522 UnLock(lock);
491 #else 523 #else
-   524 closedir(dir);
492 closedir(dir); 525 #endif
Line 493... Line 526...
493 #endif 526 free(path);
494 free(path); 527 path = NULL;
495 } 528 }
Line 512... Line 545...
512   545  
513 /* 546 /*
514 * 547 *
515 * Writes lines from the database "dbFile" to temporary filenames "tmpNames". 548 * Writes lines from the database "dbFile" to temporary filenames "tmpNames".
516 */ 549 */
517 void WriteTemporaryFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { 550 void WriteTemporaryFiles(char *dbFile, VECTOR *tmpNames, int tmpLines, int total) {
518 #if defined ___AsyncIO___ 551 #if defined ___AsyncIO___
519 struct AsyncFile *fp, *tp; 552 struct AsyncFile *fp, *tp;
520 LONG c; 553 LONG c;
521 #else 554 #else
522 FILE *fp, *tp; 555 FILE *fp, *tp;
523 char c; 556 char c;
524 #endif 557 #endif
525 int lines; 558 int lines;
-   559 int write;
Line 526... Line 560...
526 int write; 560 int files;
527   561  
528 #if defined ___AsyncIO___ 562 #if defined ___AsyncIO___
529 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 563 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
530 #else 564 #else
531 if((fp = fopen(dbFile, "r")) == NULL) { 565 if((fp = fopen(dbFile, "r")) == NULL) {
532 #endif 566 #endif
533 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); 567 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
Line -... Line 568...
-   568 return;
534 return; 569 }
535 } 570  
536   571 files = tmpNames->length;
537 #if defined ___AsyncIO___ 572 #if defined ___AsyncIO___
538 if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) { 573 if((tp = OpenAsync(tmpNames->array[--files], MODE_WRITE, ASYNC_BUF)) == NULL) {
539 #else 574 #else
540 if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) { 575 if((tp = fopen(tmpNames->array[--files], "w")) == NULL) {
541 #endif 576 #endif
542 fprintf(stderr, "Could not open file '%s' for writing.\n", tmpNames[tmpFiles]); 577 fprintf(stderr, "Could not open file '%s' for writing.\n", (char *)tmpNames->array[files]);
543 #if defined ___AsyncIO___ 578 #if defined ___AsyncIO___
544 CloseAsync(fp); 579 CloseAsync(fp);
Line 579... Line 614...
579 #if defined ___AsyncIO___ 614 #if defined ___AsyncIO___
580 if(WriteCharAsync(tp, (UBYTE)c) != 1) { 615 if(WriteCharAsync(tp, (UBYTE)c) != 1) {
581 #else 616 #else
582 if(fprintf(tp, "%c", c) != 1) { 617 if(fprintf(tp, "%c", c) != 1) {
583 #endif 618 #endif
584 fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]); 619 fprintf(stderr, "Unable to write to '%s'.\n", (char *)tmpNames->array[files]);
585 #if defined ___AsyncIO___ 620 #if defined ___AsyncIO___
586 CloseAsync(tp); 621 CloseAsync(tp);
587 CloseAsync(fp); 622 CloseAsync(fp);
588 #else 623 #else
589 fclose(tp); 624 fclose(tp);
Line 592... Line 627...
592 return; 627 return;
593 } 628 }
594 // Switch to the next temporary file. 629 // Switch to the next temporary file.
595 if(++lines >= tmpLines) { 630 if(++lines >= tmpLines) {
596 // If there are no temporary files left then run till the end. 631 // If there are no temporary files left then run till the end.
597 if(tmpFiles - 1 < 0) { 632 if(files - 1 < 0) {
598 break; 633 break;
599 } 634 }
Line 600... Line 635...
600   635  
601 // Close the previous temporary file and write to the next temporary file. 636 // Close the previous temporary file and write to the next temporary file.
602 #if defined ___AsyncIO___ 637 #if defined ___AsyncIO___
603 CloseAsync(tp); 638 CloseAsync(tp);
604 if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) { 639 if((tp = OpenAsync(tmpNames->array[--files], MODE_WRITE, ASYNC_BUF)) == NULL) {
605 #else 640 #else
606 fclose(tp); 641 fclose(tp);
607 if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) { 642 if((tp = fopen(tmpNames->array[--files], "w")) == NULL) {
608 #endif 643 #endif
609 fprintf(stderr, "Could not open '%s' for writing.\n", tmpNames[tmpFiles]); 644 fprintf(stderr, "Could not open '%s' for writing.\n", (char *)tmpNames->array[files]);
610 #if defined ___AsyncIO___ 645 #if defined ___AsyncIO___
611 CloseAsync(fp); 646 CloseAsync(fp);
612 #else 647 #else
613 fclose(fp); 648 fclose(fp);
Line 622... Line 657...
622 #if defined ___AsyncIO___ 657 #if defined ___AsyncIO___
623 if(WriteCharAsync(tp, (UBYTE)c) != 1) { 658 if(WriteCharAsync(tp, (UBYTE)c) != 1) {
624 #else 659 #else
625 if(fprintf(tp, "%c", c) != 1) { 660 if(fprintf(tp, "%c", c) != 1) {
626 #endif 661 #endif
627 fprintf(stderr, "Could not write to file '%s'.\n", tmpNames[tmpFiles]); 662 fprintf(stderr, "Could not write to file '%s'.\n", (char *)tmpNames->array[files]);
628 #if defined ___AsyncIO___ 663 #if defined ___AsyncIO___
629 CloseAsync(tp); 664 CloseAsync(tp);
630 CloseAsync(fp); 665 CloseAsync(fp);
631 #else 666 #else
632 fclose(tp); 667 fclose(tp);
Line 653... Line 688...
653   688  
654 /* 689 /*
655 * 690 *
656 * Merges temporary files "tmpNames" into a database "dbFile". 691 * Merges temporary files "tmpNames" into a database "dbFile".
657 */ 692 */
658 void MergeTemporaryFiles(char *dbFile, char **tmpNames, int files, int lines) { 693 void MergeTemporaryFiles(char *dbFile, VECTOR *tmpNames, int lines) {
659 #if defined ___AsyncIO___ 694 #if defined ___AsyncIO___
660 struct AsyncFile *fp; 695 struct AsyncFile *fp;
661 struct AsyncFile **tp; 696 struct AsyncFile **tp;
662 #else 697 #else
663 FILE *fp; 698 FILE *fp;
664 FILE **tp; 699 FILE **tp;
665 #endif 700 #endif
666 int i; 701 int i;
667 int j; 702 int j;
668 char *tmp; 703 dbLine *tmp;
669 char *rem; 704 char *rem;
670 char *min; 705 char *min;
Line 671... Line 706...
671 int count; 706 int count;
Line 678... Line 713...
678 fprintf(stderr, "Could not open file '%s' for writing.\n", dbFile); 713 fprintf(stderr, "Could not open file '%s' for writing.\n", dbFile);
679 return; 714 return;
680 } 715 }
Line 681... Line 716...
681   716  
682 // Allocate as many file pointers as temporary files. 717 // Allocate as many file pointers as temporary files.
683 if((tp = malloc(files * sizeof(*tp))) == NULL) { 718 if((tp = malloc(tmpNames->length * sizeof(*tp))) == NULL) {
684 fprintf(stderr, "Memory allocation failure.\n"); 719 fprintf(stderr, "Memory allocation failure.\n");
685 #if defined ___AsyncIO___ 720 #if defined ___AsyncIO___
686 CloseAsync(fp); 721 CloseAsync(fp);
687 #else 722 #else
688 fclose(fp); 723 fclose(fp);
689 #endif 724 #endif
690 return; 725 return;
Line 691... Line 726...
691 } 726 }
692   727  
693 // Open all temporary files for reading. 728 // Open all temporary files for reading.
694 for(i = 0; i < files; ++i) { 729 for(i = 0; i < tmpNames->length; ++i) {
695 #if defined ___AsyncIO___ 730 #if defined ___AsyncIO___
696 if((tp[i] = OpenAsync(tmpNames[i], MODE_READ, ASYNC_BUF)) == NULL) { 731 if((tp[i] = OpenAsync(tmpNames->array[i], MODE_READ, ASYNC_BUF)) == NULL) {
697 #else 732 #else
698 if((tp[i] = fopen(tmpNames[i], "r")) == NULL) { 733 if((tp[i] = fopen(tmpNames->array[i], "r")) == NULL) {
699 #endif 734 #endif
700 fprintf(stderr, "Could not open file '%s' for reading.\n", tmpNames[i]); 735 fprintf(stderr, "Could not open file '%s' for reading.\n", tmpNames->array[i]);
701 // Close all temporary files. 736 // Close all temporary files.
702 while(--i > -1) { 737 while(--i > -1) {
703 #if defined ___AsyncIO___ 738 #if defined ___AsyncIO___
Line 734... Line 769...
734 if(PROGRAM_VERBOSE) { 769 if(PROGRAM_VERBOSE) {
735 fprintf(stdout, "Merging all files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0)); 770 fprintf(stdout, "Merging all files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0));
736 } 771 }
Line 737... Line 772...
737   772  
738 min = NULL; 773 min = NULL;
739 for(i = 0; i < files; ++i) { 774 for(i = 0; i < tmpNames->length; ++i) {
740 tmp = PeekLine(tp[i]); 775 tmp = PeekLine(tp[i]);
741 if(tmp == NULL) { 776 if(tmp == NULL) {
742 continue; 777 continue;
743 } 778 }
744 #if defined ___AmigaOS___ 779 #if defined ___AmigaOS___
745 if(min == NULL || StrnCmp(locale, tmp, min, -1, SC_ASCII) < 0) { 780 if(min == NULL || StrnCmp(locale, tmp->string, min, -1, SC_ASCII) < 0) {
746 #else 781 #else
747 if(min == NULL || strcmp(tmp, min) < 0) { 782 if(min == NULL || strcmp(tmp->string, min) < 0) {
748 #endif 783 #endif
749 if(min != NULL) { 784 if(min != NULL) {
750 // Free previous instance. 785 // Free previous instance.
-   786 free(min);
751 free(min); 787 min = NULL;
752 } 788 }
753 if((min = malloc((strlen(tmp) + 1) * sizeof(*min))) == NULL) { 789 if((min = malloc((strlen(tmp->string) + 1) * sizeof(*min))) == NULL) {
-   790 fprintf(stderr, "Memory allocation failure.\n");
-   791  
754 fprintf(stderr, "Memory allocation failure.\n"); 792 free(tmp->string);
-   793 free(tmp);
-   794 tmp = NULL;
755 free(tmp); 795  
756 if(min != NULL) { 796 if(min != NULL) {
-   797 free(min);
757 free(min); 798 min = NULL;
758 } 799 }
759 if(rem != NULL) { 800 if(rem != NULL) {
-   801 free(rem);
760 free(rem); 802 rem = NULL;
761 } 803 }
762 #if defined ___AsyncIO___ 804 #if defined ___AsyncIO___
763 CloseAsync(fp); 805 CloseAsync(fp);
764 #else 806 #else
765 fclose(fp); 807 fclose(fp);
766 #endif 808 #endif
767 return; 809 return;
768 } 810 }
769 sprintf(min, "%s", tmp); 811 sprintf(min, "%s", tmp->string);
770 // Remember the index of the file where the smallest entry has been found. 812 // Remember the index of the file where the smallest entry has been found.
771 j = i; 813 j = i;
-   814 }
772 } 815 free(tmp->string);
-   816 free(tmp);
773 free(tmp); 817 tmp = NULL;
Line 774... Line 818...
774 } 818 }
775   819  
Line 784... Line 828...
784 if(StrnCmp(locale, min, rem, -1, SC_ASCII) == 0) { 828 if(StrnCmp(locale, min, rem, -1, SC_ASCII) == 0) {
785 #else 829 #else
786 if(strcmp(min, rem) == 0) { 830 if(strcmp(min, rem) == 0) {
787 #endif 831 #endif
788 free(min); 832 free(min);
-   833 min = NULL;
789 continue; 834 continue;
790 } 835 }
791 } 836 }
Line 792... Line 837...
792   837  
Line 797... Line 842...
797 fprintf(fp, "%s\n", min); 842 fprintf(fp, "%s\n", min);
798 #endif 843 #endif
Line 799... Line 844...
799   844  
800 if(rem != NULL) { 845 if(rem != NULL) {
-   846 free(rem);
801 free(rem); 847 rem = NULL;
Line 802... Line 848...
802 } 848 }
803   849  
-   850 if((rem = malloc((strlen(min) + 1) * sizeof(*rem))) == NULL) {
804 if((rem = malloc((strlen(min) + 1) * sizeof(*rem))) == NULL) { 851 fprintf(stderr, "Memory allocation failure.\n");
-   852  
-   853 free(min);
805 fprintf(stderr, "Memory allocation failure.\n"); 854 min = NULL;
806 free(min); 855  
807 #if defined ___AsyncIO___ 856 #if defined ___AsyncIO___
808 CloseAsync(fp); 857 CloseAsync(fp);
809 #else 858 #else
810 fclose(fp); 859 fclose(fp);
811 #endif 860 #endif
Line -... Line 861...
-   861 return;
812 return; 862 }
-   863  
813 } 864 // Rememb er the last minimal line.
-   865 sprintf(rem, "%s", min);
814   866  
815 sprintf(rem, "%s", min); 867 free(min);
Line 816... Line 868...
816 free(min); 868 min = NULL;
817 } 869 }
-   870 }
818 } 871  
Line 819... Line 872...
819   872 if(rem != NULL) {
820 if(rem != NULL) { 873 free(rem);
821 free(rem); 874 rem = NULL;
822 } 875 }
823   876  
824 // Write out any remaining contents from the temporary files. 877 // Write out any remaining contents from the temporary files.
825 for(i = 0; PROGRAM_RUN && i < files; ++i) { 878 for(i = 0; PROGRAM_RUN && i < tmpNames->length; ++i) {
Line 833... Line 886...
833 tmp = ReadLine(tp[i]); 886 tmp = ReadLine(tp[i]);
834 if(tmp == NULL) { 887 if(tmp == NULL) {
835 continue; 888 continue;
836 } 889 }
837 #if defined ___AsyncIO___ 890 #if defined ___AsyncIO___
838 WriteAsync(fp, tmp, (LONG)strlen(tmp)); 891 WriteAsync(fp, tmp->string, (LONG)strlen(tmp->string));
839 WriteAsync(fp, "\n", 1); 892 WriteAsync(fp, "\n", 1);
840 #else 893 #else
841 fprintf(fp, "%s\n", tmp); 894 fprintf(fp, "%s\n", tmp->string);
842 #endif 895 #endif
-   896 free(tmp->string);
843 free(tmp); 897 free(tmp);
-   898 tmp = NULL;
844 } 899 }
Line 845... Line 900...
845   900  
846 // Close all temporary files. 901 // Close all temporary files.
847 for(i = 0; i < files; ++i) { 902 for(i = 0; i < tmpNames->length; ++i) {
848 #if defined ___AsyncIO___ 903 #if defined ___AsyncIO___
849 CloseAsync(tp[i]); 904 CloseAsync(tp[i]);
850 #else 905 #else
851 fclose(tp[i]); 906 fclose(tp[i]);
Line 865... Line 920...
865   920  
866 /* 921 /*
867 * 922 *
868 * Filter the paths inside the database with provided paths. 923 * Filter the paths inside the database with provided paths.
869 */ 924 */
870 void FilterDatabasePaths(char *dbFile, char *tmpName, char **paths, int count) { 925 void FilterDatabasePaths(char *dbFile, char *tmpName, VECTOR *paths) {
871 #if defined ___AsyncIO___ 926 #if defined ___AsyncIO___
872 struct AsyncFile *fp; 927 struct AsyncFile *fp;
873 struct AsyncFile *tp; 928 struct AsyncFile *tp;
874 #else 929 #else
875 FILE *fp; 930 FILE *fp;
876 FILE *tp; 931 FILE *tp;
877 #endif 932 #endif
-   933 dbLine *line;
878 char *line; 934 dbEntry *entry;
879 int lines; 935 int lines;
880 int i; -  
Line 881... Line 936...
881 dbEntry *entry; 936 int i;
882   937  
883 // Open database file for reading. 938 // Open database file for reading.
884 #if defined ___AsyncIO___ 939 #if defined ___AsyncIO___
Line 919... Line 974...
919 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 974 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
920 PROGRAM_RUN = FALSE; 975 PROGRAM_RUN = FALSE;
921 continue; 976 continue;
922 } 977 }
923 #endif 978 #endif
-   979  
924 if((entry = CreateDatabaseEntry(line)) == NULL) { 980 if((entry = CreateDatabaseEntry(line)) == NULL) {
925 fprintf(stderr, "Unable to create database entry.\n"); 981 fprintf(stderr, "Unable to create database entry.\n");
-   982 free(line->string);
926 free(line); 983 free(line);
-   984 line = NULL;
927 continue; 985 continue;
928 } 986 }
Line 929... Line 987...
929   987  
930 for(i = 0; i < count; ++i) { 988 for(i = 0; i < paths->length; ++i) {
931 if(PathCompare(entry->path, paths[i]) == TRUE) { 989 if(PathCompare(entry->path, paths->array[i]) == TRUE) {
932 ++lines; 990 ++lines;
933 if(PROGRAM_VERBOSE) { 991 if(PROGRAM_VERBOSE) {
934 fprintf(stdout, "Removing lines: %d.\r", lines); 992 fprintf(stdout, "Removing lines: %d.\r", lines);
935 } 993 }
936 continue; 994 continue;
937 } 995 }
938 #if defined ___AsyncIO___ 996 #if defined ___AsyncIO___
939 WriteAsync(tp, line, (LONG)strlen(line)); 997 WriteAsync(tp, line->string, (LONG)strlen(line->string));
940 WriteAsync(tp, "\n", 1); 998 WriteAsync(tp, "\n", 1);
941 #else 999 #else
942 fprintf(tp, "%s\n", line); 1000 fprintf(tp, "%s\n", line->string);
943 #endif 1001 #endif
944 break; 1002 break;
Line 945... Line -...
945 } -  
-   1003 }
946   1004  
947   1005 // Free up database entry.
948 free(entry->name); 1006 free(entry->name);
-   1007 free(entry->path);
Line -... Line 1008...
-   1008 free(entry);
-   1009 entry = NULL;
949 free(entry->path); 1010  
-   1011 // Free up line.
950 free(entry); 1012 free(line->string);
Line 951... Line 1013...
951   1013 free(line);
952 free(line); 1014 line = NULL;
953 } 1015 }
Line 967... Line 1029...
967   1029  
968 /* 1030 /*
969 * 1031 *
970 * Indexes a "path" by creating a database "dbFile". 1032 * Indexes a "path" by creating a database "dbFile".
971 */ 1033 */
972 void GatherDatabaseFiles(char *dbFile, char **paths, int count) { 1034 void GatherDatabaseFiles(char *dbFile, VECTOR *paths) {
973 dbStats *stats; 1035 dbStats *stats;
974 char **tmpNames; 1036 VECTOR *tmpNames;
975 int tmpFiles; 1037 int tmpFiles;
976 int tmpLines; 1038 int tmpLines;
Line 977... Line 1039...
977 int i; 1039 int i;
978   1040  
979 // Generate the database file from the supplied paths. 1041 // Generate the database file from the supplied paths.
980 if((stats = CollectFiles(dbFile, paths, count)) == NULL) { 1042 if((stats = CollectFiles(dbFile, paths)) == NULL) {
981 fprintf(stderr, "Collecting files failed.\n"); 1043 fprintf(stderr, "Collecting files failed.\n");
Line 982... Line 1044...
982 return; 1044 return;
Line 992... Line 1054...
992 SortDatabase(dbFile, stats->lines); 1054 SortDatabase(dbFile, stats->lines);
993 return; 1055 return;
994 } 1056 }
Line 995... Line 1057...
995   1057  
996 // Calculate the number of lines per temporary file. 1058 // Calculate the number of lines per temporary file.
Line 997... Line 1059...
997 tmpLines = stats->lines / tmpFiles; 1059 tmpLines = ceil(((double)stats->lines) / ((double)tmpFiles));
998   1060  
999 // Create temporary files. 1061 // Create temporary files.
1000 if((tmpNames = CreateTemporaryFiles(tmpFiles)) == NULL) { 1062 if((tmpNames = CreateTemporaryFiles(tmpFiles)) == NULL) {
1001 fprintf(stderr, "Unable to create temporary files.\n"); 1063 fprintf(stderr, "Unable to create temporary files.\n");
Line 1002... Line 1064...
1002 return; 1064 return;
1003 } 1065 }
Line 1004... Line 1066...
1004   1066  
1005 // Write "tmpLines" to temporary files in "tmpNames" from "dbFile". 1067 // Write "tmpLines" to temporary files in "tmpNames" from "dbFile".
1006 WriteTemporaryFiles(dbFile, tmpNames, tmpFiles, tmpLines, stats->lines); -  
1007   1068 WriteTemporaryFiles(dbFile, tmpNames, tmpLines, stats->lines);
1008 // Sort the temporary files. 1069  
Line 1009... Line 1070...
1009 for(i = 0; i < tmpFiles; ++i) { 1070 // Sort the temporary files.
1010 // Twice the number of computed lines required should cover the last file. 1071 for(i = 0; i < tmpNames->length; ++i) {
Line 1011... Line 1072...
1011 SortDatabase(tmpNames[i], 2 * tmpLines); 1072 SortDatabase(tmpNames->array[i], tmpLines);
1012 } 1073 }
Line 1013... Line 1074...
1013   1074  
1014 // Merge all the temporary files to the database file. 1075 // Merge all the temporary files to the database file.
-   1076 MergeTemporaryFiles(dbFile, tmpNames, stats->lines);
Line 1015... Line 1077...
1015 MergeTemporaryFiles(dbFile, tmpNames, tmpFiles, stats->lines); 1077  
1016   1078 // Remove all temporary files.
-   1079 RemoveFiles(tmpNames, tmpFiles);
1017 // Remove all temporary files. 1080  
Line 1018... Line 1081...
1018 RemoveFiles(tmpNames, tmpFiles); 1081 // Free temporary file names.
1019   1082 free(tmpNames);
Line 1020... Line 1083...
1020 // Free temporary file names. 1083 tmpNames = NULL;
1021 free(tmpNames); 1084  
1022   1085 // Free statistics.
1023 // Free statistics. 1086 free(stats);
1024 free(stats); 1087 stats = NULL;
Line 1025... Line 1088...
1025 } 1088 }
1026   1089  
Line 1027... Line 1090...
1027 void RemoveDatabaseFiles(char *dbFile, char **paths, int count) { 1090 void RemoveDatabaseFiles(char *dbFile, VECTOR *paths) {
1028 char *tmpName; 1091 char *tmpName;
Line 1029... Line 1092...
1029   1092  
Line 1073... Line 1136...
1073 * Main entry point. 1136 * Main entry point.
1074 */ 1137 */
1075 int main(int argc, char **argv) { 1138 int main(int argc, char **argv) {
1076 int option; 1139 int option;
1077 int i; 1140 int i;
1078 int count; -  
1079 char *dbFile; 1141 char *dbFile;
1080 char *path; 1142 char *path;
1081 char **paths; 1143 VECTOR *paths;
1082 OPERATION operation = NONE; 1144 OPERATION operation = NONE;
Line 1083... Line 1145...
1083   1145  
1084 // Bind handler to SIGINT. 1146 // Bind handler to SIGINT.
1085 #if !defined ___AmigaOS___ 1147 #if !defined ___AmigaOS___
Line 1124... Line 1186...
1124 if(optind >= argc) { 1186 if(optind >= argc) {
1125 usage(argv[0]); 1187 usage(argv[0]);
1126 return 5; 1188 return 5;
1127 } 1189 }
Line -... Line 1190...
-   1190  
-   1191 // Build the path vector.
-   1192 if((paths = malloc(1 * sizeof(*paths))) == NULL) {
-   1193 fprintf(stderr, "Memory allocation failure.\n");
-   1194 return 20;
-   1195 }
1128   1196  
1129 // Go through all supplied arguments and add paths to search. 1197 // Go through all supplied arguments and add paths to search.
1130 if((paths = malloc((argc - optind) * sizeof(*paths))) == NULL) { 1198 if((paths->array = malloc((argc - optind) * sizeof(*paths))) == NULL) {
1131 fprintf(stderr, "Memory allocation failure.\n"); 1199 fprintf(stderr, "Memory allocation failure.\n");
1132 return 20; 1200 return 20;
Line 1133... Line -...
1133 } -  
1134   1201 }
1135 count = 0; 1202  
1136 for(i = optind, count = 0; i < argc; ++i) { 1203 for(i = optind, paths->length = 0; i < argc; ++i) {
1137 if((path = PathToAbsolute(argv[i])) == NULL) { 1204 if((path = PathToAbsolute(argv[i])) == NULL) {
1138 fprintf(stderr, "Absolute path for '%s' failed to resolve.\n", argv[optind]); 1205 fprintf(stderr, "Absolute path for '%s' failed to resolve.\n", argv[optind]);
Line 1139... Line 1206...
1139 continue; 1206 continue;
1140 } 1207 }
1141   1208  
1142 switch(GetFsType(path)) { 1209 switch(GetFsType(path)) {
1143 case UNKNOWN: 1210 case UNKNOWN:
-   1211 case REGULAR:
1144 case REGULAR: 1212 fprintf(stderr, "Path '%s' is not a directory.\n", path);
1145 fprintf(stderr, "Path '%s' is not a directory.\n", path); -  
1146 free(path); 1213 free(path);
1147 continue; 1214 path = NULL;
1148 break; 1215 continue;
Line 1149... Line 1216...
1149 case DIRECTORY: 1216 case DIRECTORY:
1150 break; 1217 break;
1151 } 1218 }
Line 1152... Line 1219...
1152   1219  
1153 if(PROGRAM_VERBOSE) { 1220 if(PROGRAM_VERBOSE) {
1154 fprintf(stdout, "Will process path: '%s'\n", path); 1221 fprintf(stdout, "Will process path: '%s'\n", path);
1155 } 1222 }
1156   1223  
Line 1157... Line 1224...
1157 // Add the path to the array of paths. 1224 // Add the path to the array of paths.
1158 if((paths[count] = malloc((strlen(path) + 1) * sizeof(*paths[count]))) == NULL) { 1225 if((paths->array[paths->length] = malloc((strlen(path) + 1) * sizeof(*paths->array[paths->length]))) == NULL) {
Line 1159... Line 1226...
1159 fprintf(stderr, "Memory allocation failure."); 1226 fprintf(stderr, "Memory allocation failure.");
-   1227 return 20;
Line 1160... Line 1228...
1160 return 20; 1228 }
Line 1161... Line 1229...
1161 } 1229  
1162   1230 sprintf(paths->array[paths->length], "%s", path);
-   1231 ++paths->length;
1163 sprintf(paths[count], "%s", path); 1232  
-   1233 free(path);
1164 ++count; 1234 path = NULL;
1165   1235  
Line 1166... Line 1236...
1166 free(path); 1236 }
1167   1237  
Line 1189... Line 1259...
1189 RemoveFile(dbFile); 1259 RemoveFile(dbFile);
1190 case GATHER: 1260 case GATHER:
1191 if(PROGRAM_VERBOSE) { 1261 if(PROGRAM_VERBOSE) {
1192 fprintf(stdout, "Gathering files to database...\n"); 1262 fprintf(stdout, "Gathering files to database...\n");
1193 } 1263 }
1194 GatherDatabaseFiles(dbFile, paths, count); 1264 GatherDatabaseFiles(dbFile, paths);
1195 break; 1265 break;
1196 case REMOVE: 1266 case REMOVE:
1197 if(PROGRAM_VERBOSE) { 1267 if(PROGRAM_VERBOSE) {
1198 fprintf(stdout, "Removing files from database...\n"); 1268 fprintf(stdout, "Removing files from database...\n");
1199 } 1269 }
1200 RemoveDatabaseFiles(dbFile, paths, count); 1270 RemoveDatabaseFiles(dbFile, paths);
1201 break; 1271 break;
1202 default: 1272 default:
1203 fprintf(stderr, "Unknown operation.\n"); 1273 fprintf(stderr, "Unknown operation.\n");
1204 #if defined ___AmigaOS___ 1274 #if defined ___AmigaOS___
1205 CloseLocale(locale); 1275 CloseLocale(locale);
1206 #endif 1276 #endif
Line 1207... Line 1277...
1207   1277  
-   1278 free(paths);
1208 free(paths); 1279 paths = NULL;
1209 return 5; 1280 return 5;
Line 1210... Line 1281...
1210 } 1281 }
1211   1282  
1212 #if defined ___AmigaOS___ 1283 #if defined ___AmigaOS___
Line 1213... Line 1284...
1213 CloseLocale(locale); 1284 CloseLocale(locale);
1214 #endif 1285 #endif
1215   1286  
1216 free(paths); 1287 free(paths);