ls: do not follow links with -s (closes bug 33),
[oweals/busybox.git] / coreutils / od_bloaty.c
index a083114c9165d1921bfba483b62d35288132b72c..eb45798577919b57b1a1fd8daa97650a580ef9e4 100644 (file)
@@ -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;
@@ -365,7 +364,7 @@ print_long_double(size_t n_bytes, const char *block, const char *fmt_string)
 
 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)
 {
 }
 
@@ -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 },
@@ -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,7 +1234,7 @@ int od_main(int argc, char **argv)
        /* flag_dump_strings = 0; - already is */
 
        /* Parse command line */
-       opt_complementary = "t::"; // list
+       opt_complementary = "w+:t::"; /* -w N, -t is a list */
 #if ENABLE_GETOPT_LONG
        applet_long_options = od_longopts;
 #endif
@@ -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");
@@ -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);