/trunk/HuntnGather/Hunt/Hunt.c/Hunt.c |
@@ -5,9 +5,13 @@ |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <dirent.h> |
#include <signal.h> |
#include <ctype.h> |
|
#include <sys/types.h> |
#include <sys/stat.h> |
|
#include <proto/dos.h> |
#include <proto/exec.h> |
|
@@ -23,11 +27,13 @@ |
#define FALSE 0; |
#endif |
|
#define DEFAULT_DATABASE_FILE "S:gather.db" |
|
/*************************************************************************/ |
/* Version string used for querrying the program version. */ |
/*************************************************************************/ |
TEXT version_string[] = |
"\0$VER: Hunt 1.2 "__DATE__" by Wizardry and Steamworks"; |
"\0$VER: Hunt 1.3 "__DATE__" by Wizardry and Steamworks"; |
|
int run = TRUE; |
|
@@ -37,36 +43,6 @@ |
} |
|
/* |
* |
* Counts the lines in a database file "dbFile". |
*/ |
int CountDatabaseLines(char *dbFile) { |
FILE *fp; |
int lines; |
char c; |
|
if((fp = fopen(dbFile, "r")) == NULL) { |
fprintf(stderr, "Unable to open gather database for reading.\n"); |
fclose(fp); |
return 0; |
} |
|
lines = 0; |
while(fscanf(fp, "%c", &c) == 1) { |
switch(c) { |
case '\n': |
++lines; |
break; |
} |
} |
|
fclose(fp); |
|
return lines; |
} |
|
|
/* |
* Convert string to uppercase. |
*/ |
char *strupr(char *str) { |
@@ -88,7 +64,7 @@ |
* |
* Search the database for a matching string. |
*/ |
void SearchDatabase(char *dbFile, char* string, int lines) { |
void SearchDatabase(char *dbFile, char* string) { |
FILE *fp; |
char *name; |
char *path; |
@@ -113,6 +89,7 @@ |
#if defined ___AmigaOS___ |
// Check if CTRL+C was pressed and abort the program. |
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
run = FALSE; |
break; |
} |
#endif |
@@ -167,38 +144,50 @@ |
fclose(fp); |
} |
|
/* |
* |
* Search the database for the matching string. |
*/ |
void Hunt(char *dbFile, char *needle) { |
int lines; |
|
lines = CountDatabaseLines(dbFile); |
|
// Search the database for the matching string. |
SearchDatabase(dbFile, needle, lines); |
SearchDatabase(dbFile, needle); |
} |
|
int main(int argc, char **argv) { |
int option; |
char *dbFile; |
struct stat path; |
|
// Bind handler to SIGINT. |
signal(SIGINT, SignalHandler); |
|
while((option = getopt(argc, argv, "h")) != -1) { |
dbFile = DEFAULT_DATABASE_FILE; |
while((option = getopt(argc, argv, "hd:")) != -1) { |
switch(option) { |
case 'd': |
dbFile = optarg; |
break; |
case 'h': |
fprintf(stdout, "SYNTAX: %s STRING", argv[0]); |
fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]); |
break; |
case '?': |
fprintf(stderr, "Invalid option %c.\n", optopt); |
fprintf(stdout, "SYNTAX: %s STRING\n", argv[0]); |
fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]); |
return 1; |
} |
} |
|
if(optind > argc) { |
fprintf(stdout, "SYNTAX: %s [-q] STRING\n", argv[0]); |
fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]); |
return 1; |
} |
|
stat(dbFile, &path); |
if(!S_ISREG(path.st_mode)) { |
fprintf(stderr, "%s is not a file.\n", dbFile); |
return 1; |
} |
|
Hunt("S:gather.db", argv[1]); |
|
return 0; |