ungureanuvladvictor

Untitled

Nov 28th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 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. char *checkName (char *name);
  14.  
  15. int fileinfo (char *nf, int indent) {
  16. struct stat s;
  17. char fileName[2048];
  18. int i;
  19.  
  20. if (stat (nf, &s) == -1)
  21. return -1;
  22.  
  23. if (s.st_mode & S_IFDIR && strcmp(".", basename(nf)) != 0 && strcmp("..", basename(nf)) != 0) {
  24. for (i = 0; i < indent; i++)
  25. printf (" | ");
  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. strcat (cale, "/");
  54.  
  55. while ((pde = readdir (pd)) != NULL) {
  56. strcpy (specificator, cale);
  57. strcat (specificator, pde->d_name);
  58. if (fileinfo (specificator, indent +1 ) == -1)
  59. perror (specificator);
  60. }
  61.  
  62. closedir (pd);
  63. return 0;
  64. }
  65.  
  66.  
  67.  
  68. int main (int argc, char **argv) {
  69. if (argc == 2) {
  70. if (listdir (argv[1], 0) == -1)
  71. perror (argv[1]);
  72. }
  73. else perror ("Not enough args!");
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment