HuntnGather – Diff between revs 8 and 10

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 8 Rev 10
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 #include <signal.h> 9 #include <signal.h>
10   10  
11 #include <sys/types.h> 11 #include <sys/types.h>
12 #include <sys/stat.h> 12 #include <sys/stat.h>
13   13  
14 #include <proto/dos.h> 14 #include <proto/dos.h>
15 #include <proto/exec.h> 15 #include <proto/exec.h>
16   16  
17 #include "StringStack.h" 17 #include "StringStack.h"
18   18  
19 #if !defined ___HAVE_GETOPT___ 19 #if !defined ___HAVE_GETOPT___
20 #include "getopt.h" 20 #include "getopt.h"
21 #endif 21 #endif
22   22  
23 #if defined ___AmigaOS___ 23 #if defined ___AmigaOS___
24 /*************************************************************************/ 24 /*************************************************************************/
25 /* Version string used for querrying the program version. */ 25 /* Version string used for querrying the program version. */
26 /*************************************************************************/ 26 /*************************************************************************/
27 TEXT version_string[] = 27 TEXT version_string[] =
28 "\0$VER: Gather 1.5 "__DATE__" by Wizardry and Steamworks"; 28 "\0$VER: Gather 1.5 "__DATE__" by Wizardry and Steamworks";
29 #endif 29 #endif
30   30  
31 #if !defined TRUE 31 #if !defined TRUE
32 #define TRUE 1; 32 #define TRUE 1;
33 #endif 33 #endif
34   34  
35 #if !defined FALSE 35 #if !defined FALSE
36 #define FALSE 0; 36 #define FALSE 0;
37 #endif 37 #endif
38   38  
39 #define MAX_MEM 262144 39 #define MAX_MEM 262144
40 #define DEFAULT_DATABASE_FILE "S:gather.db" 40 #define DEFAULT_DATABASE_FILE "S:gather.db"
41   41  
42 typedef struct { 42 typedef struct {
43 unsigned int dirs; 43 unsigned int dirs;
44 unsigned int files; 44 unsigned int files;
45 } stats; 45 } stats;
46   46  
47 int run = TRUE; 47 int run = TRUE;
48 int verbose = TRUE; 48 int verbose = TRUE;
49   49  
50 void SignalHandler(int sig) { 50 void SignalHandler(int sig) {
51 // Toggle the run flag to stop execution. 51 // Toggle the run flag to stop execution.
52 run = FALSE; 52 run = FALSE;
53 } 53 }
54   54  
55 int compare(const void *a, const void *b) { 55 int compare(const void *a, const void *b) {
56 const char **p = (const char **)a; 56 const char **p = (const char **)a;
57 const char **q = (const char **)b; 57 const char **q = (const char **)b;
58 return strcmp(*p, *q); 58 return strcmp(*p, *q);
59 } 59 }
60   60  
61 /* 61 /*
62 * 62 *
63 * Sorts a database file lexicographically. 63 * Sorts a database file lexicographically.
64 */ 64 */
65 void SortDatabase(char *dbFile) { 65 void SortDatabase(char *dbFile) {
66 FILE *fp; 66 FILE *fp;
67 char *name = NULL; 67 char *name = NULL;
68 char *path = NULL; 68 char *path = NULL;
69 char **database; 69 char **database;
70 char c; 70 char c;
71 int i; 71 int i;
72 int side; 72 int side;
73 unsigned int line; 73 unsigned int line;
74   74  
75 // Open database file for reading. 75 // Open database file for reading.
76 if((fp = fopen(dbFile, "r")) == NULL) { 76 if((fp = fopen(dbFile, "r")) == NULL) {
77 fprintf(stderr, "Unable to open gather database for reading.\n"); 77 fprintf(stderr, "Unable to open gather database for reading.\n");
78 return; 78 return;
79 } 79 }
80   80  
81 database = (char **) malloc(sizeof(char *)); 81 database = (char **) malloc(sizeof(char *));
82 name = (char *) malloc(sizeof(char)); 82 name = (char *) malloc(sizeof(char));
83 path = (char *) malloc(sizeof(char)); 83 path = (char *) malloc(sizeof(char));
84 line = 0; 84 line = 0;
85 side = 0; 85 side = 0;
86 i = 0; 86 i = 0;
87   87  
88 if(verbose) { 88 if(verbose) {
89 fprintf(stdout, "Sorting database: '%s'\n", dbFile); 89 fprintf(stdout, "Sorting database: '%s'\n", dbFile);
90 } 90 }
91   91  
92 while(run && fscanf(fp, "%c", &c) == 1) { 92 while(run && fscanf(fp, "%c", &c) == 1) {
93 #if defined ___AmigaOS___ 93 #if defined ___AmigaOS___
94 // Check if CTRL+C was pressed and abort the program. 94 // Check if CTRL+C was pressed and abort the program.
95 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 95 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
96 run = FALSE; 96 run = FALSE;
97 continue; 97 continue;
98 } 98 }
99 #endif 99 #endif
100 switch(c) { 100 switch(c) {
101 case '\n': 101 case '\n':
102 // Load up the name and path into the database variable. 102 // Load up the name and path into the database variable.
103 database = (char **) realloc(database, (line + 1) * sizeof(char *)); 103 database = (char **) realloc(database, (line + 1) * sizeof(char *));
104 database[line] = (char *) malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); 104 database[line] = (char *) malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char));
105 sprintf(database[line], "%s\t%s", name, path); 105 sprintf(database[line], "%s\t%s", name, path);
106 ++line; 106 ++line;
107   107  
108 free(name); 108 free(name);
109 name = (char *) malloc(sizeof(char)); 109 name = (char *) malloc(sizeof(char));
110 --side; 110 --side;
111 i = 0; 111 i = 0;
112   112  
113 break; 113 break;
114 case '\t': 114 case '\t':
115 free(path); 115 free(path);
116 path = (char *) malloc(sizeof(char)); 116 path = (char *) malloc(sizeof(char));
117 ++side; 117 ++side;
118 i = 0; 118 i = 0;
119 break; 119 break;
120 default: 120 default:
121 switch(side) { 121 switch(side) {
122 case 0: 122 case 0:
123 name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); 123 name = (char *) realloc(name, (i + 1 + 1) * sizeof(char));
124 name[i] = c; 124 name[i] = c;
125 name[i + 1] = '\0'; 125 name[i + 1] = '\0';
126 break; 126 break;
127 case 1: 127 case 1:
128 path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); 128 path = (char *) realloc(path, (i + 1 + 1) * sizeof(char));
129 path[i] = c; 129 path[i] = c;
130 path[i + 1] = '\0'; 130 path[i + 1] = '\0';
131 break; 131 break;
132 default: 132 default:
133 fprintf(stderr, "Database corrupted.\n"); 133 fprintf(stderr, "Database corrupted.\n");
134 break; 134 break;
135 } 135 }
136 ++i; 136 ++i;
137 break; 137 break;
138 } 138 }
139 } 139 }
140   140  
141 fclose(fp); 141 fclose(fp);
142   142  
143 // Sort the database. 143 // Sort the database.
144 qsort(database, line, sizeof(char *), compare); 144 qsort(database, line, sizeof(char *), compare);
145   145  
146 // Write the database lines back to the database. 146 // Write the database lines back to the database.
147 if((fp = fopen(dbFile, "w+")) == NULL) { 147 if((fp = fopen(dbFile, "w+")) == NULL) {
148 fprintf(stderr, "Unable to open gather database for writing.\n"); 148 fprintf(stderr, "Unable to open gather database for writing.\n");
149 return; 149 return;
150 } 150 }
151   151  
152 for(i = 0; i < line; ++i) { 152 for(i = 0; i < line; ++i) {
153 fprintf(fp, "%s\n", database[i]); 153 fprintf(fp, "%s\n", database[i]);
154 } 154 }
155   155  
156 free(database); 156 free(database);
157 fclose(fp); 157 fclose(fp);
158 } 158 }
159   159  
160 /* 160 /*
161 * 161 *
162 * Updates a database file "dbFile". 162 * Updates a database file "dbFile".
163 */ 163 */
164 void UpdateDatabase(char *dbFile, stringStack *dirStack, stats *stats) { 164 void UpdateDatabase(char *dbFile, stringStack *dirStack, stats *stats) {
165 FILE *fp; 165 FILE *fp;
166 DIR *dir; 166 DIR *dir;
167 struct dirent *dirEntry; 167 struct dirent *dirEntry;
168 struct stat dirStat; 168 struct stat dirStat;
169 unsigned int size; 169 unsigned int size;
170 char *path; 170 char *path;
171 char *subPath; 171 char *subPath;
172   172  
173 if((fp = fopen(dbFile, "w+")) == NULL) { 173 if((fp = fopen(dbFile, "w+")) == NULL) {
174 fprintf(stderr, "Unable to open gather database for writing.\n"); 174 fprintf(stderr, "Unable to open gather database for writing.\n");
175 return; 175 return;
176 } 176 }
177   177  
178 while(run && !stringStackIsEmpty(dirStack)) { 178 while(run && !stringStackIsEmpty(dirStack)) {
179 #if defined ___AmigaOS___ 179 #if defined ___AmigaOS___
180 // Check if CTRL+C was pressed and abort the program. 180 // Check if CTRL+C was pressed and abort the program.
181 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 181 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
182 run = FALSE; 182 run = FALSE;
183 } 183 }
184 #endif 184 #endif
185 if((path = stringStackPop(dirStack)) == NULL) { 185 if((path = stringStackPop(dirStack)) == NULL) {
186 return; 186 return;
187 } 187 }
188   188  
189 if((dir = opendir(path)) == NULL) { 189 if((dir = opendir(path)) == NULL) {
190 return; 190 return;
191 } 191 }
192   192  
193 while(run && (dirEntry = readdir(dir)) != NULL) { 193 while(run && (dirEntry = readdir(dir)) != NULL) {
194 #if defined ___AmigaOS___ 194 #if defined ___AmigaOS___
195 // Check if CTRL+C was pressed and abort the program. 195 // Check if CTRL+C was pressed and abort the program.
196 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 196 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
197 run = FALSE; 197 run = FALSE;
198 } 198 }
199 #endif 199 #endif
200 size = sizeof(path) + sizeof(dirEntry->d_name) + 1; 200 size = sizeof(path) + sizeof(dirEntry->d_name) + 1;
201 switch(path[strlen(path) - 1]) { 201 switch(path[strlen(path) - 1]) {
202 case '/': 202 case '/':
203 case ':': // This is a drive path. 203 case ':': // This is a drive path.
204 subPath = (char *) malloc(size); 204 subPath = (char *) malloc(size);
205 sprintf(subPath, "%s%s", path, dirEntry->d_name); 205 sprintf(subPath, "%s%s", path, dirEntry->d_name);
206 break; 206 break;
207 default: 207 default:
208 subPath = (char *) malloc(size + 1); 208 subPath = (char *) malloc(size + 1);
209 sprintf(subPath, "%s/%s", path, dirEntry->d_name); 209 sprintf(subPath, "%s/%s", path, dirEntry->d_name);
210 break; 210 break;
211 } 211 }
212 stat(subPath, &dirStat); 212 stat(subPath, &dirStat);
213 if(S_ISDIR(dirStat.st_mode)) { 213 if(S_ISDIR(dirStat.st_mode)) {
214 stringStackPush(dirStack, subPath); 214 stringStackPush(dirStack, subPath);
215   215  
216 ++stats->dirs; 216 ++stats->dirs;
217   217  
218 if(verbose) { 218 if(verbose) {
219 fprintf(stdout, 219 fprintf(stdout,
220 "Gathered %d directories and %d files.\r", 220 "Gathered %d directories and %d files.\r",
221 stats->dirs, 221 stats->dirs,
222 stats->files); 222 stats->files);
223 } 223 }
224   224  
225 free(subPath); 225 free(subPath);
226 continue; 226 continue;
227 } 227 }
228   228  
229 // Write to database file. 229 // Write to database file.
230 fprintf(fp, "%s\t%s\n", dirEntry->d_name, subPath); 230 fprintf(fp, "%s\t%s\n", dirEntry->d_name, subPath);
231   231  
232 ++stats->files; 232 ++stats->files;
233   233  
234 if(verbose) { 234 if(verbose) {
235 fprintf(stdout, 235 fprintf(stdout,
236 "Gathered %d directories and %d files.\r", 236 "Gathered %d directories and %d files.\r",
237 stats->dirs, 237 stats->dirs,
238 stats->files); 238 stats->files);
239 } 239 }
240   240  
241 free(subPath); 241 free(subPath);
242 } 242 }
243   243  
244 closedir(dir); 244 closedir(dir);
245 free(path); 245 free(path);
246 } 246 }
247   247  
248 if(verbose) { 248 if(verbose) {
249 fprintf(stdout, "\n"); 249 fprintf(stdout, "\n");
250 } 250 }
251   251  
252 fclose(fp); 252 fclose(fp);
253   253  
254 } 254 }
255   255  
256 /* 256 /*
257 * 257 *
258 * Gets the size of a database "dbFle". 258 * Gets the size of a database "dbFle".
259 */ 259 */
260 int GetDatabaseSize(char *dbFile) { 260 int GetDatabaseSize(char *dbFile) {
261 FILE *fp; 261 FILE *fp;
262 int size; 262 int size;
263   263  
264 if((fp = fopen(dbFile, "r")) == NULL) { 264 if((fp = fopen(dbFile, "r")) == NULL) {
265 fprintf(stderr, "Unable to open gather database for reading.\n"); 265 fprintf(stderr, "Unable to open gather database for reading.\n");
266 fclose(fp); 266 fclose(fp);
267 return 0; 267 return 0;
268 } 268 }
269   269  
270 fseek(fp, 0L, SEEK_END); 270 fseek(fp, 0L, SEEK_END);
271 size = ftell(fp); 271 size = ftell(fp);
272   272  
273 fclose(fp); 273 fclose(fp);
274 return size; 274 return size;
275 } 275 }
276   276  
277 /* 277 /*
278 * 278 *
279 * Counts the lines in a database file "dbFile". 279 * Counts the lines in a database file "dbFile".
280 */ 280 */
281 int CountDatabaseLines(char *dbFile) { 281 int CountDatabaseLines(char *dbFile) {
282 FILE *fp; 282 FILE *fp;
283 int lines; 283 int lines;
284 char c; 284 char c;
285   285  
286 if((fp = fopen(dbFile, "r")) == NULL) { 286 if((fp = fopen(dbFile, "r")) == NULL) {
287 fprintf(stderr, "Unable to open gather database for reading.\n"); 287 fprintf(stderr, "Unable to open gather database for reading.\n");
288 fclose(fp); 288 fclose(fp);
289 return 0; 289 return 0;
290 } 290 }
291   291  
292 lines = 0; 292 lines = 0;
293 while(fscanf(fp, "%c", &c) == 1) { 293 while(fscanf(fp, "%c", &c) == 1) {
294 switch(c) { 294 switch(c) {
295 case '\n': 295 case '\n':
296 ++lines; 296 ++lines;
297 break; 297 break;
298 } 298 }
299 } 299 }
300   300  
301 fclose(fp); 301 fclose(fp);
302   302  
303 return lines; 303 return lines;
304 } 304 }
305   305  
306 /* 306 /*
307 * 307 *
308 * Creates "files" temporary filenames. 308 * Creates "files" temporary filenames.
309 */ 309 */
310 char **CreateTempFiles(int files) { 310 char **CreateTempFiles(int files) {
311 char **tmpNames; 311 char **tmpNames;
312 int count; 312 int count;
313   313  
314 tmpNames = (char **) malloc(files * sizeof(char *)); 314 tmpNames = (char **) malloc(files * sizeof(char *));
315   315  
316 if(verbose) { 316 if(verbose) {
317 fprintf(stdout, "Creating temporary files.\r"); 317 fprintf(stdout, "Creating temporary files.\r");
318 } 318 }
319   319  
320 count = files; 320 count = files;
321 while(--count > -1) { 321 while(--count > -1) {
322 #if defined ___AmigaOS___ 322 #if defined ___AmigaOS___
323 // Check if CTRL+C was pressed and abort the program. 323 // Check if CTRL+C was pressed and abort the program.
324 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 324 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
325 run = FALSE; 325 run = FALSE;
326 } 326 }
327 #endif 327 #endif
328 tmpNames[count] = tmpnam(NULL); 328 tmpNames[count] = tmpnam(NULL);
329   329  
330 if(verbose) { 330 if(verbose) {
331 fprintf(stdout, "Creating temporary files: %d%%\r", 100 - (int)(((float)count / files) * 100.0)); 331 fprintf(stdout, "Creating temporary files: %d%%\r", 100 - (int)(((float)count / files) * 100.0));
332 } 332 }
333 } 333 }
334   334  
335 if(verbose) { 335 if(verbose) {
336 fprintf(stdout, "\n"); 336 fprintf(stdout, "\n");
337 } 337 }
338   338  
339 return tmpNames; 339 return tmpNames;
340 } 340 }
341   341  
342 /* 342 /*
343 * 343 *
344 * Writes lines from the database "dbFile" to temporary filenames "tmpNames". 344 * Writes lines from the database "dbFile" to temporary filenames "tmpNames".
345 */ 345 */
346 void WriteTempFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { 346 void WriteTempFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) {
347 FILE *fp, *tp; 347 FILE *fp, *tp;
348 char c; 348 char c;
349 int lines; 349 int lines;
350 int linesWritten; 350 int linesWritten;
351   351  
352 if((fp = fopen(dbFile, "r")) == NULL) { 352 if((fp = fopen(dbFile, "r")) == NULL) {
353 fprintf(stderr, "Unable to open gather database for reading.\n"); 353 fprintf(stderr, "Unable to open gather database for reading.\n");
354 return; 354 return;
355 } 355 }
356   356  
357 if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { 357 if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) {
358 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); 358 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]);
359 fclose(fp); 359 fclose(fp);
360 return; 360 return;
361 } 361 }
362   362  
363 if(verbose) { 363 if(verbose) {
364 fprintf(stdout, "Writing to temporary files.\r"); 364 fprintf(stdout, "Writing to temporary files.\r");
365 } 365 }
366   366  
367 linesWritten = 0; 367 linesWritten = 0;
368 lines = 0; 368 lines = 0;
369 while(run && fscanf(fp, "%c", &c) == 1) { 369 while(run && fscanf(fp, "%c", &c) == 1) {
370 #if defined ___AmigaOS___ 370 #if defined ___AmigaOS___
371 // Check if CTRL+C was pressed and abort the program. 371 // Check if CTRL+C was pressed and abort the program.
372 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 372 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
373 run = FALSE; 373 run = FALSE;
374 } 374 }
375 #endif 375 #endif
376 switch(c) { 376 switch(c) {
377 case '\n': 377 case '\n':
378 // Increment the total written lines. 378 // Increment the total written lines.
379 ++linesWritten; 379 ++linesWritten;
380   380  
381 if(verbose) { 381 if(verbose) {
382 fprintf(stdout, "Writing to temporary files: %d%%.\r", (int)(((float)linesWritten / total) * 100.0)); 382 fprintf(stdout, "Writing to temporary files: %d%%.\r", (int)(((float)linesWritten / total) * 100.0));
383 } 383 }
384   384  
385 // Write the newline character back. 385 // Write the newline character back.
386 if(fprintf(tp, "%c", c) != 1) { 386 if(fprintf(tp, "%c", c) != 1) {
387 fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); 387 fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]);
388 fclose(tp); 388 fclose(tp);
389 fclose(fp); 389 fclose(fp);
390 return; 390 return;
391 } 391 }
392 // Switch to the next temporary file. 392 // Switch to the next temporary file.
393 if(++lines >= tmpLines) { 393 if(++lines >= tmpLines) {
394 // If there are no temporary files left then run till the end. 394 // If there are no temporary files left then run till the end.
395 if(tmpFiles - 1 < 0) { 395 if(tmpFiles - 1 < 0) {
396 break; 396 break;
397 } 397 }
398   398  
399 // Close the previous temporary file and write to the next temporary file. 399 // Close the previous temporary file and write to the next temporary file.
400 fclose(tp); 400 fclose(tp);
401 if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { 401 if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) {
402 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); 402 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]);
403 fclose(tp); 403 fclose(tp);
404 fclose(fp); 404 fclose(fp);
405 } 405 }
406 lines = 0; 406 lines = 0;
407 break; 407 break;
408 } 408 }
409 break; 409 break;
410 default: 410 default:
411 if(fprintf(tp, "%c", c) != 1) { 411 if(fprintf(tp, "%c", c) != 1) {
412 fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); 412 fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]);
413 fclose(tp); 413 fclose(tp);
414 fclose(fp); 414 fclose(fp);
415 return; 415 return;
416 } 416 }
417 break; 417 break;
418 } 418 }
419 } 419 }
420   420  
421 fprintf(stdout, "\n"); 421 fprintf(stdout, "\n");
422   422  
423 fclose(tp); 423 fclose(tp);
424 fclose(fp); 424 fclose(fp);
425 } 425 }
426   426  
427 /* 427 /*
428 * 428 *
429 * Skips a line in a database file "fp". 429 * Skips a line in a database file "fp".
430 */ 430 */
431 void SkipDatabaseLine(FILE *fp) { 431 void SkipDatabaseLine(FILE *fp) {
432 char c; 432 char c;
433   433  
434 while(fscanf(fp, "%c", &c) == 1) { 434 while(fscanf(fp, "%c", &c) == 1) {
435 if(c == '\n') { 435 if(c == '\n') {
436 break; 436 break;
437 } 437 }
438 } 438 }
439   439  
440 return; 440 return;
441 } 441 }
442   442  
443 /* 443 /*
444 * 444 *
445 * Reads a line from the database file "fp". 445 * Reads a line from the database file "fp".
446 */ 446 */
447 char *ReadDatabaseLine(FILE *fp) { 447 char *ReadDatabaseLine(FILE *fp) {
448 char c; 448 char c;
449 char *line; 449 char *line;
450 int chars; 450 int chars;
451   451  
452 line = (char *) malloc(sizeof(char)); 452 line = (char *) malloc(sizeof(char));
453   453  
454 chars = 0; 454 chars = 0;
455 while(run && fscanf(fp, "%c", &c) == 1) { 455 while(run && fscanf(fp, "%c", &c) == 1) {
456 #if defined ___AmigaOS___ 456 #if defined ___AmigaOS___
457 // Check if CTRL+C was pressed and abort the program. 457 // Check if CTRL+C was pressed and abort the program.
458 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 458 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
459 run = FALSE; 459 run = FALSE;
460 } 460 }
461 #endif 461 #endif
462 switch(c) { 462 switch(c) {
463 case '\n': 463 case '\n':
464 // Rewind the file by the number of read characters. 464 // Rewind the file by the number of read characters.
465 fseek(fp, -(chars + 1), SEEK_CUR); 465 fseek(fp, -(chars + 1), SEEK_CUR);
466 return line; 466 return line;
467 default: 467 default:
468 line = (char *) realloc(line, (chars + 1 + 1) * sizeof(char)); 468 line = (char *) realloc(line, (chars + 1 + 1) * sizeof(char));
469 line[chars] = c; 469 line[chars] = c;
470 line[chars + 1] = '\0'; 470 line[chars + 1] = '\0';
471 break; 471 break;
472 } 472 }
473 ++chars; 473 ++chars;
474 } 474 }
475   475  
476 return NULL; 476 return NULL;
477 } 477 }
478   478  
479 /* 479 /*
480 * 480 *
481 * Merges temporary files "tmpNames" into a database "dbFile". 481 * Merges temporary files "tmpNames" into a database "dbFile".
482 */ 482 */
483 void MergeDatabase(char *dbFile, char **tmpNames, int files, int lines) { 483 void MergeDatabase(char *dbFile, char **tmpNames, int files, int lines) {
484 FILE *fp; 484 FILE *fp;
485 FILE **tp; 485 FILE **tp;
486 int i; 486 int i;
487 char *tmp; 487 char *tmp;
488 char *tmpMin; 488 char *tmpMin;
489 int idxMin; 489 int idxMin;
490 int count; 490 int count;
491   491  
492 if((fp = fopen(dbFile, "w+")) == NULL) { 492 if((fp = fopen(dbFile, "w+")) == NULL) {
493 fprintf(stderr, "Unable to open gather database for writing.\n"); 493 fprintf(stderr, "Unable to open gather database for writing.\n");
494 return; 494 return;
495 } 495 }
496   496  
497 // Allocate as many file pointers as temporary files. 497 // Allocate as many file pointers as temporary files.
498 tp = (FILE **) malloc(files * sizeof(FILE *)); 498 tp = (FILE **) malloc(files * sizeof(FILE *));
499   499  
500 // Open all temporary files for reading. 500 // Open all temporary files for reading.
501 for(i = 0; i < files; ++i) { 501 for(i = 0; i < files; ++i) {
502 if((tp[i] = fopen(tmpNames[i], "r")) == NULL) { 502 if((tp[i] = fopen(tmpNames[i], "r")) == NULL) {
503 fprintf(stderr, "Unable to open temporary file '%s' for reading.\n", tmpNames[i]); 503 fprintf(stderr, "Unable to open temporary file '%s' for reading.\n", tmpNames[i]);
504 // Close all temporary files. 504 // Close all temporary files.
505 --i; 505 --i;
506 while(i >= 0) { 506 while(i >= 0) {
507 fclose(tp[i]); 507 fclose(tp[i]);
508 } 508 }
509 return; 509 return;
510 } 510 }
511 } 511 }
512   512  
513 if(verbose) { 513 if(verbose) {
514 fprintf(stdout, "Merging all database lines in temporary files.\r"); 514 fprintf(stdout, "Merging all database lines in temporary files.\r");
515 } 515 }
516   516  
517 count = lines; 517 count = lines;
518 idxMin = 0; 518 idxMin = 0;
519 while(run && --count > -1) { 519 while(run && --count > -1) {
520 #if defined ___AmigaOS___ 520 #if defined ___AmigaOS___
521 // Check if CTRL+C was pressed and abort the program. 521 // Check if CTRL+C was pressed and abort the program.
522 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 522 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
523 run = FALSE; 523 run = FALSE;
524 } 524 }
525 #endif 525 #endif
526 // Find the smallest line in all temporary files. 526 // Find the smallest line in all temporary files.
527 if(verbose) { 527 if(verbose) {
528 fprintf(stdout, "Merging all database lines in temporary files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0)); 528 fprintf(stdout, "Merging all database lines in temporary files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0));
529 } 529 }
530   530  
531 tmpMin = NULL; 531 tmpMin = NULL;
532 for(i = 0; i < files; ++i) { 532 for(i = 0; i < files; ++i) {
533 tmp = ReadDatabaseLine(tp[i]); 533 tmp = ReadDatabaseLine(tp[i]);
534 if(tmp == NULL) { 534 if(tmp == NULL) {
535 free(tmp); 535 free(tmp);
536 continue; 536 continue;
537 } 537 }
538 if(tmpMin == NULL || strcmp(tmp, tmpMin) < 0) { 538 if(tmpMin == NULL || strcmp(tmp, tmpMin) < 0) {
539 if(tmpMin != NULL) { 539 if(tmpMin != NULL) {
540 // Free previous instance. 540 // Free previous instance.
541 free(tmpMin); 541 free(tmpMin);
542 } 542 }
543 tmpMin = (char *) malloc((strlen(tmp) + 1) * sizeof(char)); 543 tmpMin = (char *) malloc((strlen(tmp) + 1) * sizeof(char));
544 sprintf(tmpMin, "%s", tmp); 544 sprintf(tmpMin, "%s", tmp);
545 // Remember the index of the file where the smallest entry has been found. 545 // Remember the index of the file where the smallest entry has been found.
546 idxMin = i; 546 idxMin = i;
547 free(tmp); 547 free(tmp);
548 continue; 548 continue;
549 } 549 }
550 free(tmp); 550 free(tmp);
551 } 551 }
552   552  
553 // Forward the file where the smallest line was found. 553 // Forward the file where the smallest line was found.
554 SkipDatabaseLine(tp[idxMin]); 554 SkipDatabaseLine(tp[idxMin]);
555   555  
556 // Write the smallest line. 556 // Write the smallest line.
557 if(tmpMin != NULL) { 557 if(tmpMin != NULL) {
558 fprintf(fp, "%s\n", tmpMin); 558 fprintf(fp, "%s\n", tmpMin);
559 free(tmpMin); 559 free(tmpMin);
560 } 560 }
561 } 561 }
562   562  
563 // Write out any remaining contents from the temporary files. 563 // Write out any remaining contents from the temporary files.
564 for(i = 0; i < files; ++i) { 564 for(i = 0; i < files; ++i) {
565 tmp = ReadDatabaseLine(tp[i]); 565 tmp = ReadDatabaseLine(tp[i]);
566 if(tmp == NULL) { 566 if(tmp == NULL) {
567 continue; 567 continue;
568 } 568 }
569 fprintf(fp, "%s\n", tmp); 569 fprintf(fp, "%s\n", tmp);
570 } 570 }
571   571  
572 // Close and delete all temporary files. 572 // Close and delete all temporary files.
573 for(i = 0; i < files; ++i) { 573 for(i = 0; i < files; ++i) {
574 fclose(tp[i]); 574 fclose(tp[i]);
575 // Delete temporary file. 575 // Delete temporary file.
576 remove(tmpNames[i]); 576 remove(tmpNames[i]);
577 } 577 }
578   578  
579 if(verbose) { 579 if(verbose) {
580 fprintf(stdout, "\n"); 580 fprintf(stdout, "\n");
581 } 581 }
582   582  
583 fclose(fp); 583 fclose(fp);
584 } 584 }
585   585  
586 /* 586 /*
587 * 587 *
588 * Indexes a "path" by creating a database "dbFile". 588 * Indexes a "path" by creating a database "dbFile".
589 */ 589 */
590 void Gather(char *dbFile, char *path) { 590 void Gather(char *dbFile, char *path) {
591 stringStack *stack = stringStackCreate(1); 591 stringStack *stack = stringStackCreate(1);
592 stats *stats = malloc(sizeof(stats)); 592 stats *stats = malloc(sizeof(stats));
593 char **tmpNames; 593 char **tmpNames;
594 int dbSize, dbLines, tmpFiles, tmpLines; 594 int dbSize, dbLines, tmpFiles, tmpLines;
595 int i; 595 int i;
596   596  
597 // Initialize metrics. 597 // Initialize metrics.
598 stats->dirs = 0; 598 stats->dirs = 0;
599 stats->files = 0; 599 stats->files = 0;
600   600  
601 // Push the first path onto the stack. 601 // Push the first path onto the stack.
602 stringStackPush(stack, path); 602 stringStackPush(stack, path);
603   603  
604 // Generate the database file. 604 // Generate the database file.
605 UpdateDatabase(dbFile, stack, stats); 605 UpdateDatabase(dbFile, stack, stats);
606   606  
607 // Get the database metrics. 607 // Get the database metrics.
608 dbSize = GetDatabaseSize(dbFile); 608 dbSize = GetDatabaseSize(dbFile);
609 dbLines = CountDatabaseLines(dbFile); 609 dbLines = CountDatabaseLines(dbFile);
610   610  
611 // Compute the amount of temporary files needed. 611 // Compute the amount of temporary files needed.
612 tmpFiles = dbSize / MAX_MEM; 612 tmpFiles = dbSize / MAX_MEM;
613   613  
614 // In case no temporary files are required, 614 // In case no temporary files are required,
615 // just sort the database and terminate. 615 // just sort the database and terminate.
616 if(tmpFiles <= 1) { 616 if(tmpFiles <= 1) {
617 SortDatabase(dbFile); 617 SortDatabase(dbFile);
618 return; 618 return;
619 } 619 }
620   620  
621 tmpLines = dbLines / tmpFiles; 621 tmpLines = dbLines / tmpFiles;
622   622  
623 // Create temporary files. 623 // Create temporary files.
624 if((tmpNames = CreateTempFiles(tmpFiles)) == NULL) { 624 if((tmpNames = CreateTempFiles(tmpFiles)) == NULL) {
625 fprintf(stderr, "Unable to create temporary files.\n"); 625 fprintf(stderr, "Unable to create temporary files.\n");
626 return; 626 return;
627 } 627 }
628   628  
629 // Write "tmpLines" to temporary files in "tmpFiles" from "dbFile". 629 // Write "tmpLines" to temporary files in "tmpFiles" from "dbFile".
630 WriteTempFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines); 630 WriteTempFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines);
631   631  
632 // Sort the temporary files. 632 // Sort the temporary files.
633 for(i = 0; i < tmpFiles; ++i) { 633 for(i = 0; i < tmpFiles; ++i) {
634 SortDatabase(tmpNames[i]); 634 SortDatabase(tmpNames[i]);
635 } 635 }
636   636  
637 MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines); 637 MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines);
638 } 638 }
639   639  
640 /* 640 /*
641 * 641 *
642 * Main entry point. 642 * Main entry point.
643 */ 643 */
644 int main(int argc, char **argv) { 644 int main(int argc, char **argv) {
645 int option; 645 int option;
646 char *dbFile; 646 char *dbFile;
647 struct stat path; 647 struct stat dirStat;
648   648  
649 // Bind handler to SIGINT. 649 // Bind handler to SIGINT.
650 signal(SIGINT, SignalHandler); 650 signal(SIGINT, SignalHandler);
651   651  
652 dbFile = DEFAULT_DATABASE_FILE; 652 dbFile = DEFAULT_DATABASE_FILE;
653 while((option = getopt(argc, argv, "hqd:")) != -1) { 653 while((option = getopt(argc, argv, "hqd:")) != -1) {
654 switch(option) { 654 switch(option) {
655 case 'd': 655 case 'd':
656 dbFile = optarg; 656 dbFile = optarg;
657 break; 657 break;
658 case 'q': 658 case 'q':
659 verbose = FALSE; 659 verbose = FALSE;
660 break; 660 break;
661 case 'h': 661 case 'h':
662 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); 662 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]);
663 return 0; 663 return 0;
664 case '?': 664 case '?':
665 fprintf(stderr, "Invalid option %ct.\n", optopt); 665 fprintf(stderr, "Invalid option %ct.\n", optopt);
666 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); 666 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]);
667 return 1; 667 return 1;
668 } 668 }
669 } 669 }
-   670  
670   671  
671 if(optind > argc) { 672 if(optind >= argc) {
672 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); 673 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]);
673 return 1; 674 return 1;
674 } 675 }
675   676  
676 stat(argv[optind], &path); 677 stat(argv[optind], &dirStat);
677 if(!S_ISDIR(path.st_mode)) { 678 if(!S_ISDIR(dirStat.st_mode)) {
-   679 fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]);
678 fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); 680 fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]);
679 return 1; 681 return 1;
680 } 682 }
681   683  
682 if(verbose) { 684 if(verbose) {
683 fprintf(stdout, "Gathering to database file: %s\n", dbFile); 685 fprintf(stdout, "Gathering to database file: %s\n", dbFile);
684 } 686 }
685   687  
686 // Gather. 688 // Gather.
687 Gather(dbFile, argv[optind]); 689 Gather(dbFile, argv[optind]);
688   690  
689 return 0; 691 return 0;
690 } 692 }
691   693