]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
hexdump: new usage(), xalloc and err.h stuff
authorSami Kerola <kerolasa@iki.fi>
Sat, 12 Feb 2011 20:55:53 +0000 (21:55 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 21 Feb 2011 13:38:23 +0000 (14:38 +0100)
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.

[kzak@redhat.com: - minor changes in formatting and coding style]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
text-utils/Makefile.am
text-utils/display.c
text-utils/hexdump.c
text-utils/hexdump.h
text-utils/hexsyntax.c
text-utils/parse.c

index eee00c320085385c1377a7b8e5f14e726a504f2d..5a098b99df7b0c619cbe0a015876f489488b6b5b 100644 (file)
@@ -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
 
index 7a35e46fbd8e8bc274ca1977d5b98c8a15c8b619..01805a20feab7969493f070b32abf3f93b111221 100644 (file)
@@ -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);
-}
index 6132bf11501d185788ec34ae0c12b2aa54b893f0..fd4b37d47257ac30402491a18d97aea443a045e6 100644 (file)
@@ -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) {
index 1653991893b095668875ee0e490d2ce355b29363..3df8629a388ab7c7eeea97b1222b819425fcec7d 100644 (file)
@@ -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 **);
index 8fdde1922eedf64622069bb4655d75840ab80f0a..211e5d55a467fad8ef703fb87e31141e31c68fda 100644 (file)
 #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);
 }
index 8164c601cb3eafd01b88b1235d2a7d923f245788..7168aadb3b239e3132ed3acdb9fb81b1d3923c2e 100644 (file)
@@ -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);
 }