示例#1
0
文件: file.c 项目: simpleigh/hawser
void
config_load_file(const char *szFilename)
{
	FILE *pStream = fopen(szFilename, "r");
	char szLine[LINE_BUFFER];
	FileEntry *pFileEntry;
	int result;
	regmatch_t rgMatches[2];

	printf("Loading %s\n", szFilename);

	if (!pStream) {
		return;
	}

	compile_regexes(FILE_ENTRIES);

	while (fgets(szLine, LINE_BUFFER, pStream)) {
		pFileEntry = FILE_ENTRIES;
		while (pFileEntry->szParameter) {
			result = regexec(
				pFileEntry->pRegexCompiled, /* preg   */
				szLine,                     /* string */
				2,                          /* nmatch */
				rgMatches,                  /* pmatch */
				0                           /* eflags */
			);

			if (result == 0) {
				/* Insert null at end of match. */
				szLine[rgMatches[1].rm_eo] = '\0';
				pFileEntry->store(szLine + rgMatches[1].rm_so);
			}

			pFileEntry++;
		}
	}

	free_regexes(FILE_ENTRIES);

	fclose(pStream);
}
示例#2
0
int main(int argc, char **argv) {

	int c;

	bzero(CONFIG_PATH, PATH_MAX);

	while ((c = getopt(argc,argv, "vf:")) != EOF) {
		switch (c) {
			case 'f':
				snprintf(CONFIG_PATH, PATH_MAX, "%s", optarg);
				break;
			case 'v':
				verbose = 1;
				break;
			case '?':
				fprintf(stderr,"Invalid argument\n");
				exit(1);
		}
	}

	read_config();
#ifdef HAVE_LIBMAGIC
	load_magic();
#endif
	load_regexes();

	pcre_callout = pcre_callout_spider;

	umask(077);

	compile_regexes();

	if (Encrypt) {
		// do we have a password?
		if (!strlen(Password)) {
			fprintf(stderr, "No password supplied\n");
			exit(1);
		}
		// prep the log array
		startlog = (struct logarray *)malloc(sizeof(struct logarray));
		if (startlog == NULL) {
			fprintf(stderr, "Malloc: %s\n", strerror(errno));
			exit(1);
		}
		bzero(startlog -> entry, LOG_MAX);
		currlog = startlog;
		// prep our keying material
		generate_iv();
		generate_key();
	}

	if (Log2File && !LogStdout) {
		// get our filehandle open
		if (AppendLog) {
			if (CustomLogPath[0]) {
				logfp = fopen(CustomLogPath, "a+");
			} else {
				logfp = fopen(LogPath, "a+");
			}
		} else {
			if (CustomLogPath[0]) {
				logfp = fopen(CustomLogPath, "w");
			} else {
				logfp = fopen(LogPath, "w");
			}
		}
		if (logfp == NULL) {
			fprintf(stderr, "unable to open %s:%s\n",
					LogPath,
					strerror(errno));
			exit(1);
		}
	}

	if (LogSyslog) {
		(void)openlog("spider", LOG_PID|LOG_NDELAY, LOG_FAC);
	}

	
	// normally, this would run as a child process
	// we'd hold the child PID in pid_t worker
	// when the Stop Spider button is clicked, 
	// we'll send a TERM signal to the child process
	//
	run_spider(START_PATH);

	if (LogFooter[0]) {
		write_footer();
	}

	if (Encrypt) {
		// write out the encrypted log
		if (!spider_encrypt()) {
			fprintf(stderr, "failure to write encrypted log!\n");
			exit(1);
		}
	}

	if (logfp) { fclose(logfp); }

	if (WhenDone == EXIT_WHEN_DONE) {
		if (verbose) {
			fprintf(stderr, "Normal exit.\n");
		}
		exit(0);
	}
	if (WhenDone == RESTORE_WHEN_DONE) {
		// GTK fru-fru
	}
	if (WhenDone == VIEWLOG_WHEN_DONE) {
		// launch the log viewer
	}

	exit(0);
}