add bb_basename.c
[oweals/busybox.git] / libbb / dump.c
index 7ef559513762cfcc9e1218140b03f578c1335ae6..6dbbd9f8414004f9a354b80c5430f0ecedf04fb6 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,27 +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 <unistd.h>
-#include <ctype.h>             /* for isdigit() */
 #include "libbb.h"
 #include "dump.h"
 
@@ -50,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;
 
@@ -89,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;
@@ -110,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
@@ -168,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') {
@@ -201,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;
@@ -235,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);
                        }
 
                        /*
@@ -244,7 +229,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);
 
@@ -258,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;
@@ -269,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");
                        }
                }
                /*
@@ -307,7 +291,7 @@ 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;
 
@@ -333,12 +317,13 @@ 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) {
@@ -348,10 +333,11 @@ static int next(char **argv)
                                ++_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)
@@ -359,23 +345,24 @@ 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 smallint ateof = 1;
+       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;
@@ -390,19 +377,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)) {
@@ -415,16 +402,17 @@ static u_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
-                               || 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;
@@ -438,7 +426,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
@@ -447,7 +435,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);
 }
 
@@ -463,7 +452,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];
@@ -488,7 +477,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"
@@ -499,7 +488,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");
@@ -515,14 +504,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;
@@ -545,7 +534,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, "");
@@ -562,12 +551,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;
@@ -583,12 +572,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;
@@ -616,12 +605,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;
@@ -663,7 +652,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) {
@@ -680,22 +669,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 {
@@ -707,7 +695,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;
                }
@@ -715,7 +703,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;
@@ -730,23 +718,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 */