Advertisement
filip710

linux obilazak

Apr 10th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <dirent.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. http://www.cplusplus.com/forum/general/85870/
  8. void listdir(const char *name, int indent)
  9. {
  10.     DIR *dir;
  11.     struct dirent *entry;
  12.  
  13.     if (!(dir = opendir(name)))
  14.         return;
  15.  
  16.     while ((entry = readdir(dir)) != NULL) {
  17.         if (entry->d_type == DT_DIR) {
  18.             char path[1024];
  19.             if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  20.                 continue;
  21.             snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
  22.             printf("%*s[%s]\n", indent, "", entry->d_name);
  23.             listdir(path, indent + 2);
  24.         } else {
  25.             printf("%*s- %s\n", indent, "", entry->d_name);
  26.         }
  27.     }
  28.     closedir(dir);
  29. }
  30.  
  31. int main(void) {
  32.     listdir("/home/filip/Desktop/root", 0);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement