rpm2cpio: handle bz2 too; code shrink
[oweals/busybox.git] / coreutils / od_bloaty.c
index 6948bf1e14e487be2fc9180cc346fe9b7980dc83..00efec51c64779aaa7adda5b3483ff8461a519a1 100644 (file)
@@ -17,7 +17,7 @@
 
 /* Written by Jim Meyering.  */
 
-/* Busyboxed by Denis Vlasenko
+/* Busyboxed by Denys Vlasenko
 
 Based on od.c from coreutils-5.2.1
 Top bloat sources:
@@ -50,7 +50,6 @@ diff -u -a std bbox >bbox.diff || { echo Different!; sleep 1; }
 */
 
 #include "libbb.h"
-#include <getopt.h>
 
 #define assert(a) ((void)0)
 
@@ -188,7 +187,7 @@ static off_t pseudo_offset;
    a multiple of the least common multiple of the sizes associated with
    the specified output types.  It should be as large as possible, but
    no larger than 16 -- unless specified with the -w option.  */
-static size_t bytes_per_block;
+static unsigned bytes_per_block = 32; /* have to use unsigned, not size_t */
 
 /* A NULL-terminated list of the file-arguments from the command line.  */
 static const char *const *file_list;
@@ -360,12 +359,12 @@ print_long_double(size_t n_bytes, const char *block, const char *fmt_string)
 }
 
 /* print_[named]_ascii are optimized for speed.
- * Remember, someday you may want to pump gigabytes thru this thing.
+ * Remember, someday you may want to pump gigabytes through this thing.
  * Saving a dozen of .text bytes here is counter-productive */
 
 static void
 print_named_ascii(size_t n_bytes, const char *block,
-               const char *unused_fmt_string ATTRIBUTE_UNUSED)
+               const char *unused_fmt_string UNUSED_PARAM)
 {
        /* Names for some non-printing characters.  */
        static const char charname[33][3] ALIGN1 = {
@@ -405,7 +404,7 @@ print_named_ascii(size_t n_bytes, const char *block,
 
 static void
 print_ascii(size_t n_bytes, const char *block,
-               const char *unused_fmt_string ATTRIBUTE_UNUSED)
+               const char *unused_fmt_string UNUSED_PARAM)
 {
        // buf[N] pos:  01234 56789
        char buf[12] = "   x\0 0xx\0";
@@ -509,10 +508,10 @@ check_and_close(void)
 }
 
 /* If S points to a single valid modern od format string, put
-   a description of that format in *TSPEC, make *NEXT point at the
-   character following the just-decoded format (if *NEXT is non-NULL),
-   and return zero.  For example, if S were "d4afL"
-   *NEXT would be set to "afL" and *TSPEC would be
+   a description of that format in *TSPEC, return pointer to
+   character following the just-decoded format.
+   For example, if S were "d4afL", we will return a rtp to "afL"
+   and *TSPEC would be
        {
                fmt = SIGNED_DECIMAL;
                size = INT or LONG; (whichever integral_type_size[4] resolves to)
@@ -522,9 +521,8 @@ check_and_close(void)
    S_ORIG is solely for reporting errors.  It should be the full format
    string argument. */
 
-static void
-decode_one_format(const char *s_orig, const char *s, const char **next,
-                                          struct tspec *tspec)
+static const char *
+decode_one_format(const char *s_orig, const char *s, struct tspec *tspec)
 {
        enum size_spec size_spec;
        unsigned size;
@@ -537,7 +535,6 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
        unsigned field_width = 0;
        int pos;
 
-       assert(tspec != NULL);
 
        switch (*s) {
        case 'd':
@@ -563,13 +560,14 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
                                s = end;
                        }
                } else {
-                       static const uint8_t CSIL_sizeof[] = {
+                       static const uint8_t CSIL_sizeof[4] = {
                                sizeof(char),
                                sizeof(short),
                                sizeof(int),
                                sizeof(long),
                        };
                        size = CSIL_sizeof[p - CSIL];
+                       s++; /* skip C/S/I/L */
                }
 
 #define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
@@ -717,8 +715,7 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
        if (tspec->hexl_mode_trailer)
                s++;
 
-       if (next != NULL)
-               *next = s;
+       return s;
 }
 
 /* Decode the modern od format string S.  Append the decoded
@@ -734,13 +731,13 @@ decode_format_string(const char *s)
                struct tspec tspec;
                const char *next;
 
-               decode_one_format(s_orig, s, &next, &tspec);
+               next = decode_one_format(s_orig, s, &tspec);
 
                assert(s != next);
                s = next;
+               spec = xrealloc_vector(spec, 4, n_specs);
+               memcpy(&spec[n_specs], &tspec, sizeof(spec[0]));
                n_specs++;
-               spec = xrealloc(spec, n_specs * sizeof(*spec));
-               memcpy(&spec[n_specs-1], &tspec, sizeof *spec);
        }
 }
 
@@ -817,7 +814,7 @@ skip(off_t n_skip)
 typedef void FN_format_address(off_t address, char c);
 
 static void
-format_address_none(off_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED)
+format_address_none(off_t address UNUSED_PARAM, char c UNUSED_PARAM)
 {
 }
 
@@ -835,7 +832,7 @@ format_address_std(off_t address, char c)
        printf(address_fmt, address);
 }
 
-#if ENABLE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
 /* only used with --traditional */
 static void
 format_address_paren(off_t address, char c)
@@ -956,7 +953,7 @@ get_lcm(void)
        return l_c_m;
 }
 
-#if ENABLE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
 /* If S is a valid traditional offset specification with an optional
    leading '+' return nonzero and set *OFFSET to the offset it denotes.  */
 
@@ -1177,7 +1174,6 @@ dump_strings(off_t address, off_t end_offset)
 int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int od_main(int argc, char **argv)
 {
-       static char const *const default_file_list[] = { "-", NULL };
        static const struct suffix_mult bkm[] = {
                { "b", 512 },
                { "k", 1024 },
@@ -1203,9 +1199,9 @@ int od_main(int argc, char **argv)
                OPT_s = 1 << 15,
                OPT_S = 1 << 16,
                OPT_w = 1 << 17,
-               OPT_traditional = (1 << 18) * ENABLE_GETOPT_LONG,
+               OPT_traditional = (1 << 18) * ENABLE_LONG_OPTS,
        };
-#if ENABLE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
        static const char od_longopts[] ALIGN1 =
                "skip-bytes\0"        Required_argument "j"
                "address-radix\0"     Required_argument "A"
@@ -1218,7 +1214,6 @@ int od_main(int argc, char **argv)
                ;
 #endif
        char *str_A, *str_N, *str_j, *str_S;
-       char *str_w = NULL;
        llist_t *lst_t = NULL;
        unsigned opt;
        int l_c_m;
@@ -1239,8 +1234,8 @@ int od_main(int argc, char **argv)
        /* flag_dump_strings = 0; - already is */
 
        /* Parse command line */
-       opt_complementary = "t::"; // list
-#if ENABLE_GETOPT_LONG
+       opt_complementary = "w+:t::"; /* -w N, -t is a list */
+#if ENABLE_LONG_OPTS
        applet_long_options = od_longopts;
 #endif
        opt = getopt32(argv, "A:N:abcdfhij:lot:vxsS:"
@@ -1248,7 +1243,7 @@ int od_main(int argc, char **argv)
                // -S was -s and also had optional parameter
                // but in coreutils 6.3 it was renamed and now has
                // _mandatory_ parameter
-               &str_A, &str_N, &str_j, &lst_t, &str_S, &str_w);
+               &str_A, &str_N, &str_j, &lst_t, &str_S, &bytes_per_block);
        argc -= optind;
        argv += optind;
        if (opt & OPT_A) {
@@ -1286,8 +1281,7 @@ int od_main(int argc, char **argv)
        if (opt & OPT_o) decode_format_string("o2");
        //if (opt & OPT_t)...
        while (lst_t) {
-               decode_format_string(lst_t->data);
-               lst_t = lst_t->link;
+               decode_format_string(llist_pop(&lst_t));
        }
        if (opt & OPT_v) verbose = 1;
        if (opt & OPT_x) decode_format_string("x2");
@@ -1312,7 +1306,7 @@ int od_main(int argc, char **argv)
         * FIXME: POSIX 1003.1-2001 with XSI requires support for the
         * traditional syntax even if --traditional is not given.  */
 
-#if ENABLE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
        if (opt & OPT_traditional) {
                off_t o1, o2;
 
@@ -1384,7 +1378,7 @@ int od_main(int argc, char **argv)
        /* If no files were listed on the command line,
           set the global pointer FILE_LIST so that it
           references the null-terminated list of one name: "-".  */
-       file_list = default_file_list;
+       file_list = bb_argv_dash;
        if (argc > 0) {
                /* Set the global pointer FILE_LIST so that it
                   references the first file-argument on the command-line.  */
@@ -1404,9 +1398,6 @@ int od_main(int argc, char **argv)
        l_c_m = get_lcm();
 
        if (opt & OPT_w) { /* -w: width */
-               bytes_per_block = 32;
-               if (str_w)
-                       bytes_per_block = xatou(str_w);
                if (!bytes_per_block || bytes_per_block % l_c_m != 0) {
                        bb_error_msg("warning: invalid width %u; using %d instead",
                                        (unsigned)bytes_per_block, l_c_m);