Advertisement
Fox1207

Untitled

May 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<regex.h>
  5. #include<dirent.h>
  6.  
  7.  
  8. char* dirent_type_to_str(unsigned char dirent_type) {
  9.   switch (dirent_type) {
  10.   case DT_DIR:
  11.     return "Dir";
  12.   case DT_REG:
  13.     return "File";
  14.   }
  15.  
  16.   printf("DEBUG: Unknown type %x\n", dirent_type);
  17.   return "Unk";
  18. }
  19.  
  20. int fun(char* dirr, char* txt);
  21. char* fun2(char* dirr);
  22. int main()
  23. {
  24.     char* txt=(char*)malloc(300);
  25.     //scanf("%s",txt);
  26.  
  27.     char* dirr=(char*)malloc(300);
  28.     getcwd(dirr, 300);
  29.  
  30.    // printf("Кол-во файлов: %i\n", fun(dirr, txt));
  31.  
  32.     fun2(dirr);
  33.  
  34.     return 0;
  35. }
  36.  
  37.  
  38. int fun(char* dirr, char* txt)
  39. {
  40.     int sch=0;
  41.     char *re = (char*)malloc((20+strlen(txt))*sizeof(char));
  42.     strcat(re,"(.+\\.");
  43.     strcat(re,txt);
  44.     strcat(re,"$)");
  45.     regex_t reg;
  46.     regmatch_t arr[1];
  47.     regcomp(&reg, re, REG_EXTENDED);
  48.  
  49.     DIR* dir;
  50.     struct dirent* entry;
  51.     dir=opendir(dirr);
  52.     entry=readdir(dir);
  53.  
  54.  
  55.     while(entry)
  56.     {
  57.         int space=strlen(dirr)+strlen(entry->d_name);
  58.         char* dirr2=(char*)calloc(space+10,sizeof(char));
  59.         if (!opendir(entry->d_name) && !regexec(&reg,entry->d_name,1,arr,0))
  60.             sch++;
  61.        
  62.         strcat(dirr2, dirr);
  63.         strcat(dirr2, "/");
  64.         strcat(dirr2, entry->d_name);
  65.  
  66.         DIR* dir2;
  67.         dir2=opendir(dirr2);
  68.         if (dir2 && strncmp(entry->d_name, ".",1) && strncmp(entry->d_name, "..",2))
  69.             sch+=fun(dirr2, txt);
  70.         free(dirr2);
  71.  
  72.         entry=readdir(dir);
  73.     }
  74.     return sch;
  75.  
  76. }
  77.  
  78.  
  79. char* fun2(char* dirr)
  80. {
  81.     char name[1000]="";
  82.  
  83.   DIR *dir;
  84.   struct dirent *entry;
  85.   dir = opendir(dirr);
  86.   if (!dir)
  87.   {
  88.     perror("diropen");
  89.     exit(1);
  90.   }
  91.    
  92.   int sch=0;
  93.   while ( (entry = readdir(dir)) != NULL)
  94.   {
  95.    
  96.    
  97.  
  98.     if(dirent_type_to_str(entry->d_type)=="Dir")
  99.     {
  100.       if(strncmp(entry->d_name,".",1)==0 || strncmp(entry->d_name,"..",2)==0)
  101.         continue;
  102.       char helpstr[1000];
  103.       strcpy(helpstr,dirr);
  104.       strcat(helpstr,"/");
  105.       strcat(helpstr,entry->d_name);
  106.  
  107.       strcpy(name,fun2(helpstr));
  108.  
  109.       strcpy(helpstr,name);
  110.       strcpy(name,entry->d_name);
  111.       strcat(name,"/");
  112.       strcat(name,helpstr);
  113.     }
  114.     else
  115.     {
  116.         FILE *file;
  117.         char b[100];
  118.         //getcwd(b, 100);
  119.         strcpy(b,"\0");
  120.         strcpy(b,dirr);
  121.         strcat(b,"/");
  122.         strcat(b,entry->d_name);
  123.         file = fopen(b, "r");
  124.         //printf("%s\n",b);
  125.         char a[150];
  126.         //printf("%s\n",a );
  127.         char* re=(char*)malloc(1000*sizeof(char));
  128.         strcpy(re,"(.+@[a-zA-Z]+)(.[a-zA-Z][^ ]+)");
  129.  
  130.         regex_t reg;
  131.         regmatch_t arr[2];
  132.         regcomp(&reg, re, REG_EXTENDED);
  133.         while(fgets (a, 100, file))
  134.         if(!regexec(&reg,a,2,arr,0))
  135.             remove(b);
  136.  
  137.  
  138.        
  139.         fclose(file);
  140.     }
  141.   }
  142.   closedir(dir);
  143.   return name;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement