/trunk/HuntnGather/Gather/Gather.c |
@@ -796,14 +796,20 @@ |
#else |
FILE *fp; |
#endif |
stringStack *stack; |
stats *stats; |
#if defined ___AmigaOS___ |
struct FileInfoBlock *FIBp, *FIBq; |
BPTR lockp, lockq; |
#else |
DIR *dir; |
struct dirent *entry; |
#endif |
struct stat dirStat; |
|
stringStack *stack; |
stats *stats; |
int i; |
char *path; |
char *subPath; |
char *sub; |
|
// Initialize metrics. |
if((stats = malloc(sizeof(stats))) == NULL) { |
@@ -860,25 +866,59 @@ |
break; |
} |
|
if((dir = opendir(path)) == NULL) { |
fprintf(stderr, "Unable to open '%s' for reading.\n", path); |
break; |
#if defined ___AmigaOS___ |
if((lockp = Lock(path, ACCESS_READ)) == NULL) { |
fprintf(stderr, "Could not lock path '%s' for reading.\n", path); |
free(path); |
continue; |
} |
|
while(run && (entry = readdir(dir)) != NULL) { |
#if defined ___AmigaOS___ |
if((FIBp = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL)) == NULL) { |
fprintf(stderr, "Path '%s' info block allocation failure.\n", path); |
UnLock(lockp); |
free(path); |
continue; |
} |
|
if(Examine(lockp, FIBp) == FALSE) { |
fprintf(stderr, "Path '%s' could not be examined.\n", path); |
FreeDosObject(DOS_FIB, FIBp); |
UnLock(lockp); |
free(path); |
continue; |
} |
|
while(run && ExNext(lockp, FIBp)) { |
// Check if CTRL+C was pressed and abort the program. |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
run = FALSE; |
continue; |
} |
#else |
|
if((dir = opendir(path)) == NULL) { |
fprintf(stderr, "Unable to open '%s' for reading.\n", path); |
free(path); |
continue; |
} |
|
while(run && (entry = readdir(dir)) != NULL) { |
#endif |
switch(path[strlen(path) - 1]) { |
case '/': |
case ':': // This is a drive path. |
if((subPath = malloc(sizeof(path) + sizeof(entry->d_name) + 1)) == NULL) { |
#if defined ___AmigaOS___ |
if((sub = malloc(sizeof(path) + sizeof(FIBp->fib_FileName) + 1)) == NULL) { |
#else |
if((sub = malloc(sizeof(path) + sizeof(entry->d_name) + 1)) == NULL) { |
#endif |
fprintf(stderr, "Memory allocation failure.\n"); |
#if defined ___AmigaOS___ |
FreeDosObject(DOS_FIB, FIBp); |
UnLock(lockp); |
#else |
closedir(dir); |
#endif |
free(path); |
stringStackDestroy(stack); |
#if defined ___AsyncIO___ |
@@ -888,12 +928,25 @@ |
#endif |
return NULL; |
} |
sprintf(subPath, "%s%s", path, entry->d_name); |
#if defined ___AmigaOS___ |
sprintf(sub, "%s%s", path, FIBp->fib_FileName); |
#else |
sprintf(sub, "%s%s", path, entry->d_name); |
#endif |
break; |
default: |
if((subPath = malloc(sizeof(path) + sizeof(entry->d_name) + 1 + 1)) == NULL) { |
#if defined ___AmigaOS___ |
if((sub = malloc(sizeof(path) + sizeof(FIBp->fib_FileName) + 1 + 1)) == NULL) { |
#else |
if((sub = malloc(sizeof(path) + sizeof(entry->d_name) + 1 + 1)) == NULL) { |
#endif |
fprintf(stderr, "Memory allocation failure.\n"); |
#if defined ___AmigaOS___ |
FreeDosObject(DOS_FIB, FIBp); |
UnLock(lockp); |
#else |
closedir(dir); |
#endif |
free(path); |
stringStackDestroy(stack); |
#if defined ___AsyncIO___ |
@@ -903,12 +956,42 @@ |
#endif |
return NULL; |
} |
sprintf(subPath, "%s/%s", path, entry->d_name); |
#if defined ___AmigaOS___ |
sprintf(sub, "%s/%s", path, FIBp->fib_FileName); |
#else |
sprintf(sub, "%s/%s", path, entry->d_name); |
#endif |
break; |
} |
stat(subPath, &dirStat); |
|
#if defined ___AmigaOS___ |
if((lockq = Lock(sub, ACCESS_READ)) == NULL) { |
fprintf(stderr, "Could not lock path '%s' for reading.\n", sub); |
free(sub); |
continue; |
} |
|
if((FIBq = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL)) == NULL) { |
fprintf(stderr, "Path '%s' info block allocation failure.\n", sub); |
UnLock(lockq); |
free(sub); |
continue; |
} |
|
if(Examine(lockq, FIBq) == FALSE) { |
fprintf(stderr, "Path '%s' could not be examined.\n", sub); |
FreeDosObject(DOS_FIB, FIBq); |
UnLock(lockq); |
free(sub); |
continue; |
} |
|
if(FIBq->fib_DirEntryType > 0) { |
#else |
stat(sub, &dirStat); |
if(S_ISDIR(dirStat.st_mode)) { |
stringStackPush(stack, subPath); |
#endif |
stringStackPush(stack, sub); |
|
++stats->dirs; |
|
@@ -919,22 +1002,43 @@ |
stats->files); |
} |
|
free(subPath); |
#if defined ___AmigaOS___ |
FreeDosObject(DOS_FIB, FIBq); |
UnLock(lockq); |
#endif |
free(sub); |
continue; |
} |
|
#if defined ___AmigaOS___ |
FreeDosObject(DOS_FIB, FIBq); |
UnLock(lockq); |
#endif |
|
#if defined ___NOCASE_FS___ |
#if defined ___AmigaOS___ |
strupr(FIBp->fib_FileName); |
#else |
strupr(entry->d_name); |
#endif |
#endif |
// Write to database file. |
#if defined ___AsyncIO___ |
#if defined ___AmigaOS___ |
WriteAsync(fp, FIBp->fib_FileName, (LONG)strlen(FIBp->fib_FileName)); |
#else |
WriteAsync(fp, entry->d_name, (LONG)strlen(entry->d_name)); |
#endif |
WriteAsync(fp, "\t", 1); |
WriteAsync(fp, subPath, (LONG)strlen(subPath)); |
WriteAsync(fp, sub, (LONG)strlen(sub)); |
WriteAsync(fp, "\n", 1); |
#else |
fprintf(fp, "%s\t%s\n", entry->d_name, subPath); |
#if defined ___AmigaOS___ |
fprintf(fp, "%s\t%s\n", FIBp->fib_FileName, sub); |
#else |
fprintf(fp, "%s\t%s\n", entry->d_name, sub); |
#endif |
#endif |
++stats->files; |
|
if(verbose) { |
@@ -944,10 +1048,15 @@ |
stats->files); |
} |
|
free(subPath); |
free(sub); |
} |
|
#if defined ___AmigaOS___ |
FreeDosObject(DOS_FIB, FIBp); |
UnLock(lockp); |
#else |
closedir(dir); |
#endif |
free(path); |
} |
|
@@ -1240,7 +1349,7 @@ |
#if defined ___AmigaOS___ |
if(StrnCmp(locale, min, rem, -1, SC_ASCII) == 0) { |
#else |
if(strcmp(min, rem) == 0 { |
if(strcmp(min, rem) == 0) { |
#endif |
free(min); |
continue; |
@@ -1515,6 +1624,12 @@ |
* Main entry point. |
*/ |
int main(int argc, char **argv) { |
#if defined ___AmigaOS___ |
struct FileInfoBlock *FIB; |
BPTR lock; |
#else |
struct stat dirStat; |
#endif |
int option; |
unsigned int i; |
unsigned int count; |
@@ -1521,7 +1636,6 @@ |
char *dbFile; |
char *path; |
char **paths; |
struct stat dirStat; |
|
// Bind handler to SIGINT. |
#if !defined ___AmigaOS___ |
@@ -1574,15 +1688,44 @@ |
return 1; |
} |
for(i = optind, count = 0; i < argc; ++i, ++count) { |
if((path = PathToAbsolute(argv[optind])) == NULL) { |
if((path = PathToAbsolute(argv[i])) == NULL) { |
fprintf(stderr, "Absolute path for '%s' failed to resolve.\n", argv[optind]); |
continue; |
} |
|
// Check that the path is a directory. |
#if defined ___AmigaOS___ |
if((lock = Lock(path, ACCESS_READ)) == NULL) { |
fprintf(stderr, "Path '%s' is not accessible.\n", path); |
free(path); |
continue; |
} |
|
if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) { |
fprintf(stderr, "Path '%s' file information block not accessible.\n", path); |
UnLock(lock); |
free(path); |
continue; |
} |
|
if(Examine(lock, FIB) == FALSE) { |
fprintf(stderr, "Path '%s' information unexaminable.\n", path); |
UnLock(lock); |
FreeDosObject(DOS_FIB, FIB); |
free(path); |
continue; |
} |
|
if(FIB->fib_DirEntryType < 0) { |
#else |
stat(path, &dirStat); |
if(!S_ISDIR(dirStat.st_mode)) { |
#endif |
fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
#if defined ___AmigaOS___ |
UnLock(lock); |
FreeDosObject(DOS_FIB, FIB); |
#endif |
free(path); |
return 1; |
} |
@@ -1596,8 +1739,15 @@ |
fprintf(stderr, "Memory allocation failure."); |
return 1; |
} |
|
sprintf(paths[count], "%s", path); |
|
#if defined ___AmigaOS___ |
UnLock(lock); |
FreeDosObject(DOS_FIB, FIB); |
#endif |
free(path); |
|
} |
|
if(verbose) { |