Advertisement
Guest User

lr3

a guest
May 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <sys/types.h>
  6.  
  7. void find_way(char* str, char** arr, const char* direct) {
  8.     char next_dir[100];
  9.     DIR* dir = opendir(direct);
  10.     if(!dir)
  11.         return;
  12.     struct dirent* d = readdir(dir);
  13.     while(d) {
  14.         if(d->d_type == DT_DIR && strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
  15.             strcpy(next_dir, direct);
  16.             strcat(next_dir, "/");
  17.             strcat(next_dir, d->d_name);
  18.             find_way(str, arr, next_dir);
  19.         }
  20.         else if(d->d_type != DT_DIR) {
  21.             int i;
  22.             for(i = 0; i < strlen(str); i++) {
  23.                 if ((str[i] == d->d_name[0])&&(d->d_name[1] == '.')) {
  24.                     strcpy(arr[i], "./");
  25.                     strcat(arr[i], direct);
  26.                     strcat(arr[i], "/");
  27.                     strcat(arr[i], d->d_name);
  28.                     break;
  29.                 }
  30.             }
  31.         }
  32.         d = readdir(dir);
  33.     }
  34.     closedir(dir);
  35. }
  36.  
  37. int main() {
  38.    char str[300];
  39.    fgets(str, 300, stdin);
  40.    int str_len = strlen(str);
  41.    int arr_size = 100;
  42.    char** arr = (char**)malloc(str_len*sizeof(char*));
  43.    int i;
  44.    for(i = 0; i < str_len; i++) {
  45.         arr[i] = (char*)malloc(arr_size*sizeof(char));
  46.    }
  47.    
  48.    FILE *result = fopen("result.txt", "w+");
  49.    if(result)
  50.         find_way(str, arr, "tmp");
  51.    
  52.    for(i = 0; i < str_len; i++)
  53.        fprintf(result, "%s\n", arr[i]);
  54.        
  55.    fclose(result);
  56.    return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement