Advertisement
arter97

Untitled

Aug 7th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstring>
  3. #include <string>
  4.  
  5. #include <unistd.h>
  6. #include <dirent.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9.  
  10. using namespace std;
  11.  
  12. uint64_t Get_Folder_Size(const string& Path) {
  13.     DIR* d;
  14.     struct dirent* de;
  15.     struct stat st;
  16.     uint64_t dusize = 0;
  17.     string FullPath;
  18.  
  19.     d = opendir(Path.c_str());
  20.     if (d == NULL) {
  21.         return 0;
  22.     }
  23.  
  24.     while ((de = readdir(d)) != NULL) {
  25.         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
  26.             continue;
  27.         FullPath = Path + "/";
  28.         FullPath += de->d_name;
  29.         if (lstat(FullPath.c_str(), &st)) {
  30.             continue;
  31.         }
  32.         if ((st.st_mode & S_IFDIR) && de->d_type != DT_SOCK) {
  33.             dusize += Get_Folder_Size(FullPath);
  34.         } else if (st.st_mode & S_IFREG || st.st_mode & S_IFLNK) {
  35.             dusize += (uint64_t)(st.st_size);
  36.         }
  37.     }
  38.     closedir(d);
  39.     return dusize;
  40. }
  41.  
  42. int main(int argc, char **argv) {
  43.     string arg = argv[1];
  44.     printf("Size : %lu\n", Get_Folder_Size(arg));
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement