Advertisement
razvanth21

Untitled

Oct 28th, 2018
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <dirent.h>
  7. #include <sys/types.h>
  8.  
  9. void traverse(char *path, int indent)
  10. {
  11.     DIR *dir = opendir(path);
  12.  
  13.     if (dir == NULL)
  14.     {
  15.         fprintf(stderr, "Eroare la deschiderea directorului.\n");
  16.         exit(EXIT_FAILURE);
  17.     }
  18.  
  19.     struct dirent *d;
  20.  
  21.     while ((d = readdir(dir)) != NULL)
  22.     {
  23.         if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  24.             continue;
  25.  
  26.         if (d->d_type == DT_DIR)
  27.         {
  28.             char buf[256];
  29.  
  30.             printf("%*s%s\n", indent, "", d->d_name);
  31.             sprintf(buf, "%s/%s", path, d->d_name);
  32.  
  33.             traverse(buf, indent + 2);
  34.         }
  35.         else
  36.             printf("%*s%s\n", indent, "", d->d_name);
  37.     }
  38. }
  39.  
  40. int main(int argc, char **argv)
  41. {
  42.     if (argc != 2)
  43.     {
  44.         fprintf(stderr, "Syntax: %s <path>\n", argv[0]);
  45.         exit(EXIT_FAILURE);
  46.     }
  47.  
  48.     traverse(argv[1], 0);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement