Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/du.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/du.c
r1765 r2725 7 7 * Copyright (C) 2002 Edward Betts <edward@debian.org> 8 8 * 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.9 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 10 10 */ 11 11 … … 26 26 #include "libbb.h" 27 27 28 #if ENABLE_FEATURE_HUMAN_READABLE 29 # if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K 30 static unsigned long disp_hr = 1024; 31 # else 32 static unsigned long disp_hr = 512; 33 # endif 34 #elif ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K 35 static unsigned disp_k = 1; 36 #else 37 static unsigned disp_k; /* bss inits to 0 */ 38 #endif 39 40 static int max_print_depth = INT_MAX; 41 static nlink_t count_hardlinks = 1; 42 43 static int status; 44 static int print_files; 45 static int slink_depth; 46 static int du_depth; 47 static int one_file_system; 48 static dev_t dir_dev; 49 50 51 static void print(long size, const char *const filename) 28 enum { 29 OPT_a_files_too = (1 << 0), 30 OPT_H_follow_links = (1 << 1), 31 OPT_k_kbytes = (1 << 2), 32 OPT_L_follow_links = (1 << 3), 33 OPT_s_total_norecurse = (1 << 4), 34 OPT_x_one_FS = (1 << 5), 35 OPT_d_maxdepth = (1 << 6), 36 OPT_l_hardlinks = (1 << 7), 37 OPT_c_total = (1 << 8), 38 OPT_h_for_humans = (1 << 9), 39 OPT_m_mbytes = (1 << 10), 40 }; 41 42 struct globals { 43 #if ENABLE_FEATURE_HUMAN_READABLE 44 unsigned long disp_hr; 45 #else 46 unsigned disp_k; 47 #endif 48 int max_print_depth; 49 bool status; 50 int slink_depth; 51 int du_depth; 52 dev_t dir_dev; 53 } FIX_ALIASING; 54 #define G (*(struct globals*)&bb_common_bufsiz1) 55 56 57 static void print(unsigned long size, const char *filename) 52 58 { 53 59 /* TODO - May not want to defer error checking here. */ 54 60 #if ENABLE_FEATURE_HUMAN_READABLE 55 printf("%s\t%s\n", make_human_readable_str(size, 512, disp_hr), 61 printf("%s\t%s\n", 62 /* size x 512 / G.disp_hr, show one fractional, 63 * use suffixes if G.disp_hr == 0 */ 64 make_human_readable_str(size, 512, G.disp_hr), 56 65 filename); 57 66 #else 58 if ( disp_k) {67 if (G.disp_k) { 59 68 size++; 60 69 size >>= 1; 61 70 } 62 printf("%l d\t%s\n", size, filename);71 printf("%lu\t%s\n", size, filename); 63 72 #endif 64 73 } 65 74 66 75 /* tiny recursive du */ 67 static long du(const char *constfilename)76 static unsigned long du(const char *filename) 68 77 { 69 78 struct stat statbuf; 70 long sum;79 unsigned long sum; 71 80 72 81 if (lstat(filename, &statbuf) != 0) { 73 bb_ perror_msg("%s",filename);74 status = EXIT_FAILURE;82 bb_simple_perror_msg(filename); 83 G.status = EXIT_FAILURE; 75 84 return 0; 76 85 } 77 86 78 if (o ne_file_system) {79 if ( du_depth == 0) {80 dir_dev = statbuf.st_dev;81 } else if ( dir_dev != statbuf.st_dev) {87 if (option_mask32 & OPT_x_one_FS) { 88 if (G.du_depth == 0) { 89 G.dir_dev = statbuf.st_dev; 90 } else if (G.dir_dev != statbuf.st_dev) { 82 91 return 0; 83 92 } … … 87 96 88 97 if (S_ISLNK(statbuf.st_mode)) { 89 if ( slink_depth > du_depth) {/* -H or -L */98 if (G.slink_depth > G.du_depth) { /* -H or -L */ 90 99 if (stat(filename, &statbuf) != 0) { 91 bb_ perror_msg("%s",filename);92 status = EXIT_FAILURE;100 bb_simple_perror_msg(filename); 101 G.status = EXIT_FAILURE; 93 102 return 0; 94 103 } 95 104 sum = statbuf.st_blocks; 96 if (slink_depth == 1) { 97 slink_depth = INT_MAX; /* Convert -H to -L. */ 105 if (G.slink_depth == 1) { 106 /* Convert -H to -L */ 107 G.slink_depth = INT_MAX; 98 108 } 99 109 } 100 110 } 101 111 102 if (statbuf.st_nlink > count_hardlinks) { 112 if (!(option_mask32 & OPT_l_hardlinks) 113 && statbuf.st_nlink > 1 114 ) { 103 115 /* Add files/directories with links only once */ 104 116 if (is_in_ino_dev_hashtable(&statbuf)) { … … 115 127 dir = warn_opendir(filename); 116 128 if (!dir) { 117 status = EXIT_FAILURE;129 G.status = EXIT_FAILURE; 118 130 return sum; 119 131 } 120 132 121 newfile = last_char_is(filename, '/');122 if (newfile)123 *newfile = '\0';124 125 133 while ((entry = readdir(dir))) { 126 char *name = entry->d_name; 127 128 newfile = concat_subpath_file(filename, name); 134 newfile = concat_subpath_file(filename, entry->d_name); 129 135 if (newfile == NULL) 130 136 continue; 131 ++ du_depth;137 ++G.du_depth; 132 138 sum += du(newfile); 133 -- du_depth;139 --G.du_depth; 134 140 free(newfile); 135 141 } 136 142 closedir(dir); 137 } else if (du_depth > print_files) { 138 return sum; 139 } 140 if (du_depth <= max_print_depth) { 143 } else { 144 if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0) 145 return sum; 146 } 147 if (G.du_depth <= G.max_print_depth) { 141 148 print(sum, filename); 142 149 } … … 144 151 } 145 152 146 int du_main(int argc, char **argv) ;147 int du_main(int argc , char **argv)153 int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 154 int du_main(int argc UNUSED_PARAM, char **argv) 148 155 { 149 long total;156 unsigned long total; 150 157 int slink_depth_save; 151 int print_final_total;152 char *smax_print_depth;153 158 unsigned opt; 154 159 155 #if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K 156 if (getenv("POSIXLY_CORRECT")) { /* TODO - a new libbb function? */ 157 #if ENABLE_FEATURE_HUMAN_READABLE 158 disp_hr = 512; 159 #else 160 disp_k = 0; 161 #endif 162 } 163 #endif 160 #if ENABLE_FEATURE_HUMAN_READABLE 161 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;) 162 IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;) 163 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */ 164 G.disp_hr = 512; 165 #else 166 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;) 167 /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */ 168 #endif 169 G.max_print_depth = INT_MAX; 164 170 165 171 /* Note: SUSv3 specifies that -a and -s options cannot be used together … … 170 176 */ 171 177 #if ENABLE_FEATURE_HUMAN_READABLE 172 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s"; 173 opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth); 174 if (opt & (1 << 9)) { 175 /* -h opt */ 176 disp_hr = 0; 177 } 178 if (opt & (1 << 10)) { 179 /* -m opt */ 180 disp_hr = 1024*1024; 181 } 182 if (opt & (1 << 2)) { 183 /* -k opt */ 184 disp_hr = 1024; 185 } 186 #else 187 opt_complementary = "H-L:L-H:s-d:d-s"; 188 opt = getopt32(argv, "aHkLsx" "d:" "lc", &smax_print_depth); 178 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s:d+"; 179 opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth); 180 argv += optind; 181 if (opt & OPT_h_for_humans) { 182 G.disp_hr = 0; 183 } 184 if (opt & OPT_m_mbytes) { 185 G.disp_hr = 1024*1024; 186 } 187 if (opt & OPT_k_kbytes) { 188 G.disp_hr = 1024; 189 } 190 #else 191 opt_complementary = "H-L:L-H:s-d:d-s:d+"; 192 opt = getopt32(argv, "aHkLsx" "d:" "lc", &G.max_print_depth); 193 argv += optind; 189 194 #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K 190 if (opt & (1 << 2)) { 191 /* -k opt */ 192 disp_k = 1; 193 } 194 #endif 195 #endif 196 if (opt & (1 << 0)) { 197 /* -a opt */ 198 print_files = INT_MAX; 199 } 200 if (opt & (1 << 1)) { 201 /* -H opt */ 202 slink_depth = 1; 203 } 204 if (opt & (1 << 3)) { 205 /* -L opt */ 206 slink_depth = INT_MAX; 207 } 208 if (opt & (1 << 4)) { 209 /* -s opt */ 210 max_print_depth = 0; 211 } 212 one_file_system = opt & (1 << 5); /* -x opt */ 213 if (opt & (1 << 6)) { 214 /* -d opt */ 215 max_print_depth = xatoi_u(smax_print_depth); 216 } 217 if (opt & (1 << 7)) { 218 /* -l opt */ 219 count_hardlinks = MAXINT(nlink_t); 220 } 221 print_final_total = opt & (1 << 8); /* -c opt */ 195 if (opt & OPT_k_kbytes) { 196 G.disp_k = 1; 197 } 198 #endif 199 #endif 200 if (opt & OPT_H_follow_links) { 201 G.slink_depth = 1; 202 } 203 if (opt & OPT_L_follow_links) { 204 G.slink_depth = INT_MAX; 205 } 206 if (opt & OPT_s_total_norecurse) { 207 G.max_print_depth = 0; 208 } 222 209 223 210 /* go through remaining args (if any) */ 224 argv += optind; 225 if (optind >= argc) { 211 if (!*argv) { 226 212 *--argv = (char*)"."; 227 if ( slink_depth == 1) {228 slink_depth = 0;229 } 230 } 231 232 slink_depth_save = slink_depth;213 if (G.slink_depth == 1) { 214 G.slink_depth = 0; 215 } 216 } 217 218 slink_depth_save = G.slink_depth; 233 219 total = 0; 234 220 do { 235 221 total += du(*argv); 236 slink_depth = slink_depth_save; 222 /* otherwise du /dir /dir won't show /dir twice: */ 223 reset_ino_dev_hashtable(); 224 G.slink_depth = slink_depth_save; 237 225 } while (*++argv); 238 if (ENABLE_FEATURE_CLEAN_UP) 239 reset_ino_dev_hashtable(); 240 if (print_final_total) { 226 227 if (opt & OPT_c_total) 241 228 print(total, "total"); 242 } 243 244 fflush_stdout_and_exit(status); 229 230 fflush_stdout_and_exit(G.status); 245 231 }
Note:
See TracChangeset
for help on using the changeset viewer.