HuntnGather – Diff between revs 10 and 11
?pathlinks?
Rev 10 | Rev 11 | |||
---|---|---|---|---|
Line 23... | Line 23... | |||
23 | #if defined ___AmigaOS___ |
23 | #if defined ___AmigaOS___ |
|
24 | /*************************************************************************/ |
24 | /*************************************************************************/ |
|
25 | /* Version string used for querrying the program version. */ |
25 | /* Version string used for querrying the program version. */ |
|
26 | /*************************************************************************/ |
26 | /*************************************************************************/ |
|
27 | TEXT version_string[] = |
27 | TEXT version_string[] = |
|
28 | "\0$VER: Gather 1.5 "__DATE__" by Wizardry and Steamworks"; |
28 | "\0$VER: Gather 1.7 "__DATE__" by Wizardry and Steamworks"; |
|
29 | #endif |
29 | #endif |
|
Line 30... | Line 30... | |||
30 | |
30 | |
|
31 | #if !defined TRUE |
31 | #if !defined TRUE |
|
32 | #define TRUE 1; |
32 | #define TRUE 1; |
|
Line 35... | Line 35... | |||
35 | #if !defined FALSE |
35 | #if !defined FALSE |
|
36 | #define FALSE 0; |
36 | #define FALSE 0; |
|
37 | #endif |
37 | #endif |
|
Line 38... | Line 38... | |||
38 | |
38 | |
|
- | 39 | #define MAX_MEM 262144 |
||
- | 40 | #define NAME_BUF 32 |
||
- | 41 | #define PATH_BUF 128 |
||
39 | #define MAX_MEM 262144 |
42 | #define LINE_BUF 256 |
|
Line 40... | Line 43... | |||
40 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
43 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
|
41 | |
44 | |
|
42 | typedef struct { |
45 | typedef struct { |
|
Line 69... | Line 72... | |||
69 | char **database; |
72 | char **database; |
|
70 | char c; |
73 | char c; |
|
71 | int i; |
74 | int i; |
|
72 | int side; |
75 | int side; |
|
73 | unsigned int line; |
76 | unsigned int line; |
|
- | 77 | int name_size; |
||
- | 78 | int path_size; |
||
Line 74... | Line 79... | |||
74 | |
79 | |
|
75 | // Open database file for reading. |
80 | // Open database file for reading. |
|
76 | if((fp = fopen(dbFile, "r")) == NULL) { |
81 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
77 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
82 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
|
78 | return; |
83 | return; |
|
Line 79... | Line 84... | |||
79 | } |
84 | } |
|
- | 85 | |
||
80 | |
86 | database = malloc(sizeof(char *)); |
|
- | 87 | name_size = NAME_BUF; |
||
81 | database = (char **) malloc(sizeof(char *)); |
88 | name = malloc(name_size * sizeof(char)); |
|
82 | name = (char *) malloc(sizeof(char)); |
89 | path_size = PATH_BUF; |
|
83 | path = (char *) malloc(sizeof(char)); |
90 | path = malloc(path_size * sizeof(char)); |
|
84 | line = 0; |
91 | line = 0; |
|
Line 85... | Line 92... | |||
85 | side = 0; |
92 | side = 0; |
|
Line 98... | Line 105... | |||
98 | } |
105 | } |
|
99 | #endif |
106 | #endif |
|
100 | switch(c) { |
107 | switch(c) { |
|
101 | case '\n': |
108 | case '\n': |
|
102 | // Load up the name and path into the database variable. |
109 | // Load up the name and path into the database variable. |
|
103 | database = (char **) realloc(database, (line + 1) * sizeof(char *)); |
110 | database = realloc(database, (line + 1) * sizeof(char *)); |
|
104 | database[line] = (char *) malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); |
111 | database[line] = malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); |
|
105 | sprintf(database[line], "%s\t%s", name, path); |
112 | sprintf(database[line], "%s\t%s", name, path); |
|
106 | ++line; |
113 | ++line; |
|
Line 107... | Line 114... | |||
107 | |
114 | |
|
- | 115 | free(name); |
||
108 | free(name); |
116 | name_size = NAME_BUF; |
|
109 | name = (char *) malloc(sizeof(char)); |
117 | name = malloc(name_size * sizeof(char)); |
|
110 | --side; |
118 | --side; |
|
Line 111... | Line 119... | |||
111 | i = 0; |
119 | i = 0; |
|
112 | |
120 | |
|
113 | break; |
121 | break; |
|
- | 122 | case '\t': |
||
114 | case '\t': |
123 | free(path); |
|
115 | free(path); |
124 | path_size = PATH_BUF; |
|
116 | path = (char *) malloc(sizeof(char)); |
125 | path = malloc(path_size * sizeof(char)); |
|
117 | ++side; |
126 | ++side; |
|
118 | i = 0; |
127 | i = 0; |
|
119 | break; |
128 | break; |
|
120 | default: |
129 | default: |
|
- | 130 | switch(side) { |
||
- | 131 | case 0: |
||
- | 132 | if(strlen(name) == name_size) { |
||
- | 133 | name_size = name_size * 1.5; |
||
121 | switch(side) { |
134 | name = realloc(name, name_size * sizeof(char)); |
|
122 | case 0: |
135 | } |
|
123 | name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
136 | //name = realloc(name, (i + 1 + 1) * sizeof(char)); |
|
124 | name[i] = c; |
137 | name[i] = c; |
|
125 | name[i + 1] = '\0'; |
138 | name[i + 1] = '\0'; |
|
- | 139 | break; |
||
- | 140 | case 1: |
||
- | 141 | if(strlen(path) == path_size) { |
||
- | 142 | path_size = path_size * 1.5; |
||
126 | break; |
143 | path = realloc(path, path_size * sizeof(char)); |
|
127 | case 1: |
144 | } |
|
128 | path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
145 | //path = realloc(path, (i + 1 + 1) * sizeof(char)); |
|
129 | path[i] = c; |
146 | path[i] = c; |
|
130 | path[i + 1] = '\0'; |
147 | path[i + 1] = '\0'; |
|
131 | break; |
148 | break; |
|
Line 199... | Line 216... | |||
199 | #endif |
216 | #endif |
|
200 | size = sizeof(path) + sizeof(dirEntry->d_name) + 1; |
217 | size = sizeof(path) + sizeof(dirEntry->d_name) + 1; |
|
201 | switch(path[strlen(path) - 1]) { |
218 | switch(path[strlen(path) - 1]) { |
|
202 | case '/': |
219 | case '/': |
|
203 | case ':': // This is a drive path. |
220 | case ':': // This is a drive path. |
|
204 | subPath = (char *) malloc(size); |
221 | subPath = malloc(size); |
|
205 | sprintf(subPath, "%s%s", path, dirEntry->d_name); |
222 | sprintf(subPath, "%s%s", path, dirEntry->d_name); |
|
206 | break; |
223 | break; |
|
207 | default: |
224 | default: |
|
208 | subPath = (char *) malloc(size + 1); |
225 | subPath = malloc(size + 1); |
|
209 | sprintf(subPath, "%s/%s", path, dirEntry->d_name); |
226 | sprintf(subPath, "%s/%s", path, dirEntry->d_name); |
|
210 | break; |
227 | break; |
|
211 | } |
228 | } |
|
212 | stat(subPath, &dirStat); |
229 | stat(subPath, &dirStat); |
|
213 | if(S_ISDIR(dirStat.st_mode)) { |
230 | if(S_ISDIR(dirStat.st_mode)) { |
|
Line 309... | Line 326... | |||
309 | */ |
326 | */ |
|
310 | char **CreateTempFiles(int files) { |
327 | char **CreateTempFiles(int files) { |
|
311 | char **tmpNames; |
328 | char **tmpNames; |
|
312 | int count; |
329 | int count; |
|
Line 313... | Line 330... | |||
313 | |
330 | |
|
Line 314... | Line 331... | |||
314 | tmpNames = (char **) malloc(files * sizeof(char *)); |
331 | tmpNames = malloc(files * sizeof(char *)); |
|
315 | |
332 | |
|
316 | if(verbose) { |
333 | if(verbose) { |
|
Line 445... | Line 462... | |||
445 | * Reads a line from the database file "fp". |
462 | * Reads a line from the database file "fp". |
|
446 | */ |
463 | */ |
|
447 | char *ReadDatabaseLine(FILE *fp) { |
464 | char *ReadDatabaseLine(FILE *fp) { |
|
448 | char c; |
465 | char c; |
|
449 | char *line; |
466 | char *line; |
|
- | 467 | int line_size; |
||
450 | int chars; |
468 | int i; |
|
Line -... | Line 469... | |||
- | 469 | |
||
451 | |
470 | line_size = LINE_BUF; |
|
Line 452... | Line 471... | |||
452 | line = (char *) malloc(sizeof(char)); |
471 | line = malloc(line_size * sizeof(char)); |
|
453 | |
472 | |
|
454 | chars = 0; |
473 | i = 0; |
|
455 | while(run && fscanf(fp, "%c", &c) == 1) { |
474 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
456 | #if defined ___AmigaOS___ |
475 | #if defined ___AmigaOS___ |
|
457 | // Check if CTRL+C was pressed and abort the program. |
476 | // Check if CTRL+C was pressed and abort the program. |
|
458 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
477 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
459 | run = FALSE; |
478 | run = FALSE; |
|
460 | } |
479 | } |
|
461 | #endif |
480 | #endif |
|
462 | switch(c) { |
481 | switch(c) { |
|
463 | case '\n': |
482 | case '\n': |
|
464 | // Rewind the file by the number of read characters. |
483 | // Rewind the file by the number of read characters. |
|
465 | fseek(fp, -(chars + 1), SEEK_CUR); |
484 | fseek(fp, -(i + 1), SEEK_CUR); |
|
- | 485 | return line; |
||
- | 486 | default: |
||
- | 487 | if(strlen(line) == line_size) { |
||
- | 488 | line_size = line_size * 1.5; |
||
466 | return line; |
489 | line = realloc(line, line_size * sizeof(char)); |
|
467 | default: |
490 | } |
|
468 | line = (char *) realloc(line, (chars + 1 + 1) * sizeof(char)); |
491 | //line = realloc(line, (chars + 1 + 1) * sizeof(char)); |
|
469 | line[chars] = c; |
492 | line[i] = c; |
|
470 | line[chars + 1] = '\0'; |
493 | line[i + 1] = '\0'; |
|
471 | break; |
494 | break; |
|
472 | } |
495 | } |
|
Line 473... | Line 496... | |||
473 | ++chars; |
496 | ++i; |
|
474 | } |
497 | } |
|
Line 493... | Line 516... | |||
493 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
516 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
|
494 | return; |
517 | return; |
|
495 | } |
518 | } |
|
Line 496... | Line 519... | |||
496 | |
519 | |
|
497 | // Allocate as many file pointers as temporary files. |
520 | // Allocate as many file pointers as temporary files. |
|
Line 498... | Line 521... | |||
498 | tp = (FILE **) malloc(files * sizeof(FILE *)); |
521 | tp = malloc(files * sizeof(FILE *)); |
|
499 | |
522 | |
|
500 | // Open all temporary files for reading. |
523 | // Open all temporary files for reading. |
|
501 | for(i = 0; i < files; ++i) { |
524 | for(i = 0; i < files; ++i) { |
|
Line 538... | Line 561... | |||
538 | if(tmpMin == NULL || strcmp(tmp, tmpMin) < 0) { |
561 | if(tmpMin == NULL || strcmp(tmp, tmpMin) < 0) { |
|
539 | if(tmpMin != NULL) { |
562 | if(tmpMin != NULL) { |
|
540 | // Free previous instance. |
563 | // Free previous instance. |
|
541 | free(tmpMin); |
564 | free(tmpMin); |
|
542 | } |
565 | } |
|
543 | tmpMin = (char *) malloc((strlen(tmp) + 1) * sizeof(char)); |
566 | tmpMin = malloc((strlen(tmp) + 1) * sizeof(char)); |
|
544 | sprintf(tmpMin, "%s", tmp); |
567 | sprintf(tmpMin, "%s", tmp); |
|
545 | // Remember the index of the file where the smallest entry has been found. |
568 | // Remember the index of the file where the smallest entry has been found. |
|
546 | idxMin = i; |
569 | idxMin = i; |
|
547 | free(tmp); |
570 | free(tmp); |
|
548 | continue; |
571 | continue; |
|
Line 635... | Line 658... | |||
635 | } |
658 | } |
|
Line 636... | Line 659... | |||
636 | |
659 | |
|
637 | MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines); |
660 | MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines); |
|
Line -... | Line 661... | |||
- | 661 | } |
||
- | 662 | |
||
- | 663 | void usage(char *name) { |
||
- | 664 | fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
||
- | 665 | fprintf(stdout, " \n"); |
||
- | 666 | fprintf(stdout, "SYNTAX: %s [-q] DATABASE \n", name); |
||
- | 667 | fprintf(stdout, " \n"); |
||
- | 668 | fprintf(stdout, " -q Do not print out any messages. \n"); |
||
- | 669 | fprintf(stdout, " \n"); |
||
- | 670 | fprintf(stdout, "DATABASE is a path to where the indexed results will be \n"); |
||
- | 671 | fprintf(stdout, "stored for searching with the Hunt tool. \n"); |
||
- | 672 | fprintf(stdout, " \n"); |
||
- | 673 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
||
638 | } |
674 | } |
|
639 | |
675 | |
|
640 | /* |
676 | /* |
|
641 | * |
677 | * |
|
642 | * Main entry point. |
678 | * Main entry point. |
|
Line 657... | Line 693... | |||
657 | break; |
693 | break; |
|
658 | case 'q': |
694 | case 'q': |
|
659 | verbose = FALSE; |
695 | verbose = FALSE; |
|
660 | break; |
696 | break; |
|
661 | case 'h': |
697 | case 'h': |
|
662 | fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
698 | usage(argv[0]); |
|
663 | return 0; |
699 | return 0; |
|
664 | case '?': |
700 | case '?': |
|
665 | fprintf(stderr, "Invalid option %ct.\n", optopt); |
701 | fprintf(stderr, "Invalid option %ct.\n", optopt); |
|
666 | fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
- | ||
667 | return 1; |
702 | return 1; |
|
668 | } |
703 | } |
|
669 | } |
704 | } |
|
Line 670... | Line 705... | |||
670 | |
705 | |
|
671 | |
706 | |
|
672 | if(optind >= argc) { |
707 | if(optind >= argc) { |
|
673 | fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
708 | usage(argv[0]); |
|
Line 674... | Line 709... | |||
674 | return 1; |
709 | return 1; |
|
675 | } |
710 | } |
|
676 | |
711 | |
|
677 | stat(argv[optind], &dirStat); |
- | ||
678 | if(!S_ISDIR(dirStat.st_mode)) { |
712 | stat(argv[optind], &dirStat); |
|
679 | fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
713 | if(!S_ISDIR(dirStat.st_mode)) { |
|
Line 680... | Line 714... | |||
680 | fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
714 | fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
|
681 | return 1; |
715 | return 1; |