HuntnGather – Blame information for rev 37
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
||
3 | /////////////////////////////////////////////////////////////////////////// |
||
4 | |||
5 | #include <stdio.h> |
||
6 | #include <stdlib.h> |
||
7 | #include <string.h> |
||
27 | office | 8 | #include <ctype.h> |
9 | #if !defined ___AmigaOS___ |
||
1 | office | 10 | #include <signal.h> |
29 | office | 11 | #include <dirent.h> |
12 | #include <sys/stat.h> |
||
27 | office | 13 | #endif |
1 | office | 14 | |
2 | office | 15 | #include <sys/types.h> |
16 | |||
26 | office | 17 | #if defined ___AmigaOS___ |
33 | office | 18 | |
1 | office | 19 | #include <proto/dos.h> |
20 | #include <proto/exec.h> |
||
26 | office | 21 | #include <proto/utility.h> |
22 | #endif |
||
1 | office | 23 | |
22 | office | 24 | #if defined ___AsyncIO___ |
25 | #include <asyncio.h> |
||
26 | #endif |
||
27 | |||
5 | office | 28 | #if !defined ___HAVE_GETOPT___ |
33 | office | 29 | #include "/shared/getopt.h" |
1 | office | 30 | #endif |
31 | |||
33 | office | 32 | #include "/shared/utilities.h" |
33 | |||
26 | office | 34 | #define PROGRAM_VERSION "1.7.4" |
19 | office | 35 | |
5 | office | 36 | #if defined ___AmigaOS___ |
37 | /*************************************************************************/ |
||
38 | /* Version string used for querrying the program version. */ |
||
39 | /*************************************************************************/ |
||
40 | TEXT version_string[] = |
||
19 | office | 41 | "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
5 | office | 42 | #endif |
43 | |||
33 | office | 44 | int PROGRAM_RUN = TRUE; |
45 | int PROGRAM_VERBOSE = FALSE; |
||
1 | office | 46 | |
47 | void SignalHandler(int sig) { |
||
48 | // Toggle the run flag to stop execution. |
||
33 | office | 49 | PROGRAM_RUN = FALSE; |
1 | office | 50 | } |
51 | |||
52 | /* |
||
53 | * |
||
54 | * Search the database for a matching string. |
||
55 | */ |
||
24 | office | 56 | void SearchDatabase(char *dbFile, char* needle) { |
22 | office | 57 | #if defined ___AsyncIO___ |
58 | struct AsyncFile *fp; |
||
59 | #else |
||
1 | office | 60 | FILE *fp; |
22 | office | 61 | #endif |
37 | office | 62 | dbEntry *entry; |
63 | char *line = NULL; |
||
1 | office | 64 | |
37 | office | 65 | // Open database file for reading. |
22 | office | 66 | #if defined ___AsyncIO___ |
67 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
||
68 | #else |
||
1 | office | 69 | if((fp = fopen(dbFile, "r")) == NULL) { |
22 | office | 70 | #endif |
37 | office | 71 | fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); |
1 | office | 72 | return; |
73 | } |
||
74 | |||
37 | office | 75 | while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { |
1 | office | 76 | #if defined ___AmigaOS___ |
77 | // Check if CTRL+C was pressed and abort the program. |
||
37 | office | 78 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
33 | office | 79 | PROGRAM_RUN = FALSE; |
26 | office | 80 | continue; |
1 | office | 81 | } |
82 | #endif |
||
83 | |||
37 | office | 84 | if((entry = CreateDatabaseEntry(line)) == NULL) { |
85 | fprintf(stderr, "Unable to create database entry.\n"); |
||
86 | free(line); |
||
87 | #if defined ___AsyncIO___ |
||
88 | CloseAsync(fp); |
||
89 | #else |
||
90 | fclose(fp); |
||
91 | #endif |
||
92 | return; |
||
1 | office | 93 | } |
37 | office | 94 | |
95 | if(StringMatch(entry->name, needle)) { |
||
96 | fprintf(stdout, "%s\n", entry->path); |
||
97 | } |
||
98 | |||
99 | free(entry->name); |
||
100 | free(entry->path); |
||
101 | free(line); |
||
1 | office | 102 | } |
103 | |||
37 | office | 104 | if(line != NULL) { |
105 | free(line); |
||
106 | } |
||
9 | office | 107 | |
22 | office | 108 | #if defined ___AsyncIO___ |
109 | CloseAsync(fp); |
||
110 | #else |
||
1 | office | 111 | fclose(fp); |
22 | office | 112 | #endif |
1 | office | 113 | } |
114 | |||
2 | office | 115 | /* |
116 | * |
||
117 | * Search the database for the matching string. |
||
118 | */ |
||
24 | office | 119 | void Hunt(char *dbFile, char *needle) { |
1 | office | 120 | // Search the database for the matching string. |
24 | office | 121 | SearchDatabase(dbFile, needle); |
1 | office | 122 | } |
123 | |||
11 | office | 124 | void usage(char *name) { |
125 | fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); |
||
19 | office | 126 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
11 | office | 127 | fprintf(stdout, " \n"); |
128 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); |
||
129 | fprintf(stdout, " \n"); |
||
130 | fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); |
||
131 | fprintf(stdout, " Gather tool that should be searched. \n"); |
||
132 | fprintf(stdout, " \n"); |
||
133 | fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); |
||
134 | fprintf(stdout, " \n"); |
||
135 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
||
136 | } |
||
137 | |||
1 | office | 138 | int main(int argc, char **argv) { |
139 | int option; |
||
2 | office | 140 | char *dbFile; |
1 | office | 141 | |
142 | // Bind handler to SIGINT. |
||
26 | office | 143 | #if !defined ___AmigaOS___ |
1 | office | 144 | signal(SIGINT, SignalHandler); |
26 | office | 145 | #endif |
1 | office | 146 | |
2 | office | 147 | dbFile = DEFAULT_DATABASE_FILE; |
148 | while((option = getopt(argc, argv, "hd:")) != -1) { |
||
1 | office | 149 | switch(option) { |
2 | office | 150 | case 'd': |
151 | dbFile = optarg; |
||
152 | break; |
||
1 | office | 153 | case 'h': |
11 | office | 154 | usage(argv[0]); |
8 | office | 155 | return 0; |
1 | office | 156 | case '?': |
11 | office | 157 | fprintf(stderr, "Invalid option %c.\n", optopt);; |
37 | office | 158 | return 5; |
1 | office | 159 | } |
160 | } |
||
161 | |||
10 | office | 162 | if(optind >= argc) { |
11 | office | 163 | usage(argv[0]); |
37 | office | 164 | return 5; |
1 | office | 165 | } |
166 | |||
33 | office | 167 | switch(GetFsType(dbFile)) { |
168 | case UNKNOWN: |
||
169 | case DIRECTORY: |
||
170 | fprintf(stderr, "'%s' is not a file.\n", dbFile); |
||
37 | office | 171 | return 10; |
33 | office | 172 | case REGULAR: |
173 | Hunt(dbFile, argv[optind]); |
||
174 | break; |
||
29 | office | 175 | } |
176 | |||
1 | office | 177 | return 0; |
178 | } |