Advertisement
basturbator

lab

Mar 30th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <sys/types.h>
  6.  
  7. char *getString(){
  8.     int size = 30;
  9.     char *string = malloc(size * sizeof(char));
  10.     char c;
  11.     int i = 0;
  12.     c = getchar();
  13.     while(c != '\n'){
  14.         if(i == size - 2){
  15.             size += 30;
  16.             string = realloc(string, size * sizeof(char));
  17.         }
  18.         string[i] = c;
  19.         i++;
  20.         c = getchar();
  21.     }
  22.     string[i] = '\0';
  23.     return string;
  24. }
  25.  
  26. void recDirent(char *nameDir, int size, char letter, FILE *output){
  27.     DIR *dir = opendir(nameDir);
  28.     struct dirent *cur = readdir(dir);
  29.     if(dir){
  30.         while(cur){
  31.             if((cur->d_type == DT_DIR) && strcmp(cur->d_name, ".") && strcmp(cur->d_name, "..")){
  32.                 int length = strlen(nameDir);
  33.                 if(length + strlen(cur->d_name) == size - 1){
  34.                     size += 100;
  35.                     nameDir = realloc(nameDir, size * sizeof(char));
  36.                 }
  37.                 strcat(nameDir, cur->d_name);
  38.                 strcat(nameDir, "/");
  39.                 recDirent(nameDir, size, letter, output);
  40.                 nameDir[length] = '\0';
  41.             }
  42.             if(cur->d_type == DT_REG){
  43.                 if((cur->d_name[0] == letter) && (cur->d_name[1] == '.')){
  44.                     fprintf(output, "%s%s\n", nameDir, cur->d_name);
  45.                     break;
  46.                 }
  47.             }
  48.             cur = readdir(dir);
  49.         }
  50.     }
  51.     closedir(dir);
  52. }
  53.  
  54. int main(){
  55.     char *string = getString();
  56.     int size = 100;
  57.     char *nameDir = malloc(size * sizeof(char));
  58.     FILE *output;
  59.     output = fopen("result.txt", "w");
  60.     for(int i = 0; i < strlen(string); i++){
  61.         nameDir[0] = '\0';
  62.         strcat(nameDir, "./");
  63.         recDirent(nameDir, size, string[i], output);
  64.     }
  65.     fclose(output);
  66.     free(string);
  67.     free(nameDir);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement