Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <dirent.h>
  6. #define PATH "./labyrinth"
  7.  
  8.  
  9.  
  10. void writePath(char* startPath, char* name)
  11. {
  12.     strcat(strcat(startPath, "/"), name);
  13. }
  14. void clearFile()
  15. {
  16.     FILE *file = fopen("./result.txt", "w");
  17.     fclose(file);
  18. }
  19. void writeInFile(char* output)
  20. {
  21.     FILE *file = fopen("./result.txt", "r");
  22.     char buf[10000] = "\0";
  23.     char miniBuf[500] = "\0";
  24.     while (fgets(miniBuf, 500, file) != NULL)
  25.     {
  26.         strcat(buf,miniBuf);
  27.     }
  28.     fclose(file);
  29.     file = fopen("./result.txt", "w");
  30.     fprintf(file, "%s\n", output);
  31.     fprintf(file, "%s", buf);
  32.     fclose(file);
  33. }
  34.  
  35. int readFile(char* path);
  36. int searchInDir(char* path, char* filename);
  37. int main()
  38. {
  39.     char path[1000] = PATH;
  40.     char filename[100] = "file.txt";
  41.     clearFile();
  42.     searchInDir(path, filename);
  43.     return 0;
  44. }
  45.  
  46.  
  47.  
  48. int readFile(char* path)
  49. {
  50.     char buf[100];
  51.     FILE *file = fopen(path, "r");
  52.     while (fgets(buf, 100, file) != NULL)
  53.     {
  54.         buf[strlen(buf)-1] = '\0';
  55.        
  56.         if(!strcmp(buf, "Deadlock"))
  57.         {
  58.             //return 0;
  59.         }
  60.         else if(!strcmp(buf, "Minotaur"))
  61.         {
  62.             return 1;
  63.         }
  64.         else{
  65.             memcpy(buf, buf+9, 91);
  66.             char newPath[1000] = PATH;
  67.             if (searchInDir(newPath, buf))
  68.             return 1;
  69.         }
  70.            
  71.     }
  72.     fclose(file);
  73.     return 0;
  74. }
  75. int searchInDir(char* path, char* filename)
  76. {
  77.     char buf[1000];
  78.     strcpy(buf, path);
  79.     DIR* dir = opendir(path);
  80.     if (dir){
  81.         struct dirent *point = readdir(dir);
  82.         while (point){
  83.             if ((point->d_type == DT_DIR) && (strcmp(point->d_name, ".")) && (strcmp(point->d_name, "..")))
  84.             {
  85.                 writePath(path, point->d_name);
  86.                 if (searchInDir(path, filename))
  87.                 {
  88.                     closedir(dir);
  89.                     return 1;
  90.  
  91.                 }
  92.                 else
  93.                     strcpy(path, buf);
  94.             }
  95.             else if (!strcmp(point->d_name, filename))
  96.             {
  97.                 closedir(dir);
  98.                 char output[1000];
  99.                 writePath(path, point->d_name);
  100.                 strcpy(output, path);
  101.                 if (readFile(path))
  102.                 {
  103.                     writeInFile(output);
  104.                     return 1;
  105.                 }
  106.                 else
  107.                     return 0;
  108.             }
  109.                
  110.             point = readdir(dir);
  111.         }
  112.     }
  113.     closedir(dir);
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement