HuntnGather – Diff between revs 16 and 19
?pathlinks?
Rev 16 | Rev 19 | |||
---|---|---|---|---|
Line 8... | Line 8... | |||
8 | #include <dirent.h> |
8 | #include <dirent.h> |
|
9 | #include <signal.h> |
9 | #include <signal.h> |
|
Line 10... | Line 10... | |||
10 | |
10 | |
|
11 | #include <sys/types.h> |
11 | #include <sys/types.h> |
|
- | 12 | #include <sys/stat.h> |
||
Line 12... | Line 13... | |||
12 | #include <sys/stat.h> |
13 | #include <sys/syslimits.h> |
|
13 | |
14 | |
|
Line 14... | Line 15... | |||
14 | #include <proto/dos.h> |
15 | #include <proto/dos.h> |
|
Line 15... | Line 16... | |||
15 | #include <proto/exec.h> |
16 | #include <proto/exec.h> |
|
16 | |
17 | |
|
17 | #include "StringStack.h" |
18 | #include "StringStack.h" |
|
Line -... | Line 19... | |||
- | 19 | |
||
- | 20 | #if !defined ___HAVE_GETOPT___ |
||
18 | |
21 | #include "getopt.h" |
|
19 | #if !defined ___HAVE_GETOPT___ |
22 | #endif |
|
20 | #include "getopt.h" |
23 | |
|
21 | #endif |
24 | #define PROGRAM_VERSION "1.7.2" |
|
22 | |
25 | |
|
23 | #if defined ___AmigaOS___ |
26 | #if defined ___AmigaOS___ |
|
24 | /*************************************************************************/ |
27 | /*************************************************************************/ |
|
Line 25... | Line 28... | |||
25 | /* Version string used for querrying the program version. */ |
28 | /* Version string used for querrying the program version. */ |
|
26 | /*************************************************************************/ |
29 | /*************************************************************************/ |
|
27 | TEXT version_string[] = |
30 | TEXT version_string[] = |
|
Line 81... | Line 84... | |||
81 | if((fp = fopen(dbFile, "r")) == NULL) { |
84 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
82 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
85 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
|
83 | return; |
86 | return; |
|
84 | } |
87 | } |
|
Line 85... | Line 88... | |||
85 | |
88 | |
|
86 | database = malloc(sizeof(char *)); |
89 | database = malloc(sizeof(*database)); |
|
87 | name_size = NAME_BUF; |
90 | name_size = NAME_BUF; |
|
88 | name = malloc(name_size * sizeof(char)); |
91 | name = malloc(name_size * sizeof(*name)); |
|
89 | path_size = PATH_BUF; |
92 | path_size = PATH_BUF; |
|
90 | path = malloc(path_size * sizeof(char)); |
93 | path = malloc(path_size * sizeof(*path)); |
|
91 | line = 0; |
94 | line = 0; |
|
92 | side = 0; |
95 | side = 0; |
|
Line 93... | Line 96... | |||
93 | i = 0; |
96 | i = 0; |
|
Line 105... | Line 108... | |||
105 | } |
108 | } |
|
106 | #endif |
109 | #endif |
|
107 | switch(c) { |
110 | switch(c) { |
|
108 | case '\n': |
111 | case '\n': |
|
109 | // Load up the name and path into the database variable. |
112 | // Load up the name and path into the database variable. |
|
110 | database = realloc(database, (line + 1) * sizeof(char *)); |
113 | database = realloc(database, (line + 1) * sizeof(*database)); |
|
111 | database[line] = malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); |
114 | database[line] = malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(*database[line])); |
|
112 | sprintf(database[line], "%s\t%s", name, path); |
115 | sprintf(database[line], "%s\t%s", name, path); |
|
113 | ++line; |
116 | ++line; |
|
Line 114... | Line 117... | |||
114 | |
117 | |
|
115 | free(name); |
118 | free(name); |
|
116 | name_size = NAME_BUF; |
119 | name_size = NAME_BUF; |
|
117 | name = malloc(name_size * sizeof(char)); |
120 | name = malloc(name_size * sizeof(*name)); |
|
118 | --side; |
121 | --side; |
|
Line 119... | Line 122... | |||
119 | i = 0; |
122 | i = 0; |
|
120 | |
123 | |
|
121 | break; |
124 | break; |
|
122 | case '\t': |
125 | case '\t': |
|
123 | free(path); |
126 | free(path); |
|
124 | path_size = PATH_BUF; |
127 | path_size = PATH_BUF; |
|
125 | path = malloc(path_size * sizeof(char)); |
128 | path = malloc(path_size * sizeof(*path)); |
|
126 | ++side; |
129 | ++side; |
|
127 | i = 0; |
130 | i = 0; |
|
128 | break; |
131 | break; |
|
129 | default: |
132 | default: |
|
130 | switch(side) { |
133 | switch(side) { |
|
131 | case 0: |
134 | case 0: |
|
132 | if(strlen(name) == name_size) { |
135 | if(strlen(name) == name_size) { |
|
133 | name_size = name_size * 1.5; |
136 | name_size = name_size * 1.5; |
|
134 | name = realloc(name, name_size * sizeof(char)); |
137 | name = realloc(name, name_size * sizeof(*name)); |
|
135 | } |
138 | } |
|
136 | //name = realloc(name, (i + 1 + 1) * sizeof(char)); |
139 | //name = realloc(name, (i + 1 + 1) * sizeof(char)); |
|
137 | name[i] = c; |
140 | name[i] = c; |
|
138 | name[i + 1] = '\0'; |
141 | name[i + 1] = '\0'; |
|
139 | break; |
142 | break; |
|
140 | case 1: |
143 | case 1: |
|
141 | if(strlen(path) == path_size) { |
144 | if(strlen(path) == path_size) { |
|
142 | path_size = path_size * 1.5; |
145 | path_size = path_size * 1.5; |
|
143 | path = realloc(path, path_size * sizeof(char)); |
146 | path = realloc(path, path_size * sizeof(*path)); |
|
144 | } |
147 | } |
|
145 | //path = realloc(path, (i + 1 + 1) * sizeof(char)); |
148 | //path = realloc(path, (i + 1 + 1) * sizeof(char)); |
|
146 | path[i] = c; |
149 | path[i] = c; |
|
Line 197... | Line 200... | |||
197 | // Check if CTRL+C was pressed and abort the program. |
200 | // Check if CTRL+C was pressed and abort the program. |
|
198 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
201 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
199 | run = FALSE; |
202 | run = FALSE; |
|
200 | } |
203 | } |
|
201 | #endif |
204 | #endif |
|
202 | if((path = stringStackPop(dirStack)) == NULL || |
205 | if((path = stringStackPop(dirStack)) == NULL) { |
|
203 | strlen(path) == 0) { |
- | ||
204 | return; |
206 | return; |
|
205 | } |
207 | } |
|
Line 206... | Line 208... | |||
206 | |
208 | |
|
207 | if((dir = opendir(path)) == NULL) { |
209 | if((dir = opendir(path)) == NULL) { |
|
Line 284... | Line 286... | |||
284 | FILE *fp; |
286 | FILE *fp; |
|
285 | int size; |
287 | int size; |
|
Line 286... | Line 288... | |||
286 | |
288 | |
|
287 | if((fp = fopen(dbFile, "r")) == NULL) { |
289 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
- | 290 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
||
288 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
291 | fclose(fp); |
|
289 | return 0; |
292 | return 0; |
|
Line 290... | Line 293... | |||
290 | } |
293 | } |
|
291 | |
294 | |
|
Line 305... | Line 308... | |||
305 | int lines; |
308 | int lines; |
|
306 | char c; |
309 | char c; |
|
Line 307... | Line 310... | |||
307 | |
310 | |
|
308 | if((fp = fopen(dbFile, "r")) == NULL) { |
311 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
- | 312 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
||
309 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
313 | fclose(fp); |
|
310 | return 0; |
314 | return 0; |
|
Line 311... | Line 315... | |||
311 | } |
315 | } |
|
312 | |
316 | |
|
Line 375... | Line 379... | |||
375 | return; |
379 | return; |
|
376 | } |
380 | } |
|
Line 377... | Line 381... | |||
377 | |
381 | |
|
378 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
382 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
|
- | 383 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
||
379 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
384 | fclose(fp); |
|
380 | return; |
385 | return; |
|
Line 381... | Line 386... | |||
381 | } |
386 | } |
|
382 | |
387 | |
|
Line 403... | Line 408... | |||
403 | } |
408 | } |
|
Line 404... | Line 409... | |||
404 | |
409 | |
|
405 | // Write the newline character back. |
410 | // Write the newline character back. |
|
406 | if(fprintf(tp, "%c", c) != 1) { |
411 | if(fprintf(tp, "%c", c) != 1) { |
|
- | 412 | fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
||
407 | fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
413 | fclose(tp); |
|
408 | fclose(fp); |
414 | fclose(fp); |
|
409 | return; |
415 | return; |
|
410 | } |
416 | } |
|
411 | // Switch to the next temporary file. |
417 | // Switch to the next temporary file. |
|
Line 417... | Line 423... | |||
417 | |
423 | |
|
418 | // Close the previous temporary file and write to the next temporary file. |
424 | // Close the previous temporary file and write to the next temporary file. |
|
419 | fclose(tp); |
425 | fclose(tp); |
|
420 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
426 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
|
- | 427 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
||
421 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
428 | fclose(tp); |
|
422 | fclose(fp); |
429 | fclose(fp); |
|
423 | } |
430 | } |
|
424 | lines = 0; |
431 | lines = 0; |
|
425 | break; |
432 | break; |
|
Line 467... | Line 474... | |||
467 | char *line; |
474 | char *line; |
|
468 | int line_size; |
475 | int line_size; |
|
469 | int i; |
476 | int i; |
|
Line 470... | Line 477... | |||
470 | |
477 | |
|
471 | line_size = LINE_BUF; |
478 | line_size = LINE_BUF; |
|
Line 472... | Line 479... | |||
472 | line = malloc(line_size * sizeof(char)); |
479 | line = malloc(line_size * sizeof(*line)); |
|
473 | |
480 | |
|
474 | i = 0; |
481 | i = 0; |
|
475 | while(run && fscanf(fp, "%c", &c) == 1) { |
482 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
Line 485... | Line 492... | |||
485 | fseek(fp, -(i + 1), SEEK_CUR); |
492 | fseek(fp, -(i + 1), SEEK_CUR); |
|
486 | return line; |
493 | return line; |
|
487 | default: |
494 | default: |
|
488 | if(strlen(line) == line_size) { |
495 | if(strlen(line) == line_size) { |
|
489 | line_size = line_size * 1.5; |
496 | line_size = line_size * 1.5; |
|
490 | line = realloc(line, line_size * sizeof(char)); |
497 | line = realloc(line, line_size * sizeof(*line)); |
|
491 | } |
498 | } |
|
492 | //line = realloc(line, (chars + 1 + 1) * sizeof(char)); |
499 | //line = realloc(line, (chars + 1 + 1) * sizeof(char)); |
|
493 | line[i] = c; |
500 | line[i] = c; |
|
494 | line[i + 1] = '\0'; |
501 | line[i + 1] = '\0'; |
|
495 | break; |
502 | break; |
|
Line 517... | Line 524... | |||
517 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
524 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
|
518 | return; |
525 | return; |
|
519 | } |
526 | } |
|
Line 520... | Line 527... | |||
520 | |
527 | |
|
521 | // Allocate as many file pointers as temporary files. |
528 | // Allocate as many file pointers as temporary files. |
|
Line 522... | Line 529... | |||
522 | tp = malloc(files * sizeof(FILE *)); |
529 | tp = malloc(files * sizeof(*tp)); |
|
523 | |
530 | |
|
524 | // Open all temporary files for reading. |
531 | // Open all temporary files for reading. |
|
525 | for(i = 0; i < files; ++i) { |
532 | for(i = 0; i < files; ++i) { |
|
Line 561... | Line 568... | |||
561 | if(min == NULL || strncmp(tmp, min, strlen(tmp)) < 0) { |
568 | if(min == NULL || strncmp(tmp, min, strlen(tmp)) < 0) { |
|
562 | if(min != NULL) { |
569 | if(min != NULL) { |
|
563 | // Free previous instance. |
570 | // Free previous instance. |
|
564 | free(min); |
571 | free(min); |
|
565 | } |
572 | } |
|
566 | min = malloc((strlen(tmp) + 1) * sizeof(char)); |
573 | min = malloc((strlen(tmp) + 1) * sizeof(*min)); |
|
567 | sprintf(min, "%s", tmp); |
574 | sprintf(min, "%s", tmp); |
|
568 | // Remember the index of the file where the smallest entry has been found. |
575 | // Remember the index of the file where the smallest entry has been found. |
|
569 | j = i; |
576 | j = i; |
|
570 | free(tmp); |
577 | free(tmp); |
|
571 | continue; |
578 | continue; |
|
Line 661... | Line 668... | |||
661 | MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines); |
668 | MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines); |
|
662 | } |
669 | } |
|
Line 663... | Line 670... | |||
663 | |
670 | |
|
664 | void usage(char *name) { |
671 | void usage(char *name) { |
|
- | 672 | fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
||
665 | fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
673 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
|
666 | fprintf(stdout, " \n"); |
674 | fprintf(stdout, " \n"); |
|
667 | fprintf(stdout, "SYNTAX: %s [-q] DATABASE \n", name); |
675 | fprintf(stdout, "SYNTAX: %s [-q] DATABASE \n", name); |
|
668 | fprintf(stdout, " \n"); |
676 | fprintf(stdout, " \n"); |
|
669 | fprintf(stdout, " -q Do not print out any messages. \n"); |
677 | fprintf(stdout, " -q Do not print out any messages. \n"); |
|
Line 679... | Line 687... | |||
679 | * Main entry point. |
687 | * Main entry point. |
|
680 | */ |
688 | */ |
|
681 | int main(int argc, char **argv) { |
689 | int main(int argc, char **argv) { |
|
682 | int option; |
690 | int option; |
|
683 | char *dbFile; |
691 | char *dbFile; |
|
- | 692 | char *path; |
||
684 | struct stat dirStat; |
693 | struct stat dirStat; |
|
- | 694 | #if defined ___AmigaOS___ |
||
- | 695 | BPTR lock; |
||
- | 696 | #endif |
||
Line 685... | Line 697... | |||
685 | |
697 | |
|
686 | // Bind handler to SIGINT. |
698 | // Bind handler to SIGINT. |
|
Line 687... | Line 699... | |||
687 | signal(SIGINT, SignalHandler); |
699 | signal(SIGINT, SignalHandler); |
|
Line 708... | Line 720... | |||
708 | if(optind >= argc) { |
720 | if(optind >= argc) { |
|
709 | usage(argv[0]); |
721 | usage(argv[0]); |
|
710 | return 1; |
722 | return 1; |
|
711 | } |
723 | } |
|
Line -... | Line 724... | |||
- | 724 | |
||
- | 725 | #if defined ___AmigaOS___ |
||
- | 726 | path = malloc(PATH_MAX * sizeof(*path)); |
||
- | 727 | lock = Lock(argv[optind], SHARED_LOCK); |
||
- | 728 | NameFromLock(lock, path, PATH_MAX); |
||
- | 729 | UnLock(lock); |
||
- | 730 | #else |
||
- | 731 | path = realpath(argv[optind], NULL); |
||
- | 732 | #endif |
||
712 | |
733 | |
|
713 | stat(argv[optind], &dirStat); |
734 | stat(path, &dirStat); |
|
714 | if(!S_ISDIR(dirStat.st_mode)) { |
735 | if(!S_ISDIR(dirStat.st_mode)) { |
|
715 | fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
736 | fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
|
716 | return 1; |
737 | return 1; |
|
Line 717... | Line 738... | |||
717 | } |
738 | } |
|
718 | |
739 | |
|
719 | if(verbose) { |
740 | if(verbose) { |
|
Line 720... | Line 741... | |||
720 | fprintf(stdout, "Gathering to database file: %s\n", dbFile); |
741 | fprintf(stdout, "Gathering to database file: %s\n", dbFile); |
|
721 | } |
742 | } |
|
- | 743 | |
||
- | 744 | // Gather. |
||
Line 722... | Line 745... | |||
722 | |
745 | Gather(dbFile, path); |
|
723 | // Gather. |
746 | |