sakiir

Untitled

Oct 17th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7.  
  8. #define PATH_MAX 4096
  9.  
  10.  
  11. void parcours_rep(char *path, char *end)
  12. {
  13.         struct dirent *lecture;
  14.         struct stat statbuf;
  15.         DIR *rep;
  16.  
  17.         printf("%s\n", path);
  18.  
  19.         if (lstat(path, &statbuf))  // on saute si lstat échoue
  20.                 return;
  21.  
  22.         if (S_ISLNK(statbuf.st_mode))  // on saute les symlinks;
  23.                 return;
  24.  
  25.         if (!(rep = opendir(path)))
  26.                 return;  // opendir échoue, on se contente de retourner
  27.        
  28.         *end++ = '/';   // on ajoute un / a la fin du path
  29.  
  30.         while ((lecture = readdir(rep)))//parcours des elements du repertoire
  31.         {
  32.                 if (lecture->d_name[0] == '.')
  33.                         continue;
  34.                 strcpy(end, lecture->d_name);
  35.                 parcours_rep(path, end + strlen(lecture->d_name));
  36.         }
  37.  
  38.         closedir(rep);
  39. }
  40.  
  41. int main()
  42. {
  43.         char path[4096] = "/";
  44.         parcours_rep(path, path + strlen(path));
  45.         return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment