Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <sys/stat.h>
- typedef char String[1024];
- typedef struct Node{
- String filename;
- int instances;
- struct Node* nextNode;
- }Filenode;
- void toLowerCase(String text){
- int i;
- for(i = 0 ; text[i] ; i++){
- if(text[i] >= 65 && text[i] <= 90){
- text[i] += 32;
- }
- }
- }
- int searchkeyword(String searchtext,String keyword){
- int i,j,k;
- int notfound = 1;
- int instances = 0;
- toLowerCase(searchtext);
- toLowerCase(keyword);
- for(i = 0 ; i < strlen(searchtext) ; i++){
- notfound = 1;
- for(j = 0, k = i ; j < strlen(keyword) && notfound ; j++, k++){
- if(keyword[j] != searchtext[k]){
- notfound = 0;
- }
- }
- if(notfound){
- instances++;
- }
- }
- return instances;
- }
- int search(String filename, String keyword){
- int found = 0;
- FILE *fp;
- String phrase;
- if(strstr(filename,".txt") != NULL){
- fp = fopen(filename,"r");
- if(fp == NULL){
- printf("\n### FILE NOT FOUND ###\n");
- }else{
- while(fscanf(fp, "%s", phrase) == 1){
- //printf("%s",phrase);
- toLowerCase(phrase);
- toLowerCase(keyword);
- if(strstr(phrase, keyword) != NULL){
- found++;
- //printf(" +++ hit");
- }
- //printf("\n");
- }
- }
- fclose(fp);
- }else if(strstr(filename,".doc") != NULL){
- fp = fopen(filename,"rb");
- if(fp == NULL){
- printf("\n### FILE NOT FOUND ###\n");
- }else{
- char chdump;
- String buffer;
- int ctr = 0;
- fseek(fp,2560,SEEK_SET);
- do{
- fread(&chdump,1,1,fp);
- buffer[ctr] = chdump;
- ctr++;
- }while(chdump >= 32 && chdump <= 126);
- found = searchkeyword(buffer,keyword);
- }
- fclose(fp);
- }
- return found;
- }
- void newFilenode(String filename, int instances, Filenode** filelist){
- Filenode* newNode = malloc(sizeof(Filenode));
- strcpy(newNode->filename,filename);
- newNode->instances = instances;
- newNode->nextNode = *filelist;
- *filelist = newNode;
- }
- void traverse(String pathname, String oldpathname, String keyword, Filenode** filelist){
- chdir(pathname);
- DIR *d = opendir(".");
- struct dirent *dir;
- if (d){
- while ((dir = readdir(d)) != NULL){
- if(!(strcmp(dir->d_name,".") == 0)&&!(strcmp(dir->d_name,"..") == 0)){
- char *fi = dir->d_name;
- struct stat fileinfo;
- int status = stat(fi, &fileinfo);
- if (status != 0) {
- return;
- }
- if (S_ISDIR (fileinfo.st_mode)) {
- String newpathname;
- strcpy(newpathname, pathname);
- strcat(newpathname,"\\");
- strcat(newpathname,dir->d_name);
- printf("%s\n",newpathname);
- traverse(newpathname,pathname,keyword,filelist);
- }else if (S_ISREG (fileinfo.st_mode)) {
- int instances = search(dir->d_name,keyword);
- if(instances > 0){
- String newpathname;
- strcpy(newpathname, pathname);
- strcat(newpathname,"\\");
- strcat(newpathname,dir->d_name);
- newFilenode(newpathname,instances,filelist);
- }
- }
- }
- }
- closedir(d);
- }
- chdir(oldpathname);
- }
- int main(){
- String keyword;
- String pathname;
- Filenode* filelist = NULL;
- printf("Pathname: ");
- scanf("%s",pathname);
- printf("Keyword: ");
- scanf("%s",keyword);
- printf("\n");
- if(chdir(pathname) == 0){
- traverse(pathname,"C:\\",keyword,&filelist);
- }else{
- printf("Invalid path name.\n");
- }
- if(filelist != NULL){
- while(filelist != NULL){
- printf("\nPath: %s\n",filelist->filename);
- printf("Number of instances: %d\n",filelist->instances);
- filelist = filelist->nextNode;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement