+/* vi: set sw=4 ts=4: */
/*
* Support code for the hexdump and od applets,
* based on code from util-linux v 2.11l
* 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"
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;
}
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;
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
}
/* 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') {
} 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;
}
} 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);
}
/*
*/
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++);
{
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;
/* 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");
}
}
/*
}
}
-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) ||
static int next(char **argv)
{
- static int done;
+ static smallint done;
+
int statok;
if (argv) {
_argv = argv;
- return (1);
+ return 1;
}
for (;;) {
if (*_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)
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;
*/
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)) {
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;
static void bpad(PR * pr)
{
- register char *p1, *p2;
+ char *p1, *p2;
/*
* remove all conversion flags; '-' is the only one valid
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);
}
"\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];
}
}
-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"
/* 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");
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;
/* PRINT; */
switch (pr->flags) {
case F_ADDRESS:
- printf(pr->fmt, address);
+ printf(pr->fmt, (unsigned int) address);
break;
case F_BPAD:
printf(pr->fmt, "");
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;
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;
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;
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) {
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 {
/* 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;
}
/* 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;
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 */