-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdu.c
82 lines (73 loc) · 1.83 KB
/
du.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <bsd/string.h>
#define PATH_LEN (64)
char slash[] = "/";
void reset_path(char *dir_path, char* path){
strlcpy(path, dir_path, PATH_LEN);
strlcat(path, slash, PATH_LEN);
}
void int2str(int value, int i, char* output){
int j;
while(i){
j = (value / i) % 10;
*output++ = '0' + j;
i /= 10;
}
}
void set_num_str(int value, char *buf){
int size, mod;
size = value / 4096;
mod = value % 4096;
int2str(size, 10, buf);
if(*buf == '0'){
*buf = ' ';
}
buf += 2;
*buf++ = '.';
int2str(mod, 10, buf);
buf += 2;
*buf = '\0';
}
size_t count_dir_size(char *dir_path){
struct dirent* dir;
struct stat statbuf;
size_t count = 0;
char *path;
char size_buf[10];
DIR* directory = opendir(dir_path);
if(!directory){
fprintf(stderr, "err:%s\n", dir_path);
return 0;
}
path = malloc(PATH_LEN);
reset_path(dir_path, path);
// printf("opening %s fd %d\n", dir_path, directory->fd);
while((dir = readdir(directory)) != NULL){
if(*dir->d_name == '.')
continue;
strlcat(path, (const char*)dir->d_name, PATH_LEN);
if(dir->d_type == DT_DIR){
count += count_dir_size(path);
}else{
(void)stat(path, &statbuf);
// printf("stat %s %d size %d\n", dir->d_name, ret, statbuf.st_size);
count += (size_t)statbuf.st_size;
}
reset_path(dir_path, path);
}
set_num_str(count, size_buf);
printf("%sKB %s\n", size_buf, dir_path);
closedir(directory);
free(path);
return count;
}
int main(int argc, char **argv){
char *curr_dir = ".";
if(argc > 1)
curr_dir = argv[1];
count_dir_size(curr_dir);
return 0;
}