-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotalsize.cpp
More file actions
42 lines (36 loc) · 924 Bytes
/
Copy pathtotalsize.cpp
File metadata and controls
42 lines (36 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
using namespace std;
int main(int argc, char *argv[]) {
long long sumsize = 0;
char *dir_path = argv[1];
struct dirent *dirp;
DIR *dp = opendir(dir_path);
if (dp == NULL) {
return 1;
}
char *fullpath = new char[1000];
int dirlen = strlen(dir_path);
strcpy(fullpath, dir_path);
if (fullpath[dirlen] != '/') {
fullpath[dirlen] = '/';
fullpath[dirlen+1] = '\0';
}
dirlen++;
while ((dirp = readdir(dp)) != NULL) {
struct stat buf;
strcpy(fullpath + dirlen, dirp->d_name);
int err = lstat(fullpath, &buf);
if (err == -1)
continue;
if (S_ISREG(buf.st_mode)) {
sumsize += buf.st_size;
}
}
cout << sumsize << endl;
}