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#include "libbb.h"
76#include "common_bufsiz.h"
77
78enum {
79 OPT_a_files_too = (1 << 0),
80 OPT_H_follow_links = (1 << 1),
81 OPT_k_kbytes = (1 << 2),
82 OPT_L_follow_links = (1 << 3),
83 OPT_s_total_norecurse = (1 << 4),
84 OPT_x_one_FS = (1 << 5),
85 OPT_d_maxdepth = (1 << 6),
86 OPT_l_hardlinks = (1 << 7),
87 OPT_c_total = (1 << 8),
88 OPT_b = (1 << 9),
89 OPT_h_for_humans = (1 << 10),
90 OPT_m_mbytes = (1 << 11),
91};
92
93struct globals {
94#if ENABLE_FEATURE_HUMAN_READABLE
95 unsigned long disp_unit;
96#else
97 unsigned disp_k;
98#endif
99 int max_print_depth;
100 bool status;
101 int slink_depth;
102 int du_depth;
103 dev_t dir_dev;
104} FIX_ALIASING;
105#define G (*(struct globals*)bb_common_bufsiz1)
106#define INIT_G() do { setup_common_bufsiz(); } while (0)
107
108
109static void print(unsigned long long size, const char *filename)
110{
111
112#if ENABLE_FEATURE_HUMAN_READABLE
113# if ENABLE_DESKTOP
114
115
116
117
118
119
120
121 if (G.disp_unit)
122 size += (G.disp_unit-1) / (unsigned)(512 * 2);
123# endif
124 printf("%s\t%s\n",
125
126
127
128
129 make_human_readable_str(size, (option_mask32 & OPT_b) ? 1 : 512, G.disp_unit),
130 filename);
131#else
132 if (G.disp_k) {
133 if (!(option_mask32 & OPT_b)) {
134 size++;
135 size >>= 1;
136 } else {
137 size >>= 10;
138 }
139 }
140 printf("%llu\t%s\n", size, filename);
141#endif
142}
143
144
145static unsigned long long du(const char *filename)
146{
147 struct stat statbuf;
148 unsigned long long sum;
149
150 if (lstat(filename, &statbuf) != 0) {
151 bb_simple_perror_msg(filename);
152 G.status = EXIT_FAILURE;
153 return 0;
154 }
155
156 if (option_mask32 & OPT_x_one_FS) {
157 if (G.du_depth == 0) {
158 G.dir_dev = statbuf.st_dev;
159 } else if (G.dir_dev != statbuf.st_dev) {
160 return 0;
161 }
162 }
163
164 sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
165
166 if (S_ISLNK(statbuf.st_mode)) {
167 if (G.slink_depth > G.du_depth) {
168 if (stat(filename, &statbuf) != 0) {
169 bb_simple_perror_msg(filename);
170 G.status = EXIT_FAILURE;
171 return 0;
172 }
173 sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
174 if (G.slink_depth == 1) {
175
176 G.slink_depth = INT_MAX;
177 }
178 }
179 }
180
181 if (!(option_mask32 & OPT_l_hardlinks)
182 && statbuf.st_nlink > 1
183 ) {
184
185 if (is_in_ino_dev_hashtable(&statbuf)) {
186 return 0;
187 }
188 add_to_ino_dev_hashtable(&statbuf, NULL);
189 }
190
191 if (S_ISDIR(statbuf.st_mode)) {
192 DIR *dir;
193 struct dirent *entry;
194 char *newfile;
195
196 dir = warn_opendir(filename);
197 if (!dir) {
198 G.status = EXIT_FAILURE;
199 return sum;
200 }
201
202 while ((entry = readdir(dir))) {
203 newfile = concat_subpath_file(filename, entry->d_name);
204 if (newfile == NULL)
205 continue;
206 ++G.du_depth;
207 sum += du(newfile);
208 --G.du_depth;
209 free(newfile);
210 }
211 closedir(dir);
212 } else {
213 if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
214 return sum;
215 }
216 if (G.du_depth <= G.max_print_depth) {
217 print(sum, filename);
218 }
219 return sum;
220}
221
222int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
223int du_main(int argc UNUSED_PARAM, char **argv)
224{
225 unsigned long long total;
226 int slink_depth_save;
227 unsigned opt;
228
229 INIT_G();
230
231#if ENABLE_FEATURE_HUMAN_READABLE
232 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
233 IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
234 if (getenv("POSIXLY_CORRECT"))
235 G.disp_unit = 512;
236#else
237 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
238
239#endif
240 G.max_print_depth = INT_MAX;
241
242
243
244
245
246
247
248#if ENABLE_FEATURE_HUMAN_READABLE
249 opt = getopt32(argv, "^"
250 "aHkLsxd:+lcbhm"
251 "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
252 &G.max_print_depth
253 );
254 argv += optind;
255 if (opt & OPT_b) {
256 G.disp_unit = 1;
257 }
258 if (opt & OPT_h_for_humans) {
259 G.disp_unit = 0;
260 }
261 if (opt & OPT_m_mbytes) {
262 G.disp_unit = 1024*1024;
263 }
264 if (opt & OPT_k_kbytes) {
265 G.disp_unit = 1024;
266 }
267#else
268 opt = getopt32(argv, "^"
269 "aHkLsxd:+lcb"
270 "\0" "H-L:L-H:s-d:d-s",
271 &G.max_print_depth
272 );
273 argv += optind;
274# if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
275 if (opt & OPT_k_kbytes) {
276 G.disp_k = 1;
277 }
278# endif
279#endif
280 if (opt & OPT_H_follow_links) {
281 G.slink_depth = 1;
282 }
283 if (opt & OPT_L_follow_links) {
284 G.slink_depth = INT_MAX;
285 }
286 if (opt & OPT_s_total_norecurse) {
287 G.max_print_depth = 0;
288 }
289
290
291 if (!*argv) {
292 *--argv = (char*)".";
293 if (G.slink_depth == 1) {
294 G.slink_depth = 0;
295 }
296 }
297
298 slink_depth_save = G.slink_depth;
299 total = 0;
300 do {
301 total += du(*argv);
302 G.slink_depth = slink_depth_save;
303 } while (*++argv);
304
305 if (ENABLE_FEATURE_CLEAN_UP)
306 reset_ino_dev_hashtable();
307 if (opt & OPT_c_total)
308 print(total, "total");
309
310 fflush_stdout_and_exit(G.status);
311}
312