usage.c: remove reference to busybox.h
[oweals/busybox.git] / libbb / dump.c
index f071ea09c32496945bf938b52962bb20c3f83229..f078afc1cb818284957c403350f7b9b3a237db64 100644 (file)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /*
  * Support code for the hexdump and od applets,
  * based on code from util-linux v 2.11l
@@ -5,26 +6,11 @@
  * Copyright (c) 1989
  *     The Regents of the University of California.  All rights reserved.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
  * Original copyright notice is retained at the end of this file.
  */
 
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>             /* for isdigit() */
 #include "libbb.h"
 #include "dump.h"
 
@@ -49,9 +35,9 @@ static const char lcc[] = "diouxX";
 
 int bb_dump_size(FS * fs)
 {
-       register FU *fu;
-       register int bcnt, cur_size;
-       register char *fmt;
+       FU *fu;
+       int bcnt, cur_size;
+       char *fmt;
        const char *p;
        int prec;
 
@@ -88,15 +74,15 @@ int bb_dump_size(FS * fs)
                }
                cur_size += bcnt * fu->reps;
        }
-       return (cur_size);
+       return cur_size;
 }
 
 static void rewrite(FS * fs)
 {
        enum { NOTOKAY, USEBCNT, USEPREC } sokay;
-       register PR *pr, **nextpr = NULL;
-       register FU *fu;
-       register char *p1, *p2, *p3;
+       PR *pr, **nextpr = NULL;
+       FU *fu;
+       char *p1, *p2, *p3;
        char savech, *fmtp;
        const char *byte_count_str;
        int nconv, prec = 0;
@@ -109,7 +95,7 @@ static void rewrite(FS * fs)
                for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
                        /* NOSTRICT */
                        /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
-                       pr = (PR *) xcalloc(1,sizeof(PR));
+                       pr = xzalloc(sizeof(PR));
                        if (!fu->nextpr)
                                fu->nextpr = pr;
                        /* ignore nextpr -- its unused inside the loop and is
@@ -167,7 +153,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') {
@@ -200,7 +186,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;
@@ -234,7 +220,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);
                        }
 
                        /*
@@ -243,13 +229,13 @@ 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);
 
                        /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
-                        * Skip subsequent text and up to the next % sign and tack the 
-                        * additional text onto fmt: eg. if fmt is "%x is a HEX number", 
+                        * Skip subsequent text and up to the next % sign and tack the
+                        * additional text onto fmt: eg. if fmt is "%x is a HEX number",
                         * we lose the " is a HEX number" part of fmt.
                         */
                        for (p3 = p2; *p3 && *p3 != '%'; p3++);
@@ -257,8 +243,7 @@ static void rewrite(FS * fs)
                        {
                                savech = *p3;
                                *p3 = '\0';
-                               if (!(pr->fmt = realloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1)))
-                                       bb_perror_msg_and_die("hexdump");
+                               pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1);
                                strcat(pr->fmt, p2);
                                *p3 = savech;
                                p2 = p3;
@@ -268,7 +253,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");
                        }
                }
                /*
@@ -306,12 +291,12 @@ 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(fileno(stdin), &sbuf)) {
+               if (fstat(STDIN_FILENO, &sbuf)) {
                        bb_perror_msg_and_die("%s", fname);
                }
                if ((!(S_ISCHR(sbuf.st_mode) ||
@@ -337,7 +322,7 @@ static int next(char **argv)
 
        if (argv) {
                _argv = argv;
-               return (1);
+               return 1;
        }
        for (;;) {
                if (*_argv) {
@@ -350,7 +335,7 @@ static int next(char **argv)
                        statok = done = 1;
                } else {
                        if (done++)
-                               return (0);
+                               return 0;
                        statok = 0;
                }
                if (bb_dump_skip)
@@ -358,23 +343,23 @@ static int next(char **argv)
                if (*_argv)
                        ++_argv;
                if (!bb_dump_skip)
-                       return (1);
+                       return 1;
        }
        /* NOTREACHED */
 }
 
-static u_char *get(void)
+static unsigned char *get(void)
 {
        static int ateof = 1;
-       static u_char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
-       register int n;
+       static unsigned char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
+       int n;
        int need, nread;
-       u_char *tmpp;
+       unsigned char *tmpp;
 
        if (!curp) {
                address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
-               curp = (u_char *) xmalloc(bb_dump_blocksize);
-               savp = (u_char *) xmalloc(bb_dump_blocksize);
+               curp = xmalloc(bb_dump_blocksize);
+               savp = xmalloc(bb_dump_blocksize);
        } else {
                tmpp = curp;
                curp = savp;
@@ -389,19 +374,19 @@ static u_char *get(void)
                 */
                if (!bb_dump_length || (ateof && !next((char **) NULL))) {
                        if (need == bb_dump_blocksize) {
-                               return ((u_char *) NULL);
+                               return NULL;
                        }
-                       if (bb_dump_vflag != ALL && !bcmp(curp, savp, nread)) {
+                       if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
                                if (bb_dump_vflag != DUP) {
-                                       printf("*\n");
+                                       puts("*");
                                }
-                               return ((u_char *) NULL);
+                               return NULL;
                        }
-                       bzero((char *) curp + nread, need);
+                       memset((char *) curp + nread, 0, need);
                        eaddress = address + nread;
-                       return (curp);
+                       return curp;
                }
-               n = fread((char *) curp + nread, sizeof(u_char),
+               n = fread((char *) curp + nread, sizeof(unsigned char),
                                  bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
                if (!n) {
                        if (ferror(stdin)) {
@@ -416,14 +401,14 @@ static u_char *get(void)
                }
                if (!(need -= n)) {
                        if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
-                               || bcmp(curp, savp, bb_dump_blocksize)) {
+                               || 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;
@@ -437,7 +422,7 @@ static u_char *get(void)
 
 static void bpad(PR * pr)
 {
-       register char *p1, *p2;
+       char *p1, *p2;
 
        /*
         * remove all conversion flags; '-' is the only one valid
@@ -446,7 +431,8 @@ static void bpad(PR * pr)
        pr->flags = F_BPAD;
        *pr->cchar = 's';
        for (p1 = pr->fmt; *p1 != '%'; ++p1);
-       for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1);
+       for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
+               if (pr->nospace) pr->nospace--;
        while ((*p2++ = *p1++) != 0);
 }
 
@@ -462,7 +448,7 @@ static const char conv_str[] =
        "\0";
 
 
-static void conv_c(PR * pr, u_char * p)
+static void conv_c(PR * pr, unsigned char * p)
 {
        const char *str = conv_str;
        char buf[10];
@@ -487,7 +473,7 @@ static void conv_c(PR * pr, u_char * p)
        }
 }
 
-static void conv_u(PR * pr, u_char * p)
+static void conv_u(PR * pr, unsigned char * p)
 {
        static const char list[] =
                "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
@@ -498,7 +484,7 @@ static void conv_u(PR * pr, u_char * p)
        /* od used nl, not lf */
        if (*p <= 0x1f) {
                *pr->cchar = 's';
-               printf(pr->fmt, list[4 * (int)(*p)]);
+               printf(pr->fmt, list + (4 * (int)*p));
        } else if (*p == 0x7f) {
                *pr->cchar = 's';
                printf(pr->fmt, "del");
@@ -514,14 +500,14 @@ static void conv_u(PR * pr, u_char * p)
 static void display(void)
 {
 /*  extern FU *endfu; */
-       register FS *fs;
-       register FU *fu;
-       register PR *pr;
-       register int cnt;
-       register u_char *bp;
+       FS *fs;
+       FU *fu;
+       PR *pr;
+       int cnt;
+       unsigned char *bp;
 
        off_t saveaddress;
-       u_char savech = 0, *savebp;
+       unsigned char savech = 0, *savebp;
 
        while ((bp = get()) != NULL) {
                for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
@@ -544,7 +530,7 @@ static void display(void)
 /*                      PRINT; */
                                                switch (pr->flags) {
                                                case F_ADDRESS:
-                                                       printf(pr->fmt, address);
+                                                       printf(pr->fmt, (unsigned int) address);
                                                        break;
                                                case F_BPAD:
                                                        printf(pr->fmt, "");
@@ -561,12 +547,12 @@ static void display(void)
 
                                                        switch (pr->bcnt) {
                                                        case 4:
-                                                               bcopy((char *) bp, (char *) &fval,
+                                                               memmove((char *) &fval, (char *) bp,
                                                                          sizeof(fval));
                                                                printf(pr->fmt, fval);
                                                                break;
                                                        case 8:
-                                                               bcopy((char *) bp, (char *) &dval,
+                                                               memmove((char *) &dval, (char *) bp,
                                                                          sizeof(dval));
                                                                printf(pr->fmt, dval);
                                                                break;
@@ -582,12 +568,12 @@ static void display(void)
                                                                printf(pr->fmt, (int) *bp);
                                                                break;
                                                        case 2:
-                                                               bcopy((char *) bp, (char *) &sval,
+                                                               memmove((char *) &sval, (char *) bp,
                                                                          sizeof(sval));
                                                                printf(pr->fmt, (int) sval);
                                                                break;
                                                        case 4:
-                                                               bcopy((char *) bp, (char *) &ival,
+                                                               memmove((char *) &ival, (char *) bp,
                                                                          sizeof(ival));
                                                                printf(pr->fmt, ival);
                                                                break;
@@ -615,12 +601,12 @@ static void display(void)
                                                                printf(pr->fmt, (unsigned int) * bp);
                                                                break;
                                                        case 2:
-                                                               bcopy((char *) bp, (char *) &sval,
+                                                               memmove((char *) &sval, (char *) bp,
                                                                          sizeof(sval));
                                                                printf(pr->fmt, (unsigned int) sval);
                                                                break;
                                                        case 4:
-                                                               bcopy((char *) bp, (char *) &ival,
+                                                               memmove((char *) &ival, (char *) bp,
                                                                          sizeof(ival));
                                                                printf(pr->fmt, ival);
                                                                break;
@@ -650,7 +636,7 @@ static void display(void)
                for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
                        switch (pr->flags) {
                        case F_ADDRESS:
-                               (void) printf(pr->fmt, eaddress);
+                               (void) printf(pr->fmt, (unsigned int) eaddress);
                                break;
                        case F_TEXT:
                                (void) printf(pr->fmt);
@@ -662,7 +648,7 @@ static void display(void)
 
 int bb_dump_dump(char **argv)
 {
-       register FS *tfs;
+       FS *tfs;
 
        /* figure out the data block bb_dump_size */
        for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
@@ -679,22 +665,21 @@ int bb_dump_dump(char **argv)
        next(argv);
        display();
 
-       return (exitval);
+       return exitval;
 }
 
 void bb_dump_add(const char *fmt)
 {
-       register const char *p;
-       register char *p1;
-       register char *p2;
+       const char *p;
+       char *p1;
+       char *p2;
        static FS **nextfs;
        FS *tfs;
        FU *tfu, **nextfu;
        const char *savep;
 
        /* start new linked list of format units */
-       /* NOSTRICT */
-       tfs = (FS *) xcalloc(1,sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
+       tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
        if (!bb_dump_fshead) {
                bb_dump_fshead = tfs;
        } else {
@@ -706,7 +691,7 @@ void bb_dump_add(const char *fmt)
        /* take the format string and break it up into format units */
        for (p = fmt;;) {
                /* bb_dump_skip leading white space */
-               p = bb_skip_whitespace(p);
+               p = skip_whitespace(p);
                if (!*p) {
                        break;
                }
@@ -714,7 +699,7 @@ void bb_dump_add(const char *fmt)
                /* allocate a new format unit and link it in */
                /* NOSTRICT */
                /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
-               tfu = (FU *) xcalloc(1,sizeof(FU));
+               tfu = xzalloc(sizeof(FU));
                *nextfu = tfu;
                nextfu = &tfu->nextfu;
                tfu->reps = 1;
@@ -729,23 +714,25 @@ void bb_dump_add(const char *fmt)
                        tfu->reps = atoi(savep);
                        tfu->flags = F_SETREP;
                        /* bb_dump_skip trailing white space */
-                       p = bb_skip_whitespace(++p);
+                       p = skip_whitespace(++p);
                }
 
                /* bb_dump_skip slash and trailing white space */
                if (*p == '/') {
-                       p = bb_skip_whitespace(++p);
+                       p = skip_whitespace(++p);
                }
 
                /* 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);
                        }
                        tfu->bcnt = atoi(savep);
                        /* bb_dump_skip trailing white space */
-                       p = bb_skip_whitespace(++p);
+                       p = skip_whitespace(++p);
                }
 
                /* format */