New usage help screen and print version switch. Also fixes to exit codes, util linux xmalloc replaced emalloc and every error print is using libc error function. Signed-off-by: Sami Kerola <kerolasa@xxxxxx> --- text-utils/Makefile.am | 2 +- text-utils/display.c | 44 ++++++++++----------------------------- text-utils/hexdump.c | 8 +++--- text-utils/hexdump.h | 4 +-- text-utils/hexsyntax.c | 53 +++++++++++++++++++++++++++++++---------------- text-utils/parse.c | 46 ++++++++++++++++++----------------------- 6 files changed, 72 insertions(+), 85 deletions(-) diff --git a/text-utils/Makefile.am b/text-utils/Makefile.am index eee00c3..5a098b9 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 7a35e46..01805a2 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 6132bf1..62c46fd 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, + } else + errx(EXIT_FAILURE, _("Calling hexdump as od has been deprecated in favour to GNU coreutils od.\n")); - return(1); - } /* 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 1653991..3df8629 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 8fdde19..1212810 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,14 @@ newsyntax(int argc, char ***argvp) add("\"%07.7_Ax\n\""); add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\""); break; - case '?': - usage(); + case 'V': + printf(_("hexdump (%s)\n"), PACKAGE_STRING); + exit(EXIT_SUCCESS); + break; + default: + usage(stderr); } + } if (!fshead) { add("\"%07.7_Ax\n\""); @@ -124,10 +127,24 @@ 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, _("Usage: %s [options] file...\n\n"), + program_invocation_short_name); + fprintf(out, _( + "Options:\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")); + + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } diff --git a/text-utils/parse.c b/text-utils/parse.c index 8164c60..d4261c5 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,24 @@ 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, + _("hexdump: %%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, + _("hexdump: 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, + _("hexdump: bad conversion character %%%s"), ch); } -- 1.7.4 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html