1 /* vi: set sw=4 ts=4: */
3 * Mini du implementation for busybox
5 * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 * Copyright (C) 2002 Edward Betts <edward@debian.org>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 /* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
13 /* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
15 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
17 * Mostly rewritten for SUSv3 compliance and to fix bugs/defects.
18 * 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options.
19 * The -d option allows setting of max depth (similar to gnu --max-depth).
20 * 2) Fixed incorrect size calculations for links and directories, especially
21 * when errors occurred. Calculates sizes should now match gnu du output.
22 * 3) Added error checking of output.
23 * 4) Fixed busybox bug #1284 involving long overflow with human_readable.
29 #if ENABLE_FEATURE_HUMAN_READABLE
30 unsigned long disp_hr;
36 nlink_t count_hardlinks;
45 #define G (*(struct globals*)&bb_common_bufsiz1)
48 static void print(unsigned long size, const char *filename)
50 /* TODO - May not want to defer error checking here. */
51 #if ENABLE_FEATURE_HUMAN_READABLE
52 printf("%s\t%s\n", make_human_readable_str(size, 512, G.disp_hr),
59 printf("%ld\t%s\n", size, filename);
63 /* tiny recursive du */
64 static unsigned long du(const char *filename)
69 if (lstat(filename, &statbuf) != 0) {
70 bb_perror_msg("%s", filename);
71 G.status = EXIT_FAILURE;
75 if (G.one_file_system) {
76 if (G.du_depth == 0) {
77 G.dir_dev = statbuf.st_dev;
78 } else if (G.dir_dev != statbuf.st_dev) {
83 sum = statbuf.st_blocks;
85 if (S_ISLNK(statbuf.st_mode)) {
86 if (G.slink_depth > G.du_depth) { /* -H or -L */
87 if (stat(filename, &statbuf) != 0) {
88 bb_perror_msg("%s", filename);
89 G.status = EXIT_FAILURE;
92 sum = statbuf.st_blocks;
93 if (G.slink_depth == 1) {
94 G.slink_depth = INT_MAX; /* Convert -H to -L. */
99 if (statbuf.st_nlink > G.count_hardlinks) {
100 /* Add files/directories with links only once */
101 if (is_in_ino_dev_hashtable(&statbuf)) {
104 add_to_ino_dev_hashtable(&statbuf, NULL);
107 if (S_ISDIR(statbuf.st_mode)) {
109 struct dirent *entry;
112 dir = warn_opendir(filename);
114 G.status = EXIT_FAILURE;
118 newfile = last_char_is(filename, '/');
122 while ((entry = readdir(dir))) {
123 char *name = entry->d_name;
125 newfile = concat_subpath_file(filename, name);
134 } else if (G.du_depth > G.print_files) {
137 if (G.du_depth <= G.max_print_depth) {
138 print(sum, filename);
143 int du_main(int argc, char **argv);
144 int du_main(int argc, char **argv)
147 int slink_depth_save;
148 bool print_final_total;
149 char *smax_print_depth;
152 #if ENABLE_FEATURE_HUMAN_READABLE
153 USE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
154 SKIP_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
155 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
158 USE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
159 /* SKIP_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
161 G.max_print_depth = INT_MAX;
162 G.count_hardlinks = 1;
164 /* Note: SUSv3 specifies that -a and -s options cannot be used together
165 * in strictly conforming applications. However, it also says that some
166 * du implementations may produce output when -a and -s are used together.
167 * gnu du exits with an error code in this case. We choose to simply
168 * ignore -a. This is consistent with -s being equivalent to -d 0.
170 #if ENABLE_FEATURE_HUMAN_READABLE
171 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
172 opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
174 if (opt & (1 << 9)) {
178 if (opt & (1 << 10)) {
180 G.disp_hr = 1024*1024;
182 if (opt & (1 << 2)) {
187 opt_complementary = "H-L:L-H:s-d:d-s";
188 opt = getopt32(argv, "aHkLsx" "d:" "lc", &smax_print_depth);
190 #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
191 if (opt & (1 << 2)) {
197 if (opt & (1 << 0)) {
199 G.print_files = INT_MAX;
201 if (opt & (1 << 1)) {
205 if (opt & (1 << 3)) {
207 G.slink_depth = INT_MAX;
209 if (opt & (1 << 4)) {
211 G.max_print_depth = 0;
213 G.one_file_system = opt & (1 << 5); /* -x opt */
214 if (opt & (1 << 6)) {
216 G.max_print_depth = xatoi_u(smax_print_depth);
218 if (opt & (1 << 7)) {
220 G.count_hardlinks = MAXINT(nlink_t);
222 print_final_total = opt & (1 << 8); /* -c opt */
224 /* go through remaining args (if any) */
226 *--argv = (char*)".";
227 if (G.slink_depth == 1) {
232 slink_depth_save = G.slink_depth;
236 G.slink_depth = slink_depth_save;
239 if (ENABLE_FEATURE_CLEAN_UP)
240 reset_ino_dev_hashtable();
241 if (print_final_total)
242 print(total, "total");
244 fflush_stdout_and_exit(G.status);