HuntnGather

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ HEAD  →  ?path2? @ 1
/trunk/HuntnGather/Hunt/Hunt.c/Hunt.c
@@ -5,133 +5,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#if !defined ___AmigaOS___
#include <signal.h>
#include <dirent.h>
#include <sys/stat.h>
#endif
 
#include <sys/types.h>
#include <proto/dos.h>
#include <proto/exec.h>
 
#if defined ___AmigaOS___
 
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/utility.h>
#include "getopt.h"
#endif
 
#if defined ___AsyncIO___
#include <asyncio.h>
#if !defined TRUE
#define TRUE 1;
#endif
 
#if !defined ___HAVE_GETOPT___
#include "/shared/getopt.h"
#if !defined FALSE
#define FALSE 0;
#endif
 
#include "/shared/utilities.h"
 
#define PROGRAM_VERSION "1.7.7"
 
#if defined ___AmigaOS___
/*************************************************************************/
/* Version string used for querrying the program version. */
/*************************************************************************/
TEXT version_string[] =
"\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks";
#endif
"\0$VER: Hunt 1.2 "__DATE__" by Wizardry and Steamworks";
 
int PROGRAM_RUN = TRUE;
int PROGRAM_VERBOSE = FALSE;
int run = TRUE;
 
void SignalHandler(int sig) {
// Toggle the run flag to stop execution.
PROGRAM_RUN = FALSE;
run = FALSE;
}
 
/*
*
* Search the database for a matching string.
* Counts the lines in a database file "dbFile".
*/
void SearchDatabaseExact(char *dbFile, char *needle) {
#if defined ___AsyncIO___
struct AsyncFile *fp;
#else
int CountDatabaseLines(char *dbFile) {
FILE *fp;
#endif
dbEntry *entry;
dbLine *line = NULL;
int lines;
char c;
 
// Open database file for reading.
#if defined ___AsyncIO___
if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
return;
fprintf(stderr, "Unable to open gather database for reading.\n");
fclose(fp);
return 0;
}
 
#if defined ___NOCASE_FS___
StrUpr(needle);
#endif
 
while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
PROGRAM_RUN = FALSE;
continue;
lines = 0;
while(fscanf(fp, "%c", &c) == 1) {
switch(c) {
case '\n':
++lines;
break;
}
#endif
}
 
if((entry = CreateDatabaseEntry(line)) == NULL) {
fprintf(stderr, "Unable to create database entry.\n");
free(line->string);
free(line);
line = NULL;
fclose(fp);
 
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return;
}
return lines;
}
 
#if defined ___AmigaOS___
#if defined ___NOCASE_FS___
if(Strnicmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) {
#else
if(StrnCmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) {
#endif
#else
if(strstr(entry->name, needle) != NULL) {
#endif
fprintf(stdout, "%s\n", entry->path);
}
 
free(entry->name);
free(entry->path);
free(entry);
entry = NULL;
/*
* Convert string to uppercase.
*/
char *strupr(char *str) {
char *up;
int i;
 
free(line->string);
free(line);
line = NULL;
}
up = (char *) malloc((strlen(str) + 1) * sizeof(char));
sprintf(up, "%s", str);
 
if(line != NULL) {
free(line->string);
free(line);
line = NULL;
i = strlen(up);
while(--i > -1) {
up[i] = toupper(up[i]);
}
 
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return up;
}
 
/*
@@ -138,166 +88,118 @@
*
* Search the database for a matching string.
*/
#if defined ___AmigaOS___
void SearchDatabasePattern(char *dbFile, UBYTE *pattern) {
#if defined ___AsyncIO___
struct AsyncFile *fp;
#else
void SearchDatabase(char *dbFile, char* string, int lines) {
FILE *fp;
#endif
dbEntry *entry;
dbLine *line = NULL;
char *name;
char *path;
char c;
int i;
int side;
int match;
int total;
 
// Open database file for reading.
#if defined ___AsyncIO___
if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
fprintf(stderr, "Unable to open gather database for reading.\n");
return;
}
 
while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
name = (char *) malloc(sizeof(char));
path = (char *) malloc(sizeof(char));
i = 0;
side = 0;
match = FALSE;
total = 0;
while(run && fscanf(fp, "%c", &c) == 1) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
PROGRAM_RUN = FALSE;
continue;
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
break;
}
#endif
 
if((entry = CreateDatabaseEntry(line)) == NULL) {
fprintf(stderr, "Unable to create database entry.\n");
free(line->string);
free(line);
line = NULL;
 
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return;
switch(c) {
case '\n':
++total;
if(match) {
fprintf(stdout, "%s\n", path);
match = FALSE;
}
if(name != NULL) {
free(name);
name = (char *) malloc(sizeof(char));
}
--side;
i = 0;
break;
case '\t':
// Case insensitive match.
if(strstr(strupr(name), strupr(string)) != NULL) {
match = TRUE;
}
if(path != NULL) {
free(path);
path = (char *) malloc(sizeof(char));
}
++side;
i = 0;
break;
default:
switch(side) {
case 0:
name = (char *) realloc(name, (i + 1 + 1) * sizeof(char));
name[i] = c;
name[i + 1] = '\0';
break;
case 1:
path = (char *) realloc(path, (i + 1 + 1) * sizeof(char));
path[i] = c;
path[i + 1] = '\0';
break;
default:
fprintf(stderr, "Database corrupted.\n");
break;
}
++i;
break;
}
 
if(MatchPatternNoCase(pattern, entry->name)) {
fprintf(stdout, "%s\n", entry->path);
}
 
free(entry->name);
free(entry->path);
free(entry);
entry = NULL;
 
free(line->string);
free(line);
line = NULL;
}
 
if(line != NULL) {
free(line->string);
free(line);
line = NULL;
}
 
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
}
#endif
 
/*
*
* Search the database for the matching string.
*/
void Hunt(char *dbFile, char *needle) {
#if defined ___AmigaOS___
UBYTE *pattern;
ULONG size;
int lines;
 
#if defined ___NOCASE_FS___
StrUpr(needle);
#endif
lines = CountDatabaseLines(dbFile);
 
size = strlen(needle) * 2 + 2;
 
if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) {
switch(ParsePatternNoCase(needle, pattern, (LONG)size)) {
case 1: // the needle contains wildcards
SearchDatabasePattern(dbFile, pattern);
break;
case 0: // no wildcards contained in needle
SearchDatabaseExact(dbFile, needle);
break;
case -1: // overflow condition
fprintf(stderr, "Pattern '%s' could not be parsed.\n", needle);
break;
}
FreeVec(pattern);
pattern = NULL;
return;
}
#endif
 
// Search the database for the matching string.
SearchDatabaseExact(dbFile, needle);
SearchDatabase(dbFile, needle, lines);
}
 
void usage(char *name) {
fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name);
fprintf(stdout, "Version: %s \n", PROGRAM_VERSION);
fprintf(stdout, " \n");
fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name);
fprintf(stdout, " \n");
fprintf(stdout, " -d DATABASE A path to a database generated by the \n");
fprintf(stdout, " Gather tool that should be searched. \n");
fprintf(stdout, " \n");
fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n");
fprintf(stdout, " \n");
fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n");
}
 
int main(int argc, char **argv) {
int option;
char *dbFile;
 
// Bind handler to SIGINT.
#if !defined ___AmigaOS___
signal(SIGINT, SignalHandler);
#endif
 
dbFile = DEFAULT_DATABASE_FILE;
while((option = getopt(argc, argv, "hd:")) != -1) {
while((option = getopt(argc, argv, "h")) != -1) {
switch(option) {
case 'd':
dbFile = optarg;
case 'h':
fprintf(stdout, "SYNTAX: %s STRING", argv[0]);
break;
case 'h':
usage(argv[0]);
return 0;
case '?':
fprintf(stderr, "Invalid option %c.\n", optopt);;
return 5;
fprintf(stderr, "Invalid option %c.\n", optopt);
fprintf(stdout, "SYNTAX: %s STRING\n", argv[0]);
return 1;
}
}
 
if(optind >= argc) {
usage(argv[0]);
return 5;
if(optind > argc) {
fprintf(stdout, "SYNTAX: %s [-q] STRING\n", argv[0]);
return 1;
}
 
switch(GetFsType(dbFile)) {
case UNKNOWN:
case DIRECTORY:
fprintf(stderr, "'%s' is not a file.\n", dbFile);
return 10;
case REGULAR:
Hunt(dbFile, argv[optind]);
break;
}
Hunt("S:gather.db", argv[1]);
 
return 0;
}