Advertisement
pochti_da

Untitled

Oct 30th, 2020
2,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <dirent.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <stdint.h>
  9. #include <inttypes.h>
  10. #include <string.h>
  11.  
  12. enum
  13. {
  14.     DIR_SIZE = 20
  15. };
  16.  
  17. int
  18. cmp(const void *a, const void *b)
  19. {
  20.     return strcasecmp(*(const char **)a, *(const char **)b);
  21. }
  22.  
  23. char *
  24. name(char *path) {
  25.     int len = strlen(path);
  26.  
  27.     int ind;
  28.     for (ind = len - 1; ind >= 0; --ind) {
  29.         if (path[ind] == '/') {
  30.             return path + ind + 1;
  31.         }
  32.     }
  33.  
  34.     return path;
  35. }
  36.  
  37. void
  38. check_dir(char *dir_path)
  39. {
  40.     DIR *dir = opendir(dir_path);
  41.  
  42.     if (dir == NULL) {
  43.         return;
  44.     }
  45.  
  46.     printf("cd %s\n", name(dir_path));
  47.  
  48.     char files[DIR_SIZE][PATH_MAX];
  49.     int size = 0;
  50.  
  51.     struct dirent *dirent;
  52.     while ((dirent = readdir(dir)) != NULL) {
  53.         struct stat st;
  54.  
  55.         if (!strcmp(dirent->d_name, "..") ||
  56.                 !strcmp(dirent->d_name, ".")) {
  57.             continue;
  58.         }
  59.  
  60.         char name[PATH_MAX];
  61.         snprintf(name, sizeof(name), "%s/%s", dir_path, dirent->d_name);
  62.         stat(name, &st);
  63.  
  64.         if (S_ISDIR(st.st_mode)) {
  65.             strcpy(files[size], name);
  66.             ++size;
  67.         }
  68.     }
  69.  
  70.     for (int i = 0; i < size; ++i)
  71.         printf("%s\n", files[i]);
  72.  
  73.     qsort(files, size, sizeof(char[PATH_MAX]), cmp);
  74.  
  75.     for (int i = 0; i < size; ++i) {
  76.         check_dir(files[i]);
  77.     }
  78.  
  79.     printf("cd ..\n");
  80. }
  81.  
  82. int
  83. main(int argc, char *argv[])
  84. {
  85.     check_dir(argv[1]);
  86.     return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement