Advertisement
KirillSamokhin

LB_3

May 2nd, 2022 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5.  
  6. #define START_FOLDER "./labyrinth"
  7.  
  8. void minoPasses(char** filenames, char* Pass, char* wordToSearch, int* counter){
  9.     DIR* curDir = opendir(Pass);
  10.     struct dirent* curFile = readdir(curDir);
  11.     while(curFile){
  12.         if (strcmp(curFile->d_name, ".") == 0 || strcmp(curFile->d_name, "..") == 0) {
  13.             curFile = readdir(curDir);
  14.             continue;
  15.         }
  16.         else if(strstr(curFile->d_name, ".txt") == NULL){
  17.             char* newPass = calloc(strlen(Pass)+strlen(curFile->d_name)+2, sizeof(char));
  18.             strcat(newPass, Pass);
  19.             strcat(newPass, "/");
  20.             strcat(newPass, curFile->d_name);
  21.             minoPasses(filenames, newPass, wordToSearch, counter);
  22.             free(newPass);
  23.         }
  24.         else{
  25.             char buf[100];
  26.             char* newPass = calloc(strlen(Pass)+strlen(curFile->d_name)+2, sizeof(char));
  27.             strcat(newPass, Pass);
  28.             strcat(newPass, "/");
  29.             strcat(newPass, curFile->d_name);
  30.             FILE* fp = fopen(newPass, "r");
  31.             while(fgets(buf, 100, fp)){
  32.                 if((strstr(buf, wordToSearch) != NULL) && (strstr(buf, "Deadlock") == NULL)){
  33.                     if (strcmp(curFile->d_name, "file.txt") != 0) {
  34.                         minoPasses(filenames, START_FOLDER, curFile->d_name, counter);
  35.                     }
  36.                     filenames[*counter] = newPass;
  37.                     (*counter)++;
  38.                 }
  39.             }
  40.             fclose(fp);
  41.         }
  42.         curFile = readdir(curDir);
  43.     }
  44.     closedir(curDir);
  45. }
  46.  
  47. int main()
  48. {
  49.     char **filenames = calloc(3000, sizeof(char *));
  50.     int counter = 0;
  51.     minoPasses(filenames, START_FOLDER, "Minotaur", &counter);
  52.     FILE *resultFile = fopen("./result.txt", "w");
  53.     int j = 0;
  54.     int i = 0;
  55.     for (i; i < counter; i++){
  56.         fprintf(resultFile, "%s\n", filenames[i]);
  57.     }
  58.     for(j; j<counter; j++){
  59.         free(filenames[j]);
  60.     }
  61.     fclose(resultFile);
  62.     free(filenames);
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement