Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <cstring>
- #include <string>
- #include <unistd.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- using namespace std;
- uint64_t Get_Folder_Size(const string& Path) {
- DIR* d;
- struct dirent* de;
- struct stat st;
- uint64_t dusize = 0;
- string FullPath;
- d = opendir(Path.c_str());
- if (d == NULL) {
- return 0;
- }
- while ((de = readdir(d)) != NULL) {
- if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
- continue;
- FullPath = Path + "/";
- FullPath += de->d_name;
- if (lstat(FullPath.c_str(), &st)) {
- continue;
- }
- if ((st.st_mode & S_IFDIR) && de->d_type != DT_SOCK) {
- dusize += Get_Folder_Size(FullPath);
- } else if (st.st_mode & S_IFREG || st.st_mode & S_IFLNK) {
- dusize += (uint64_t)(st.st_size);
- }
- }
- closedir(d);
- return dusize;
- }
- int main(int argc, char **argv) {
- string arg = argv[1];
- printf("Size : %lu\n", Get_Folder_Size(arg));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement