/trunk/HuntnGather/Gather/Gather.c |
@@ -10,6 +10,7 @@ |
|
#include <sys/types.h> |
#include <sys/stat.h> |
#include <sys/syslimits.h> |
|
#include <proto/dos.h> |
#include <proto/exec.h> |
@@ -20,12 +21,14 @@ |
#include "getopt.h" |
#endif |
|
#define PROGRAM_VERSION "1.7.2" |
|
#if defined ___AmigaOS___ |
/*************************************************************************/ |
/* Version string used for querrying the program version. */ |
/*************************************************************************/ |
TEXT version_string[] = |
"\0$VER: Gather 1.7 "__DATE__" by Wizardry and Steamworks"; |
"\0$VER: Gather " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
#endif |
|
#if !defined TRUE |
@@ -83,11 +86,11 @@ |
return; |
} |
|
database = malloc(sizeof(char *)); |
database = malloc(sizeof(*database)); |
name_size = NAME_BUF; |
name = malloc(name_size * sizeof(char)); |
name = malloc(name_size * sizeof(*name)); |
path_size = PATH_BUF; |
path = malloc(path_size * sizeof(char)); |
path = malloc(path_size * sizeof(*path)); |
line = 0; |
side = 0; |
i = 0; |
@@ -107,14 +110,14 @@ |
switch(c) { |
case '\n': |
// Load up the name and path into the database variable. |
database = realloc(database, (line + 1) * sizeof(char *)); |
database[line] = malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); |
database = realloc(database, (line + 1) * sizeof(*database)); |
database[line] = malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(*database[line])); |
sprintf(database[line], "%s\t%s", name, path); |
++line; |
|
free(name); |
name_size = NAME_BUF; |
name = malloc(name_size * sizeof(char)); |
name = malloc(name_size * sizeof(*name)); |
--side; |
i = 0; |
|
@@ -122,7 +125,7 @@ |
case '\t': |
free(path); |
path_size = PATH_BUF; |
path = malloc(path_size * sizeof(char)); |
path = malloc(path_size * sizeof(*path)); |
++side; |
i = 0; |
break; |
@@ -131,7 +134,7 @@ |
case 0: |
if(strlen(name) == name_size) { |
name_size = name_size * 1.5; |
name = realloc(name, name_size * sizeof(char)); |
name = realloc(name, name_size * sizeof(*name)); |
} |
//name = realloc(name, (i + 1 + 1) * sizeof(char)); |
name[i] = c; |
@@ -140,7 +143,7 @@ |
case 1: |
if(strlen(path) == path_size) { |
path_size = path_size * 1.5; |
path = realloc(path, path_size * sizeof(char)); |
path = realloc(path, path_size * sizeof(*path)); |
} |
//path = realloc(path, (i + 1 + 1) * sizeof(char)); |
path[i] = c; |
@@ -199,8 +202,7 @@ |
run = FALSE; |
} |
#endif |
if((path = stringStackPop(dirStack)) == NULL || |
strlen(path) == 0) { |
if((path = stringStackPop(dirStack)) == NULL) { |
return; |
} |
|
@@ -286,6 +288,7 @@ |
|
if((fp = fopen(dbFile, "r")) == NULL) { |
fprintf(stderr, "Unable to open gather database for reading.\n"); |
fclose(fp); |
return 0; |
} |
|
@@ -307,6 +310,7 @@ |
|
if((fp = fopen(dbFile, "r")) == NULL) { |
fprintf(stderr, "Unable to open gather database for reading.\n"); |
fclose(fp); |
return 0; |
} |
|
@@ -377,6 +381,7 @@ |
|
if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
fclose(fp); |
return; |
} |
|
@@ -405,6 +410,7 @@ |
// Write the newline character back. |
if(fprintf(tp, "%c", c) != 1) { |
fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
fclose(tp); |
fclose(fp); |
return; |
} |
@@ -419,6 +425,7 @@ |
fclose(tp); |
if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
fclose(tp); |
fclose(fp); |
} |
lines = 0; |
@@ -469,7 +476,7 @@ |
int i; |
|
line_size = LINE_BUF; |
line = malloc(line_size * sizeof(char)); |
line = malloc(line_size * sizeof(*line)); |
|
i = 0; |
while(run && fscanf(fp, "%c", &c) == 1) { |
@@ -487,7 +494,7 @@ |
default: |
if(strlen(line) == line_size) { |
line_size = line_size * 1.5; |
line = realloc(line, line_size * sizeof(char)); |
line = realloc(line, line_size * sizeof(*line)); |
} |
//line = realloc(line, (chars + 1 + 1) * sizeof(char)); |
line[i] = c; |
@@ -519,7 +526,7 @@ |
} |
|
// Allocate as many file pointers as temporary files. |
tp = malloc(files * sizeof(FILE *)); |
tp = malloc(files * sizeof(*tp)); |
|
// Open all temporary files for reading. |
for(i = 0; i < files; ++i) { |
@@ -563,7 +570,7 @@ |
// Free previous instance. |
free(min); |
} |
min = malloc((strlen(tmp) + 1) * sizeof(char)); |
min = malloc((strlen(tmp) + 1) * sizeof(*min)); |
sprintf(min, "%s", tmp); |
// Remember the index of the file where the smallest entry has been found. |
j = i; |
@@ -663,6 +670,7 @@ |
|
void usage(char *name) { |
fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
fprintf(stdout, " \n"); |
fprintf(stdout, "SYNTAX: %s [-q] DATABASE \n", name); |
fprintf(stdout, " \n"); |
@@ -681,7 +689,11 @@ |
int main(int argc, char **argv) { |
int option; |
char *dbFile; |
char *path; |
struct stat dirStat; |
#if defined ___AmigaOS___ |
BPTR lock; |
#endif |
|
// Bind handler to SIGINT. |
signal(SIGINT, SignalHandler); |
@@ -710,7 +722,16 @@ |
return 1; |
} |
|
stat(argv[optind], &dirStat); |
#if defined ___AmigaOS___ |
path = malloc(PATH_MAX * sizeof(*path)); |
lock = Lock(argv[optind], SHARED_LOCK); |
NameFromLock(lock, path, PATH_MAX); |
UnLock(lock); |
#else |
path = realpath(argv[optind], NULL); |
#endif |
|
stat(path, &dirStat); |
if(!S_ISDIR(dirStat.st_mode)) { |
fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
return 1; |
@@ -721,7 +742,9 @@ |
} |
|
// Gather. |
Gather(dbFile, argv[optind]); |
Gather(dbFile, path); |
|
free(path); |
|
return 0; |
} |