Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 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[50];
  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 search(String filename, String keyword){
  31.    
  32.     FILE *fp;
  33.     String phrase;
  34.     int found = 0;
  35.    
  36.     fp = fopen(filename,"r");
  37.    
  38.     if(fp == NULL){
  39.         printf("\n### FILE NOT FOUND ###\n");
  40.     }else{
  41.  
  42.     while(fscanf(fp, "%s", phrase) == 1){  
  43.         //printf("%s",phrase);
  44.         toLowerCase(phrase);
  45.         toLowerCase(keyword);
  46.         if(strstr(phrase, keyword) != NULL){
  47.             found++;
  48.             //printf(" +++ hit");
  49.         }
  50.         //printf("\n");
  51.     }  
  52.    
  53.     }  
  54.    
  55.     fclose(fp);
  56.    
  57.     return found;
  58.    
  59. }
  60.  
  61. void newFilenode(String filename, int instances, Filenode** filelist){
  62.    
  63.     Filenode* newNode = malloc(sizeof(Filenode));
  64.     strcpy(newNode->filename,filename);
  65.     newNode->instances = instances;
  66.     newNode->nextNode = *filelist;
  67.     *filelist = newNode;
  68.    
  69. }
  70.  
  71. void traverse(String pathname, String oldpathname, String keyword, Filenode** filelist){
  72.  
  73.   chdir(pathname); 
  74.  
  75.   DIR *d = opendir(".");
  76.   struct dirent *dir;
  77.  
  78.   if (d){
  79.    
  80.     while ((dir = readdir(d)) != NULL){
  81.        
  82.         if(!(strcmp(dir->d_name,".") == 0)&&!(strcmp(dir->d_name,"..") == 0)){
  83.            
  84.             //printf("%s", dir->d_name);  
  85.              
  86.             char *fi = dir->d_name;
  87.             struct stat fileinfo;          
  88.            
  89.             int status = stat(fi, &fileinfo);
  90.            
  91.             if (status != 0) {
  92.                 return;
  93.             }
  94.  
  95.             if (S_ISDIR (fileinfo.st_mode)) {
  96.  
  97.                String newpathname;
  98.                strcpy(newpathname, pathname);
  99.                strcat(newpathname,"\\");
  100.                strcat(newpathname,dir->d_name);
  101.  
  102.                traverse(newpathname,pathname,keyword,filelist);
  103.                
  104.             }else if (S_ISREG (fileinfo.st_mode)) {
  105.                
  106.                //printf (" - FILE\n");
  107.                //printf("### %d hits ###\n",search(dir->d_name,keyword));
  108.                int instances = search(dir->d_name,keyword);
  109.                
  110.                if(instances > 0){
  111.  
  112.                 String newpathname;
  113.                 strcpy(newpathname, pathname);
  114.                 strcat(newpathname,"\\");
  115.                 strcat(newpathname,dir->d_name);
  116.                
  117.                 newFilenode(newpathname,instances,filelist);
  118.                
  119.                }
  120.        
  121.             }else{
  122.                
  123.                //printf(" - ???\n");
  124.                
  125.             }
  126.                            
  127.         }
  128.        
  129.     }
  130.  
  131.     closedir(d);
  132.    
  133.   }
  134.  
  135.     chdir(oldpathname);
  136.  
  137. }
  138.  
  139. int main(){
  140.    
  141.     String keyword;
  142.     String pathname;
  143.     Filenode* filelist = NULL;
  144.    
  145.     printf("Pathname: ");
  146.     scanf("%s",pathname);
  147.    
  148.     printf("Keyword: ");
  149.     scanf("%s",keyword);
  150.    
  151.     traverse(pathname,"C:\\",keyword,&filelist);
  152.    
  153.     if(filelist != NULL){
  154.        
  155.         while(filelist != NULL){
  156.            
  157.             printf("\nPath: %s\n",filelist->filename);
  158.             printf("Number of instances: %d\n",filelist->instances);   
  159.             filelist = filelist->nextNode; 
  160.                    
  161.         }
  162.            
  163.     }
  164.    
  165.     return 0;
  166.    
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement