www.pudn.com > fsch.zip > fsch.c


/**
 * Title: cploader.c
 * Type: C (*.c)
 * Complier: gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)
 * Description: send file to armboard's flash
 * Copyright: Copyright (c) 2004
 * Company: djws
 * author djws
 * version 1.0
 */

 /* search the file or directory's content */

#include 
#include 
#include 

#include 
#include 

#define PRINT_VERSION printf("version 1.0\n");

long flen(FILE *fp);
void print_help();
void default_init(char *filename, char *topic, char *schstr);
void myerror(char *str);
void file_search(char *filename, char *str, char *topic);
void do_search(char *filename, char *topic, char *str);

int count;
//int showcount;
//char show[5] = "/-\\-";

long
flen(FILE *fp) {
	int i = 0;
	if(fp == NULL)
		return 0;
	if(fseek(fp, 0, SEEK_SET))
		return 0;
	while(!feof(fp)) {
		i++;
		fgetc(fp);
	}
	if(fseek(fp, 0, SEEK_SET))
		return 0;
	return i-1;
}

void
print_help() {
	printf("Example: fsch [options]... [parameter]...\n");
	printf("Options: \n");
	printf("               -f, --filename  [PATH]    set your search path (or file's name), default is '/root'\n");
	printf("               -t, --topic     [KEY]     set the key which files' name include, default is empty\n");
	printf("               -s, --string    [string]  set the string your want to search in the file, default is 'DJWS'\n");
	printf("               -h, --help                print help\n");
	printf("               -v, --version             print version\n");
	printf("Finished.\n");
}

void
default_init(char *filename, char *topic, char *schstr) {
	strcpy(filename, "/root");
	strcpy(topic, "");
	strcat(schstr, "DJWS");
}

void
myerror(char *str) {
	printf("%s\n", str);
	exit(0);
}

void
file_search(char *filename, char *str, char *topic) {
	FILE *fp = NULL;
	long len = 0;
	char *buf = NULL;
	int i, j, k;
	char tmp[1024];
	if(strstr(filename, topic) == NULL) {
		//printf("! filename=%s topic=%s\n", filename, topic);
		strcpy(tmp, filename);
		return;
	}
	//l = strlen(filename)+12;
	//printf("Check file: %s\n", filename);
	if((fp=fopen(filename, "rb")) == NULL) {
		printf("fail to open the file: %s\n", filename);
		return;
	}
	fseek(fp, 0, SEEK_SET);
	len = flen(fp);
	if(len < strlen(str)) {
		fclose(fp);
		return;
	}
	buf = (char *)malloc(2048);
	while(len > 2048) {
		fread((void *)buf, 2048, 1, fp);
		for(i=0;i<1024;i++) {
			for(j=0;jd_name);
			if((strcmp(dirp->d_name, ".")==0) || (strcmp(dirp->d_name, "..")==0)) {
			strcpy(tmp, filename);
			continue;
			}
			/*if(strstr(tmp, topic) == NULL) {
				strcpy(tmp, filename);
				continue;
			}*/
			if(stat(tmp, &filestat) < 0)
				myerror("Stat Read Error.\n");
			if(S_ISDIR(filestat.st_mode)) {
				do_search(tmp, topic, str);
			}
			else if(S_ISREG(filestat.st_mode)) {
				file_search(tmp, str, topic);
			}
			strcpy(tmp, filename);
		}
		closedir(dp);
	}
	else {
		printf("File type error, must be a file or a directory!\n");
		return;
	}
}


int
main(int argc, char **argv) {
	char cmd[1024];
	char param[1024];
	int i = 1;
	int readparam = 0;

	char topic[1024];
	char filename[1024];
	char schstr[1024];
	default_init(filename, topic, schstr);
	while(i < argc) {
		i++;
		if(strlen(argv[i-1]) > 1023) {
			printf("Too long Parameter, Error.\n");
			print_help();
			exit(0);
		}
		if(readparam == 1) {
			strcpy(param, argv[i-1]);
			if(param[0] == '-') {
				printf("Parameter Error.\n");
				print_help();
				exit(0);
			}
			if((strcmp(cmd, "--string")==0) || (strcmp(cmd, "-s")==0)) {
				strcpy(schstr, param);
			}
			else if((strcmp(cmd, "--filename")==0) || (strcmp(cmd, "-f")==0)) {
				strcpy(filename, param);
			}
			else if((strcmp(cmd, "--topic")==0) || (strcmp(cmd, "-t")==0)) {
				strcpy(topic, param);
			}
			else {
				printf("Parameter Error.\n");
				print_help();
				exit(0);
			}
			readparam = 0;
		}
		else {
			strcpy(cmd, argv[i-1]);
			if(strlen(cmd) <= 1) {
				printf("Parameter Error.\n");
				print_help();
				exit(0);
			}
			if(cmd[0] != '-') {
				printf("Parameter Error.\n");
				print_help();
				exit(0);
			}
			if((strcmp(cmd, "--help")==0) || (strcmp(cmd, "-h")==0)) {
				print_help();
				exit(0);
			}
			if((strcmp(cmd, "-version")==0) || (strcmp(cmd, "-v")==0)) {
				PRINT_VERSION
				exit(0);
			}
			readparam = 1;
		}
	}
	if((readparam==1) && (argc!=1)) {
			print_help();
			exit(0);
	}
	printf("\nfound string \"%s\" in followed files:\n\n", schstr);
	count = 0;
//	showcount = 0;
	do_search(filename, topic, schstr);
	printf("\n%d files found.\nEND!\n", count);
}