Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include <sys/stat.h>
  7.  
  8. typedef char String[1024];
  9.  
  10. typedef struct Node{
  11.    
  12.     String filename;
  13.     int instances;
  14.     struct Node* nextNode;
  15.    
  16. }Filenode;
  17.  
  18. void toLowerCase(String text){
  19.    
  20.     int i;
  21.    
  22.     for(i = 0 ; text[i] ; i++){
  23.         if(text[i] >= 65 && text[i] <= 90){
  24.             text[i] += 32;
  25.         }
  26.     }
  27.        
  28. }
  29.  
  30. int searchkeyword(String searchtext,String keyword){
  31.    
  32.     int i,j,k;
  33.     int notfound = 1;
  34.     int instances = 0;
  35.    
  36.     toLowerCase(searchtext);
  37.     toLowerCase(keyword);
  38.    
  39.     for(i = 0 ; i < strlen(searchtext) ; i++){
  40.        
  41.         notfound = 1;
  42.    
  43.         for(j = 0, k = i ; j < strlen(keyword) && notfound ; j++, k++){
  44.  
  45.             if(keyword[j] != searchtext[k]){
  46.                 notfound = 0;
  47.             }
  48.            
  49.         }
  50.    
  51.         if(notfound){
  52.             instances++;
  53.         }
  54.        
  55.     }
  56.    
  57.     return instances;
  58.    
  59. }
  60.  
  61. int search(String filename, String keyword){
  62.    
  63.     int found = 0;
  64.     FILE *fp;
  65.     String phrase; 
  66.    
  67.     if(strstr(filename,".txt") != NULL){
  68.        
  69.         fp = fopen(filename,"r");
  70.        
  71.         if(fp == NULL){
  72.             printf("\n### FILE NOT FOUND ###\n");
  73.         }else{
  74.        
  75.             while(fscanf(fp, "%s", phrase) == 1){  
  76.                 //printf("%s",phrase);
  77.                 toLowerCase(phrase);
  78.                 toLowerCase(keyword);
  79.                 if(strstr(phrase, keyword) != NULL){
  80.                     found++;
  81.                     //printf(" +++ hit");
  82.                 }
  83.                 //printf("\n");
  84.             }  
  85.        
  86.         }  
  87.        
  88.         fclose(fp);
  89.            
  90.     }else if(strstr(filename,".doc") != NULL){
  91.        
  92.         fp = fopen(filename,"rb");
  93.        
  94.         if(fp == NULL){
  95.             printf("\n### FILE NOT FOUND ###\n");
  96.         }else{
  97.            
  98.             char chdump;
  99.             String buffer;
  100.             int ctr = 0;
  101.            
  102.             fseek(fp,2560,SEEK_SET);
  103.            
  104.             do{
  105.                 fread(&chdump,1,1,fp);
  106.                 buffer[ctr] = chdump;
  107.                 ctr++;                 
  108.             }while(chdump >= 32 && chdump <= 126);
  109.  
  110.             found = searchkeyword(buffer,keyword);
  111.            
  112.         }  
  113.    
  114.         fclose(fp);
  115.            
  116.     }
  117.    
  118.     return found;
  119.    
  120. }
  121.  
  122. void newFilenode(String filename, int instances, Filenode** filelist){
  123.    
  124.     Filenode* newNode = malloc(sizeof(Filenode));
  125.     strcpy(newNode->filename,filename);
  126.     newNode->instances = instances;
  127.     newNode->nextNode = *filelist;
  128.     *filelist = newNode;
  129.    
  130. }
  131.  
  132. void traverse(String pathname, String oldpathname, String keyword, Filenode** filelist){
  133.  
  134.   chdir(pathname); 
  135.  
  136.   DIR *d = opendir(".");
  137.   struct dirent *dir;
  138.  
  139.   if (d){
  140.    
  141.     while ((dir = readdir(d)) != NULL){
  142.        
  143.         if(!(strcmp(dir->d_name,".") == 0)&&!(strcmp(dir->d_name,"..") == 0)){
  144.  
  145.             char *fi = dir->d_name;
  146.             struct stat fileinfo;          
  147.            
  148.             int status = stat(fi, &fileinfo);
  149.            
  150.             if (status != 0) {
  151.                 return;
  152.             }
  153.  
  154.             if (S_ISDIR (fileinfo.st_mode)) {
  155.                
  156.                String newpathname;
  157.                strcpy(newpathname, pathname);
  158.                strcat(newpathname,"\\");
  159.                strcat(newpathname,dir->d_name);
  160.                printf("%s\n",newpathname);
  161.                traverse(newpathname,pathname,keyword,filelist);
  162.                
  163.             }else if (S_ISREG (fileinfo.st_mode)) {
  164.                
  165.                int instances = search(dir->d_name,keyword);
  166.                
  167.                if(instances > 0){
  168.  
  169.                 String newpathname;
  170.                 strcpy(newpathname, pathname);
  171.                 strcat(newpathname,"\\");
  172.                 strcat(newpathname,dir->d_name);
  173.                
  174.                 newFilenode(newpathname,instances,filelist);
  175.                
  176.                }
  177.        
  178.             }
  179.                        
  180.         }
  181.        
  182.     }
  183.  
  184.     closedir(d);
  185.    
  186.   }
  187.  
  188.     chdir(oldpathname);
  189.  
  190. }
  191.  
  192. int main(){
  193.    
  194.     String keyword;
  195.     String pathname;
  196.     Filenode* filelist = NULL;
  197.    
  198.     printf("Pathname: ");
  199.     scanf("%s",pathname);
  200.    
  201.     printf("Keyword: ");
  202.     scanf("%s",keyword);
  203.  
  204.     printf("\n");  
  205.    
  206.     if(chdir(pathname) == 0){
  207.         traverse(pathname,"C:\\",keyword,&filelist);       
  208.     }else{
  209.         printf("Invalid path name.\n");
  210.     }
  211.    
  212.     if(filelist != NULL){
  213.        
  214.         while(filelist != NULL){
  215.            
  216.             printf("\nPath: %s\n",filelist->filename);
  217.             printf("Number of instances: %d\n",filelist->instances);   
  218.             filelist = filelist->nextNode; 
  219.                    
  220.         }
  221.            
  222.     }
  223.    
  224.     return 0;
  225.    
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement