dos2unix: tiny shrink
[oweals/busybox.git] / libbb / dump.c
index f1d5df2d6537a003e89d8ede2038b4c1d3802cfa..5e43d42a40e20fb833f20b8db7e8f0879c7f95b5 100644 (file)
@@ -12,9 +12,6 @@
  */
 
 #include "libbb.h"
-#include <string.h>
-#include <unistd.h>
-#include <ctype.h>             /* for isdigit() */
 #include "dump.h"
 
 enum _vflag bb_dump_vflag = FIRST;
@@ -29,12 +26,12 @@ static int exitval;                 /* final exit value */
 int bb_dump_blocksize;                 /* data block size */
 int bb_dump_length = -1;               /* max bytes to read */
 
-static const char index_str[] = ".#-+ 0123456789";
+static const char index_str[] ALIGN1 = ".#-+ 0123456789";
 
-static const char size_conv_str[] =
+static const char size_conv_str[] ALIGN1 =
 "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
 
-static const char lcc[] = "diouxX";
+static const char lcc[] ALIGN1 = "diouxX";
 
 int bb_dump_size(FS * fs)
 {
@@ -62,7 +59,8 @@ int bb_dump_size(FS * fs)
                                prec = atoi(fmt);
                                while (isdigit(*++fmt));
                        }
-                       if (!(p = strchr(size_conv_str + 12, *fmt))) {
+                       p = strchr(size_conv_str + 12, *fmt);
+                       if (!p) {
                                if (*fmt == 's') {
                                        bcnt += prec;
                                } else if (*fmt == '_') {
@@ -77,7 +75,7 @@ int bb_dump_size(FS * fs)
                }
                cur_size += bcnt * fu->reps;
        }
-       return (cur_size);
+       return cur_size;
 }
 
 static void rewrite(FS * fs)
@@ -102,7 +100,7 @@ static void rewrite(FS * fs)
                        if (!fu->nextpr)
                                fu->nextpr = pr;
                        /* ignore nextpr -- its unused inside the loop and is
-                        * uninitialized 1st time thru.
+                        * uninitialized 1st time through.
                         */
 
                        /* bb_dump_skip preceding text and up to the next % sign */
@@ -156,7 +154,7 @@ static void rewrite(FS * fs)
                                }
                                /* Unlike the original, output the remainder of the format string. */
                                if (!*byte_count_str) {
-                                       bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
+                                       bb_error_msg_and_die("bad byte count for conversion character %s", p1);
                                }
                                pr->bcnt = *byte_count_str;
                        } else if (*p1 == 'l') {
@@ -165,7 +163,8 @@ static void rewrite(FS * fs)
                        DO_INT_CONV:
                                {
                                        const char *e;
-                                       if (!(e = strchr(lcc, *p1))) {
+                                       e = strchr(lcc, *p1);
+                                       if (!e) {
                                                goto DO_BAD_CONV_CHAR;
                                        }
                                        pr->flags = F_INT;
@@ -189,7 +188,7 @@ static void rewrite(FS * fs)
                                } else if (sokay == USEPREC) {
                                        pr->bcnt = prec;
                                } else {        /* NOTOKAY */
-                                       bb_error_msg_and_die("%%s requires a precision or a byte count.");
+                                       bb_error_msg_and_die("%%s requires a precision or a byte count");
                                }
                        } else if (*p1 == '_') {
                                ++p2;
@@ -223,7 +222,7 @@ static void rewrite(FS * fs)
                                }
                        } else {
                        DO_BAD_CONV_CHAR:
-                               bb_error_msg_and_die("bad conversion character %%%s.\n", p1);
+                               bb_error_msg_and_die("bad conversion character %%%s", p1);
                        }
 
                        /*
@@ -232,7 +231,7 @@ static void rewrite(FS * fs)
                         */
                        savech = *p2;
                        p1[1] = '\0';
-                       pr->fmt = bb_xstrdup(fmtp);
+                       pr->fmt = xstrdup(fmtp);
                        *p2 = savech;
                        pr->cchar = pr->fmt + (p1 - fmtp);
 
@@ -256,7 +255,7 @@ static void rewrite(FS * fs)
 
                        /* only one conversion character if byte count */
                        if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
-                               bb_error_msg_and_die("byte count with multiple conversion characters.\n");
+                               bb_error_msg_and_die("byte count with multiple conversion characters");
                        }
                }
                /*
@@ -294,13 +293,13 @@ static void rewrite(FS * fs)
        }
 }
 
-static void do_skip(char *fname, int statok)
+static void do_skip(const char *fname, int statok)
 {
        struct stat sbuf;
 
        if (statok) {
                if (fstat(STDIN_FILENO, &sbuf)) {
-                       bb_perror_msg_and_die("%s", fname);
+                       bb_simple_perror_msg_and_die(fname);
                }
                if ((!(S_ISCHR(sbuf.st_mode) ||
                           S_ISBLK(sbuf.st_mode) ||
@@ -312,7 +311,7 @@ static void do_skip(char *fname, int statok)
                }
        }
        if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
-               bb_perror_msg_and_die("%s", fname);
+               bb_simple_perror_msg_and_die(fname);
        }
        savaddress = address += bb_dump_skip;
        bb_dump_skip = 0;
@@ -320,25 +319,27 @@ static void do_skip(char *fname, int statok)
 
 static int next(char **argv)
 {
-       static int done;
+       static smallint done;
+
        int statok;
 
        if (argv) {
                _argv = argv;
-               return (1);
+               return 1;
        }
        for (;;) {
                if (*_argv) {
                        if (!(freopen(*_argv, "r", stdin))) {
-                               bb_perror_msg("%s", *_argv);
+                               bb_simple_perror_msg(*_argv);
                                exitval = 1;
                                ++_argv;
                                continue;
                        }
-                       statok = done = 1;
+                       done = statok = 1;
                } else {
-                       if (done++)
-                               return (0);
+                       if (done)
+                               return 0;
+                       done = 1;
                        statok = 0;
                }
                if (bb_dump_skip)
@@ -346,23 +347,24 @@ static int next(char **argv)
                if (*_argv)
                        ++_argv;
                if (!bb_dump_skip)
-                       return (1);
+                       return 1;
        }
        /* NOTREACHED */
 }
 
 static unsigned char *get(void)
 {
-       static int ateof = 1;
-       static unsigned char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
+       static smallint ateof = 1;
+       static unsigned char *curp = NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
+
        int n;
        int need, nread;
        unsigned char *tmpp;
 
        if (!curp) {
                address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
-               curp = (unsigned char *) xmalloc(bb_dump_blocksize);
-               savp = (unsigned char *) xmalloc(bb_dump_blocksize);
+               curp = xmalloc(bb_dump_blocksize);
+               savp = xmalloc(bb_dump_blocksize);
        } else {
                tmpp = curp;
                curp = savp;
@@ -377,23 +379,23 @@ static unsigned char *get(void)
                 */
                if (!bb_dump_length || (ateof && !next((char **) NULL))) {
                        if (need == bb_dump_blocksize) {
-                               return ((unsigned char *) NULL);
+                               return NULL;
                        }
                        if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
                                if (bb_dump_vflag != DUP) {
-                                       printf("*\n");
+                                       puts("*");
                                }
-                               return ((unsigned char *) NULL);
+                               return NULL;
                        }
                        memset((char *) curp + nread, 0, need);
                        eaddress = address + nread;
-                       return (curp);
+                       return curp;
                }
                n = fread((char *) curp + nread, sizeof(unsigned char),
                                  bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
                if (!n) {
                        if (ferror(stdin)) {
-                               bb_perror_msg("%s", _argv[-1]);
+                               bb_simple_perror_msg(_argv[-1]);
                        }
                        ateof = 1;
                        continue;
@@ -402,16 +404,17 @@ static unsigned char *get(void)
                if (bb_dump_length != -1) {
                        bb_dump_length -= n;
                }
-               if (!(need -= n)) {
+               need -= n;
+               if (!need) {
                        if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
                                || memcmp(curp, savp, bb_dump_blocksize)) {
                                if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
                                        bb_dump_vflag = WAIT;
                                }
-                               return (curp);
+                               return curp;
                        }
                        if (bb_dump_vflag == WAIT) {
-                               printf("*\n");
+                               puts("*");
                        }
                        bb_dump_vflag = DUP;
                        address = savaddress += bb_dump_blocksize;
@@ -439,7 +442,7 @@ static void bpad(PR * pr)
        while ((*p2++ = *p1++) != 0);
 }
 
-static const char conv_str[] =
+static const char conv_str[] ALIGN1 =
        "\0\\0\0"
        "\007\\a\0"                             /* \a */
        "\b\\b\0"
@@ -448,7 +451,7 @@ static const char conv_str[] =
        "\r\\r\0"
        "\t\\t\0"
        "\v\\v\0"
-       "\0";
+       ;
 
 
 static void conv_c(PR * pr, unsigned char * p)
@@ -478,7 +481,7 @@ static void conv_c(PR * pr, unsigned char * p)
 
 static void conv_u(PR * pr, unsigned char * p)
 {
-       static const char list[] =
+       static const char list[] ALIGN1 =
                "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
                "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
                "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
@@ -668,7 +671,7 @@ int bb_dump_dump(char **argv)
        next(argv);
        display();
 
-       return (exitval);
+       return exitval;
 }
 
 void bb_dump_add(const char *fmt)
@@ -727,7 +730,9 @@ void bb_dump_add(const char *fmt)
 
                /* byte count */
                if (isdigit(*p)) {
-                       for (savep = p; isdigit(*p); ++p);
+// TODO: use bb_strtou
+                       savep = p;
+                       do p++; while (isdigit(*p));
                        if (!isspace(*p)) {
                                bb_error_msg_and_die("bad format {%s}", fmt);
                        }