a bit more IPv6-ization work
[oweals/busybox.git] / coreutils / du.c
index b204a025ad47ff1c001e54b9c502d9214bce2696..a1ca5b59b6cd73c2bedc9667ca471451ff6b05fa 100644 (file)
  * 4) Fixed busybox bug #1284 involving long overflow with human_readable.
  */
 
-#include <stdlib.h>
-#include <limits.h>
-#include <unistd.h>
-#include <dirent.h>
-#include <sys/stat.h>
 #include "busybox.h"
 
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-# ifdef CONFIG_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
-static unsigned long disp_hr = KILOBYTE;
+#if ENABLE_FEATURE_HUMAN_READABLE
+# if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
+static unsigned long disp_hr = 1024;
 # else
 static unsigned long disp_hr = 512;
 # endif
-#elif defined CONFIG_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
-static unsigned int disp_k = 1;
+#elif ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
+static unsigned disp_k = 1;
 #else
-static unsigned int disp_k;    /* bss inits to 0 */
+static unsigned disp_k;        /* bss inits to 0 */
 #endif
 
 static int max_print_depth = INT_MAX;
 static nlink_t count_hardlinks = 1;
 
-static int status
-#if EXIT_SUCCESS == 0
-       = EXIT_SUCCESS
-#endif
-       ;
+static int status;
 static int print_files;
 static int slink_depth;
 static int du_depth;
@@ -57,28 +48,28 @@ static int one_file_system;
 static dev_t dir_dev;
 
 
-static void print(long size, char *filename)
+static void print(long size, const char * const filename)
 {
        /* TODO - May not want to defer error checking here. */
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-       bb_printf("%s\t%s\n", make_human_readable_str(size, 512, disp_hr),
-                  filename);
+#if ENABLE_FEATURE_HUMAN_READABLE
+       printf("%s\t%s\n", make_human_readable_str(size, 512, disp_hr),
+                       filename);
 #else
        if (disp_k) {
                size++;
                size >>= 1;
        }
-       bb_printf("%ld\t%s\n", size, filename);
+       printf("%ld\t%s\n", size, filename);
 #endif
 }
 
 /* tiny recursive du */
-static long du(char *filename)
+static long du(const char * const filename)
 {
        struct stat statbuf;
        long sum;
 
-       if ((lstat(filename, &statbuf)) != 0) {
+       if (lstat(filename, &statbuf) != 0) {
                bb_perror_msg("%s", filename);
                status = EXIT_FAILURE;
                return 0;
@@ -96,7 +87,7 @@ static long du(char *filename)
 
        if (S_ISLNK(statbuf.st_mode)) {
                if (slink_depth > du_depth) {   /* -H or -L */
-                       if ((stat(filename, &statbuf)) != 0) {
+                       if (stat(filename, &statbuf) != 0) {
                                bb_perror_msg("%s", filename);
                                status = EXIT_FAILURE;
                                return 0;
@@ -121,7 +112,7 @@ static long du(char *filename)
                struct dirent *entry;
                char *newfile;
 
-               dir = bb_opendir(filename);
+               dir = warn_opendir(filename);
                if (!dir) {
                        status = EXIT_FAILURE;
                        return sum;
@@ -135,7 +126,7 @@ static long du(char *filename)
                        char *name = entry->d_name;
 
                        newfile = concat_subpath_file(filename, name);
-                       if(newfile == NULL)
+                       if (newfile == NULL)
                                continue;
                        ++du_depth;
                        sum += du(newfile);
@@ -158,11 +149,11 @@ int du_main(int argc, char **argv)
        int slink_depth_save;
        int print_final_total;
        char *smax_print_depth;
-       unsigned long opt;
+       unsigned opt;
 
-#ifdef CONFIG_FEATURE_DU_DEFUALT_BLOCKSIZE_1K
+#if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
        if (getenv("POSIXLY_CORRECT")) {        /* TODO - a new libbb function? */
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
+#if ENABLE_FEATURE_HUMAN_READABLE
                disp_hr = 512;
 #else
                disp_k = 0;
@@ -170,61 +161,61 @@ int du_main(int argc, char **argv)
        }
 #endif
 
-       /* Note: SUSv3 specifies that -a and -s options can not be used together
+       /* Note: SUSv3 specifies that -a and -s options cannot be used together
         * in strictly conforming applications.  However, it also says that some
         * du implementations may produce output when -a and -s are used together.
         * gnu du exits with an error code in this case.  We choose to simply
         * ignore -a.  This is consistent with -s being equivalent to -d 0.
         */
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-       bb_opt_complementally = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
-       opt = bb_getopt_ulflags(argc, argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
-       if((opt & (1 << 9))) {
+#if ENABLE_FEATURE_HUMAN_READABLE
+       opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
+       opt = getopt32(argc, argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
+       if (opt & (1 << 9)) {
                /* -h opt */
                disp_hr = 0;
        }
-       if((opt & (1 << 10))) {
+       if (opt & (1 << 10)) {
                /* -m opt */
-               disp_hr = MEGABYTE;
+               disp_hr = 1024*1024;
        }
-       if((opt & (1 << 2))) {
+       if (opt & (1 << 2)) {
                /* -k opt */
-                       disp_hr = KILOBYTE;
+               disp_hr = 1024;
        }
 #else
-       bb_opt_complementally = "H-L:L-H:s-d:d-s";
-       opt = bb_getopt_ulflags(argc, argv, "aHkLsx" "d:" "lc", &smax_print_depth);
-#if !defined CONFIG_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
-       if((opt & (1 << 2))) {
+       opt_complementary = "H-L:L-H:s-d:d-s";
+       opt = getopt32(argc, argv, "aHkLsx" "d:" "lc", &smax_print_depth);
+#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
+       if (opt & (1 << 2)) {
                /* -k opt */
-                       disp_k = 1;
+               disp_k = 1;
        }
 #endif
 #endif
-       if((opt & (1 << 0))) {
+       if (opt & (1 << 0)) {
                /* -a opt */
                print_files = INT_MAX;
        }
-       if((opt & (1 << 1))) {
+       if (opt & (1 << 1)) {
                /* -H opt */
                slink_depth = 1;
        }
-       if((opt & (1 << 3))) {
+       if (opt & (1 << 3)) {
                /* -L opt */
-                       slink_depth = INT_MAX;
+               slink_depth = INT_MAX;
        }
-       if((opt & (1 << 4))) {
+       if (opt & (1 << 4)) {
                /* -s opt */
-                       max_print_depth = 0;
-               }
+               max_print_depth = 0;
+       }
        one_file_system = opt & (1 << 5); /* -x opt */
-       if((opt & (1 << 6))) {
+       if (opt & (1 << 6)) {
                /* -d opt */
-               max_print_depth = bb_xgetularg10_bnd(smax_print_depth, 0, INT_MAX);
+               max_print_depth = xatoi_u(smax_print_depth);
        }
-       if((opt & (1 << 7))) {
+       if (opt & (1 << 7)) {
                /* -l opt */
-               count_hardlinks = INT_MAX;
+               count_hardlinks = MAXINT(nlink_t);
        }
        print_final_total = opt & (1 << 8); /* -c opt */
 
@@ -243,7 +234,7 @@ int du_main(int argc, char **argv)
                total += du(*argv);
                slink_depth = slink_depth_save;
        } while (*++argv);
-#ifdef CONFIG_FEATURE_CLEAN_UP
+#if ENABLE_FEATURE_CLEAN_UP
        reset_ino_dev_hashtable();
 #endif
 
@@ -251,5 +242,5 @@ int du_main(int argc, char **argv)
                print(total, "total");
        }
 
-       bb_fflush_stdout_and_exit(status);
+       fflush_stdout_and_exit(status);
 }