Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <limits.h>
  7.  
  8. #define WRONG_ARGS_NUM 1
  9.  
  10. typedef int MyFunc(const char*, const struct stat*, int);
  11.  
  12. static MyFunc myfunc;
  13. static int myftw(char*, MyFunc*);
  14. static int dopath(MyFunc*);
  15.  
  16. static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20. if (argc != 2)
  21. {
  22. printf("Usage: ./nFilesDir <dir>\n");
  23. return WRONG_ARGS_NUM;
  24. }
  25.  
  26. int ret_val = myftw(argv[1], myfunc);
  27.  
  28. ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
  29. ntot = (ntot == 0) ? 1 : ntot;
  30.  
  31. printf("Rgular files:\t\t %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
  32. printf("Directories:\t\t %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot);
  33. printf("Special block files:\t %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot);
  34. printf("Special symbolic files:\t %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);
  35. printf("FIFO:\t\t\t %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot);
  36. printf("Symbolic links:\t\t %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);
  37. printf("Sockets:\t\t %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot);
  38.  
  39. return ret_val;
  40. }
  41.  
  42. #define FTW_F 1 // File, not directory
  43. #define FTW_D 2 // Directory
  44. #define FTW_DNR 3 // Private directory
  45. #define FTW_NS 4 // Can't get info about file with stat()
  46.  
  47. static char *fullpath;
  48.  
  49. static int myftw(char* pathname, MyFunc *func)
  50. {
  51. int len;
  52.  
  53. //fullpath = path_alloc(&len);
  54. fullpath = calloc(PATH_MAX+1, sizeof(char));
  55. len = PATH_MAX;
  56.  
  57. strncpy(fullpath, pathname, len);
  58. fullpath[len-1] = 0;
  59.  
  60. return dopath(func);
  61. }
  62.  
  63. static int dopath(MyFunc *func)
  64. {
  65. struct stat statbuf;
  66. struct dirent *dirp;
  67. DIR *dp;
  68. int ret;
  69. char *ptr;
  70.  
  71. if (lstat(fullpath, &statbuf) < 0) // stat ERROR
  72. {
  73. return func(fullpath, &statbuf, FTW_NS);
  74. }
  75.  
  76. if (S_ISDIR(statbuf.st_mode) == 0) // Not dir
  77. {
  78. return func(fullpath, &statbuf, FTW_F);
  79. }
  80.  
  81. // Working with dir
  82. if ((ret = func(fullpath, &statbuf, FTW_D)) != 0)
  83. {
  84. return ret;
  85. }
  86.  
  87. ptr = fullpath + strlen(fullpath);
  88. *ptr++ = '/';
  89. *ptr = 0;
  90.  
  91. if ((dp = opendir(fullpath)) == NULL)
  92. {
  93. return func(fullpath, &statbuf, FTW_DNR);
  94. }
  95.  
  96. while ((dirp = readdir(dp)) != NULL)
  97. {
  98. if ((strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..") == 0))
  99. {
  100. continue;
  101. }
  102.  
  103. strcpy(ptr, dirp->d_name);
  104.  
  105. if ((ret = dopath(func)) != 0)
  106. {
  107. break;
  108. }
  109. }
  110.  
  111. ptr[-1] = 0;
  112.  
  113. if (closedir(dp) < 0)
  114. {
  115. printf("Can't close dir %s", fullpath);
  116. }
  117.  
  118. return ret;
  119. }
  120.  
  121. static int myfunc(const char *pathname, const struct stat *statptr, int type)
  122. {
  123. switch (type)
  124. {
  125. case FTW_F:
  126. switch (statptr->st_mode & S_IFMT)
  127. {
  128. case S_IFREG: nreg++; break;
  129. case S_IFBLK: nblk++; break;
  130. case S_IFCHR: nchr++; break;
  131. case S_IFIFO: nfifo++; break;
  132. case S_IFLNK: nslink++; break;
  133. case S_IFSOCK: nsock++; break;
  134. case S_IFDIR: printf("S_IFDIR for %s", pathname);
  135. }
  136. break;
  137. case FTW_D:
  138. ndir++;
  139. break;
  140. case FTW_DNR:
  141. printf("Private dir %s", pathname);
  142. break;
  143. case FTW_NS:
  144. printf("Can't stat() %s", pathname);
  145. break;
  146. default:
  147. printf("Unknown type %d for %s", type, pathname);
  148. break;
  149. }
  150.  
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement