summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--text-utils/Makefile.am2
-rw-r--r--text-utils/display.c44
-rw-r--r--text-utils/hexdump.c10
-rw-r--r--text-utils/hexdump.h4
-rw-r--r--text-utils/hexsyntax.c56
-rw-r--r--text-utils/parse.c42
6 files changed, 72 insertions, 86 deletions
diff --git a/text-utils/Makefile.am b/text-utils/Makefile.am
index eee00c320..5a098b99d 100644
--- a/text-utils/Makefile.am
+++ b/text-utils/Makefile.am
@@ -5,7 +5,7 @@ EXTRA_DIST = README.clear README.col
usrbin_exec_PROGRAMS = col colcrt colrm column hexdump rev line tailf
hexdump_SOURCES = hexdump.c conv.c display.c hexsyntax.c parse.c \
- hexdump.h
+ hexdump.h $(top_srcdir)/lib/strutils.c
dist_man_MANS = col.1 colcrt.1 colrm.1 column.1 hexdump.1 rev.1 line.1 tailf.1
diff --git a/text-utils/display.c b/text-utils/display.c
index 7a35e46fb..01805a20f 100644
--- a/text-utils/display.c
+++ b/text-utils/display.c
@@ -40,6 +40,7 @@
#include <stdlib.h>
#include <string.h>
#include "hexdump.h"
+#include "xalloc.h"
static void doskip(const char *, int);
static u_char *get(void);
@@ -227,13 +228,13 @@ get(void)
{
static int ateof = 1;
static u_char *curp, *savp;
- int n;
+ ssize_t n;
int need, nread;
u_char *tmpp;
if (!curp) {
- curp = emalloc(blocksize);
- savp = emalloc(blocksize);
+ curp = xmalloc(blocksize);
+ savp = xmalloc(blocksize);
} else {
tmpp = curp;
curp = savp;
@@ -264,8 +265,7 @@ get(void)
length == -1 ? need : MIN(length, need), stdin);
if (!n) {
if (ferror(stdin))
- (void)fprintf(stderr, "hexdump: %s: %s\n",
- _argv[-1], strerror(errno));
+ warn("%s", _argv[-1]);
ateof = 1;
continue;
}
@@ -303,9 +303,8 @@ int next(char **argv)
for (;;) {
if (*_argv) {
if (!(freopen(*_argv, "r", stdin))) {
- (void)fprintf(stderr, "hexdump: %s: %s\n",
- *_argv, strerror(errno));
- exitval = 1;
+ warn("%s", *_argv);
+ exitval = EXIT_FAILURE;
++_argv;
continue;
}
@@ -331,11 +330,8 @@ doskip(const char *fname, int statok)
struct stat sbuf;
if (statok) {
- if (fstat(fileno(stdin), &sbuf)) {
- (void)fprintf(stderr, "hexdump: %s: %s.\n",
- fname, strerror(errno));
- exit(1);
- }
+ if (fstat(fileno(stdin), &sbuf))
+ err(EXIT_FAILURE, "%s", fname);
if (S_ISREG(sbuf.st_mode) && skip > sbuf.st_size) {
/* If size valid and skip >= size */
skip -= sbuf.st_size;
@@ -344,26 +340,8 @@ doskip(const char *fname, int statok)
}
}
/* sbuf may be undefined here - do not test it */
- if (fseek(stdin, skip, SEEK_SET)) {
- (void)fprintf(stderr, "hexdump: %s: %s.\n",
- fname, strerror(errno));
- exit(1);
- }
+ if (fseek(stdin, skip, SEEK_SET))
+ err(EXIT_FAILURE, "%s", fname);
address += skip;
skip = 0;
}
-
-void *
-emalloc(int sz) {
- void *p;
-
- if (!(p = malloc((u_int)sz)))
- nomem();
- memset(p, 0, sz);
- return(p);
-}
-
-void nomem() {
- (void)fprintf(stderr, "hexdump: %s.\n", strerror(errno));
- exit(1);
-}
diff --git a/text-utils/hexdump.c b/text-utils/hexdump.c
index 6132bf115..fd4b37d47 100644
--- a/text-utils/hexdump.c
+++ b/text-utils/hexdump.c
@@ -38,6 +38,8 @@
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
+#include <err.h>
+#include <stdlib.h>
#include "hexdump.h"
#include "nls.h"
@@ -57,11 +59,9 @@ int main(int argc, char **argv)
if (!(p = strrchr(argv[0], 'o')) || strcmp(p, "od")) {
newsyntax(argc, &argv);
- } else {
- fprintf(stderr,
- _("Calling hexdump as od has been deprecated in favour to GNU coreutils od.\n"));
- return(1);
- }
+ } else
+ errx(EXIT_FAILURE, _("calling hexdump as od has been deprecated "
+ "in favour to GNU coreutils od."));
/* figure out the data block size */
for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) {
diff --git a/text-utils/hexdump.h b/text-utils/hexdump.h
index 165399189..3df8629a3 100644
--- a/text-utils/hexdump.h
+++ b/text-utils/hexdump.h
@@ -81,14 +81,12 @@ extern off_t skip; /* bytes to skip */
enum _vflag { ALL, DUP, FIRST, WAIT }; /* -v values */
extern enum _vflag vflag;
-void *emalloc(int);
int size(FS *);
void add(const char *);
void rewrite(FS *);
void addfile(char *);
void display(void);
-void nomem(void);
-void usage(void);
+void __attribute__((__noreturn__)) usage(FILE *out);
void conv_c(PR *, u_char *);
void conv_u(PR *, u_char *);
int next(char **);
diff --git a/text-utils/hexsyntax.c b/text-utils/hexsyntax.c
index 8fdde1922..211e5d55a 100644
--- a/text-utils/hexsyntax.c
+++ b/text-utils/hexsyntax.c
@@ -39,11 +39,16 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
+#include <errno.h>
+#include <err.h>
+#include <limits.h>
#include "hexdump.h"
#include "nls.h"
+#include "strutils.h"
off_t skip; /* bytes to skip */
+
void
newsyntax(int argc, char ***argvp)
{
@@ -51,7 +56,7 @@ newsyntax(int argc, char ***argvp)
char *p, **argv;
argv = *argvp;
- while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
+ while ((ch = getopt(argc, argv, "bcCde:f:n:os:vxV")) != -1) {
switch (ch) {
case 'b':
add("\"%07.7_Ax\n\"");
@@ -77,22 +82,15 @@ newsyntax(int argc, char ***argvp)
addfile(optarg);
break;
case 'n':
- if ((length = atoi(optarg)) < 0) {
- fprintf(stderr,
- _("hexdump: bad length value.\n"));
- exit(1);
- }
+ length = strtol_or_err(optarg, _("bad length value"));
break;
case 'o':
add("\"%07.7_Ax\n\"");
add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
break;
case 's':
- if ((skip = strtol(optarg, &p, 0)) < 0) {
- fprintf(stderr,
- _("hexdump: bad skip value.\n"));
- exit(1);
- }
+ if ((skip = strtol(optarg, &p, 0)) < 0)
+ err(EXIT_FAILURE, _("bad skip value"));
switch(*p) {
case 'b':
skip *= 512;
@@ -112,9 +110,16 @@ newsyntax(int argc, char ***argvp)
add("\"%07.7_Ax\n\"");
add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"");
break;
- case '?':
- usage();
+ case 'V':
+ printf(_("%s from %s\n"),
+ program_invocation_short_name,
+ PACKAGE_STRING);
+ exit(EXIT_SUCCESS);
+ break;
+ default:
+ usage(stderr);
}
+ }
if (!fshead) {
add("\"%07.7_Ax\n\"");
@@ -124,10 +129,25 @@ newsyntax(int argc, char ***argvp)
*argvp += optind;
}
-void
-usage(void)
+void __attribute__((__noreturn__)) usage(FILE *out)
{
- fprintf(stderr,
-_("hexdump: [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]\n"));
- exit(1);
+ fprintf(out, _("\nUsage:\n"
+ " %s [options] file...\n"),
+ program_invocation_short_name);
+ fprintf(out, _(
+ "\nOptions:\n"
+ " -b one-byte octal display\n"
+ " -c one-byte character display\n"
+ " -C canonical hex+ASCII display\n"
+ " -d two-byte decimal display\n"
+ " -o two-byte octal display\n"
+ " -x two-byte hexadecimal display\n"
+ " -e format format string to be used for displaying data\n"
+ " -f format_file file that contains format strings\n"
+ " -n length interpret only length bytes of input\n"
+ " -s offset skip offset bytes from the beginnin\n"
+ " -v display without squeezing similar lines\n"
+ " -V output version information and exit\n\n"));
+
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
diff --git a/text-utils/parse.c b/text-utils/parse.c
index 8164c601c..7168aadb3 100644
--- a/text-utils/parse.c
+++ b/text-utils/parse.c
@@ -43,6 +43,7 @@
#include <string.h>
#include "hexdump.h"
#include "nls.h"
+#include "xalloc.h"
static void escape(char *p1);
static void badcnt(const char *s);
@@ -59,13 +60,11 @@ void addfile(char *name)
int ch;
char buf[2048 + 1];
- if ((fp = fopen(name, "r")) == NULL) {
- (void)fprintf(stderr, _("hexdump: can't read %s.\n"), name);
- exit(1);
- }
+ if ((fp = fopen(name, "r")) == NULL)
+ err(EXIT_FAILURE, _("can't read %s"), name);
while (fgets(buf, sizeof(buf), fp)) {
if ((p = strchr(buf, '\n')) == NULL) {
- (void)fprintf(stderr, _("hexdump: line too long.\n"));
+ warnx(_("line too long"));
while ((ch = getchar()) != '\n' && ch != EOF);
continue;
}
@@ -87,7 +86,7 @@ void add(const char *fmt)
const char *savep;
/* Start new linked list of format units. */
- tfs = emalloc(sizeof(FS));
+ tfs = xmalloc(sizeof(FS));
if (!fshead)
fshead = tfs;
else
@@ -103,7 +102,7 @@ void add(const char *fmt)
break;
/* Allocate a new format unit and link it in. */
- tfu = emalloc(sizeof(FU));
+ tfu = xmalloc(sizeof(FU));
*nextfu = tfu;
nextfu = &tfu->nextfu;
tfu->reps = 1;
@@ -140,8 +139,7 @@ void add(const char *fmt)
for (savep = ++p; *p != '"';)
if (*p++ == 0)
badfmt(fmt);
- if (!(tfu->fmt = malloc(p - savep + 1)))
- nomem();
+ tfu->fmt = xmalloc(p - savep + 1);
(void) strncpy(tfu->fmt, savep, p - savep);
tfu->fmt[p - savep] = '\0';
escape(tfu->fmt);
@@ -221,7 +219,7 @@ void rewrite(FS *fs)
* conversion character gets its own.
*/
for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
- pr = emalloc(sizeof(PR));
+ pr = xmalloc(sizeof(PR));
if (!fu->nextpr)
fu->nextpr = pr;
else
@@ -388,7 +386,7 @@ isint2: switch(fu->bcnt) {
*/
savech = *p2;
p1[0] = '\0';
- pr->fmt = emalloc(strlen(fmtp) + strlen(cs) + 1);
+ pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1);
(void)strcpy(pr->fmt, fmtp);
(void)strcat(pr->fmt, cs);
*p2 = savech;
@@ -396,11 +394,9 @@ isint2: switch(fu->bcnt) {
fmtp = p2;
/* Only one conversion character if byte count */
- if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) {
- (void)fprintf(stderr,
- _("hexdump: byte count with multiple conversion characters.\n"));
- exit(1);
- }
+ if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
+ errx(EXIT_FAILURE,
+ _("byte count with multiple conversion characters"));
}
/*
* If format unit byte count not specified, figure it out
@@ -479,26 +475,20 @@ static void escape(char *p1)
static void badcnt(const char *s)
{
- (void)fprintf(stderr,
- _("hexdump: bad byte count for conversion character %s.\n"), s);
- exit(1);
+ errx(EXIT_FAILURE, _("bad byte count for conversion character %s"), s);
}
static void badsfmt(void)
{
- (void)fprintf(stderr,
- _("hexdump: %%s requires a precision or a byte count.\n"));
- exit(1);
+ errx(EXIT_FAILURE, _("%%s requires a precision or a byte count"));
}
static void badfmt(const char *fmt)
{
- (void)fprintf(stderr, _("hexdump: bad format {%s}\n"), fmt);
- exit(1);
+ errx(EXIT_FAILURE, _("bad format {%s}"), fmt);
}
static void badconv(const char *ch)
{
- (void)fprintf(stderr, _("hexdump: bad conversion character %%%s.\n"), ch);
- exit(1);
+ errx(EXIT_FAILURE, _("bad conversion character %%%s"), ch);
}