HuntnGather – Diff between revs 27 and 28
?pathlinks?
Rev 27 | Rev 28 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
|
3 | /////////////////////////////////////////////////////////////////////////// |
3 | /////////////////////////////////////////////////////////////////////////// |
|
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 <dirent.h> |
8 | #include <dirent.h> |
|
9 | #if !defined ___AmigaOS___ |
9 | #if !defined ___AmigaOS___ |
|
10 | #include <signal.h> |
10 | #include <signal.h> |
|
11 | #endif |
11 | #endif |
|
12 | |
12 | |
|
13 | #include <sys/types.h> |
13 | #include <sys/types.h> |
|
14 | #include <sys/stat.h> |
14 | #include <sys/stat.h> |
|
15 | #include <sys/syslimits.h> |
15 | #include <sys/syslimits.h> |
|
16 | |
16 | |
|
17 | #if defined ___AmigaOS___ |
17 | #if defined ___AmigaOS___ |
|
18 | #include <proto/dos.h> |
18 | #include <proto/dos.h> |
|
19 | #include <proto/exec.h> |
19 | #include <proto/exec.h> |
|
20 | #include <proto/locale.h> |
20 | #include <proto/locale.h> |
|
21 | #endif |
21 | #endif |
|
22 | |
22 | |
|
23 | #if defined ___AsyncIO___ |
23 | #if defined ___AsyncIO___ |
|
24 | #include <asyncio.h> |
24 | #include <asyncio.h> |
|
25 | #endif |
25 | #endif |
|
26 | |
26 | |
|
27 | #include "StringStack.h" |
27 | #include "StringStack.h" |
|
28 | |
28 | |
|
29 | #if !defined ___HAVE_GETOPT___ |
29 | #if !defined ___HAVE_GETOPT___ |
|
30 | #include "getopt.h" |
30 | #include "getopt.h" |
|
31 | #endif |
31 | #endif |
|
32 | |
32 | |
|
33 | #define PROGRAM_VERSION "1.7.4" |
33 | #define PROGRAM_VERSION "1.7.4" |
|
34 | |
34 | |
|
35 | #if defined ___AmigaOS___ |
35 | #if defined ___AmigaOS___ |
|
36 | /*************************************************************************/ |
36 | /*************************************************************************/ |
|
37 | /* Version string used for querrying the program version. */ |
37 | /* Version string used for querrying the program version. */ |
|
38 | /*************************************************************************/ |
38 | /*************************************************************************/ |
|
39 | TEXT version_string[] = |
39 | TEXT version_string[] = |
|
40 | "\0$VER: Gather " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
40 | "\0$VER: Gather " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
|
41 | #endif |
41 | #endif |
|
42 | |
42 | |
|
43 | #if !defined TRUE |
43 | #if !defined TRUE |
|
44 | #define TRUE 1; |
44 | #define TRUE 1; |
|
45 | #endif |
45 | #endif |
|
46 | |
46 | |
|
47 | #if !defined FALSE |
47 | #if !defined FALSE |
|
48 | #define FALSE 0; |
48 | #define FALSE 0; |
|
49 | #endif |
49 | #endif |
|
50 | |
50 | |
|
51 | #define ASYNC_BUF 8192 |
51 | #define ASYNC_BUF 8192 |
|
52 | #define MAX_MEM 262144 |
52 | #define MAX_MEM 262144 |
|
53 | #define LINE_BUF 256 |
53 | #define LINE_BUF 256 |
|
54 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
54 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
|
55 | |
55 | |
|
56 | typedef struct { |
56 | typedef struct { |
|
57 | unsigned int dirs; |
57 | unsigned int dirs; |
|
58 | unsigned int files; |
58 | unsigned int files; |
|
59 | } stats; |
59 | } stats; |
|
60 | |
60 | |
|
61 | typedef struct { |
61 | typedef struct { |
|
62 | char *name; |
62 | char *name; |
|
63 | char *path; |
63 | char *path; |
|
64 | } dbEntry; |
64 | } dbEntry; |
|
65 | |
65 | |
|
66 | typedef struct { |
66 | typedef struct { |
|
67 | char **database; |
67 | char **database; |
|
68 | unsigned int count; |
68 | unsigned int count; |
|
69 | } dbArray; |
69 | } dbArray; |
|
70 | |
70 | |
|
71 | enum MODE { |
71 | enum MODE { |
|
72 | NONE, |
72 | NONE, |
|
73 | GATHER, |
73 | GATHER, |
|
74 | REMOVE, |
74 | REMOVE, |
|
75 | CREATE |
75 | CREATE |
|
76 | } operation; |
76 | } operation; |
|
77 | |
77 | |
|
78 | unsigned int run = TRUE; |
78 | unsigned int run = TRUE; |
|
79 | unsigned int verbose = TRUE; |
79 | unsigned int verbose = TRUE; |
|
80 | unsigned int maxmem = MAX_MEM; |
80 | unsigned int maxmem = MAX_MEM; |
|
81 | // Define global locale for string compare. |
81 | // Define global locale for string compare. |
|
82 | #if defined ___AmigaOS___ |
82 | #if defined ___AmigaOS___ |
|
83 | struct Locale *locale; |
83 | struct Locale *locale; |
|
84 | #endif |
84 | #endif |
|
85 | |
85 | |
|
86 | void SignalHandler(int sig) { |
86 | void SignalHandler(int sig) { |
|
87 | // Toggle the run flag to stop execution. |
87 | // Toggle the run flag to stop execution. |
|
88 | run = FALSE; |
88 | run = FALSE; |
|
89 | } |
89 | } |
|
90 | |
90 | |
|
91 | /* |
91 | /* |
|
92 | * |
92 | * |
|
93 | * Used for sorting database lines. |
93 | * Used for sorting database lines. |
|
94 | */ |
94 | */ |
|
95 | int QsortCompare(const void *a, const void *b) { |
95 | int QsortCompare(const void *a, const void *b) { |
|
96 | const char **p = (const char **)a; |
96 | const char **p = (const char **)a; |
|
97 | const char **q = (const char **)b; |
97 | const char **q = (const char **)b; |
|
98 | #if defined ___AmigaOS___ |
98 | #if defined ___AmigaOS___ |
|
99 | return StrnCmp(locale, (STRPTR)*p, (STRPTR)*q, -1, SC_ASCII); |
99 | return StrnCmp(locale, (STRPTR)*p, (STRPTR)*q, -1, SC_ASCII); |
|
100 | #else |
100 | #else |
|
101 | return strcmp(*p, *q); |
101 | return strcmp(*p, *q); |
|
102 | #endif |
102 | #endif |
|
103 | } |
103 | } |
|
104 | |
104 | |
|
105 | /* |
105 | /* |
|
106 | * |
106 | * |
|
107 | * Gets the absolute path to file by name. |
107 | * Gets the absolute path to file by name. |
|
108 | */ |
108 | */ |
|
109 | char *PathToAbsolute(char *path) { |
109 | char *PathToAbsolute(char *path) { |
|
110 | char *abs; |
110 | char *abs; |
|
111 | #if defined ___AmigaOS___ |
111 | #if defined ___AmigaOS___ |
|
112 | BPTR lock; |
112 | BPTR lock; |
|
113 | #endif |
113 | #endif |
|
114 | |
114 | |
|
115 | #if defined ___AmigaOS___ |
115 | #if defined ___AmigaOS___ |
|
116 | if((abs = malloc(PATH_MAX * sizeof(*abs))) == NULL) { |
116 | if((abs = malloc(PATH_MAX * sizeof(*abs))) == NULL) { |
|
117 | fprintf(stderr, "Memory allocation failure.\n"); |
117 | fprintf(stderr, "Memory allocation failure.\n"); |
|
118 | return NULL; |
118 | return NULL; |
|
119 | } |
119 | } |
|
120 | if((lock = Lock(path, SHARED_LOCK)) == 0) { |
120 | if((lock = Lock(path, SHARED_LOCK)) == 0) { |
|
121 | fprintf(stderr, "Lock on %s failed.\n", path); |
121 | fprintf(stderr, "Lock on %s failed.\n", path); |
|
122 | return NULL; |
122 | return NULL; |
|
123 | } |
123 | } |
|
124 | if(NameFromLock(lock, abs, PATH_MAX) == FALSE) { |
124 | if(NameFromLock(lock, abs, PATH_MAX) == FALSE) { |
|
125 | fprintf(stderr, "Lock on %s failed.\n", path); |
125 | fprintf(stderr, "Lock on %s failed.\n", path); |
|
126 | UnLock(lock); |
126 | UnLock(lock); |
|
127 | return NULL; |
127 | return NULL; |
|
128 | } |
128 | } |
|
129 | UnLock(lock); |
129 | UnLock(lock); |
|
130 | #else |
130 | #else |
|
131 | abs = realpath(path, NULL); |
131 | abs = realpath(path, NULL); |
|
132 | #endif |
132 | #endif |
|
133 | |
133 | |
|
134 | return abs; |
134 | return abs; |
|
135 | } |
135 | } |
|
136 | |
136 | |
|
137 | /* |
137 | /* |
|
138 | * |
138 | * |
|
139 | * Compares path parts for equality. |
139 | * Compares path parts for equality. |
|
140 | */ |
140 | */ |
|
141 | #if defined ___AmigaOS___ |
141 | #if defined ___AmigaOS___ |
|
142 | BOOL PathCompare(char *path, char *look) { |
142 | BOOL PathCompare(char *path, char *look) { |
|
143 | #else |
143 | #else |
|
144 | int PathCompare(char *path, char *look) { |
144 | int PathCompare(char *path, char *look) { |
|
145 | #endif |
145 | #endif |
|
146 | char *a; |
146 | char *a; |
|
147 | char *b; |
147 | char *b; |
|
148 | |
148 | |
|
149 | for(a = path, b = look; *a != '\0' && *b != '\0'; ++a, ++b) { |
149 | for(a = path, b = look; *a != '\0' && *b != '\0'; ++a, ++b) { |
|
150 | if(*b != '\0' && *a != *b) { |
150 | if(*b != '\0' && *a != *b) { |
|
151 | return FALSE; |
151 | return FALSE; |
|
152 | } |
152 | } |
|
153 | } |
153 | } |
|
154 | |
154 | |
|
155 | return *b == '\0'; |
155 | return *b == '\0'; |
|
156 | } |
156 | } |
|
157 | |
157 | |
|
158 | /* |
158 | /* |
|
159 | * |
159 | * |
|
160 | * Gets the size of a file by name. |
160 | * Gets the size of a file by name. |
|
161 | */ |
161 | */ |
|
162 | int GetFileSize(char *dbFile) { |
162 | int GetFileSize(char *dbFile) { |
|
163 | #if defined ___AsyncIO___ |
163 | #if defined ___AsyncIO___ |
|
164 | struct AsyncFile *fp; |
164 | struct AsyncFile *fp; |
|
165 | LONG size; |
165 | LONG size; |
|
166 | #else |
166 | #else |
|
167 | FILE *fp; |
167 | FILE *fp; |
|
168 | int size; |
168 | int size; |
|
169 | #endif |
169 | #endif |
|
170 | |
170 | |
|
171 | #if defined ___AsyncIO___ |
171 | #if defined ___AsyncIO___ |
|
172 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
172 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
173 | #else |
173 | #else |
|
174 | if((fp = fopen(dbFile, "r")) == NULL) { |
174 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
175 | #endif |
175 | #endif |
|
176 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
176 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
|
177 | return -1; |
177 | return -1; |
|
178 | } |
178 | } |
|
179 | |
179 | |
|
180 | #if defined ___AsyncIO___ |
180 | #if defined ___AsyncIO___ |
|
181 | if(SeekAsync(fp, 0, MODE_END) == -1) { |
181 | if(SeekAsync(fp, 0, MODE_END) == -1) { |
|
182 | #else |
182 | #else |
|
183 | if(fseek(fp, 0L, SEEK_END) == 0) { |
183 | if(fseek(fp, 0L, SEEK_END) == 0) { |
|
184 | #endif |
184 | #endif |
|
185 | fprintf(stderr, "Seek in file %s failed.\n", dbFile); |
185 | fprintf(stderr, "Seek in file %s failed.\n", dbFile); |
|
186 | #if defined ___AsyncIO___ |
186 | #if defined ___AsyncIO___ |
|
187 | CloseAsync(fp); |
187 | CloseAsync(fp); |
|
188 | #else |
188 | #else |
|
189 | fclose(fp); |
189 | fclose(fp); |
|
190 | #endif |
190 | #endif |
|
191 | return -1; |
191 | return -1; |
|
192 | } |
192 | } |
|
193 | #if defined ___AsyncIO___ |
193 | #if defined ___AsyncIO___ |
|
194 | if((size = SeekAsync(fp, 0, MODE_CURRENT)) == -1) { |
194 | if((size = SeekAsync(fp, 0, MODE_CURRENT)) == -1) { |
|
195 | fprintf(stderr, "Seek in file %s failed.\n", dbFile); |
195 | fprintf(stderr, "Seek in file %s failed.\n", dbFile); |
|
196 | CloseAsync(fp); |
196 | CloseAsync(fp); |
|
197 | return -1; |
197 | return -1; |
|
198 | } |
198 | } |
|
199 | #else |
199 | #else |
|
200 | size = ftell(fp); |
200 | size = ftell(fp); |
|
201 | #endif |
201 | #endif |
|
202 | |
202 | |
|
203 | #if defined ___AsyncIO___ |
203 | #if defined ___AsyncIO___ |
|
204 | CloseAsync(fp); |
204 | CloseAsync(fp); |
|
205 | #else |
205 | #else |
|
206 | fclose(fp); |
206 | fclose(fp); |
|
207 | #endif |
207 | #endif |
|
208 | |
208 | |
|
209 | return size; |
209 | return size; |
|
210 | } |
210 | } |
|
211 | |
211 | |
|
212 | /* |
212 | /* |
|
213 | * |
213 | * |
|
214 | * Counts the lines of a file. |
214 | * Counts the lines of a file. |
|
215 | */ |
215 | */ |
|
216 | int CountFileLines(char *dbFile) { |
216 | int CountFileLines(char *dbFile) { |
|
217 | #if defined ___AsyncIO___ |
217 | #if defined ___AsyncIO___ |
|
218 | struct AsyncFile *fp; |
218 | struct AsyncFile *fp; |
|
219 | LONG c; |
219 | LONG c; |
|
220 | #else |
220 | #else |
|
221 | FILE *fp; |
221 | FILE *fp; |
|
222 | char c; |
222 | char c; |
|
223 | #endif |
223 | #endif |
|
224 | int lines; |
224 | int lines; |
|
225 | |
225 | |
|
226 | #if defined ___AsyncIO___ |
226 | #if defined ___AsyncIO___ |
|
227 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
227 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
228 | #else |
228 | #else |
|
229 | if((fp = fopen(dbFile, "r")) == NULL) { |
229 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
230 | #endif |
230 | #endif |
|
231 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
231 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
|
232 | return -1; |
232 | return -1; |
|
233 | } |
233 | } |
|
234 | |
234 | |
|
235 | lines = 0; |
235 | lines = 0; |
|
236 | #if defined ___AsyncIO___ |
236 | #if defined ___AsyncIO___ |
|
237 | while(run && (c = ReadCharAsync(fp)) != -1) { |
237 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
238 | #else |
238 | #else |
|
239 | while(run && fscanf(fp, "%c", &c) == 1) { |
239 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
240 | #endif |
240 | #endif |
|
241 | #if defined ___AmigaOS___ |
241 | #if defined ___AmigaOS___ |
|
242 | // Check if CTRL+C was pressed and abort the program. |
242 | // Check if CTRL+C was pressed and abort the program. |
|
243 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
243 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
244 | run = FALSE; |
244 | run = FALSE; |
|
245 | continue; |
245 | continue; |
|
246 | } |
246 | } |
|
247 | #endif |
247 | #endif |
|
248 | switch(c) { |
248 | switch(c) { |
|
249 | case '\n': |
249 | case '\n': |
|
250 | ++lines; |
250 | ++lines; |
|
251 | break; |
251 | break; |
|
252 | } |
252 | } |
|
253 | } |
253 | } |
|
254 | |
254 | |
|
255 | #if defined ___AsyncIO___ |
255 | #if defined ___AsyncIO___ |
|
256 | CloseAsync(fp); |
256 | CloseAsync(fp); |
|
257 | #else |
257 | #else |
|
258 | fclose(fp); |
258 | fclose(fp); |
|
259 | #endif |
259 | #endif |
|
260 | |
260 | |
|
261 | return lines; |
261 | return lines; |
|
262 | } |
262 | } |
|
263 | |
263 | |
|
264 | /* |
264 | /* |
|
265 | * |
265 | * |
|
266 | * Creates a temporary file and returns its name. |
266 | * Creates a temporary file and returns its name. |
|
267 | */ |
267 | */ |
|
268 | char *CreateTemporaryFile(void) { |
268 | char *CreateTemporaryFile(void) { |
|
269 | char *name; |
269 | char *name; |
|
270 | |
270 | |
|
271 | name = tmpnam(NULL); |
271 | name = tmpnam(NULL); |
|
272 | |
272 | |
|
273 | return name; |
273 | return name; |
|
274 | } |
274 | } |
|
275 | |
275 | |
|
276 | /* |
276 | /* |
|
277 | * |
277 | * |
|
278 | * Create multiple temporary files and return their names. |
278 | * Create multiple temporary files and return their names. |
|
279 | */ |
279 | */ |
|
280 | char **CreateTemporaryFiles(int files) { |
280 | char **CreateTemporaryFiles(int files) { |
|
281 | char **tmpNames; |
281 | char **tmpNames; |
|
282 | int count; |
282 | int count; |
|
283 | |
283 | |
|
284 | if((tmpNames = malloc(files * sizeof(*tmpNames))) == NULL) { |
284 | if((tmpNames = malloc(files * sizeof(*tmpNames))) == NULL) { |
|
285 | fprintf(stderr, "Memory allocation failure.\n"); |
285 | fprintf(stderr, "Memory allocation failure.\n"); |
|
286 | return NULL; |
286 | return NULL; |
|
287 | } |
287 | } |
|
288 | |
288 | |
|
289 | if(verbose) { |
289 | if(verbose) { |
|
290 | fprintf(stdout, "Creating temporary files...\r"); |
290 | fprintf(stdout, "Creating temporary files...\r"); |
|
291 | } |
291 | } |
|
292 | |
292 | |
|
293 | count = files; |
293 | count = files; |
|
294 | while(run && --count > -1) { |
294 | while(run && --count > -1) { |
|
295 | #if defined ___AmigaOS___ |
295 | #if defined ___AmigaOS___ |
|
296 | // Check if CTRL+C was pressed and abort the program. |
296 | // Check if CTRL+C was pressed and abort the program. |
|
297 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
297 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
298 | run = FALSE; |
298 | run = FALSE; |
|
299 | continue; |
299 | continue; |
|
300 | } |
300 | } |
|
301 | #endif |
301 | #endif |
|
302 | tmpNames[count] = CreateTemporaryFile(); |
302 | tmpNames[count] = CreateTemporaryFile(); |
|
303 | |
303 | |
|
304 | if(verbose) { |
304 | if(verbose) { |
|
305 | fprintf(stdout, "Creating temporary files: %d%%\r", 100 - (int)(((float)count / files) * 100.0)); |
305 | fprintf(stdout, "Creating temporary files: %d%%\r", 100 - (int)(((float)count / files) * 100.0)); |
|
306 | } |
306 | } |
|
307 | } |
307 | } |
|
308 | |
308 | |
|
309 | if(verbose) { |
309 | if(verbose) { |
|
310 | fprintf(stdout, "\n"); |
310 | fprintf(stdout, "\n"); |
|
311 | } |
311 | } |
|
312 | |
312 | |
|
313 | return tmpNames; |
313 | return tmpNames; |
|
314 | } |
314 | } |
|
315 | |
315 | |
|
316 | |
316 | |
|
317 | /* |
317 | /* |
|
318 | * |
318 | * |
|
319 | * Skips a line in a file. |
319 | * Skips a line in a file. |
|
320 | */ |
320 | */ |
|
321 | #if defined ___AsyncIO___ |
321 | #if defined ___AsyncIO___ |
|
322 | void SkipLine(struct AsyncFile *fp) { |
322 | void SkipLine(struct AsyncFile *fp) { |
|
323 | LONG c; |
323 | LONG c; |
|
324 | while(run && (c = ReadCharAsync(fp)) != -1) { |
324 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
325 | #else |
325 | #else |
|
326 | void SkipLine(FILE *fp) { |
326 | void SkipLine(FILE *fp) { |
|
327 | char c; |
327 | char c; |
|
328 | while(run && fscanf(fp, "%c", &c) == 1) { |
328 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
329 | #endif |
329 | #endif |
|
330 | #if defined ___AmigaOS___ |
330 | #if defined ___AmigaOS___ |
|
331 | // Check if CTRL+C was pressed and abort the program. |
331 | // Check if CTRL+C was pressed and abort the program. |
|
332 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
332 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
333 | run = FALSE; |
333 | run = FALSE; |
|
334 | continue; |
334 | continue; |
|
335 | } |
335 | } |
|
336 | #endif |
336 | #endif |
|
337 | switch(c) { |
337 | switch(c) { |
|
338 | case '\n': |
338 | case '\n': |
|
339 | return; |
339 | return; |
|
340 | } |
340 | } |
|
341 | } |
341 | } |
|
342 | } |
342 | } |
|
343 | |
343 | |
|
344 | /* |
344 | /* |
|
345 | * |
345 | * |
|
346 | * Peeks at a line from a file. |
346 | * Peeks at a line from a file. |
|
347 | */ |
347 | */ |
|
348 | #if defined ___AsyncIO___ |
348 | #if defined ___AsyncIO___ |
|
349 | char *PeekLine(struct AsyncFile *fp) { |
349 | char *PeekLine(struct AsyncFile *fp) { |
|
350 | LONG c; |
350 | LONG c; |
|
351 | #else |
351 | #else |
|
352 | char *PeekLine(FILE *fp) { |
352 | char *PeekLine(FILE *fp) { |
|
353 | char c; |
353 | char c; |
|
354 | #endif |
354 | #endif |
|
355 | char *line; |
355 | char *line; |
|
356 | unsigned int size; |
356 | unsigned int size; |
|
357 | int i; |
357 | int i; |
|
358 | |
358 | |
|
359 | size = LINE_BUF; |
359 | size = LINE_BUF; |
|
360 | if((line = malloc(size * sizeof(*line))) == NULL) { |
360 | if((line = malloc(size * sizeof(*line))) == NULL) { |
|
361 | fprintf(stderr, "Memory allocation failure.\n"); |
361 | fprintf(stderr, "Memory allocation failure.\n"); |
|
362 | return NULL; |
362 | return NULL; |
|
363 | } |
363 | } |
|
364 | |
364 | |
|
365 | i = 0; |
365 | i = 0; |
|
366 | #if defined ___AsyncIO___ |
366 | #if defined ___AsyncIO___ |
|
367 | while(run && (c = ReadCharAsync(fp)) != -1) { |
367 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
368 | #else |
368 | #else |
|
369 | while(run && fscanf(fp, "%c", &c) == 1) { |
369 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
370 | #endif |
370 | #endif |
|
371 | #if defined ___AmigaOS___ |
371 | #if defined ___AmigaOS___ |
|
372 | // Check if CTRL+C was pressed and abort the program. |
372 | // Check if CTRL+C was pressed and abort the program. |
|
373 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
373 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
374 | run = FALSE; |
374 | run = FALSE; |
|
375 | continue; |
375 | continue; |
|
376 | } |
376 | } |
|
377 | #endif |
377 | #endif |
|
378 | switch(c) { |
378 | switch(c) { |
|
379 | case '\n': |
379 | case '\n': |
|
380 | // Rewind the file by the number of read characters. |
380 | // Rewind the file by the number of read characters. |
|
381 | #if defined ___AsyncIO___ |
381 | #if defined ___AsyncIO___ |
|
382 | if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) { |
382 | if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) { |
|
383 | fprintf(stderr, "Could not seek in file.\n"); |
383 | fprintf(stderr, "Could not seek in file.\n"); |
|
384 | free(line); |
384 | free(line); |
|
385 | return NULL; |
385 | return NULL; |
|
386 | } |
386 | } |
|
387 | #else |
387 | #else |
|
388 | fseek(fp, -(i + 1), SEEK_CUR); |
388 | fseek(fp, -(i + 1), SEEK_CUR); |
|
389 | #endif |
389 | #endif |
|
390 | return line; |
390 | return line; |
|
391 | default: |
391 | default: |
|
392 | if(strlen(line) == size) { |
392 | if(strlen(line) == size) { |
|
393 | size = size * 1.5; |
393 | size = size * 1.5; |
|
394 | line = realloc(line, size * sizeof(*line)); |
394 | line = realloc(line, size * sizeof(*line)); |
|
395 | } |
395 | } |
|
396 | line[i] = c; |
396 | line[i] = c; |
|
397 | line[i + 1] = '\0'; |
397 | line[i + 1] = '\0'; |
|
398 | break; |
398 | break; |
|
399 | } |
399 | } |
|
400 | ++i; |
400 | ++i; |
|
401 | } |
401 | } |
|
402 | |
402 | |
|
403 | return NULL; |
403 | return NULL; |
|
404 | } |
404 | } |
|
405 | |
405 | |
|
406 | /* |
406 | /* |
|
407 | * |
407 | * |
|
408 | * Read a line from a file. |
408 | * Read a line from a file. |
|
409 | */ |
409 | */ |
|
410 | #if defined ___AsyncIO___ |
410 | #if defined ___AsyncIO___ |
|
411 | char *ReadLine(struct AsyncFile *fp) { |
411 | char *ReadLine(struct AsyncFile *fp) { |
|
412 | LONG c; |
412 | LONG c; |
|
413 | #else |
413 | #else |
|
414 | char *ReadLine(FILE *fp) { |
414 | char *ReadLine(FILE *fp) { |
|
415 | char c; |
415 | char c; |
|
416 | #endif |
416 | #endif |
|
417 | char *line; |
417 | char *line; |
|
418 | unsigned int size; |
418 | unsigned int size; |
|
419 | unsigned int i; |
419 | unsigned int i; |
|
420 | |
420 | |
|
421 | size = LINE_BUF; |
421 | size = LINE_BUF; |
|
422 | if((line = malloc(size * sizeof(*line))) == NULL) { |
422 | if((line = malloc(size * sizeof(*line))) == NULL) { |
|
423 | fprintf(stderr, "Memory allication failure.\n"); |
423 | fprintf(stderr, "Memory allication failure.\n"); |
|
424 | return NULL; |
424 | return NULL; |
|
425 | } |
425 | } |
|
426 | |
426 | |
|
427 | i = 0; |
427 | i = 0; |
|
428 | #if defined ___AsyncIO___ |
428 | #if defined ___AsyncIO___ |
|
429 | while(run && (c = ReadCharAsync(fp)) != -1) { |
429 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
430 | #else |
430 | #else |
|
431 | while(run && fscanf(fp, "%c", &c) == 1) { |
431 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
432 | #endif |
432 | #endif |
|
433 | #if defined ___AmigaOS___ |
433 | #if defined ___AmigaOS___ |
|
434 | // Check if CTRL+C was pressed and abort the program. |
434 | // Check if CTRL+C was pressed and abort the program. |
|
435 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
435 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
436 | run = FALSE; |
436 | run = FALSE; |
|
437 | continue; |
437 | continue; |
|
438 | } |
438 | } |
|
439 | #endif |
439 | #endif |
|
440 | switch(c) { |
440 | switch(c) { |
|
441 | case '\n': |
441 | case '\n': |
|
442 | return line; |
442 | return line; |
|
443 | default: |
443 | default: |
|
444 | if(strlen(line) == size) { |
444 | if(strlen(line) == size) { |
|
445 | size = size * 1.5; |
445 | size = size * 1.5; |
|
446 | line = realloc(line, size * sizeof(*line)); |
446 | line = realloc(line, size * sizeof(*line)); |
|
447 | } |
447 | } |
|
448 | line[i] = c; |
448 | line[i] = c; |
|
449 | line[i + 1] = '\0'; |
449 | line[i + 1] = '\0'; |
|
450 | break; |
450 | break; |
|
451 | } |
451 | } |
|
452 | ++i; |
452 | ++i; |
|
453 | } |
453 | } |
|
454 | |
454 | |
|
455 | return NULL; |
455 | return NULL; |
|
456 | } |
456 | } |
|
457 | |
457 | |
|
458 | /* |
458 | /* |
|
459 | * |
459 | * |
|
460 | * Delete a file. |
460 | * Delete a file. |
|
461 | */ |
461 | */ |
|
462 | #if defined ___AmigaOS___ |
462 | #if defined ___AmigaOS___ |
|
463 | BOOL RemoveFile(char *name) { |
463 | BOOL RemoveFile(char *name) { |
|
464 | return DeleteFile(name); |
464 | return DeleteFile(name); |
|
465 | #else |
465 | #else |
|
466 | int RemoveFile(char *name) { |
466 | int RemoveFile(char *name) { |
|
467 | return remove(name) == 0; |
467 | return remove(name) == 0; |
|
468 | #endif |
468 | #endif |
|
469 | } |
469 | } |
|
470 | |
470 | |
|
471 | /* |
471 | /* |
|
472 | * |
472 | * |
|
473 | * Deletes files. |
473 | * Deletes files. |
|
474 | */ |
474 | */ |
|
475 | void RemoveFiles(char **names, int count) { |
475 | void RemoveFiles(char **names, int count) { |
|
476 | unsigned int i; |
476 | unsigned int i; |
|
477 | for(i = 0; i < count; ++i) { |
477 | for(i = 0; i < count; ++i) { |
|
478 | if(RemoveFile(names[i]) == FALSE) { |
478 | if(RemoveFile(names[i]) == FALSE) { |
|
479 | fprintf(stderr, "Unable to remove %s...\n", names[i]); |
479 | fprintf(stderr, "Unable to remove %s...\n", names[i]); |
|
480 | continue; |
480 | continue; |
|
481 | } |
481 | } |
|
482 | fprintf(stderr, "Removing file: %s\n", names[i]); |
482 | fprintf(stderr, "Removing file: %s\n", names[i]); |
|
483 | } |
483 | } |
|
484 | } |
484 | } |
|
485 | |
485 | |
|
486 | /* |
486 | /* |
|
487 | * |
487 | * |
|
488 | * Copies a file to another file by name. |
488 | * Copies a file to another file by name. |
|
489 | */ |
489 | */ |
|
490 | void CopyFile(char *a, char *b) { |
490 | void CopyFile(char *a, char *b) { |
|
491 | #if defined ___AsyncIO___ |
491 | #if defined ___AsyncIO___ |
|
492 | struct AsyncFile *ap; |
492 | struct AsyncFile *ap; |
|
493 | struct AsyncFile *bp; |
493 | struct AsyncFile *bp; |
|
494 | LONG c; |
494 | LONG c; |
|
495 | #else |
495 | #else |
|
496 | FILE *ap; |
496 | FILE *ap; |
|
497 | FILE *bp; |
497 | FILE *bp; |
|
498 | char c; |
498 | char c; |
|
499 | #endif |
499 | #endif |
|
500 | |
500 | |
|
501 | |
501 | |
|
502 | // Open database file for writing. |
502 | // Open database file for writing. |
|
503 | #if defined ___AsyncIO___ |
503 | #if defined ___AsyncIO___ |
|
504 | if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) { |
504 | if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) { |
|
505 | #else |
505 | #else |
|
506 | if((ap = fopen(a, "r")) == NULL) { |
506 | if((ap = fopen(a, "r")) == NULL) { |
|
507 | #endif |
507 | #endif |
|
508 | fprintf(stderr, "Unable to open '%s' for reading.\n", a); |
508 | fprintf(stderr, "Unable to open '%s' for reading.\n", a); |
|
509 | return; |
509 | return; |
|
510 | } |
510 | } |
|
511 | |
511 | |
|
512 | // Open temporary file for reading. |
512 | // Open temporary file for reading. |
|
513 | #if defined ___AsyncIO___ |
513 | #if defined ___AsyncIO___ |
|
514 | if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) { |
514 | if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
515 | #else |
515 | #else |
|
516 | if((bp = fopen(b, "w+")) == NULL) { |
516 | if((bp = fopen(b, "w+")) == NULL) { |
|
517 | #endif |
517 | #endif |
|
518 | fprintf(stderr, "Unable to open file '%s' for writing.\n", b); |
518 | fprintf(stderr, "Unable to open file '%s' for writing.\n", b); |
|
519 | |
519 | |
|
520 | // Close database file. |
520 | // Close database file. |
|
521 | #if defined ___AsyncIO___ |
521 | #if defined ___AsyncIO___ |
|
522 | CloseAsync(ap); |
522 | CloseAsync(ap); |
|
523 | #else |
523 | #else |
|
524 | fclose(ap); |
524 | fclose(ap); |
|
525 | #endif |
525 | #endif |
|
526 | |
526 | |
|
527 | return; |
527 | return; |
|
528 | } |
528 | } |
|
529 | |
529 | |
|
530 | #if defined ___AsyncIO___ |
530 | #if defined ___AsyncIO___ |
|
531 | while(run && (c = ReadCharAsync(ap)) != -1) { |
531 | while(run && (c = ReadCharAsync(ap)) != -1) { |
|
532 | #else |
532 | #else |
|
533 | while(run && fscanf(ap, "%c", &c) == 1) { |
533 | while(run && fscanf(ap, "%c", &c) == 1) { |
|
534 | #endif |
534 | #endif |
|
535 | #if defined ___AmigaOS___ |
535 | #if defined ___AmigaOS___ |
|
536 | // Check if CTRL+C was pressed and abort the program. |
536 | // Check if CTRL+C was pressed and abort the program. |
|
537 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
537 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
538 | run = FALSE; |
538 | run = FALSE; |
|
539 | continue; |
539 | continue; |
|
540 | } |
540 | } |
|
541 | #endif |
541 | #endif |
|
542 | #if defined ___AsyncIO___ |
542 | #if defined ___AsyncIO___ |
|
543 | if(WriteCharAsync(bp, (UBYTE)c) != 1) { |
543 | if(WriteCharAsync(bp, (UBYTE)c) != 1) { |
|
544 | #else |
544 | #else |
|
545 | if(fprintf(bp, "%c", c) != 1) { |
545 | if(fprintf(bp, "%c", c) != 1) { |
|
546 | #endif |
546 | #endif |
|
547 | fprintf(stderr, "Unable to write to '%s'.\n", b); |
547 | fprintf(stderr, "Unable to write to '%s'.\n", b); |
|
548 | break; |
548 | break; |
|
549 | } |
549 | } |
|
550 | } |
550 | } |
|
551 | |
551 | |
|
552 | #if defined ___AsyncIO___ |
552 | #if defined ___AsyncIO___ |
|
553 | CloseAsync(ap); |
553 | CloseAsync(ap); |
|
554 | CloseAsync(bp); |
554 | CloseAsync(bp); |
|
555 | #else |
555 | #else |
|
556 | fclose(ap); |
556 | fclose(ap); |
|
557 | fclose(bp); |
557 | fclose(bp); |
|
558 | #endif |
558 | #endif |
|
559 | } |
559 | } |
|
560 | |
560 | |
|
561 | /* |
561 | /* |
|
562 | * |
562 | * |
|
563 | * Write lines to a file. |
563 | * Write lines to a file. |
|
564 | */ |
564 | */ |
|
565 | void WriteLinesToFile(char *dbFile, char **lines, unsigned int count) { |
565 | void WriteLinesToFile(char *dbFile, char **lines, unsigned int count) { |
|
566 | #if defined ___AsyncIO___ |
566 | #if defined ___AsyncIO___ |
|
567 | struct AsyncFile *fp; |
567 | struct AsyncFile *fp; |
|
568 | #else |
568 | #else |
|
569 | FILE *fp; |
569 | FILE *fp; |
|
570 | #endif |
570 | #endif |
|
571 | int i; |
571 | int i; |
|
572 | char *rem; |
572 | char *rem; |
|
573 | |
573 | |
|
574 | // Write the database lines back to the database. |
574 | // Write the database lines back to the database. |
|
575 | #if defined ___AsyncIO___ |
575 | #if defined ___AsyncIO___ |
|
576 | if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) { |
576 | if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
577 | #else |
577 | #else |
|
578 | if((fp = fopen(dbFile, "w")) == NULL) { |
578 | if((fp = fopen(dbFile, "w")) == NULL) { |
|
579 | #endif |
579 | #endif |
|
580 | fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
580 | fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
|
581 | return; |
581 | return; |
|
582 | } |
582 | } |
|
583 | |
583 | |
|
584 | rem = NULL; |
584 | rem = NULL; |
|
585 | for(i = 0; i < count; ++i) { |
585 | for(i = 0; i < count; ++i) { |
|
586 | #if defined ___AmigaOS___ |
586 | #if defined ___AmigaOS___ |
|
587 | // Check if CTRL+C was pressed and abort the program. |
587 | // Check if CTRL+C was pressed and abort the program. |
|
588 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
588 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
589 | run = FALSE; |
589 | run = FALSE; |
|
590 | continue; |
590 | continue; |
|
591 | } |
591 | } |
|
592 | #endif |
592 | #endif |
|
593 | |
593 | |
|
594 | if(rem != NULL) { |
594 | if(rem != NULL) { |
|
595 | #if defined ___AmigaOS___ |
595 | #if defined ___AmigaOS___ |
|
596 | if(StrnCmp(locale, lines[i], rem, -1, SC_ASCII) == 0) { |
596 | if(StrnCmp(locale, lines[i], rem, -1, SC_ASCII) == 0) { |
|
597 | #else |
597 | #else |
|
598 | if(strcmp(lines[i], rem) == 0) { |
598 | if(strcmp(lines[i], rem) == 0) { |
|
599 | #endif |
599 | #endif |
|
600 | continue; |
600 | continue; |
|
601 | } |
601 | } |
|
602 | } |
602 | } |
|
603 | |
603 | |
|
604 | #if defined ___AsyncIO___ |
604 | #if defined ___AsyncIO___ |
|
605 | WriteAsync(fp, lines[i], (LONG)strlen(lines[i])); |
605 | WriteAsync(fp, lines[i], (LONG)strlen(lines[i])); |
|
606 | WriteAsync(fp, "\n", 1); |
606 | WriteAsync(fp, "\n", 1); |
|
607 | #else |
607 | #else |
|
608 | fprintf(fp, "%s\n", lines[i]); |
608 | fprintf(fp, "%s\n", lines[i]); |
|
609 | #endif |
609 | #endif |
|
610 | |
610 | |
|
611 | if(rem != NULL) { |
611 | if(rem != NULL) { |
|
612 | free(rem); |
612 | free(rem); |
|
613 | } |
613 | } |
|
614 | |
614 | |
|
615 | rem = malloc(strlen(lines[i]) + 1); |
615 | rem = malloc(strlen(lines[i]) + 1); |
|
616 | sprintf(rem, "%s", lines[i]); |
616 | sprintf(rem, "%s", lines[i]); |
|
617 | } |
617 | } |
|
618 | |
618 | |
|
619 | #if defined ___AsyncIO___ |
619 | #if defined ___AsyncIO___ |
|
620 | CloseAsync(fp); |
620 | CloseAsync(fp); |
|
621 | #else |
621 | #else |
|
622 | fclose(fp); |
622 | fclose(fp); |
|
623 | #endif |
623 | #endif |
|
624 | } |
624 | } |
|
625 | |
625 | |
|
626 | /* |
626 | /* |
|
627 | * |
627 | * |
|
628 | * Create a database entry from a line of text. |
628 | * Create a database entry from a line of text. |
|
629 | */ |
629 | */ |
|
630 | dbEntry* CreateDataseEntry(char *line) { |
630 | dbEntry* CreateDataseEntry(char *line) { |
|
631 | dbEntry *entry; |
631 | dbEntry *entry; |
|
632 | char *ptr; |
632 | char *ptr; |
|
633 | unsigned int side; |
633 | unsigned int side; |
|
634 | unsigned int i; |
634 | unsigned int i; |
|
635 | unsigned int j; |
635 | unsigned int j; |
|
636 | |
636 | |
|
637 | if((entry = malloc(1 * sizeof(*entry))) == NULL) { |
637 | if((entry = malloc(1 * sizeof(*entry))) == NULL) { |
|
638 | fprintf(stderr, "Memory allocation failure.\n"); |
638 | fprintf(stderr, "Memory allocation failure.\n"); |
|
639 | return NULL; |
639 | return NULL; |
|
640 | } |
640 | } |
|
641 | |
641 | |
|
642 | if((entry->name = malloc(strlen(line) * sizeof(*entry->name))) == NULL) { |
642 | if((entry->name = malloc(strlen(line) * sizeof(*entry->name))) == NULL) { |
|
643 | fprintf(stderr, "Memory allocation failure.\n"); |
643 | fprintf(stderr, "Memory allocation failure.\n"); |
|
644 | return NULL; |
644 | return NULL; |
|
645 | } |
645 | } |
|
646 | |
646 | |
|
647 | if((entry->path = malloc(strlen(line) * sizeof(*entry->path))) == NULL) { |
647 | if((entry->path = malloc(strlen(line) * sizeof(*entry->path))) == NULL) { |
|
648 | fprintf(stderr, "Memory allocation failure.\n"); |
648 | fprintf(stderr, "Memory allocation failure.\n"); |
|
649 | return NULL; |
649 | return NULL; |
|
650 | } |
650 | } |
|
651 | |
651 | |
|
652 | for(ptr = line, side = 0, i = 0, j = 0; run && *ptr != '\0'; ++ptr) { |
652 | for(ptr = line, side = 0, i = 0, j = 0; run && *ptr != '\0'; ++ptr) { |
|
653 | #if defined ___AmigaOS___ |
653 | #if defined ___AmigaOS___ |
|
654 | // Check if CTRL+C was pressed and abort the program. |
654 | // Check if CTRL+C was pressed and abort the program. |
|
655 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
655 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
656 | run = FALSE; |
656 | run = FALSE; |
|
657 | continue; |
657 | continue; |
|
658 | } |
658 | } |
|
659 | #endif |
659 | #endif |
|
660 | switch(*ptr) { |
660 | switch(*ptr) { |
|
661 | case '\t': |
661 | case '\t': |
|
662 | entry->name[i] = '\0'; |
662 | entry->name[i] = '\0'; |
|
663 | ++side; |
663 | ++side; |
|
664 | break; |
664 | break; |
|
665 | case '\n': |
665 | case '\n': |
|
666 | entry->path[j] = '\0'; |
666 | entry->path[j] = '\0'; |
|
667 | return entry; |
667 | return entry; |
|
668 | default: |
668 | default: |
|
669 | switch(side) { |
669 | switch(side) { |
|
670 | case 0: |
670 | case 0: |
|
671 | entry->name[i++] = *ptr; |
671 | entry->name[i++] = *ptr; |
|
672 | break; |
672 | break; |
|
673 | case 1: |
673 | case 1: |
|
674 | entry->path[j++] = *ptr; |
674 | entry->path[j++] = *ptr; |
|
675 | break; |
675 | break; |
|
676 | } |
676 | } |
|
677 | break; |
677 | break; |
|
678 | } |
678 | } |
|
679 | } |
679 | } |
|
680 | |
680 | |
|
681 | return entry; |
681 | return entry; |
|
682 | } |
682 | } |
|
683 | |
683 | |
|
684 | /* |
684 | /* |
|
685 | * |
685 | * |
|
686 | * |
686 | * |
|
687 | */ |
687 | */ |
|
688 | dbArray *GetDatabaseArray(char *dbFile) { |
688 | dbArray *GetDatabaseArray(char *dbFile) { |
|
689 | #if defined ___AsyncIO___ |
689 | #if defined ___AsyncIO___ |
|
690 | struct AsyncFile *fp; |
690 | struct AsyncFile *fp; |
|
691 | #else |
691 | #else |
|
692 | FILE *fp; |
692 | FILE *fp; |
|
693 | #endif |
693 | #endif |
|
694 | dbArray *array; |
694 | dbArray *array; |
|
695 | dbEntry *entry; |
695 | dbEntry *entry; |
|
696 | char *line; |
696 | char *line; |
|
697 | unsigned int count; |
697 | unsigned int count; |
|
698 | |
698 | |
|
699 | if((array = malloc(1 * sizeof(*array))) == NULL) { |
699 | if((array = malloc(1 * sizeof(*array))) == NULL) { |
|
700 | fprintf(stderr, "Memory allocation failure.\n"); |
700 | fprintf(stderr, "Memory allocation failure.\n"); |
|
701 | return NULL; |
701 | return NULL; |
|
702 | } |
702 | } |
|
703 | |
703 | |
|
704 | // Open database file for reading. |
704 | // Open database file for reading. |
|
705 | #if defined ___AsyncIO___ |
705 | #if defined ___AsyncIO___ |
|
706 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
706 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
707 | #else |
707 | #else |
|
708 | if((fp = fopen(dbFile, "r")) == NULL) { |
708 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
709 | #endif |
709 | #endif |
|
710 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
710 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
|
711 | return NULL; |
711 | return NULL; |
|
712 | } |
712 | } |
|
713 | |
713 | |
|
714 | count = 0; |
714 | count = 0; |
|
715 | while(run && (line = ReadLine(fp)) != NULL) { |
715 | while(run && (line = ReadLine(fp)) != NULL) { |
|
716 | #if defined ___AmigaOS___ |
716 | #if defined ___AmigaOS___ |
|
717 | // Check if CTRL+C was pressed and abort the program. |
717 | // Check if CTRL+C was pressed and abort the program. |
|
718 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
718 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
719 | run = FALSE; |
719 | run = FALSE; |
|
720 | continue; |
720 | continue; |
|
721 | } |
721 | } |
|
722 | #endif |
722 | #endif |
|
723 | if((entry = CreateDataseEntry(line)) == NULL) { |
723 | if((entry = CreateDataseEntry(line)) == NULL) { |
|
724 | fprintf(stderr, "Unable to create database entry.\n"); |
724 | fprintf(stderr, "Unable to create database entry.\n"); |
|
725 | #if defined ___AsyncIO___ |
725 | #if defined ___AsyncIO___ |
|
726 | CloseAsync(fp); |
726 | CloseAsync(fp); |
|
727 | #else |
727 | #else |
|
728 | fclose(fp); |
728 | fclose(fp); |
|
729 | #endif |
729 | #endif |
|
730 | return NULL; |
730 | return NULL; |
|
731 | } |
731 | } |
|
732 | |
732 | |
|
733 | // Load up the name and path into the database variable. |
733 | // Load up the name and path into the database variable. |
|
734 | array->database = realloc(array->database, (count + 1) * sizeof(*array->database)); |
734 | array->database = realloc(array->database, (count + 1) * sizeof(*array->database)); |
|
735 | if((array->database[count] = malloc((strlen(entry->name) + strlen(entry->path) + 1 + 1) * sizeof(*array->database[count]))) == NULL) { |
735 | if((array->database[count] = malloc((strlen(entry->name) + strlen(entry->path) + 1 + 1) * sizeof(*array->database[count]))) == NULL) { |
|
736 | fprintf(stderr, "Memory allocation failure.\n"); |
736 | fprintf(stderr, "Memory allocation failure.\n"); |
|
737 | free(entry); |
737 | free(entry); |
|
738 | free(line); |
738 | free(line); |
|
739 | #if defined ___AsyncIO___ |
739 | #if defined ___AsyncIO___ |
|
740 | CloseAsync(fp); |
740 | CloseAsync(fp); |
|
741 | #else |
741 | #else |
|
742 | fclose(fp); |
742 | fclose(fp); |
|
743 | #endif |
743 | #endif |
|
744 | return NULL; |
744 | return NULL; |
|
745 | } |
745 | } |
|
746 | sprintf(array->database[count], "%s\t%s", entry->name, entry->path); |
746 | sprintf(array->database[count], "%s\t%s", entry->name, entry->path); |
|
747 | ++count; |
747 | ++count; |
|
748 | |
748 | |
|
749 | free(entry); |
749 | free(entry); |
|
750 | free(line); |
750 | free(line); |
|
751 | } |
751 | } |
|
752 | |
752 | |
|
753 | #if defined ___AsyncIO___ |
753 | #if defined ___AsyncIO___ |
|
754 | CloseAsync(fp); |
754 | CloseAsync(fp); |
|
755 | #else |
755 | #else |
|
756 | fclose(fp); |
756 | fclose(fp); |
|
757 | #endif |
757 | #endif |
|
758 | |
758 | |
|
759 | array->count = count; |
759 | array->count = count; |
|
760 | return array; |
760 | return array; |
|
761 | } |
761 | } |
|
762 | |
762 | |
|
763 | /* |
763 | /* |
|
764 | * |
764 | * |
|
765 | * Sorts a database file lexicographically. |
765 | * Sorts a database file lexicographically. |
|
766 | */ |
766 | */ |
|
767 | void SortDatabase(char *dbFile) { |
767 | void SortDatabase(char *dbFile) { |
|
768 | dbArray *array; |
768 | dbArray *array; |
|
769 | |
769 | |
|
770 | if(verbose) { |
770 | if(verbose) { |
|
771 | fprintf(stdout, "Sorting '%s'...\n", dbFile); |
771 | fprintf(stdout, "Sorting '%s'...\n", dbFile); |
|
772 | } |
772 | } |
|
773 | |
773 | |
|
774 | // Retrieve the database as an array. |
774 | // Retrieve the database as an array. |
|
775 | if((array = GetDatabaseArray(dbFile)) == NULL) { |
775 | if((array = GetDatabaseArray(dbFile)) == NULL) { |
|
776 | fprintf(stderr, "Unable to read '%s' as a database file.\n", dbFile); |
776 | fprintf(stderr, "Unable to read '%s' as a database file.\n", dbFile); |
|
777 | return; |
777 | return; |
|
778 | } |
778 | } |
|
779 | |
779 | |
|
780 | // Sort the database. |
780 | // Sort the database. |
|
781 | qsort(array->database, array->count, sizeof(char *), QsortCompare); |
781 | qsort(array->database, array->count, sizeof(char *), QsortCompare); |
|
782 | |
782 | |
|
783 | // Write back the database to the database file. |
783 | // Write back the database to the database file. |
|
784 | WriteLinesToFile(dbFile, array->database, array->count); |
784 | WriteLinesToFile(dbFile, array->database, array->count); |
|
785 | |
785 | |
|
786 | free(array); |
786 | free(array); |
|
787 | } |
787 | } |
|
788 | |
788 | |
|
789 | /* |
789 | /* |
|
790 | * |
790 | * |
|
791 | * Updates a database file "dbFile". |
791 | * Updates a database file "dbFile". |
|
792 | */ |
792 | */ |
|
793 | stats *CollectFiles(char *dbFile, char **paths, unsigned int count) { |
793 | stats *CollectFiles(char *dbFile, char **paths, unsigned int count) { |
|
794 | #if defined ___AsyncIO___ |
794 | #if defined ___AsyncIO___ |
|
795 | struct AsyncFile *fp; |
795 | struct AsyncFile *fp; |
|
796 | #else |
796 | #else |
|
797 | FILE *fp; |
797 | FILE *fp; |
|
798 | #endif |
798 | #endif |
|
- | 799 | #if defined ___AmigaOS___ |
||
799 | stringStack *stack; |
800 | struct FileInfoBlock *FIBp, *FIBq; |
|
800 | stats *stats; |
801 | BPTR lockp, lockq; |
|
- | 802 | #else |
||
801 | DIR *dir; |
803 | DIR *dir; |
|
802 | struct dirent *entry; |
804 | struct dirent *entry; |
|
- | 805 | #endif |
||
803 | struct stat dirStat; |
806 | struct stat dirStat; |
|
- | 807 | |
||
- | 808 | stringStack *stack; |
||
- | 809 | stats *stats; |
||
804 | int i; |
810 | int i; |
|
805 | char *path; |
811 | char *path; |
|
806 | char *subPath; |
812 | char *sub; |
|
807 | |
813 | |
|
808 | // Initialize metrics. |
814 | // Initialize metrics. |
|
809 | if((stats = malloc(sizeof(stats))) == NULL) { |
815 | if((stats = malloc(sizeof(stats))) == NULL) { |
|
810 | fprintf(stderr, "Memory allocation failure.\n"); |
816 | fprintf(stderr, "Memory allocation failure.\n"); |
|
811 | return NULL; |
817 | return NULL; |
|
812 | } |
818 | } |
|
813 | |
819 | |
|
814 | stats->dirs = 0; |
820 | stats->dirs = 0; |
|
815 | stats->files = 0; |
821 | stats->files = 0; |
|
816 | |
822 | |
|
817 | #if defined ___AsyncIO___ |
823 | #if defined ___AsyncIO___ |
|
818 | if((fp = OpenAsync(dbFile, MODE_APPEND, ASYNC_BUF)) == NULL) { |
824 | if((fp = OpenAsync(dbFile, MODE_APPEND, ASYNC_BUF)) == NULL) { |
|
819 | #else |
825 | #else |
|
820 | if((fp = fopen(dbFile, "r+")) == NULL) { |
826 | if((fp = fopen(dbFile, "r+")) == NULL) { |
|
821 | #endif |
827 | #endif |
|
822 | fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
828 | fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
|
823 | return stats; |
829 | return stats; |
|
824 | } |
830 | } |
|
825 | |
831 | |
|
826 | // Seek to the end of the database. |
832 | // Seek to the end of the database. |
|
827 | #if defined ___AsyncIO___ |
833 | #if defined ___AsyncIO___ |
|
828 | if(SeekAsync(fp, 0, MODE_END) == -1) { |
834 | if(SeekAsync(fp, 0, MODE_END) == -1) { |
|
829 | #else |
835 | #else |
|
830 | if(fseek(fp, 0, SEEK_END) == 0) { |
836 | if(fseek(fp, 0, SEEK_END) == 0) { |
|
831 | #endif |
837 | #endif |
|
832 | fprintf(stderr, "Unable to seek in '%s' for appending.\n", dbFile); |
838 | fprintf(stderr, "Unable to seek in '%s' for appending.\n", dbFile); |
|
833 | #if defined ___AsyncIO___ |
839 | #if defined ___AsyncIO___ |
|
834 | CloseAsync(fp); |
840 | CloseAsync(fp); |
|
835 | #else |
841 | #else |
|
836 | fclose(fp); |
842 | fclose(fp); |
|
837 | #endif |
843 | #endif |
|
838 | return stats; |
844 | return stats; |
|
839 | } |
845 | } |
|
840 | |
846 | |
|
841 | if(verbose) { |
847 | if(verbose) { |
|
842 | fprintf(stdout, "Collecting files...\r"); |
848 | fprintf(stdout, "Collecting files...\r"); |
|
843 | } |
849 | } |
|
844 | |
850 | |
|
845 | // Push the first path onto the stack. |
851 | // Push the first path onto the stack. |
|
846 | stack = stringStackCreate(count); |
852 | stack = stringStackCreate(count); |
|
847 | for(i = 0; run && i < count; ++i) { |
853 | for(i = 0; run && i < count; ++i) { |
|
848 | stringStackPush(stack, paths[i]); |
854 | stringStackPush(stack, paths[i]); |
|
849 | } |
855 | } |
|
850 | |
856 | |
|
851 | while(run && !stringStackIsEmpty(stack)) { |
857 | while(run && !stringStackIsEmpty(stack)) { |
|
852 | #if defined ___AmigaOS___ |
858 | #if defined ___AmigaOS___ |
|
853 | // Check if CTRL+C was pressed and abort the program. |
859 | // Check if CTRL+C was pressed and abort the program. |
|
854 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
860 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
855 | run = FALSE; |
861 | run = FALSE; |
|
856 | continue; |
862 | continue; |
|
857 | } |
863 | } |
|
858 | #endif |
864 | #endif |
|
859 | if((path = stringStackPop(stack)) == NULL) { |
865 | if((path = stringStackPop(stack)) == NULL) { |
|
860 | break; |
866 | break; |
|
861 | } |
867 | } |
|
- | 868 | |
||
862 | |
869 | #if defined ___AmigaOS___ |
|
863 | if((dir = opendir(path)) == NULL) { |
870 | if((lockp = Lock(path, ACCESS_READ)) == NULL) { |
|
864 | fprintf(stderr, "Unable to open '%s' for reading.\n", path); |
871 | fprintf(stderr, "Could not lock path '%s' for reading.\n", path); |
|
- | 872 | free(path); |
||
865 | break; |
873 | continue; |
|
- | 874 | } |
||
- | 875 | |
||
- | 876 | if((FIBp = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL)) == NULL) { |
||
- | 877 | fprintf(stderr, "Path '%s' info block allocation failure.\n", path); |
||
- | 878 | UnLock(lockp); |
||
- | 879 | free(path); |
||
- | 880 | continue; |
||
866 | } |
881 | } |
|
- | 882 | |
||
867 | |
883 | if(Examine(lockp, FIBp) == FALSE) { |
|
- | 884 | fprintf(stderr, "Path '%s' could not be examined.\n", path); |
||
- | 885 | FreeDosObject(DOS_FIB, FIBp); |
||
- | 886 | UnLock(lockp); |
||
- | 887 | free(path); |
||
- | 888 | continue; |
||
- | 889 | } |
||
868 | while(run && (entry = readdir(dir)) != NULL) { |
890 | |
|
869 | #if defined ___AmigaOS___ |
891 | while(run && ExNext(lockp, FIBp)) { |
|
870 | // Check if CTRL+C was pressed and abort the program. |
892 | // Check if CTRL+C was pressed and abort the program. |
|
871 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
893 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
872 | run = FALSE; |
894 | run = FALSE; |
|
873 | continue; |
895 | continue; |
|
874 | } |
896 | } |
|
- | 897 | #else |
||
- | 898 | |
||
- | 899 | if((dir = opendir(path)) == NULL) { |
||
- | 900 | fprintf(stderr, "Unable to open '%s' for reading.\n", path); |
||
- | 901 | free(path); |
||
- | 902 | continue; |
||
- | 903 | } |
||
- | 904 | |
||
- | 905 | while(run && (entry = readdir(dir)) != NULL) { |
||
875 | #endif |
906 | #endif |
|
876 | switch(path[strlen(path) - 1]) { |
907 | switch(path[strlen(path) - 1]) { |
|
877 | case '/': |
908 | case '/': |
|
878 | case ':': // This is a drive path. |
909 | case ':': // This is a drive path. |
|
- | 910 | #if defined ___AmigaOS___ |
||
- | 911 | if((sub = malloc(sizeof(path) + sizeof(FIBp->fib_FileName) + 1)) == NULL) { |
||
- | 912 | #else |
||
879 | if((subPath = malloc(sizeof(path) + sizeof(entry->d_name) + 1)) == NULL) { |
913 | if((sub = malloc(sizeof(path) + sizeof(entry->d_name) + 1)) == NULL) { |
|
- | 914 | #endif |
||
880 | fprintf(stderr, "Memory allocation failure.\n"); |
915 | fprintf(stderr, "Memory allocation failure.\n"); |
|
- | 916 | #if defined ___AmigaOS___ |
||
- | 917 | FreeDosObject(DOS_FIB, FIBp); |
||
- | 918 | UnLock(lockp); |
||
- | 919 | #else |
||
881 | closedir(dir); |
920 | closedir(dir); |
|
- | 921 | #endif |
||
882 | free(path); |
922 | free(path); |
|
883 | stringStackDestroy(stack); |
923 | stringStackDestroy(stack); |
|
884 | #if defined ___AsyncIO___ |
924 | #if defined ___AsyncIO___ |
|
885 | CloseAsync(fp); |
925 | CloseAsync(fp); |
|
886 | #else |
926 | #else |
|
887 | fclose(fp); |
927 | fclose(fp); |
|
888 | #endif |
928 | #endif |
|
889 | return NULL; |
929 | return NULL; |
|
890 | } |
930 | } |
|
- | 931 | #if defined ___AmigaOS___ |
||
- | 932 | sprintf(sub, "%s%s", path, FIBp->fib_FileName); |
||
- | 933 | #else |
||
891 | sprintf(subPath, "%s%s", path, entry->d_name); |
934 | sprintf(sub, "%s%s", path, entry->d_name); |
|
- | 935 | #endif |
||
892 | break; |
936 | break; |
|
893 | default: |
937 | default: |
|
- | 938 | #if defined ___AmigaOS___ |
||
- | 939 | if((sub = malloc(sizeof(path) + sizeof(FIBp->fib_FileName) + 1 + 1)) == NULL) { |
||
- | 940 | #else |
||
894 | if((subPath = malloc(sizeof(path) + sizeof(entry->d_name) + 1 + 1)) == NULL) { |
941 | if((sub = malloc(sizeof(path) + sizeof(entry->d_name) + 1 + 1)) == NULL) { |
|
- | 942 | #endif |
||
895 | fprintf(stderr, "Memory allocation failure.\n"); |
943 | fprintf(stderr, "Memory allocation failure.\n"); |
|
- | 944 | #if defined ___AmigaOS___ |
||
- | 945 | FreeDosObject(DOS_FIB, FIBp); |
||
- | 946 | UnLock(lockp); |
||
- | 947 | #else |
||
896 | closedir(dir); |
948 | closedir(dir); |
|
- | 949 | #endif |
||
897 | free(path); |
950 | free(path); |
|
898 | stringStackDestroy(stack); |
951 | stringStackDestroy(stack); |
|
899 | #if defined ___AsyncIO___ |
952 | #if defined ___AsyncIO___ |
|
900 | CloseAsync(fp); |
953 | CloseAsync(fp); |
|
901 | #else |
954 | #else |
|
902 | fclose(fp); |
955 | fclose(fp); |
|
903 | #endif |
956 | #endif |
|
904 | return NULL; |
957 | return NULL; |
|
905 | } |
958 | } |
|
- | 959 | #if defined ___AmigaOS___ |
||
- | 960 | sprintf(sub, "%s/%s", path, FIBp->fib_FileName); |
||
- | 961 | #else |
||
906 | sprintf(subPath, "%s/%s", path, entry->d_name); |
962 | sprintf(sub, "%s/%s", path, entry->d_name); |
|
- | 963 | #endif |
||
907 | break; |
964 | break; |
|
908 | } |
965 | } |
|
- | 966 | |
||
- | 967 | #if defined ___AmigaOS___ |
||
- | 968 | if((lockq = Lock(sub, ACCESS_READ)) == NULL) { |
||
- | 969 | fprintf(stderr, "Could not lock path '%s' for reading.\n", sub); |
||
- | 970 | free(sub); |
||
- | 971 | continue; |
||
- | 972 | } |
||
- | 973 | |
||
- | 974 | if((FIBq = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL)) == NULL) { |
||
- | 975 | fprintf(stderr, "Path '%s' info block allocation failure.\n", sub); |
||
- | 976 | UnLock(lockq); |
||
- | 977 | free(sub); |
||
- | 978 | continue; |
||
- | 979 | } |
||
- | 980 | |
||
- | 981 | if(Examine(lockq, FIBq) == FALSE) { |
||
- | 982 | fprintf(stderr, "Path '%s' could not be examined.\n", sub); |
||
- | 983 | FreeDosObject(DOS_FIB, FIBq); |
||
- | 984 | UnLock(lockq); |
||
- | 985 | free(sub); |
||
- | 986 | continue; |
||
- | 987 | } |
||
- | 988 | |
||
- | 989 | if(FIBq->fib_DirEntryType > 0) { |
||
- | 990 | #else |
||
909 | stat(subPath, &dirStat); |
991 | stat(sub, &dirStat); |
|
910 | if(S_ISDIR(dirStat.st_mode)) { |
992 | if(S_ISDIR(dirStat.st_mode)) { |
|
- | 993 | #endif |
||
911 | stringStackPush(stack, subPath); |
994 | stringStackPush(stack, sub); |
|
912 | |
995 | |
|
913 | ++stats->dirs; |
996 | ++stats->dirs; |
|
914 | |
997 | |
|
915 | if(verbose) { |
998 | if(verbose) { |
|
916 | fprintf(stdout, |
999 | fprintf(stdout, |
|
917 | "Gathered %d directories and %d files.\r", |
1000 | "Gathered %d directories and %d files.\r", |
|
918 | stats->dirs, |
1001 | stats->dirs, |
|
919 | stats->files); |
1002 | stats->files); |
|
920 | } |
1003 | } |
|
- | 1004 | |
||
- | 1005 | #if defined ___AmigaOS___ |
||
- | 1006 | FreeDosObject(DOS_FIB, FIBq); |
||
- | 1007 | UnLock(lockq); |
||
921 | |
1008 | #endif |
|
922 | free(subPath); |
1009 | free(sub); |
|
923 | continue; |
1010 | continue; |
|
924 | } |
1011 | } |
|
- | 1012 | |
||
- | 1013 | #if defined ___AmigaOS___ |
||
- | 1014 | FreeDosObject(DOS_FIB, FIBq); |
||
- | 1015 | UnLock(lockq); |
||
- | 1016 | #endif |
||
925 | |
1017 | |
|
- | 1018 | #if defined ___NOCASE_FS___ |
||
- | 1019 | #if defined ___AmigaOS___ |
||
- | 1020 | strupr(FIBp->fib_FileName); |
||
926 | #if defined ___NOCASE_FS___ |
1021 | #else |
|
927 | strupr(entry->d_name); |
1022 | strupr(entry->d_name); |
|
- | 1023 | #endif |
||
928 | #endif |
1024 | #endif |
|
929 | // Write to database file. |
1025 | // Write to database file. |
|
- | 1026 | #if defined ___AsyncIO___ |
||
- | 1027 | #if defined ___AmigaOS___ |
||
- | 1028 | WriteAsync(fp, FIBp->fib_FileName, (LONG)strlen(FIBp->fib_FileName)); |
||
930 | #if defined ___AsyncIO___ |
1029 | #else |
|
- | 1030 | WriteAsync(fp, entry->d_name, (LONG)strlen(entry->d_name)); |
||
931 | WriteAsync(fp, entry->d_name, (LONG)strlen(entry->d_name)); |
1031 | #endif |
|
932 | WriteAsync(fp, "\t", 1); |
1032 | WriteAsync(fp, "\t", 1); |
|
933 | WriteAsync(fp, subPath, (LONG)strlen(subPath)); |
1033 | WriteAsync(fp, sub, (LONG)strlen(sub)); |
|
934 | WriteAsync(fp, "\n", 1); |
1034 | WriteAsync(fp, "\n", 1); |
|
- | 1035 | #else |
||
- | 1036 | #if defined ___AmigaOS___ |
||
- | 1037 | fprintf(fp, "%s\t%s\n", FIBp->fib_FileName, sub); |
||
935 | #else |
1038 | #else |
|
- | 1039 | fprintf(fp, "%s\t%s\n", entry->d_name, sub); |
||
936 | fprintf(fp, "%s\t%s\n", entry->d_name, subPath); |
1040 | #endif |
|
937 | #endif |
1041 | #endif |
|
938 | ++stats->files; |
1042 | ++stats->files; |
|
939 | |
1043 | |
|
940 | if(verbose) { |
1044 | if(verbose) { |
|
941 | fprintf(stdout, |
1045 | fprintf(stdout, |
|
942 | "Gathered %d directories and %d files.\r", |
1046 | "Gathered %d directories and %d files.\r", |
|
943 | stats->dirs, |
1047 | stats->dirs, |
|
944 | stats->files); |
1048 | stats->files); |
|
945 | } |
1049 | } |
|
946 | |
1050 | |
|
947 | free(subPath); |
1051 | free(sub); |
|
- | 1052 | } |
||
- | 1053 | |
||
- | 1054 | #if defined ___AmigaOS___ |
||
- | 1055 | FreeDosObject(DOS_FIB, FIBp); |
||
948 | } |
1056 | UnLock(lockp); |
|
- | 1057 | #else |
||
949 | |
1058 | closedir(dir); |
|
950 | closedir(dir); |
1059 | #endif |
|
951 | free(path); |
1060 | free(path); |
|
952 | } |
1061 | } |
|
953 | |
1062 | |
|
954 | if(verbose) { |
1063 | if(verbose) { |
|
955 | fprintf(stdout, "\n"); |
1064 | fprintf(stdout, "\n"); |
|
956 | } |
1065 | } |
|
957 | |
1066 | |
|
958 | stringStackDestroy(stack); |
1067 | stringStackDestroy(stack); |
|
959 | |
1068 | |
|
960 | #if defined ___AsyncIO___ |
1069 | #if defined ___AsyncIO___ |
|
961 | CloseAsync(fp); |
1070 | CloseAsync(fp); |
|
962 | #else |
1071 | #else |
|
963 | fclose(fp); |
1072 | fclose(fp); |
|
964 | #endif |
1073 | #endif |
|
965 | |
1074 | |
|
966 | return stats; |
1075 | return stats; |
|
967 | |
1076 | |
|
968 | } |
1077 | } |
|
969 | |
1078 | |
|
970 | /* |
1079 | /* |
|
971 | * |
1080 | * |
|
972 | * Writes lines from the database "dbFile" to temporary filenames "tmpNames". |
1081 | * Writes lines from the database "dbFile" to temporary filenames "tmpNames". |
|
973 | */ |
1082 | */ |
|
974 | void WriteTemporaryFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { |
1083 | void WriteTemporaryFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { |
|
975 | #if defined ___AsyncIO___ |
1084 | #if defined ___AsyncIO___ |
|
976 | struct AsyncFile *fp, *tp; |
1085 | struct AsyncFile *fp, *tp; |
|
977 | LONG c; |
1086 | LONG c; |
|
978 | #else |
1087 | #else |
|
979 | FILE *fp, *tp; |
1088 | FILE *fp, *tp; |
|
980 | char c; |
1089 | char c; |
|
981 | #endif |
1090 | #endif |
|
982 | int lines; |
1091 | int lines; |
|
983 | int write; |
1092 | int write; |
|
984 | |
1093 | |
|
985 | #if defined ___AsyncIO___ |
1094 | #if defined ___AsyncIO___ |
|
986 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
1095 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
987 | #else |
1096 | #else |
|
988 | if((fp = fopen(dbFile, "r")) == NULL) { |
1097 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
989 | #endif |
1098 | #endif |
|
990 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
1099 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
|
991 | return; |
1100 | return; |
|
992 | } |
1101 | } |
|
993 | |
1102 | |
|
994 | #if defined ___AsyncIO___ |
1103 | #if defined ___AsyncIO___ |
|
995 | if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) { |
1104 | if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
996 | #else |
1105 | #else |
|
997 | if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) { |
1106 | if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) { |
|
998 | #endif |
1107 | #endif |
|
999 | fprintf(stderr, "Unable to open '%s' for writing.\n", tmpNames[tmpFiles]); |
1108 | fprintf(stderr, "Unable to open '%s' for writing.\n", tmpNames[tmpFiles]); |
|
1000 | #if defined ___AsyncIO___ |
1109 | #if defined ___AsyncIO___ |
|
1001 | CloseAsync(fp); |
1110 | CloseAsync(fp); |
|
1002 | #else |
1111 | #else |
|
1003 | fclose(fp); |
1112 | fclose(fp); |
|
1004 | #endif |
1113 | #endif |
|
1005 | return; |
1114 | return; |
|
1006 | } |
1115 | } |
|
1007 | |
1116 | |
|
1008 | if(verbose) { |
1117 | if(verbose) { |
|
1009 | fprintf(stdout, "Writing to temporary files...\r"); |
1118 | fprintf(stdout, "Writing to temporary files...\r"); |
|
1010 | } |
1119 | } |
|
1011 | |
1120 | |
|
1012 | write = 0; |
1121 | write = 0; |
|
1013 | lines = 0; |
1122 | lines = 0; |
|
1014 | #if defined ___AsyncIO___ |
1123 | #if defined ___AsyncIO___ |
|
1015 | while(run && (c = ReadCharAsync(fp)) != -1) { |
1124 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
1016 | #else |
1125 | #else |
|
1017 | while(run && fscanf(fp, "%c", &c) == 1) { |
1126 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
1018 | #endif |
1127 | #endif |
|
1019 | #if defined ___AmigaOS___ |
1128 | #if defined ___AmigaOS___ |
|
1020 | // Check if CTRL+C was pressed and abort the program. |
1129 | // Check if CTRL+C was pressed and abort the program. |
|
1021 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
1130 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
1022 | run = FALSE; |
1131 | run = FALSE; |
|
1023 | continue; |
1132 | continue; |
|
1024 | } |
1133 | } |
|
1025 | #endif |
1134 | #endif |
|
1026 | switch(c) { |
1135 | switch(c) { |
|
1027 | case '\n': |
1136 | case '\n': |
|
1028 | // Increment the total written lines. |
1137 | // Increment the total written lines. |
|
1029 | ++write; |
1138 | ++write; |
|
1030 | |
1139 | |
|
1031 | if(verbose) { |
1140 | if(verbose) { |
|
1032 | fprintf(stdout, "Writing to temporary files: %d%%.\r", (int)(((float)write / total) * 100.0)); |
1141 | fprintf(stdout, "Writing to temporary files: %d%%.\r", (int)(((float)write / total) * 100.0)); |
|
1033 | } |
1142 | } |
|
1034 | |
1143 | |
|
1035 | // Write the newline character back. |
1144 | // Write the newline character back. |
|
1036 | #if defined ___AsyncIO___ |
1145 | #if defined ___AsyncIO___ |
|
1037 | if(WriteCharAsync(tp, (UBYTE)c) != 1) { |
1146 | if(WriteCharAsync(tp, (UBYTE)c) != 1) { |
|
1038 | #else |
1147 | #else |
|
1039 | if(fprintf(tp, "%c", c) != 1) { |
1148 | if(fprintf(tp, "%c", c) != 1) { |
|
1040 | #endif |
1149 | #endif |
|
1041 | fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]); |
1150 | fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]); |
|
1042 | #if defined ___AsyncIO___ |
1151 | #if defined ___AsyncIO___ |
|
1043 | CloseAsync(tp); |
1152 | CloseAsync(tp); |
|
1044 | CloseAsync(fp); |
1153 | CloseAsync(fp); |
|
1045 | #else |
1154 | #else |
|
1046 | fclose(tp); |
1155 | fclose(tp); |
|
1047 | fclose(fp); |
1156 | fclose(fp); |
|
1048 | #endif |
1157 | #endif |
|
1049 | return; |
1158 | return; |
|
1050 | } |
1159 | } |
|
1051 | // Switch to the next temporary file. |
1160 | // Switch to the next temporary file. |
|
1052 | if(++lines >= tmpLines) { |
1161 | if(++lines >= tmpLines) { |
|
1053 | // If there are no temporary files left then run till the end. |
1162 | // If there are no temporary files left then run till the end. |
|
1054 | if(tmpFiles - 1 < 0) { |
1163 | if(tmpFiles - 1 < 0) { |
|
1055 | break; |
1164 | break; |
|
1056 | } |
1165 | } |
|
1057 | |
1166 | |
|
1058 | // Close the previous temporary file and write to the next temporary file. |
1167 | // Close the previous temporary file and write to the next temporary file. |
|
1059 | #if defined ___AsyncIO___ |
1168 | #if defined ___AsyncIO___ |
|
1060 | CloseAsync(tp); |
1169 | CloseAsync(tp); |
|
1061 | if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) { |
1170 | if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
1062 | #else |
1171 | #else |
|
1063 | fclose(tp); |
1172 | fclose(tp); |
|
1064 | if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) { |
1173 | if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) { |
|
1065 | #endif |
1174 | #endif |
|
1066 | fprintf(stderr, "Unable to open '%s' for writing.\n", tmpNames[tmpFiles]); |
1175 | fprintf(stderr, "Unable to open '%s' for writing.\n", tmpNames[tmpFiles]); |
|
1067 | #if defined ___AsyncIO___ |
1176 | #if defined ___AsyncIO___ |
|
1068 | CloseAsync(fp); |
1177 | CloseAsync(fp); |
|
1069 | #else |
1178 | #else |
|
1070 | fclose(fp); |
1179 | fclose(fp); |
|
1071 | #endif |
1180 | #endif |
|
1072 | return; |
1181 | return; |
|
1073 | } |
1182 | } |
|
1074 | lines = 0; |
1183 | lines = 0; |
|
1075 | break; |
1184 | break; |
|
1076 | } |
1185 | } |
|
1077 | break; |
1186 | break; |
|
1078 | default: |
1187 | default: |
|
1079 | #if defined ___AsyncIO___ |
1188 | #if defined ___AsyncIO___ |
|
1080 | if(WriteCharAsync(tp, (UBYTE)c) != 1) { |
1189 | if(WriteCharAsync(tp, (UBYTE)c) != 1) { |
|
1081 | #else |
1190 | #else |
|
1082 | if(fprintf(tp, "%c", c) != 1) { |
1191 | if(fprintf(tp, "%c", c) != 1) { |
|
1083 | #endif |
1192 | #endif |
|
1084 | fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]); |
1193 | fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]); |
|
1085 | #if defined ___AsyncIO___ |
1194 | #if defined ___AsyncIO___ |
|
1086 | CloseAsync(tp); |
1195 | CloseAsync(tp); |
|
1087 | CloseAsync(fp); |
1196 | CloseAsync(fp); |
|
1088 | #else |
1197 | #else |
|
1089 | fclose(tp); |
1198 | fclose(tp); |
|
1090 | fclose(fp); |
1199 | fclose(fp); |
|
1091 | #endif |
1200 | #endif |
|
1092 | return; |
1201 | return; |
|
1093 | } |
1202 | } |
|
1094 | break; |
1203 | break; |
|
1095 | } |
1204 | } |
|
1096 | } |
1205 | } |
|
1097 | |
1206 | |
|
1098 | if(verbose) { |
1207 | if(verbose) { |
|
1099 | fprintf(stdout, "\n"); |
1208 | fprintf(stdout, "\n"); |
|
1100 | } |
1209 | } |
|
1101 | |
1210 | |
|
1102 | #if defined ___AsyncIO___ |
1211 | #if defined ___AsyncIO___ |
|
1103 | CloseAsync(tp); |
1212 | CloseAsync(tp); |
|
1104 | CloseAsync(fp); |
1213 | CloseAsync(fp); |
|
1105 | #else |
1214 | #else |
|
1106 | fclose(tp); |
1215 | fclose(tp); |
|
1107 | fclose(fp); |
1216 | fclose(fp); |
|
1108 | #endif |
1217 | #endif |
|
1109 | } |
1218 | } |
|
1110 | |
1219 | |
|
1111 | /* |
1220 | /* |
|
1112 | * |
1221 | * |
|
1113 | * Merges temporary files "tmpNames" into a database "dbFile". |
1222 | * Merges temporary files "tmpNames" into a database "dbFile". |
|
1114 | */ |
1223 | */ |
|
1115 | void MergeTemporaryFiles(char *dbFile, char **tmpNames, int files, int lines) { |
1224 | void MergeTemporaryFiles(char *dbFile, char **tmpNames, int files, int lines) { |
|
1116 | #if defined ___AsyncIO___ |
1225 | #if defined ___AsyncIO___ |
|
1117 | struct AsyncFile *fp; |
1226 | struct AsyncFile *fp; |
|
1118 | struct AsyncFile **tp; |
1227 | struct AsyncFile **tp; |
|
1119 | #else |
1228 | #else |
|
1120 | FILE *fp; |
1229 | FILE *fp; |
|
1121 | FILE **tp; |
1230 | FILE **tp; |
|
1122 | #endif |
1231 | #endif |
|
1123 | int i; |
1232 | int i; |
|
1124 | int j; |
1233 | int j; |
|
1125 | char *tmp; |
1234 | char *tmp; |
|
1126 | char *rem; |
1235 | char *rem; |
|
1127 | char *min; |
1236 | char *min; |
|
1128 | int count; |
1237 | int count; |
|
1129 | |
1238 | |
|
1130 | #if defined ___AsyncIO___ |
1239 | #if defined ___AsyncIO___ |
|
1131 | if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) { |
1240 | if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
1132 | #else |
1241 | #else |
|
1133 | if((fp = fopen(dbFile, "w")) == NULL) { |
1242 | if((fp = fopen(dbFile, "w")) == NULL) { |
|
1134 | #endif |
1243 | #endif |
|
1135 | fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
1244 | fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
|
1136 | return; |
1245 | return; |
|
1137 | } |
1246 | } |
|
1138 | |
1247 | |
|
1139 | // Allocate as many file pointers as temporary files. |
1248 | // Allocate as many file pointers as temporary files. |
|
1140 | if((tp = malloc(files * sizeof(*tp))) == NULL) { |
1249 | if((tp = malloc(files * sizeof(*tp))) == NULL) { |
|
1141 | fprintf(stderr, "Memory allocation failure.\n"); |
1250 | fprintf(stderr, "Memory allocation failure.\n"); |
|
1142 | #if defined ___AsyncIO___ |
1251 | #if defined ___AsyncIO___ |
|
1143 | CloseAsync(fp); |
1252 | CloseAsync(fp); |
|
1144 | #else |
1253 | #else |
|
1145 | fclose(fp); |
1254 | fclose(fp); |
|
1146 | #endif |
1255 | #endif |
|
1147 | return; |
1256 | return; |
|
1148 | } |
1257 | } |
|
1149 | |
1258 | |
|
1150 | // Open all temporary files for reading. |
1259 | // Open all temporary files for reading. |
|
1151 | for(i = 0; i < files; ++i) { |
1260 | for(i = 0; i < files; ++i) { |
|
1152 | #if defined ___AsyncIO___ |
1261 | #if defined ___AsyncIO___ |
|
1153 | if((tp[i] = OpenAsync(tmpNames[i], MODE_READ, ASYNC_BUF)) == NULL) { |
1262 | if((tp[i] = OpenAsync(tmpNames[i], MODE_READ, ASYNC_BUF)) == NULL) { |
|
1154 | #else |
1263 | #else |
|
1155 | if((tp[i] = fopen(tmpNames[i], "r")) == NULL) { |
1264 | if((tp[i] = fopen(tmpNames[i], "r")) == NULL) { |
|
1156 | #endif |
1265 | #endif |
|
1157 | fprintf(stderr, "Unable to open '%s' for reading.\n", tmpNames[i]); |
1266 | fprintf(stderr, "Unable to open '%s' for reading.\n", tmpNames[i]); |
|
1158 | // Close all temporary files. |
1267 | // Close all temporary files. |
|
1159 | while(--i > -1) { |
1268 | while(--i > -1) { |
|
1160 | #if defined ___AsyncIO___ |
1269 | #if defined ___AsyncIO___ |
|
1161 | CloseAsync(tp[i]); |
1270 | CloseAsync(tp[i]); |
|
1162 | #else |
1271 | #else |
|
1163 | fclose(tp[i]); |
1272 | fclose(tp[i]); |
|
1164 | #endif |
1273 | #endif |
|
1165 | } |
1274 | } |
|
1166 | #if defined ___AsyncIO___ |
1275 | #if defined ___AsyncIO___ |
|
1167 | CloseAsync(fp); |
1276 | CloseAsync(fp); |
|
1168 | #else |
1277 | #else |
|
1169 | fclose(fp); |
1278 | fclose(fp); |
|
1170 | #endif |
1279 | #endif |
|
1171 | return; |
1280 | return; |
|
1172 | } |
1281 | } |
|
1173 | } |
1282 | } |
|
1174 | |
1283 | |
|
1175 | if(verbose) { |
1284 | if(verbose) { |
|
1176 | fprintf(stdout, "Merging all files...\r"); |
1285 | fprintf(stdout, "Merging all files...\r"); |
|
1177 | } |
1286 | } |
|
1178 | |
1287 | |
|
1179 | rem = NULL; |
1288 | rem = NULL; |
|
1180 | count = lines; |
1289 | count = lines; |
|
1181 | j = 0; |
1290 | j = 0; |
|
1182 | while(run && --count > -1) { |
1291 | while(run && --count > -1) { |
|
1183 | #if defined ___AmigaOS___ |
1292 | #if defined ___AmigaOS___ |
|
1184 | // Check if CTRL+C was pressed and abort the program. |
1293 | // Check if CTRL+C was pressed and abort the program. |
|
1185 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
1294 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
1186 | run = FALSE; |
1295 | run = FALSE; |
|
1187 | continue; |
1296 | continue; |
|
1188 | } |
1297 | } |
|
1189 | #endif |
1298 | #endif |
|
1190 | // Find the smallest line in all temporary files. |
1299 | // Find the smallest line in all temporary files. |
|
1191 | if(verbose) { |
1300 | if(verbose) { |
|
1192 | fprintf(stdout, "Merging all files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0)); |
1301 | fprintf(stdout, "Merging all files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0)); |
|
1193 | } |
1302 | } |
|
1194 | |
1303 | |
|
1195 | min = NULL; |
1304 | min = NULL; |
|
1196 | for(i = 0; i < files; ++i) { |
1305 | for(i = 0; i < files; ++i) { |
|
1197 | tmp = PeekLine(tp[i]); |
1306 | tmp = PeekLine(tp[i]); |
|
1198 | if(tmp == NULL) { |
1307 | if(tmp == NULL) { |
|
1199 | continue; |
1308 | continue; |
|
1200 | } |
1309 | } |
|
1201 | #if defined ___AmigaOS___ |
1310 | #if defined ___AmigaOS___ |
|
1202 | if(min == NULL || StrnCmp(locale, tmp, min, -1, SC_ASCII) < 0) { |
1311 | if(min == NULL || StrnCmp(locale, tmp, min, -1, SC_ASCII) < 0) { |
|
1203 | #else |
1312 | #else |
|
1204 | if(min == NULL || strcmp(tmp, min) < 0) { |
1313 | if(min == NULL || strcmp(tmp, min) < 0) { |
|
1205 | #endif |
1314 | #endif |
|
1206 | if(min != NULL) { |
1315 | if(min != NULL) { |
|
1207 | // Free previous instance. |
1316 | // Free previous instance. |
|
1208 | free(min); |
1317 | free(min); |
|
1209 | } |
1318 | } |
|
1210 | if((min = malloc((strlen(tmp) + 1) * sizeof(*min))) == NULL) { |
1319 | if((min = malloc((strlen(tmp) + 1) * sizeof(*min))) == NULL) { |
|
1211 | fprintf(stderr, "Memory allication failure.\n"); |
1320 | fprintf(stderr, "Memory allication failure.\n"); |
|
1212 | free(tmp); |
1321 | free(tmp); |
|
1213 | if(min != NULL) { |
1322 | if(min != NULL) { |
|
1214 | free(min); |
1323 | free(min); |
|
1215 | } |
1324 | } |
|
1216 | if(rem != NULL) { |
1325 | if(rem != NULL) { |
|
1217 | free(rem); |
1326 | free(rem); |
|
1218 | } |
1327 | } |
|
1219 | #if defined ___AsyncIO___ |
1328 | #if defined ___AsyncIO___ |
|
1220 | CloseAsync(fp); |
1329 | CloseAsync(fp); |
|
1221 | #else |
1330 | #else |
|
1222 | fclose(fp); |
1331 | fclose(fp); |
|
1223 | #endif |
1332 | #endif |
|
1224 | return; |
1333 | return; |
|
1225 | } |
1334 | } |
|
1226 | sprintf(min, "%s", tmp); |
1335 | sprintf(min, "%s", tmp); |
|
1227 | // Remember the index of the file where the smallest entry has been found. |
1336 | // Remember the index of the file where the smallest entry has been found. |
|
1228 | j = i; |
1337 | j = i; |
|
1229 | } |
1338 | } |
|
1230 | free(tmp); |
1339 | free(tmp); |
|
1231 | } |
1340 | } |
|
1232 | |
1341 | |
|
1233 | // Forward the file where the smallest line was found. |
1342 | // Forward the file where the smallest line was found. |
|
1234 | SkipLine(tp[j]); |
1343 | SkipLine(tp[j]); |
|
1235 | |
1344 | |
|
1236 | // Write the smallest line. |
1345 | // Write the smallest line. |
|
1237 | if(min != NULL) { |
1346 | if(min != NULL) { |
|
1238 | // If current minimum line is identical to previous minimum line then skip to remove duplicates. |
1347 | // If current minimum line is identical to previous minimum line then skip to remove duplicates. |
|
1239 | if(rem != NULL) { |
1348 | if(rem != NULL) { |
|
1240 | #if defined ___AmigaOS___ |
1349 | #if defined ___AmigaOS___ |
|
1241 | if(StrnCmp(locale, min, rem, -1, SC_ASCII) == 0) { |
1350 | if(StrnCmp(locale, min, rem, -1, SC_ASCII) == 0) { |
|
1242 | #else |
1351 | #else |
|
1243 | if(strcmp(min, rem) == 0 { |
1352 | if(strcmp(min, rem) == 0) { |
|
1244 | #endif |
1353 | #endif |
|
1245 | free(min); |
1354 | free(min); |
|
1246 | continue; |
1355 | continue; |
|
1247 | } |
1356 | } |
|
1248 | } |
1357 | } |
|
1249 | |
1358 | |
|
1250 | #if defined ___AsyncIO___ |
1359 | #if defined ___AsyncIO___ |
|
1251 | WriteAsync(fp, min, (LONG)strlen(min)); |
1360 | WriteAsync(fp, min, (LONG)strlen(min)); |
|
1252 | WriteAsync(fp, "\n", 1); |
1361 | WriteAsync(fp, "\n", 1); |
|
1253 | #else |
1362 | #else |
|
1254 | fprintf(fp, "%s\n", min); |
1363 | fprintf(fp, "%s\n", min); |
|
1255 | #endif |
1364 | #endif |
|
1256 | |
1365 | |
|
1257 | if(rem != NULL) { |
1366 | if(rem != NULL) { |
|
1258 | free(rem); |
1367 | free(rem); |
|
1259 | } |
1368 | } |
|
1260 | |
1369 | |
|
1261 | if((rem = malloc((strlen(min) + 1) * sizeof(*rem))) == NULL) { |
1370 | if((rem = malloc((strlen(min) + 1) * sizeof(*rem))) == NULL) { |
|
1262 | fprintf(stderr, "Memory allocation failure.\n"); |
1371 | fprintf(stderr, "Memory allocation failure.\n"); |
|
1263 | free(min); |
1372 | free(min); |
|
1264 | #if defined ___AsyncIO___ |
1373 | #if defined ___AsyncIO___ |
|
1265 | CloseAsync(fp); |
1374 | CloseAsync(fp); |
|
1266 | #else |
1375 | #else |
|
1267 | fclose(fp); |
1376 | fclose(fp); |
|
1268 | #endif |
1377 | #endif |
|
1269 | return; |
1378 | return; |
|
1270 | } |
1379 | } |
|
1271 | |
1380 | |
|
1272 | sprintf(rem, "%s", min); |
1381 | sprintf(rem, "%s", min); |
|
1273 | free(min); |
1382 | free(min); |
|
1274 | } |
1383 | } |
|
1275 | } |
1384 | } |
|
1276 | |
1385 | |
|
1277 | if(rem != NULL) { |
1386 | if(rem != NULL) { |
|
1278 | free(rem); |
1387 | free(rem); |
|
1279 | } |
1388 | } |
|
1280 | |
1389 | |
|
1281 | // Write out any remaining contents from the temporary files. |
1390 | // Write out any remaining contents from the temporary files. |
|
1282 | for(i = 0; run && i < files; ++i) { |
1391 | for(i = 0; run && i < files; ++i) { |
|
1283 | #if defined ___AmigaOS___ |
1392 | #if defined ___AmigaOS___ |
|
1284 | // Check if CTRL+C was pressed and abort the program. |
1393 | // Check if CTRL+C was pressed and abort the program. |
|
1285 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
1394 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
1286 | run = FALSE; |
1395 | run = FALSE; |
|
1287 | continue; |
1396 | continue; |
|
1288 | } |
1397 | } |
|
1289 | #endif |
1398 | #endif |
|
1290 | tmp = ReadLine(tp[i]); |
1399 | tmp = ReadLine(tp[i]); |
|
1291 | if(tmp == NULL) { |
1400 | if(tmp == NULL) { |
|
1292 | continue; |
1401 | continue; |
|
1293 | } |
1402 | } |
|
1294 | #if defined ___AsyncIO___ |
1403 | #if defined ___AsyncIO___ |
|
1295 | WriteAsync(fp, tmp, (LONG)strlen(tmp)); |
1404 | WriteAsync(fp, tmp, (LONG)strlen(tmp)); |
|
1296 | WriteAsync(fp, "\n", 1); |
1405 | WriteAsync(fp, "\n", 1); |
|
1297 | #else |
1406 | #else |
|
1298 | fprintf(fp, "%s\n", tmp); |
1407 | fprintf(fp, "%s\n", tmp); |
|
1299 | #endif |
1408 | #endif |
|
1300 | free(tmp); |
1409 | free(tmp); |
|
1301 | } |
1410 | } |
|
1302 | |
1411 | |
|
1303 | // Close all temporary files. |
1412 | // Close all temporary files. |
|
1304 | for(i = 0; i < files; ++i) { |
1413 | for(i = 0; i < files; ++i) { |
|
1305 | #if defined ___AsyncIO___ |
1414 | #if defined ___AsyncIO___ |
|
1306 | CloseAsync(tp[i]); |
1415 | CloseAsync(tp[i]); |
|
1307 | #else |
1416 | #else |
|
1308 | fclose(tp[i]); |
1417 | fclose(tp[i]); |
|
1309 | #endif |
1418 | #endif |
|
1310 | } |
1419 | } |
|
1311 | |
1420 | |
|
1312 | if(verbose) { |
1421 | if(verbose) { |
|
1313 | fprintf(stdout, "\n"); |
1422 | fprintf(stdout, "\n"); |
|
1314 | } |
1423 | } |
|
1315 | |
1424 | |
|
1316 | #if defined ___AsyncIO___ |
1425 | #if defined ___AsyncIO___ |
|
1317 | CloseAsync(fp); |
1426 | CloseAsync(fp); |
|
1318 | #else |
1427 | #else |
|
1319 | fclose(fp); |
1428 | fclose(fp); |
|
1320 | #endif |
1429 | #endif |
|
1321 | } |
1430 | } |
|
1322 | |
1431 | |
|
1323 | /* |
1432 | /* |
|
1324 | * |
1433 | * |
|
1325 | * Filter the paths inside the database with provided paths. |
1434 | * Filter the paths inside the database with provided paths. |
|
1326 | */ |
1435 | */ |
|
1327 | void FilterDatabasePaths(char *dbFile, char *tmpName, char **paths, unsigned int count) { |
1436 | void FilterDatabasePaths(char *dbFile, char *tmpName, char **paths, unsigned int count) { |
|
1328 | #if defined ___AsyncIO___ |
1437 | #if defined ___AsyncIO___ |
|
1329 | struct AsyncFile *fp; |
1438 | struct AsyncFile *fp; |
|
1330 | struct AsyncFile *tp; |
1439 | struct AsyncFile *tp; |
|
1331 | #else |
1440 | #else |
|
1332 | FILE *fp; |
1441 | FILE *fp; |
|
1333 | FILE *tp; |
1442 | FILE *tp; |
|
1334 | #endif |
1443 | #endif |
|
1335 | char *line; |
1444 | char *line; |
|
1336 | int i; |
1445 | int i; |
|
1337 | dbEntry *entry; |
1446 | dbEntry *entry; |
|
1338 | |
1447 | |
|
1339 | // Open database file for reading. |
1448 | // Open database file for reading. |
|
1340 | #if defined ___AsyncIO___ |
1449 | #if defined ___AsyncIO___ |
|
1341 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
1450 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
1342 | #else |
1451 | #else |
|
1343 | if((fp = fopen(dbFile, "r")) == NULL) { |
1452 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
1344 | #endif |
1453 | #endif |
|
1345 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
1454 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
|
1346 | return; |
1455 | return; |
|
1347 | } |
1456 | } |
|
1348 | |
1457 | |
|
1349 | // Open temporary file for writing. |
1458 | // Open temporary file for writing. |
|
1350 | #if defined ___AsyncIO___ |
1459 | #if defined ___AsyncIO___ |
|
1351 | if((tp = OpenAsync(tmpName, MODE_WRITE, ASYNC_BUF)) == NULL) { |
1460 | if((tp = OpenAsync(tmpName, MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
1352 | #else |
1461 | #else |
|
1353 | if((tp = fopen(tmpName, "w")) == NULL) { |
1462 | if((tp = fopen(tmpName, "w")) == NULL) { |
|
1354 | #endif |
1463 | #endif |
|
1355 | fprintf(stderr, "Unable to open '%s' for writing.\n", tmpName); |
1464 | fprintf(stderr, "Unable to open '%s' for writing.\n", tmpName); |
|
1356 | |
1465 | |
|
1357 | // Close database file. |
1466 | // Close database file. |
|
1358 | #if defined ___AsyncIO___ |
1467 | #if defined ___AsyncIO___ |
|
1359 | CloseAsync(fp); |
1468 | CloseAsync(fp); |
|
1360 | #else |
1469 | #else |
|
1361 | fclose(fp); |
1470 | fclose(fp); |
|
1362 | #endif |
1471 | #endif |
|
1363 | |
1472 | |
|
1364 | return; |
1473 | return; |
|
1365 | } |
1474 | } |
|
1366 | |
1475 | |
|
1367 | while(run && (line = ReadLine(fp)) != NULL) { |
1476 | while(run && (line = ReadLine(fp)) != NULL) { |
|
1368 | #if defined ___AmigaOS___ |
1477 | #if defined ___AmigaOS___ |
|
1369 | // Check if CTRL+C was pressed and abort the program. |
1478 | // Check if CTRL+C was pressed and abort the program. |
|
1370 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
1479 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
1371 | run = FALSE; |
1480 | run = FALSE; |
|
1372 | continue; |
1481 | continue; |
|
1373 | } |
1482 | } |
|
1374 | #endif |
1483 | #endif |
|
1375 | if((entry = CreateDataseEntry(line)) == NULL) { |
1484 | if((entry = CreateDataseEntry(line)) == NULL) { |
|
1376 | fprintf(stderr, "Unable to create database entry.\n"); |
1485 | fprintf(stderr, "Unable to create database entry.\n"); |
|
1377 | continue; |
1486 | continue; |
|
1378 | } |
1487 | } |
|
1379 | for(i = 0; i < count; ++i) { |
1488 | for(i = 0; i < count; ++i) { |
|
1380 | if(PathCompare(entry->path, paths[i]) == TRUE) { |
1489 | if(PathCompare(entry->path, paths[i]) == TRUE) { |
|
1381 | continue; |
1490 | continue; |
|
1382 | } |
1491 | } |
|
1383 | #if defined ___AsyncIO___ |
1492 | #if defined ___AsyncIO___ |
|
1384 | WriteAsync(tp, line, (LONG)strlen(line)); |
1493 | WriteAsync(tp, line, (LONG)strlen(line)); |
|
1385 | WriteAsync(tp, "\n", 1); |
1494 | WriteAsync(tp, "\n", 1); |
|
1386 | #else |
1495 | #else |
|
1387 | fprintf(tp, "%s\n", line); |
1496 | fprintf(tp, "%s\n", line); |
|
1388 | #endif |
1497 | #endif |
|
1389 | break; |
1498 | break; |
|
1390 | } |
1499 | } |
|
1391 | |
1500 | |
|
1392 | free(entry); |
1501 | free(entry); |
|
1393 | free(line); |
1502 | free(line); |
|
1394 | } |
1503 | } |
|
1395 | |
1504 | |
|
1396 | |
1505 | |
|
1397 | #if defined ___AsyncIO___ |
1506 | #if defined ___AsyncIO___ |
|
1398 | CloseAsync(fp); |
1507 | CloseAsync(fp); |
|
1399 | CloseAsync(tp); |
1508 | CloseAsync(tp); |
|
1400 | #else |
1509 | #else |
|
1401 | fclose(fp); |
1510 | fclose(fp); |
|
1402 | fclose(tp); |
1511 | fclose(tp); |
|
1403 | #endif |
1512 | #endif |
|
1404 | } |
1513 | } |
|
1405 | |
1514 | |
|
1406 | /* |
1515 | /* |
|
1407 | * |
1516 | * |
|
1408 | * Indexes a "path" by creating a database "dbFile". |
1517 | * Indexes a "path" by creating a database "dbFile". |
|
1409 | */ |
1518 | */ |
|
1410 | void GatherDatabaseFiles(char *dbFile, char **paths, unsigned int count) { |
1519 | void GatherDatabaseFiles(char *dbFile, char **paths, unsigned int count) { |
|
1411 | stats *stats; |
1520 | stats *stats; |
|
1412 | char **tmpNames; |
1521 | char **tmpNames; |
|
1413 | int dbSize; |
1522 | int dbSize; |
|
1414 | int dbLines; |
1523 | int dbLines; |
|
1415 | int tmpFiles; |
1524 | int tmpFiles; |
|
1416 | int tmpLines; |
1525 | int tmpLines; |
|
1417 | int i; |
1526 | int i; |
|
1418 | |
1527 | |
|
1419 | // Generate the database file from the supplied paths. |
1528 | // Generate the database file from the supplied paths. |
|
1420 | if((stats = CollectFiles(dbFile, paths, count)) == NULL) { |
1529 | if((stats = CollectFiles(dbFile, paths, count)) == NULL) { |
|
1421 | fprintf(stderr, "Collecting files failed.\n"); |
1530 | fprintf(stderr, "Collecting files failed.\n"); |
|
1422 | return; |
1531 | return; |
|
1423 | } |
1532 | } |
|
1424 | free(stats); |
1533 | free(stats); |
|
1425 | |
1534 | |
|
1426 | // Compute the amount of temporary files needed. |
1535 | // Compute the amount of temporary files needed. |
|
1427 | dbSize = GetFileSize(dbFile); |
1536 | dbSize = GetFileSize(dbFile); |
|
1428 | if(dbSize == -1) { |
1537 | if(dbSize == -1) { |
|
1429 | fprintf(stderr, "File size for '%s' failed.\n", dbFile); |
1538 | fprintf(stderr, "File size for '%s' failed.\n", dbFile); |
|
1430 | return; |
1539 | return; |
|
1431 | } |
1540 | } |
|
1432 | tmpFiles = dbSize / maxmem; |
1541 | tmpFiles = dbSize / maxmem; |
|
1433 | |
1542 | |
|
1434 | /* In case no temporary files are required, |
1543 | /* In case no temporary files are required, |
|
1435 | * just sort the database and terminate. |
1544 | * just sort the database and terminate. |
|
1436 | */ |
1545 | */ |
|
1437 | if(tmpFiles <= 1) { |
1546 | if(tmpFiles <= 1) { |
|
1438 | SortDatabase(dbFile); |
1547 | SortDatabase(dbFile); |
|
1439 | return; |
1548 | return; |
|
1440 | } |
1549 | } |
|
1441 | |
1550 | |
|
1442 | // Get the database metrics. |
1551 | // Get the database metrics. |
|
1443 | dbLines = CountFileLines(dbFile); |
1552 | dbLines = CountFileLines(dbFile); |
|
1444 | if(dbLines == -1) { |
1553 | if(dbLines == -1) { |
|
1445 | fprintf(stderr, "Counting lines of '%s' failed.\n", dbFile); |
1554 | fprintf(stderr, "Counting lines of '%s' failed.\n", dbFile); |
|
1446 | } |
1555 | } |
|
1447 | tmpLines = dbLines / tmpFiles; |
1556 | tmpLines = dbLines / tmpFiles; |
|
1448 | |
1557 | |
|
1449 | // Create temporary files. |
1558 | // Create temporary files. |
|
1450 | if((tmpNames = CreateTemporaryFiles(tmpFiles)) == NULL) { |
1559 | if((tmpNames = CreateTemporaryFiles(tmpFiles)) == NULL) { |
|
1451 | fprintf(stderr, "Unable to create temporary files.\n"); |
1560 | fprintf(stderr, "Unable to create temporary files.\n"); |
|
1452 | return; |
1561 | return; |
|
1453 | } |
1562 | } |
|
1454 | |
1563 | |
|
1455 | // Write "tmpLines" to temporary files in "tmpNames" from "dbFile". |
1564 | // Write "tmpLines" to temporary files in "tmpNames" from "dbFile". |
|
1456 | WriteTemporaryFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines); |
1565 | WriteTemporaryFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines); |
|
1457 | |
1566 | |
|
1458 | // Sort the temporary files. |
1567 | // Sort the temporary files. |
|
1459 | for(i = 0; run && i < tmpFiles; ++i) { |
1568 | for(i = 0; run && i < tmpFiles; ++i) { |
|
1460 | SortDatabase(tmpNames[i]); |
1569 | SortDatabase(tmpNames[i]); |
|
1461 | } |
1570 | } |
|
1462 | |
1571 | |
|
1463 | // Merge all the temporary files to the database file. |
1572 | // Merge all the temporary files to the database file. |
|
1464 | MergeTemporaryFiles(dbFile, tmpNames, tmpFiles, dbLines); |
1573 | MergeTemporaryFiles(dbFile, tmpNames, tmpFiles, dbLines); |
|
1465 | |
1574 | |
|
1466 | // Remove all temporary files. |
1575 | // Remove all temporary files. |
|
1467 | RemoveFiles(tmpNames, tmpFiles); |
1576 | RemoveFiles(tmpNames, tmpFiles); |
|
1468 | |
1577 | |
|
1469 | free(tmpNames); |
1578 | free(tmpNames); |
|
1470 | } |
1579 | } |
|
1471 | |
1580 | |
|
1472 | void RemoveDatabaseFiles(char *dbFile, char **paths, unsigned int count) { |
1581 | void RemoveDatabaseFiles(char *dbFile, char **paths, unsigned int count) { |
|
1473 | char *tmpName; |
1582 | char *tmpName; |
|
1474 | |
1583 | |
|
1475 | // Create a temporary file to hold the changes. |
1584 | // Create a temporary file to hold the changes. |
|
1476 | if((tmpName = CreateTemporaryFile()) == NULL) { |
1585 | if((tmpName = CreateTemporaryFile()) == NULL) { |
|
1477 | fprintf(stderr, "Unable to create temporary file.\n"); |
1586 | fprintf(stderr, "Unable to create temporary file.\n"); |
|
1478 | return; |
1587 | return; |
|
1479 | } |
1588 | } |
|
1480 | |
1589 | |
|
1481 | // Filter the database of the provided paths. |
1590 | // Filter the database of the provided paths. |
|
1482 | FilterDatabasePaths(dbFile, tmpName, paths, count); |
1591 | FilterDatabasePaths(dbFile, tmpName, paths, count); |
|
1483 | |
1592 | |
|
1484 | // Overwrite the database file with the filtered paths. |
1593 | // Overwrite the database file with the filtered paths. |
|
1485 | CopyFile(tmpName, dbFile); |
1594 | CopyFile(tmpName, dbFile); |
|
1486 | |
1595 | |
|
1487 | // Remove temporary file. |
1596 | // Remove temporary file. |
|
1488 | RemoveFile(tmpName); |
1597 | RemoveFile(tmpName); |
|
1489 | } |
1598 | } |
|
1490 | |
1599 | |
|
1491 | void usage(char *name) { |
1600 | void usage(char *name) { |
|
1492 | fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
1601 | fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
|
1493 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
1602 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
|
1494 | fprintf(stdout, " \n"); |
1603 | fprintf(stdout, " \n"); |
|
1495 | fprintf(stdout, "SYNTAX: %s [-q] <-a|-r|-c> <PATH PATH PATH...> \n", name); |
1604 | fprintf(stdout, "SYNTAX: %s [-q] <-a|-r|-c> <PATH PATH PATH...> \n", name); |
|
1496 | fprintf(stdout, " \n"); |
1605 | fprintf(stdout, " \n"); |
|
1497 | fprintf(stdout, "Required: \n"); |
1606 | fprintf(stdout, "Required: \n"); |
|
1498 | fprintf(stdout, " -a [PATH...] Add files. \n"); |
1607 | fprintf(stdout, " -a [PATH...] Add files. \n"); |
|
1499 | fprintf(stdout, " -c [PATH...] Create from scratch. \n"); |
1608 | fprintf(stdout, " -c [PATH...] Create from scratch. \n"); |
|
1500 | fprintf(stdout, " -r [PATH...] Remove files. \n"); |
1609 | fprintf(stdout, " -r [PATH...] Remove files. \n"); |
|
1501 | fprintf(stdout, " \n"); |
1610 | fprintf(stdout, " \n"); |
|
1502 | fprintf(stdout, "Optional: \n"); |
1611 | fprintf(stdout, "Optional: \n"); |
|
1503 | fprintf(stdout, " -d [FIILE] Where to store the database. \n"); |
1612 | fprintf(stdout, " -d [FIILE] Where to store the database. \n"); |
|
1504 | fprintf(stdout, " -m BYTES Memory to use (default: %d). \n", maxmem); |
1613 | fprintf(stdout, " -m BYTES Memory to use (default: %d). \n", maxmem); |
|
1505 | fprintf(stdout, " -q Do not print out any messages. \n"); |
1614 | fprintf(stdout, " -q Do not print out any messages. \n"); |
|
1506 | fprintf(stdout, " \n"); |
1615 | fprintf(stdout, " \n"); |
|
1507 | fprintf(stdout, "DATABASE is a path to where the indexed results will be \n"); |
1616 | fprintf(stdout, "DATABASE is a path to where the indexed results will be \n"); |
|
1508 | fprintf(stdout, "stored for searching with the Hunt tool. \n"); |
1617 | fprintf(stdout, "stored for searching with the Hunt tool. \n"); |
|
1509 | fprintf(stdout, " \n"); |
1618 | fprintf(stdout, " \n"); |
|
1510 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
1619 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
|
1511 | } |
1620 | } |
|
1512 | |
1621 | |
|
1513 | /* |
1622 | /* |
|
1514 | * |
1623 | * |
|
1515 | * Main entry point. |
1624 | * Main entry point. |
|
1516 | */ |
1625 | */ |
|
1517 | int main(int argc, char **argv) { |
1626 | int main(int argc, char **argv) { |
|
- | 1627 | #if defined ___AmigaOS___ |
||
- | 1628 | struct FileInfoBlock *FIB; |
||
- | 1629 | BPTR lock; |
||
- | 1630 | #else |
||
- | 1631 | struct stat dirStat; |
||
- | 1632 | #endif |
||
1518 | int option; |
1633 | int option; |
|
1519 | unsigned int i; |
1634 | unsigned int i; |
|
1520 | unsigned int count; |
1635 | unsigned int count; |
|
1521 | char *dbFile; |
1636 | char *dbFile; |
|
1522 | char *path; |
1637 | char *path; |
|
1523 | char **paths; |
1638 | char **paths; |
|
1524 | struct stat dirStat; |
- | ||
1525 | |
1639 | |
|
1526 | // Bind handler to SIGINT. |
1640 | // Bind handler to SIGINT. |
|
1527 | #if !defined ___AmigaOS___ |
1641 | #if !defined ___AmigaOS___ |
|
1528 | signal(SIGINT, SignalHandler); |
1642 | signal(SIGINT, SignalHandler); |
|
1529 | #endif |
1643 | #endif |
|
1530 | |
1644 | |
|
1531 | dbFile = DEFAULT_DATABASE_FILE; |
1645 | dbFile = DEFAULT_DATABASE_FILE; |
|
1532 | while((option = getopt(argc, argv, "hqdm:arc")) != -1) { |
1646 | while((option = getopt(argc, argv, "hqdm:arc")) != -1) { |
|
1533 | switch(option) { |
1647 | switch(option) { |
|
1534 | case 'a': |
1648 | case 'a': |
|
1535 | operation = GATHER; |
1649 | operation = GATHER; |
|
1536 | break; |
1650 | break; |
|
1537 | case 'r': |
1651 | case 'r': |
|
1538 | operation = REMOVE; |
1652 | operation = REMOVE; |
|
1539 | break; |
1653 | break; |
|
1540 | case 'c': |
1654 | case 'c': |
|
1541 | operation = CREATE; |
1655 | operation = CREATE; |
|
1542 | break; |
1656 | break; |
|
1543 | case 'm': |
1657 | case 'm': |
|
1544 | maxmem = strtoul(optarg, NULL, 10); |
1658 | maxmem = strtoul(optarg, NULL, 10); |
|
1545 | break; |
1659 | break; |
|
1546 | case 'd': |
1660 | case 'd': |
|
1547 | dbFile = optarg; |
1661 | dbFile = optarg; |
|
1548 | break; |
1662 | break; |
|
1549 | case 'q': |
1663 | case 'q': |
|
1550 | verbose = FALSE; |
1664 | verbose = FALSE; |
|
1551 | break; |
1665 | break; |
|
1552 | case 'h': |
1666 | case 'h': |
|
1553 | usage(argv[0]); |
1667 | usage(argv[0]); |
|
1554 | return 0; |
1668 | return 0; |
|
1555 | case '?': |
1669 | case '?': |
|
1556 | fprintf(stderr, "Invalid option %ct.\n", optopt); |
1670 | fprintf(stderr, "Invalid option %ct.\n", optopt); |
|
1557 | return 1; |
1671 | return 1; |
|
1558 | } |
1672 | } |
|
1559 | } |
1673 | } |
|
1560 | |
1674 | |
|
1561 | if(operation == NONE) { |
1675 | if(operation == NONE) { |
|
1562 | usage(argv[0]); |
1676 | usage(argv[0]); |
|
1563 | return 1; |
1677 | return 1; |
|
1564 | } |
1678 | } |
|
1565 | |
1679 | |
|
1566 | if(optind >= argc) { |
1680 | if(optind >= argc) { |
|
1567 | usage(argv[0]); |
1681 | usage(argv[0]); |
|
1568 | return 1; |
1682 | return 1; |
|
1569 | } |
1683 | } |
|
1570 | |
1684 | |
|
1571 | // Go through all supplied arguments and add paths to search. |
1685 | // Go through all supplied arguments and add paths to search. |
|
1572 | if((paths = malloc((argc - optind) * sizeof(*paths))) == NULL) { |
1686 | if((paths = malloc((argc - optind) * sizeof(*paths))) == NULL) { |
|
1573 | fprintf(stderr, "Memory allocation failure.\n"); |
1687 | fprintf(stderr, "Memory allocation failure.\n"); |
|
1574 | return 1; |
1688 | return 1; |
|
1575 | } |
1689 | } |
|
1576 | for(i = optind, count = 0; i < argc; ++i, ++count) { |
1690 | for(i = optind, count = 0; i < argc; ++i, ++count) { |
|
1577 | if((path = PathToAbsolute(argv[optind])) == NULL) { |
1691 | if((path = PathToAbsolute(argv[i])) == NULL) { |
|
1578 | fprintf(stderr, "Absolute path for '%s' failed to resolve.\n", argv[optind]); |
1692 | fprintf(stderr, "Absolute path for '%s' failed to resolve.\n", argv[optind]); |
|
1579 | continue; |
1693 | continue; |
|
1580 | } |
1694 | } |
|
1581 | |
1695 | |
|
1582 | // Check that the path is a directory. |
1696 | // Check that the path is a directory. |
|
- | 1697 | #if defined ___AmigaOS___ |
||
- | 1698 | if((lock = Lock(path, ACCESS_READ)) == NULL) { |
||
- | 1699 | fprintf(stderr, "Path '%s' is not accessible.\n", path); |
||
- | 1700 | free(path); |
||
- | 1701 | continue; |
||
- | 1702 | } |
||
- | 1703 | |
||
- | 1704 | if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) { |
||
- | 1705 | fprintf(stderr, "Path '%s' file information block not accessible.\n", path); |
||
- | 1706 | UnLock(lock); |
||
- | 1707 | free(path); |
||
- | 1708 | continue; |
||
- | 1709 | } |
||
- | 1710 | |
||
- | 1711 | if(Examine(lock, FIB) == FALSE) { |
||
- | 1712 | fprintf(stderr, "Path '%s' information unexaminable.\n", path); |
||
- | 1713 | UnLock(lock); |
||
- | 1714 | FreeDosObject(DOS_FIB, FIB); |
||
- | 1715 | free(path); |
||
- | 1716 | continue; |
||
- | 1717 | } |
||
- | 1718 | |
||
- | 1719 | if(FIB->fib_DirEntryType < 0) { |
||
- | 1720 | #else |
||
1583 | stat(path, &dirStat); |
1721 | stat(path, &dirStat); |
|
1584 | if(!S_ISDIR(dirStat.st_mode)) { |
1722 | if(!S_ISDIR(dirStat.st_mode)) { |
|
- | 1723 | #endif |
||
1585 | fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
1724 | fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
|
- | 1725 | #if defined ___AmigaOS___ |
||
- | 1726 | UnLock(lock); |
||
- | 1727 | FreeDosObject(DOS_FIB, FIB); |
||
- | 1728 | #endif |
||
1586 | free(path); |
1729 | free(path); |
|
1587 | return 1; |
1730 | return 1; |
|
1588 | } |
1731 | } |
|
1589 | |
1732 | |
|
1590 | if(verbose) { |
1733 | if(verbose) { |
|
1591 | fprintf(stdout, "Will process path: '%s'\n", path); |
1734 | fprintf(stdout, "Will process path: '%s'\n", path); |
|
1592 | } |
1735 | } |
|
1593 | |
1736 | |
|
1594 | // Add the path to the array of paths. |
1737 | // Add the path to the array of paths. |
|
1595 | if((paths[count] = malloc(strlen(path) * sizeof(*paths[count]))) == NULL) { |
1738 | if((paths[count] = malloc(strlen(path) * sizeof(*paths[count]))) == NULL) { |
|
1596 | fprintf(stderr, "Memory allocation failure."); |
1739 | fprintf(stderr, "Memory allocation failure."); |
|
1597 | return 1; |
1740 | return 1; |
|
1598 | } |
1741 | } |
|
- | 1742 | |
||
1599 | sprintf(paths[count], "%s", path); |
1743 | sprintf(paths[count], "%s", path); |
|
- | 1744 | |
||
- | 1745 | #if defined ___AmigaOS___ |
||
- | 1746 | UnLock(lock); |
||
- | 1747 | FreeDosObject(DOS_FIB, FIB); |
||
- | 1748 | #endif |
||
1600 | free(path); |
1749 | free(path); |
|
- | 1750 | |
||
1601 | } |
1751 | } |
|
1602 | |
1752 | |
|
1603 | if(verbose) { |
1753 | if(verbose) { |
|
1604 | fprintf(stdout, "Gathering to: '%s'\n", dbFile); |
1754 | fprintf(stdout, "Gathering to: '%s'\n", dbFile); |
|
1605 | } |
1755 | } |
|
1606 | |
1756 | |
|
1607 | #if defined ___AmigaOS___ |
1757 | #if defined ___AmigaOS___ |
|
1608 | locale = OpenLocale(NULL); |
1758 | locale = OpenLocale(NULL); |
|
1609 | #endif |
1759 | #endif |
|
1610 | |
1760 | |
|
1611 | // Gather. |
1761 | // Gather. |
|
1612 | switch(operation) { |
1762 | switch(operation) { |
|
1613 | case CREATE: |
1763 | case CREATE: |
|
1614 | if(verbose) { |
1764 | if(verbose) { |
|
1615 | fprintf(stdout, "Removing '%s' and creating a new database.\n", dbFile); |
1765 | fprintf(stdout, "Removing '%s' and creating a new database.\n", dbFile); |
|
1616 | } |
1766 | } |
|
1617 | RemoveFile(dbFile); |
1767 | RemoveFile(dbFile); |
|
1618 | case GATHER: |
1768 | case GATHER: |
|
1619 | if(verbose) { |
1769 | if(verbose) { |
|
1620 | fprintf(stdout, "Gathering files to database...\n"); |
1770 | fprintf(stdout, "Gathering files to database...\n"); |
|
1621 | } |
1771 | } |
|
1622 | GatherDatabaseFiles(dbFile, paths, count); |
1772 | GatherDatabaseFiles(dbFile, paths, count); |
|
1623 | break; |
1773 | break; |
|
1624 | case REMOVE: |
1774 | case REMOVE: |
|
1625 | if(verbose) { |
1775 | if(verbose) { |
|
1626 | fprintf(stdout, "Removing files from database...\n"); |
1776 | fprintf(stdout, "Removing files from database...\n"); |
|
1627 | } |
1777 | } |
|
1628 | RemoveDatabaseFiles(dbFile, paths, count); |
1778 | RemoveDatabaseFiles(dbFile, paths, count); |
|
1629 | break; |
1779 | break; |
|
1630 | default: |
1780 | default: |
|
1631 | break; |
1781 | break; |
|
1632 | } |
1782 | } |
|
1633 | |
1783 | |
|
1634 | #if defined ___AmigaOS___ |
1784 | #if defined ___AmigaOS___ |
|
1635 | CloseLocale(locale); |
1785 | CloseLocale(locale); |
|
1636 | #endif |
1786 | #endif |
|
1637 | |
1787 | |
|
1638 | free(paths); |
1788 | free(paths); |
|
1639 | |
1789 | |
|
1640 | return 0; |
1790 | return 0; |
|
1641 | } |
1791 | } |
|
1642 | |
1792 | |