Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/coreutils/od_bloaty.c

    r1765 r2725  
    1818/* Written by Jim Meyering.  */
    1919
    20 /* Busyboxed by Denis Vlasenko
     20/* Busyboxed by Denys Vlasenko
    2121
    2222Based on od.c from coreutils-5.2.1
     
    5151
    5252#include "libbb.h"
    53 #include <getopt.h>
    5453
    5554#define assert(a) ((void)0)
     
    182181   the number of bytes to skip.  */
    183182static off_t pseudo_offset;
    184 /* The number of input bytes to skip before formatting and writing.  */
    185 static off_t n_bytes_to_skip;
    186183/* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
    187184   input is formatted.  */
    188 /* The maximum number of bytes that will be formatted.  */
    189 static off_t max_bytes_to_format;
    190 /* The offset of the first byte after the last byte to be formatted.  */
    191 static off_t end_offset;
    192185
    193186/* The number of input bytes formatted per output line.  It must be
     
    195188   the specified output types.  It should be as large as possible, but
    196189   no larger than 16 -- unless specified with the -w option.  */
    197 static size_t bytes_per_block;
    198 
    199 /* Human-readable representation of *file_list (for error messages).
    200    It differs from *file_list only when *file_list is "-".  */
    201 static char const *input_filename;
     190static unsigned bytes_per_block = 32; /* have to use unsigned, not size_t */
    202191
    203192/* A NULL-terminated list of the file-arguments from the command line.  */
    204 static char const *const *file_list;
    205 
    206 /* Initializer for file_list if no file-arguments
    207    were specified on the command line.  */
    208 static char const *const default_file_list[] = { "-", NULL };
     193static const char *const *file_list;
    209194
    210195/* The input stream associated with the current file.  */
     
    212197
    213198#define MAX_INTEGRAL_TYPE_SIZE sizeof(ulonglong_t)
    214 static unsigned char integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1] ALIGN1 = {
     199static const unsigned char integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1] ALIGN1 = {
    215200    [sizeof(char)] = CHAR,
    216201#if USHRT_MAX != UCHAR_MAX
     
    229214
    230215#define MAX_FP_TYPE_SIZE sizeof(longdouble_t)
    231 static unsigned char fp_type_size[MAX_FP_TYPE_SIZE + 1] ALIGN1 = {
     216static const unsigned char fp_type_size[MAX_FP_TYPE_SIZE + 1] ALIGN1 = {
    232217    /* gcc seems to allow repeated indexes. Last one stays */
    233218    [sizeof(longdouble_t)] = FLOAT_LONG_DOUBLE,
     
    375360
    376361/* print_[named]_ascii are optimized for speed.
    377  * Remember, someday you may want to pump gigabytes thru this thing.
     362 * Remember, someday you may want to pump gigabytes through this thing.
    378363 * Saving a dozen of .text bytes here is counter-productive */
    379364
    380365static void
    381366print_named_ascii(size_t n_bytes, const char *block,
    382         const char *unused_fmt_string ATTRIBUTE_UNUSED)
     367        const char *unused_fmt_string UNUSED_PARAM)
    383368{
    384369    /* Names for some non-printing characters.  */
     
    420405static void
    421406print_ascii(size_t n_bytes, const char *block,
    422         const char *unused_fmt_string ATTRIBUTE_UNUSED)
     407        const char *unused_fmt_string UNUSED_PARAM)
    423408{
    424409    // buf[N] pos:  01234 56789
     
    482467{
    483468    while (1) {
    484         input_filename = *file_list;
    485         if (!input_filename)
     469        if (!*file_list)
    486470            return;
    487         file_list++;
    488         in_stream = fopen_or_warn_stdin(input_filename);
     471        in_stream = fopen_or_warn_stdin(*file_list++);
    489472        if (in_stream) {
    490             if (in_stream == stdin)
    491                 input_filename = bb_msg_standard_input;
    492473            break;
    493474        }
     
    511492    if (in_stream) {
    512493        if (ferror(in_stream))  {
    513             bb_error_msg("%s: read error", input_filename);
     494            bb_error_msg("%s: read error", (in_stream == stdin)
     495                    ? bb_msg_standard_input
     496                    : file_list[-1]
     497            );
    514498            ioerror = 1;
    515499        }
     
    519503
    520504    if (ferror(stdout)) {
    521         bb_error_msg("write error");
     505        bb_error_msg(bb_msg_write_error);
    522506        ioerror = 1;
    523507    }
     
    525509
    526510/* If S points to a single valid modern od format string, put
    527    a description of that format in *TSPEC, make *NEXT point at the
    528    character following the just-decoded format (if *NEXT is non-NULL),
    529    and return zero.  For example, if S were "d4afL"
    530    *NEXT would be set to "afL" and *TSPEC would be
     511   a description of that format in *TSPEC, return pointer to
     512   character following the just-decoded format.
     513   For example, if S were "d4afL", we will return a rtp to "afL"
     514   and *TSPEC would be
    531515    {
    532516        fmt = SIGNED_DECIMAL;
     
    538522   string argument. */
    539523
    540 static void
    541 decode_one_format(const char *s_orig, const char *s, const char **next,
    542                        struct tspec *tspec)
     524static NOINLINE const char *
     525decode_one_format(const char *s_orig, const char *s, struct tspec *tspec)
    543526{
    544527    enum size_spec size_spec;
     
    553536    int pos;
    554537
    555     assert(tspec != NULL);
    556538
    557539    switch (*s) {
     
    564546        c = *s++;
    565547        p = strchr(CSIL, *s);
    566         if (!p) {
     548        /* if *s == NUL, p != NULL! Testcase: "od -tx" */
     549        if (!p || *p == '\0') {
    567550            size = sizeof(int);
    568551            if (isdigit(s[0])) {
     
    579562            }
    580563        } else {
    581             static const uint8_t CSIL_sizeof[] = {
     564            static const uint8_t CSIL_sizeof[4] = {
    582565                sizeof(char),
    583566                sizeof(short),
     
    586569            };
    587570            size = CSIL_sizeof[p - CSIL];
     571            s++; /* skip C/S/I/L */
    588572        }
    589573
     
    733717        s++;
    734718
    735     if (next != NULL)
    736         *next = s;
     719    return s;
    737720}
    738721
    739722/* Decode the modern od format string S.  Append the decoded
    740723   representation to the global array SPEC, reallocating SPEC if
    741    necessary.  Return zero if S is valid, nonzero otherwise.  */
     724   necessary.  */
    742725
    743726static void
     
    750733        const char *next;
    751734
    752         decode_one_format(s_orig, s, &next, &tspec);
     735        next = decode_one_format(s_orig, s, &tspec);
    753736
    754737        assert(s != next);
    755738        s = next;
     739        spec = xrealloc_vector(spec, 4, n_specs);
     740        memcpy(&spec[n_specs], &tspec, sizeof(spec[0]));
    756741        n_specs++;
    757         spec = xrealloc(spec, n_specs * sizeof(*spec));
    758         memcpy(&spec[n_specs-1], &tspec, sizeof *spec);
    759742    }
    760743}
     
    792775               decrement n_skip and go on to the next file.  */
    793776        if (fstat(fileno(in_stream), &file_stats) == 0
    794          && S_ISREG(file_stats.st_mode) && file_stats.st_size >= 0
     777         && S_ISREG(file_stats.st_mode) && file_stats.st_size > 0
    795778        ) {
    796779            if (file_stats.st_size < n_skip) {
    797780                n_skip -= file_stats.st_size;
    798                 /* take check&close / open_next route */
     781                /* take "check & close / open_next" route */
    799782            } else {
    800783                if (fseeko(in_stream, n_skip, SEEK_CUR) != 0)
     
    803786            }
    804787        } else {
    805             /* If it's not a regular file with nonnegative size,
     788            /* If it's not a regular file with positive size,
    806789               position the file pointer by reading.  */
    807             char buf[BUFSIZ];
    808             size_t n_bytes_read, n_bytes_to_read = BUFSIZ;
     790            char buf[1024];
     791            size_t n_bytes_to_read = 1024;
     792            size_t n_bytes_read;
    809793
    810794            while (n_skip > 0) {
     
    825809
    826810    if (n_skip)
    827         bb_error_msg_and_die("cannot skip past end of combined input");
     811        bb_error_msg_and_die("can't skip past end of combined input");
    828812}
    829813
     
    832816
    833817static void
    834 format_address_none(off_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED)
     818format_address_none(off_t address UNUSED_PARAM, char c UNUSED_PARAM)
    835819{
    836820}
     
    850834}
    851835
    852 #if ENABLE_GETOPT_LONG
     836#if ENABLE_LONG_OPTS
    853837/* only used with --traditional */
    854838static void
     
    971955}
    972956
    973 #if ENABLE_GETOPT_LONG
     957#if ENABLE_LONG_OPTS
    974958/* If S is a valid traditional offset specification with an optional
    975959   leading '+' return nonzero and set *OFFSET to the offset it denotes.  */
     
    981965        { "B", 1024 },
    982966        { "b", 512 },
    983         { }
     967        { "", 0 }
    984968    };
    985969    char *p;
     
    1015999   multiple of all format spec sizes.  Write the final block.  Finally,
    10161000   write on a line by itself the offset of the byte after the last byte
    1017    read.  Accumulate return values from calls to read_block and
    1018    check_and_close, and if any was nonzero, return nonzero.
    1019    Otherwise, return zero.  */
    1020 
    1021 static void
    1022 dump(void)
     1001   read.  */
     1002
     1003static void
     1004dump(off_t current_offset, off_t end_offset)
    10231005{
    10241006    char *block[2];
    1025     off_t current_offset;
    10261007    int idx;
    10271008    size_t n_bytes_read;
     
    10291010    block[0] = xmalloc(2*bytes_per_block);
    10301011    block[1] = block[0] + bytes_per_block;
    1031 
    1032     current_offset = n_bytes_to_skip;
    10331012
    10341013    idx = 0;
     
    10961075   Then try to read a byte from the newly opened file.  Repeat if
    10971076   necessary until EOF is reached for the last file in FILE_LIST, then
    1098    set *C to EOF and return.  Subsequent calls do likewise.  The return
    1099    value is nonzero if any errors occured, zero otherwise.  */
     1077   set *C to EOF and return.  Subsequent calls do likewise.  */
    11001078
    11011079static void
     
    11301108   graphic (or formatting) characters terminated by a null.
    11311109   Based on a function written by Richard Stallman for a
    1132    traditional version of od.  Return nonzero if an error
    1133    occurs.  Otherwise, return zero.  */
    1134 
    1135 static void
    1136 dump_strings(void)
     1110   traditional version of od.  */
     1111
     1112static void
     1113dump_strings(off_t address, off_t end_offset)
    11371114{
    11381115    size_t bufsize = MAX(100, string_min);
    11391116    char *buf = xmalloc(bufsize);
    1140     off_t address = n_bytes_to_skip;
    11411117
    11421118    while (1) {
     
    11701146            goto tryline;
    11711147
    1172         /* If we get here, the string is all printable and null-terminated,
     1148        /* If we get here, the string is all printable and NUL-terminated,
    11731149         * so print it.  It is all in 'buf' and 'i' is its length.  */
    11741150        buf[i] = 0;
     
    11841160            case '\t': fputs("\\t", stdout); break;
    11851161            case '\v': fputs("\\v", stdout); break;
    1186             default: putc(c, stdout);
     1162            default: putchar(c);
    11871163            }
    11881164        }
     
    11971173}
    11981174
    1199 int od_main(int argc, char **argv);
     1175int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
    12001176int od_main(int argc, char **argv)
    12011177{
     
    12041180        { "k", 1024 },
    12051181        { "m", 1024*1024 },
    1206         { }
     1182        { "", 0 }
    12071183    };
    1208     unsigned opt;
    1209     int l_c_m;
    1210     /* The old-style 'pseudo starting address' to be printed in parentheses
    1211        after any true address.  */
    1212     off_t pseudo_start = 0; // only for gcc
    12131184    enum {
    12141185        OPT_A = 1 << 0,
     
    12301201        OPT_S = 1 << 16,
    12311202        OPT_w = 1 << 17,
    1232         OPT_traditional = (1 << 18) * ENABLE_GETOPT_LONG,
     1203        OPT_traditional = (1 << 18) * ENABLE_LONG_OPTS,
    12331204    };
    1234 #if ENABLE_GETOPT_LONG
     1205#if ENABLE_LONG_OPTS
    12351206    static const char od_longopts[] ALIGN1 =
    12361207        "skip-bytes\0"        Required_argument "j"
     
    12451216#endif
    12461217    char *str_A, *str_N, *str_j, *str_S;
    1247     char *str_w = NULL;
    12481218    llist_t *lst_t = NULL;
     1219    unsigned opt;
     1220    int l_c_m;
     1221    /* The old-style 'pseudo starting address' to be printed in parentheses
     1222       after any true address.  */
     1223    off_t pseudo_start = pseudo_start; // for gcc
     1224    /* The number of input bytes to skip before formatting and writing.  */
     1225    off_t n_bytes_to_skip = 0;
     1226    /* The offset of the first byte after the last byte to be formatted.  */
     1227    off_t end_offset = 0;
     1228    /* The maximum number of bytes that will be formatted.  */
     1229    off_t max_bytes_to_format = 0;
    12491230
    12501231    spec = NULL;
     
    12551236
    12561237    /* Parse command line */
    1257     opt_complementary = "t::"; // list
    1258 #if ENABLE_GETOPT_LONG
     1238    opt_complementary = "w+:t::"; /* -w N, -t is a list */
     1239#if ENABLE_LONG_OPTS
    12591240    applet_long_options = od_longopts;
    12601241#endif
     
    12641245        // but in coreutils 6.3 it was renamed and now has
    12651246        // _mandatory_ parameter
    1266         &str_A, &str_N, &str_j, &lst_t, &str_S, &str_w);
     1247        &str_A, &str_N, &str_j, &lst_t, &str_S, &bytes_per_block);
    12671248    argc -= optind;
    12681249    argv += optind;
     
    13021283    //if (opt & OPT_t)...
    13031284    while (lst_t) {
    1304         decode_format_string(lst_t->data);
    1305         lst_t = lst_t->link;
     1285        decode_format_string(llist_pop(&lst_t));
    13061286    }
    13071287    if (opt & OPT_v) verbose = 1;
     
    13281308     * traditional syntax even if --traditional is not given.  */
    13291309
    1330 #if ENABLE_GETOPT_LONG
     1310#if ENABLE_LONG_OPTS
    13311311    if (opt & OPT_traditional) {
    13321312        off_t o1, o2;
     
    14001380       set the global pointer FILE_LIST so that it
    14011381       references the null-terminated list of one name: "-".  */
    1402     file_list = default_file_list;
     1382    file_list = bb_argv_dash;
    14031383    if (argc > 0) {
    14041384        /* Set the global pointer FILE_LIST so that it
     
    14121392    skip(n_bytes_to_skip);
    14131393    if (!in_stream)
    1414         return 1;
     1394        return EXIT_FAILURE;
    14151395
    14161396    pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0);
     
    14201400
    14211401    if (opt & OPT_w) { /* -w: width */
    1422         bytes_per_block = 32;
    1423         if (str_w)
    1424             bytes_per_block = xatou(str_w);
    14251402        if (!bytes_per_block || bytes_per_block % l_c_m != 0) {
    1426             bb_error_msg("warning: invalid width %zu; using %d instead",
    1427                     bytes_per_block, l_c_m);
     1403            bb_error_msg("warning: invalid width %u; using %d instead",
     1404                    (unsigned)bytes_per_block, l_c_m);
    14281405            bytes_per_block = l_c_m;
    14291406        }
     
    14421419
    14431420    if (flag_dump_strings)
    1444         dump_strings();
     1421        dump_strings(n_bytes_to_skip, end_offset);
    14451422    else
    1446         dump();
     1423        dump(n_bytes_to_skip, end_offset);
    14471424
    14481425    if (fclose(stdin) == EOF)
Note: See TracChangeset for help on using the changeset viewer.