avoid signed<->unsigned warning
[oweals/busybox.git] / libbb / dump.c
index 26dabe57f33cefb35b56dc193201ab60461fbe2f..7d923083ad7e94255060e2aa79c2a4ac9788bc2e 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <ctype.h>             /* for isdigit() */
 #include "libbb.h"
 #include "dump.h"
@@ -96,7 +97,7 @@ static void rewrite(FS * fs)
        enum { NOTOKAY, USEBCNT, USEPREC } sokay;
        register PR *pr, **nextpr = NULL;
        register FU *fu;
-       register char *p1, *p2;
+       register char *p1, *p2, *p3;
        char savech, *fmtp;
        const char *byte_count_str;
        int nconv, prec = 0;
@@ -108,11 +109,13 @@ static void rewrite(FS * fs)
                 */
                for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
                        /* NOSTRICT */
-                       pr = (PR *) xmalloc(sizeof(PR));
+                       /* DBU:[dvae@cray.com] calloc so that forward ptrs start out NULL*/
+                       pr = (PR *) xcalloc(1,sizeof(PR));
                        if (!fu->nextpr)
                                fu->nextpr = pr;
-                       else
-                               *nextpr = pr;
+                       /* ignore nextpr -- its unused inside the loop and is
+                        * uninitialized 1st time thru.
+                        */
 
                        /* bb_dump_skip preceding text and up to the next % sign */
                        for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
@@ -244,6 +247,24 @@ static void rewrite(FS * fs)
                        pr->fmt = bb_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",
+                        * we lose the " is a HEX number" part of fmt.
+                        */
+                       for (p3 = p2; *p3 && *p3 != '%'; p3++);
+                       if (p3 > p2)
+                       {
+                               savech = *p3;
+                               *p3 = '\0';
+                               if (!(pr->fmt = realloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1)))
+                                       bb_perror_msg_and_die("hexdump");
+                               strcat(pr->fmt, p2);
+                               *p3 = savech;
+                               p2 = p3;
+                       }
+
                        fmtp = p2;
 
                        /* only one conversion character if byte count */
@@ -291,7 +312,7 @@ static void do_skip(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) ||
@@ -346,12 +367,13 @@ static int next(char **argv)
 static u_char *get(void)
 {
        static int ateof = 1;
-       static u_char *curp, *savp;
+       static u_char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
        register int n;
        int need, nread;
        u_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);
        } else {
@@ -477,7 +499,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");
@@ -523,7 +545,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, "");
@@ -586,17 +608,17 @@ static void display(void)
                                                        conv_u(pr, bp);
                                                        break;
                                                case F_UINT:{
-                                                       u_int ival;
-                                                       u_short sval;
+                                                       unsigned int ival;
+                                                       unsigned short sval;
 
                                                        switch (pr->bcnt) {
                                                        case 1:
-                                                               printf(pr->fmt, (u_int) * bp);
+                                                               printf(pr->fmt, (unsigned int) * bp);
                                                                break;
                                                        case 2:
                                                                bcopy((char *) bp, (char *) &sval,
                                                                          sizeof(sval));
-                                                               printf(pr->fmt, (u_int) sval);
+                                                               printf(pr->fmt, (unsigned int) sval);
                                                                break;
                                                        case 4:
                                                                bcopy((char *) bp, (char *) &ival,
@@ -629,7 +651,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);
@@ -673,7 +695,7 @@ void bb_dump_add(const char *fmt)
 
        /* start new linked list of format units */
        /* NOSTRICT */
-       tfs = (FS *) xmalloc(sizeof(FS));
+       tfs = (FS *) xcalloc(1,sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
        if (!bb_dump_fshead) {
                bb_dump_fshead = tfs;
        } else {
@@ -692,7 +714,8 @@ void bb_dump_add(const char *fmt)
 
                /* allocate a new format unit and link it in */
                /* NOSTRICT */
-               tfu = (FU *) xmalloc(sizeof(FU));
+               /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
+               tfu = (FU *) xcalloc(1,sizeof(FU));
                *nextfu = tfu;
                nextfu = &tfu->nextfu;
                tfu->reps = 1;