⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsefile.c

📁 遍历指定文件夹所有文件
💻 C
字号:

/*parse http file, get script from the file*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <fcntl.h>
#include <time.h>
#include "parsefile.h"
#include "text.h"
#include "rshash.h"

#define FIFONAME "pathfifo"
#define CONFNAME "parsefile.conf"
#define SAVENAME "script/"

static FILE *pathfifofd = NULL;
static off_t fifohead = 0;
static off_t fifotail = 0;
extern FileFilter filefilter;

int main(int argc, char *argv[])
{
	
	if(argc != 2){
		printf("Error agrc number\n");
		return -1;
	}
	pathfifofd = fopen(FIFONAME, "w+");
	if(pathfifofd == NULL){
		printf("Error open file %s\n", FIFONAME);
		return -1;
	}
	if(parseConfFile(CONFNAME) == -1){
		printf("Error parse configure file %s\n", CONFNAME);
		return -1;
	}
	writefifo(argv[1]);
	while(fifohead != fifotail){
		searchfile();
	}
	return 0;
}


int searchfile()\\ruhe shi xian gui di
{
	struct stat statbuf;
	struct dirent *dirp;
	DIR *dp;
	char basepath[PATH_MAX + 1];
	char newpath[PATH_MAX + 1];

	if(readfifo(basepath, sizeof(basepath)) == -1)
		return 0;
	if(lstat(basepath, &statbuf) < 0){
		printf("Error path %s\n", basepath);
		return 0;
	}
	if(S_ISDIR(statbuf.st_mode)){
		if((dp = opendir(basepath)) == NULL){
			printf("Cann't open dir %s\n", basepath);
			return -1;
		}
		strcat(basepath, "/");
		while((dirp = readdir(dp)) != NULL){
			if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
				continue;
			strcpy(newpath, basepath);
			strcat(newpath, dirp->d_name);
			writefifo(newpath);
		}
		closedir(dp);
	}else if(S_ISREG(statbuf.st_mode)){
		handlefile(basepath);
	}else{
		return 0;
	}
	return 0;
}

int writefifo(char *pathname)
{
	static char wpathbuf[PATH_MAX + 1];
	int pathlen;
	
	if(pathfifofd == NULL){
		printf("%s:%d Error pathfifofd -1\n", __FUNCTION__, __LINE__);
		return -1;
	}
	pathlen = strlen(pathname);
	if(pathlen > PATH_MAX){
		printf("%s:%d Error pathname length %d\n", __FUNCTION__, __LINE__, pathlen);
		return -1;
	}
	sprintf(wpathbuf, "%s\n", pathname);
	fseek(pathfifofd, 0, SEEK_END);
	fputs(wpathbuf, pathfifofd);
	fseek(pathfifofd, 0, SEEK_END);
	fifotail = ftell(pathfifofd);
	#ifdef DEBUG
	printf("[fifotail] = %d [wrpath] = %s\n", (int)fifotail, wpathbuf);
	#endif
	return 0;
}

int readfifo(char *rpathbuf, size_t nbytes)
{
	char *ptr;
	
	if(fifohead == fifotail)
		return -1;
	fseek(pathfifofd, fifohead, SEEK_SET);
	ptr = fgets(rpathbuf, nbytes, pathfifofd);
	if(ptr == NULL){
		printf("%s:%d fgets error\n", __FUNCTION__, __LINE__);
		return -1;
	}
	fifohead = ftell(pathfifofd);
	ptr = rpathbuf + strlen(rpathbuf);
	*--ptr = '\0';
	#ifdef DEBUG
	printf("[rdpath] = %s\n", rpathbuf);
	#endif
	return 0;
}

int handlefile(char *pathname)
{
	FILE *oldfd, *newfd;
	char *newfilename;
	char tmpbuf[2048];
	char *ptr;
	size_t rlen, wlen;
	int eff = 0;

	if(ishtmlfile(pathname) == -1){
		#ifdef DEBUG
		printf("%s is not a html file, save on the disk directly\n", pathname);
		#endif
		newfilename = hashfilename(pathname);
		if((oldfd = fopen(pathname, "r")) == NULL){
			free(newfilename);
			printf("ERROR: open file %s fail!!!\n", pathname);
			return -1;
		}
		if((newfd = fopen(newfilename, "w+")) == NULL){
			fclose(oldfd);
			free(newfilename);
			printf("ERROR: open file %s fail!!!\n", newfilename);
			return -1;
		}
		while(!feof(oldfd)){
			rlen = fread(tmpbuf, sizeof(char), sizeof(tmpbuf), oldfd);
			if(rlen == 0)
				break;
			wlen = fwrite(tmpbuf, sizeof(char), rlen, newfd);
		}
		fclose(oldfd);
		fclose(newfd);
		free(newfilename);
	}else{
		#ifdef DEBUG
		printf("%s is a html file, parse the file immediately\n", pathname);
		#endif
		newfilename = hashfilename(pathname);
		if((oldfd = fopen(pathname, "r")) == NULL){
			free(newfilename);
			printf("ERROR: open file %s fail!!!\n", pathname);
			return -1;
		}
		if((newfd = fopen(newfilename, "w+")) == NULL){
			fclose(oldfd);
			free(newfilename);
			printf("ERROR: open file %s fail!!!\n", newfilename);
			return -1;
		}
		while(!feof(oldfd)){
			if((ptr = fgets(tmpbuf, sizeof(tmpbuf), oldfd)) == NULL)
				break;
			if((ptr = strstr(tmpbuf, "<script")) != NULL ||
				(ptr = strstr(tmpbuf, "<Script")) != NULL ||
				(ptr = strstr(tmpbuf, "<SCRIPT")) != NULL){
				eff ++;
			}
			if(eff){
				fputs(tmpbuf, newfd);
			}
			if((ptr = strstr(tmpbuf, "</script")) != NULL ||
				(ptr = strstr(tmpbuf, "</Script")) != NULL ||
				(ptr = strstr(tmpbuf, "</SCRIPT")) != NULL){
				eff --;
			}
		}
		fclose(oldfd);
		fclose(newfd);
		free(newfilename);
	}
	return 0;
}

int ishtmlfile(char *pathname)
{
	int ret = -1;
	int i;
	
	for(i = 0; i < filefilter.num; i ++){
		if((strstr(pathname, filefilter.filter[i])) != NULL)
			ret = 0;
	}
	return ret;
}

char *hashfilename(char *pathname)
{
	char *filename;
	unsigned int rshash;
	char tmp[32];

	filename = malloc(NAME_MAX);
	bzero(filename, NAME_MAX);
	strcat(filename, SAVENAME);
	rshash = RSHash(pathname);
	sprintf(tmp, "%x", rshash);
	strcat(filename, tmp);
	strcat(filename, ".s");
	#ifdef DEBUG
	printf("old=%s hash=%s\n", pathname, filename);
	#endif
	return filename;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -