ungureanuvladvictor

Untitled

Nov 28th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <pwd.h>
  5. #include <sys/types.h>
  6. #include <time.h>
  7. #include <dirent.h>
  8. #include <string.h>
  9. #include <libgen.h>
  10.  
  11. int fileinfo (char *nf, int indent);
  12. int listdir (char *nf, int indent);
  13.  
  14. int fileinfo (char *nf, int indent) {
  15. struct stat s;
  16. char fileName[2048];
  17. int i;
  18.  
  19. if (stat (nf, &s) == -1)
  20. return -1;
  21.  
  22. if (s.st_mode & S_IFDIR && strcmp(".", basename(nf)) != 0 && strcmp("..", basename(nf)) != 0) {
  23. for (i = 0; i < indent; i++)
  24. printf (" ");
  25. //printf("[DIR]%s\n %s", basename(nf), nf);
  26. strcpy(fileName, nf);
  27. strcat(fileName, "/");
  28. listdir(fileName, indent + 1);
  29. }
  30.  
  31. if(s.st_mode & S_IFREG) {
  32. for (i = 0; i < indent; i++)
  33. printf (" ");
  34. printf ("%s\n", basename(nf));
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40.  
  41.  
  42. int listdir (char *nf, int indent) {
  43. DIR *pd;
  44. struct dirent *pde;
  45. char cale[256], specificator[256];
  46.  
  47. if ((pd = opendir (nf)) == NULL)
  48. return -1;
  49.  
  50. printf("[DIR]%s\n", basename(nf));
  51.  
  52. strcpy (cale, nf);
  53.  
  54. while ((pde = readdir (pd)) != NULL) {
  55. strcpy (specificator, cale);
  56. strcat (specificator, pde->d_name);
  57. if (fileinfo (specificator, indent +1 ) == -1)
  58. perror (specificator);
  59. }
  60.  
  61. closedir (pd);
  62. return 0;
  63. }
  64.  
  65.  
  66.  
  67. int main (int argc, char **argv) {
  68. if (argc == 2) {
  69. if (listdir (argv[1], 0) == -1)
  70. perror (argv[1]);
  71. }
  72. else perror ("Not enough args!");
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment