ungureanuvladvictor

Untitled

Nov 28th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 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[4096];
  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. strcpy(fileName, nf);
  26. strcat(fileName, "/");
  27. listdir(fileName, indent + 1);
  28. }
  29.  
  30. if(s.st_mode & S_IFREG) {
  31. for (i = 0; i < indent; i++)
  32. printf (" | ");
  33. printf ("%s\n", basename(nf));
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39.  
  40.  
  41. int listdir (char *nf, int indent) {
  42. DIR *pd;
  43. struct dirent *pde;
  44. char cale[4096], specificator[4096];
  45.  
  46. if ((pd = opendir (nf)) == NULL)
  47. return -1;
  48.  
  49. printf("[DIR]%s\n", basename(nf));
  50.  
  51. strcpy (cale, nf);
  52. strcat (cale, "/");
  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. char location[2048];
  70. strcpy (location, argv[1]);
  71.  
  72. if (argv[1][strlen(argv[1])-1] != '/') {
  73. strcpy (location, "/");
  74. }
  75.  
  76. if (listdir (location, 0) == -1)
  77. perror (location);
  78. }
  79. else perror ("Not enough args!");
  80.  
  81.  
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment