Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<regex.h>
- #include<dirent.h>
- char* dirent_type_to_str(unsigned char dirent_type) {
- switch (dirent_type) {
- case DT_DIR:
- return "Dir";
- case DT_REG:
- return "File";
- }
- printf("DEBUG: Unknown type %x\n", dirent_type);
- return "Unk";
- }
- int fun(char* dirr, char* txt);
- char* fun2(char* dirr);
- int main()
- {
- char* txt=(char*)malloc(300);
- //scanf("%s",txt);
- char* dirr=(char*)malloc(300);
- getcwd(dirr, 300);
- // printf("Кол-во файлов: %i\n", fun(dirr, txt));
- fun2(dirr);
- return 0;
- }
- int fun(char* dirr, char* txt)
- {
- int sch=0;
- char *re = (char*)malloc((20+strlen(txt))*sizeof(char));
- strcat(re,"(.+\\.");
- strcat(re,txt);
- strcat(re,"$)");
- regex_t reg;
- regmatch_t arr[1];
- regcomp(®, re, REG_EXTENDED);
- DIR* dir;
- struct dirent* entry;
- dir=opendir(dirr);
- entry=readdir(dir);
- while(entry)
- {
- int space=strlen(dirr)+strlen(entry->d_name);
- char* dirr2=(char*)calloc(space+10,sizeof(char));
- if (!opendir(entry->d_name) && !regexec(®,entry->d_name,1,arr,0))
- sch++;
- strcat(dirr2, dirr);
- strcat(dirr2, "/");
- strcat(dirr2, entry->d_name);
- DIR* dir2;
- dir2=opendir(dirr2);
- if (dir2 && strncmp(entry->d_name, ".",1) && strncmp(entry->d_name, "..",2))
- sch+=fun(dirr2, txt);
- free(dirr2);
- entry=readdir(dir);
- }
- return sch;
- }
- char* fun2(char* dirr)
- {
- char name[1000]="";
- DIR *dir;
- struct dirent *entry;
- dir = opendir(dirr);
- if (!dir)
- {
- perror("diropen");
- exit(1);
- }
- int sch=0;
- while ( (entry = readdir(dir)) != NULL)
- {
- if(dirent_type_to_str(entry->d_type)=="Dir")
- {
- if(strncmp(entry->d_name,".",1)==0 || strncmp(entry->d_name,"..",2)==0)
- continue;
- char helpstr[1000];
- strcpy(helpstr,dirr);
- strcat(helpstr,"/");
- strcat(helpstr,entry->d_name);
- strcpy(name,fun2(helpstr));
- strcpy(helpstr,name);
- strcpy(name,entry->d_name);
- strcat(name,"/");
- strcat(name,helpstr);
- }
- else
- {
- FILE *file;
- char b[100];
- //getcwd(b, 100);
- strcpy(b,"\0");
- strcpy(b,dirr);
- strcat(b,"/");
- strcat(b,entry->d_name);
- file = fopen(b, "r");
- //printf("%s\n",b);
- char a[150];
- //printf("%s\n",a );
- char* re=(char*)malloc(1000*sizeof(char));
- strcpy(re,"(.+@[a-zA-Z]+)(.[a-zA-Z][^ ]+)");
- regex_t reg;
- regmatch_t arr[2];
- regcomp(®, re, REG_EXTENDED);
- while(fgets (a, 100, file))
- if(!regexec(®,a,2,arr,0))
- remove(b);
- fclose(file);
- }
- }
- closedir(dir);
- return name;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement