/trunk/HuntnGather/Gather/Gather.c |
@@ -128,7 +128,12 @@ |
} |
UnLock(lock); |
#else |
abs = realpath(path, NULL); |
//abs = realpath(path, NULL); |
if((abs = malloc((strlen(path) + 1) * sizeof(*abs))) == NULL) { |
fprintf(stderr, "Memory allocation failure.\n"); |
return NULL; |
} |
sprintf(abs, "%s", path); |
#endif |
|
return abs; |
@@ -180,7 +185,7 @@ |
#if defined ___AsyncIO___ |
if(SeekAsync(fp, 0, MODE_END) == -1) { |
#else |
if(fseek(fp, 0L, SEEK_END) == 0) { |
if(fseek(fp, 0L, SEEK_END) != 0) { |
#endif |
fprintf(stderr, "Seek in file %s failed.\n", dbFile); |
#if defined ___AsyncIO___ |
@@ -233,6 +238,10 @@ |
} |
|
lines = 0; |
if(verbose) { |
fprintf(stdout, "Lines in '%s' so far: %d\r", dbFile, lines); |
} |
|
#if defined ___AsyncIO___ |
while(run && (c = ReadCharAsync(fp)) != -1) { |
#else |
@@ -248,6 +257,10 @@ |
switch(c) { |
case '\n': |
++lines; |
|
if(verbose) { |
fprintf(stdout, "Lines in '%s' so far: %d\r", dbFile, lines); |
} |
break; |
} |
} |
@@ -258,6 +271,10 @@ |
fclose(fp); |
#endif |
|
if(verbose) { |
fprintf(stdout, "\n"); |
} |
|
return lines; |
} |
|
@@ -385,7 +402,11 @@ |
return NULL; |
} |
#else |
fseek(fp, -(i + 1), SEEK_CUR); |
if(fseek(fp, -(i + 1), SEEK_CUR) != 0) { |
fprintf(stderr, "Could not seek in file.\n"); |
free(line); |
return NULL; |
} |
#endif |
return line; |
default: |
@@ -498,7 +519,6 @@ |
char c; |
#endif |
|
|
// Open database file for writing. |
#if defined ___AsyncIO___ |
if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) { |
@@ -513,7 +533,7 @@ |
#if defined ___AsyncIO___ |
if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) { |
#else |
if((bp = fopen(b, "w+")) == NULL) { |
if((bp = fopen(b, "w")) == NULL) { |
#endif |
fprintf(stderr, "Unable to open file '%s' for writing.\n", b); |
|
@@ -534,7 +554,7 @@ |
#endif |
#if defined ___AmigaOS___ |
// Check if CTRL+C was pressed and abort the program. |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
run = FALSE; |
continue; |
} |
@@ -568,7 +588,7 @@ |
#else |
FILE *fp; |
#endif |
int i; |
unsigned int i; |
char *rem; |
|
// Write the database lines back to the database. |
@@ -585,7 +605,7 @@ |
for(i = 0; i < count; ++i) { |
#if defined ___AmigaOS___ |
// Check if CTRL+C was pressed and abort the program. |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
run = FALSE; |
continue; |
} |
@@ -746,7 +766,11 @@ |
sprintf(array->database[count], "%s\t%s", entry->name, entry->path); |
++count; |
|
// Free the database entry. |
free(entry->name); |
free(entry->path); |
free(entry); |
|
free(line); |
} |
|
@@ -766,6 +790,7 @@ |
*/ |
void SortDatabase(char *dbFile) { |
dbArray *array; |
int i; |
|
if(verbose) { |
fprintf(stdout, "Sorting '%s'...\n", dbFile); |
@@ -783,6 +808,11 @@ |
// Write back the database to the database file. |
WriteLinesToFile(dbFile, array->database, array->count); |
|
// Deallocate all the lines. |
for(i = 0; i < array->count; ++i) { |
free(array->database[i]); |
} |
|
free(array); |
} |
|
@@ -811,7 +841,7 @@ |
char *sub; |
|
// Initialize metrics. |
if((stats = malloc(sizeof(stats))) == NULL) { |
if((stats = malloc(sizeof(*stats))) == NULL) { |
fprintf(stderr, "Memory allocation failure.\n"); |
return NULL; |
} |
@@ -822,27 +852,12 @@ |
#if defined ___AsyncIO___ |
if((fp = OpenAsync(dbFile, MODE_APPEND, ASYNC_BUF)) == NULL) { |
#else |
if((fp = fopen(dbFile, "r+")) == NULL) { |
if((fp = fopen(dbFile, "a")) == NULL) { |
#endif |
fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile); |
return stats; |
} |
|
// Seek to the end of the database. |
#if defined ___AsyncIO___ |
if(SeekAsync(fp, 0, MODE_END) == -1) { |
#else |
if(fseek(fp, 0, SEEK_END) == 0) { |
#endif |
fprintf(stderr, "Unable to seek in '%s' for appending.\n", dbFile); |
#if defined ___AsyncIO___ |
CloseAsync(fp); |
#else |
fclose(fp); |
#endif |
return stats; |
} |
|
if(verbose) { |
fprintf(stdout, "Collecting files...\r"); |
} |
@@ -872,7 +887,7 @@ |
continue; |
} |
|
if((FIBp = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL)) == NULL) { |
if((FIBp = AllocDosObject(DOS_FIB, NULL)) == NULL) { |
fprintf(stderr, "Path '%s' info block allocation failure.\n", path); |
UnLock(lockp); |
free(path); |
@@ -891,7 +906,10 @@ |
// Check if CTRL+C was pressed and abort the program. |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
run = FALSE; |
continue; |
FreeDosObject(DOS_FIB, FIBp); |
UnLock(lockp); |
free(path); |
break; |
} |
#else |
|
@@ -907,9 +925,9 @@ |
case '/': |
case ':': // This is a drive path. |
#if defined ___AmigaOS___ |
if((sub = malloc(sizeof(path) + sizeof(FIBp->fib_FileName) + 1)) == NULL) { |
if((sub = malloc(strlen(path) + strlen(FIBp->fib_FileName) + 1)) == NULL) { |
#else |
if((sub = malloc(sizeof(path) + sizeof(entry->d_name) + 1)) == NULL) { |
if((sub = malloc(strlen(path) + strlen(entry->d_name) + 1)) == NULL) { |
#endif |
fprintf(stderr, "Memory allocation failure.\n"); |
#if defined ___AmigaOS___ |
@@ -935,9 +953,9 @@ |
break; |
default: |
#if defined ___AmigaOS___ |
if((sub = malloc(sizeof(path) + sizeof(FIBp->fib_FileName) + 1 + 1)) == NULL) { |
if((sub = malloc(strlen(path) + strlen(FIBp->fib_FileName) + 1 + 1)) == NULL) { |
#else |
if((sub = malloc(sizeof(path) + sizeof(entry->d_name) + 1 + 1)) == NULL) { |
if((sub = malloc(strlen(path) + strlen(entry->d_name) + 1 + 1)) == NULL) { |
#endif |
fprintf(stderr, "Memory allocation failure.\n"); |
#if defined ___AmigaOS___ |
@@ -963,6 +981,7 @@ |
break; |
} |
|
|
#if defined ___AmigaOS___ |
if((lockq = Lock(sub, ACCESS_READ)) == NULL) { |
fprintf(stderr, "Could not lock path '%s' for reading.\n", sub); |
@@ -970,7 +989,7 @@ |
continue; |
} |
|
if((FIBq = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL)) == NULL) { |
if((FIBq = AllocDosObject(DOS_FIB, NULL)) == NULL) { |
fprintf(stderr, "Path '%s' info block allocation failure.\n", sub); |
UnLock(lockq); |
free(sub); |
@@ -1014,6 +1033,15 @@ |
UnLock(lockq); |
#endif |
|
++stats->files; |
|
if(verbose) { |
fprintf(stdout, |
"Gathered %d directories and %d files.\r", |
stats->dirs, |
stats->files); |
} |
|
#if defined ___NOCASE_FS___ |
#if defined ___AmigaOS___ |
strupr(FIBp->fib_FileName); |
@@ -1021,6 +1049,8 @@ |
strupr(entry->d_name); |
#endif |
#endif |
|
|
// Write to database file. |
#if defined ___AsyncIO___ |
#if defined ___AmigaOS___ |
@@ -1038,15 +1068,6 @@ |
fprintf(fp, "%s\t%s\n", entry->d_name, sub); |
#endif |
#endif |
++stats->files; |
|
if(verbose) { |
fprintf(stdout, |
"Gathered %d directories and %d files.\r", |
stats->dirs, |
stats->files); |
} |
|
free(sub); |
} |
|
@@ -1734,7 +1755,7 @@ |
} |
|
// Add the path to the array of paths. |
if((paths[count] = malloc(strlen(path) * sizeof(*paths[count]))) == NULL) { |
if((paths[count] = malloc((strlen(path) + 1) * sizeof(*paths[count]))) == NULL) { |
fprintf(stderr, "Memory allocation failure."); |
return 1; |
} |