]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
hexdump: Create struct hexdump containing previously global variables.
authorOndrej Oprala <ooprala@redhat.com>
Fri, 8 Nov 2013 16:13:14 +0000 (17:13 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 2 Dec 2013 12:44:24 +0000 (13:44 +0100)
Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
text-utils/hexdump-display.c
text-utils/hexdump-parse.c
text-utils/hexdump.c
text-utils/hexdump.h

index bd463b2b82496e5c88473e8ecd28aaaaa3ad29dd..67f426df81ea112be9ebbe46b5c0c524aa935a36 100644 (file)
@@ -45,8 +45,8 @@
 #include "c.h"
 #include "nls.h"
 
-static void doskip(const char *, int);
-static u_char *get(void);
+static void doskip(const char *, int, struct hexdump *);
+static u_char *get(struct hexdump *);
 
 enum _vflag vflag = FIRST;
 
@@ -175,7 +175,7 @@ static void bpad(struct hexdump_pr *pr)
                ;
 }
 
-void display(void)
+void display(struct hexdump *hex)
 {
        register struct list_head *fs;
        register struct hexdump_fs *fss;
@@ -187,8 +187,8 @@ void display(void)
        unsigned char savech = 0, *savebp;
        struct list_head *p, *q, *r;
 
-       while ((bp = get()) != NULL) {
-               fs = &fshead; savebp = bp; saveaddress = address;
+       while ((bp = get(hex)) != NULL) {
+               fs = &hex->fshead; savebp = bp; saveaddress = address;
 
                list_for_each(p, fs) {
                        fss = list_entry(p, struct hexdump_fs, fslist);
@@ -254,7 +254,7 @@ void display(void)
 static char **_argv;
 
 static u_char *
-get(void)
+get(struct hexdump *hex)
 {
        static int ateof = 1;
        static u_char *curp, *savp;
@@ -262,23 +262,23 @@ get(void)
        u_char *tmpp;
 
        if (!curp) {
-               curp = xcalloc(1, blocksize);
-               savp = xcalloc(1, blocksize);
+               curp = xcalloc(1, hex->blocksize);
+               savp = xcalloc(1, hex->blocksize);
        } else {
                tmpp = curp;
                curp = savp;
                savp = tmpp;
-               address += blocksize;
+               address += hex->blocksize;
        }
-       need = blocksize, nread = 0;
+       need = hex->blocksize, nread = 0;
        while (TRUE) {
                /*
                 * if read the right number of bytes, or at EOF for one file,
                 * and no other files are available, zero-pad the rest of the
                 * block and set the end flag.
                 */
-               if (!length || (ateof && !next(NULL))) {
-                       if (need == blocksize)
+               if (!hex->length || (ateof && !next(NULL, hex))) {
+                       if (need == hex->blocksize)
                                goto retnul;
                        if (!need && vflag != ALL &&
                            !memcmp(curp, savp, nread)) {
@@ -296,7 +296,7 @@ get(void)
                        goto retnul;
                }
                n = fread((char *)curp + nread, sizeof(unsigned char),
-                   length == -1 ? need : min(length, need), stdin);
+                   hex->length == -1 ? need : min(hex->length, need), stdin);
                if (!n) {
                        if (ferror(stdin))
                                warn("%s", _argv[-1]);
@@ -304,11 +304,11 @@ get(void)
                        continue;
                }
                ateof = 0;
-               if (length != -1)
-                       length -= n;
+               if (hex->length != -1)
+                       hex->length -= n;
                if (!(need -= n)) {
                        if (vflag == ALL || vflag == FIRST ||
-                           memcmp(curp, savp, blocksize)) {
+                           memcmp(curp, savp, hex->blocksize)) {
                                if (vflag == DUP || vflag == FIRST)
                                        vflag = WAIT;
                                return(curp);
@@ -316,8 +316,8 @@ get(void)
                        if (vflag == WAIT)
                                printf("*\n");
                        vflag = DUP;
-                       address += blocksize;
-                       need = blocksize;
+                       address += hex->blocksize;
+                       need = hex->blocksize;
                        nread = 0;
                }
                else
@@ -329,7 +329,7 @@ retnul:
        return NULL;
 }
 
-int next(char **argv)
+int next(char **argv, struct hexdump *hex)
 {
        static int done;
        int statok;
@@ -342,7 +342,7 @@ int next(char **argv)
                if (*_argv) {
                        if (!(freopen(*_argv, "r", stdin))) {
                                warn("%s", *_argv);
-                               exitval = EXIT_FAILURE;
+                               hex->exitval = EXIT_FAILURE;
                                ++_argv;
                                continue;
                        }
@@ -352,34 +352,34 @@ int next(char **argv)
                                return(0);
                        statok = 0;
                }
-               if (skip)
-                       doskip(statok ? *_argv : "stdin", statok);
+               if (hex->skip)
+                       doskip(statok ? *_argv : "stdin", statok, hex);
                if (*_argv)
                        ++_argv;
-               if (!skip)
+               if (!hex->skip)
                        return(1);
        }
        /* NOTREACHED */
 }
 
 static void
-doskip(const char *fname, int statok)
+doskip(const char *fname, int statok, struct hexdump *hex)
 {
        struct stat sbuf;
 
        if (statok) {
                if (fstat(fileno(stdin), &sbuf))
                        err(EXIT_FAILURE, "%s", fname);
-               if (S_ISREG(sbuf.st_mode) && skip > sbuf.st_size) {
+               if (S_ISREG(sbuf.st_mode) && hex->skip > sbuf.st_size) {
                  /* If size valid and skip >= size */
-                       skip -= sbuf.st_size;
+                       hex->skip -= sbuf.st_size;
                        address += sbuf.st_size;
                        return;
                }
        }
        /* sbuf may be undefined here - do not test it */
-       if (fseek(stdin, skip, SEEK_SET))
+       if (fseek(stdin, hex->skip, SEEK_SET))
                err(EXIT_FAILURE, "%s", fname);
-       address += skip;
-       skip = 0;
+       address += hex->skip;
+       hex->skip = 0;
 }
index ed94bb8393d06eed855c0736a73b49f4b9808e08..17dd77343647195981c731d4cb89e816beea95ba 100644 (file)
@@ -72,7 +72,7 @@ static void __attribute__ ((__noreturn__)) badconv(const char *ch)
 
 struct hexdump_fu *endfu;                                      /* format at end-of-data */
 
-void addfile(char *name)
+void addfile(char *name, struct hexdump *hex)
 {
        char *fmt, *buf = NULL;
        FILE *fp;
@@ -89,14 +89,14 @@ void addfile(char *name)
                if (!*fmt || *fmt == '#')
                        continue;
 
-               add_fmt(fmt);
+               add_fmt(fmt, hex);
        }
 
        free(buf);
        fclose(fp);
 }
 
-void add_fmt(const char *fmt)
+void add_fmt(const char *fmt, struct hexdump *hex)
 {
        const char *p, *savep;
        struct hexdump_fs *tfs;
@@ -106,7 +106,7 @@ void add_fmt(const char *fmt)
        tfs = xcalloc(1, sizeof(struct hexdump_fs));
        INIT_LIST_HEAD(&tfs->fslist);
        INIT_LIST_HEAD(&tfs->fulist);
-       list_add_tail(&tfs->fslist, &fshead);
+       list_add_tail(&tfs->fslist, &hex->fshead);
 
        /* Take the format string and break it up into format units. */
        p = fmt;
@@ -217,7 +217,7 @@ int block_size(struct hexdump_fs *fs)
        return(cursize);
 }
 
-void rewrite_rules(struct hexdump_fs *fs)
+void rewrite_rules(struct hexdump_fs *fs, struct hexdump *hex)
 {
        enum { NOTOKAY, USEBCNT, USEPREC } sokay;
        struct hexdump_pr *pr;
@@ -431,9 +431,9 @@ isint:                              cs[2] = '\0';
                fu = list_entry(p, struct hexdump_fu, fulist);
 
                if (list_entry_is_last(&fu->fulist, &fs->fulist) &&
-                       fs->bcnt < blocksize &&
+                       fs->bcnt < hex->blocksize &&
                        !(fu->flags&F_SETREP) && fu->bcnt)
-                               fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
+                               fu->reps += (hex->blocksize - fs->bcnt) / fu->bcnt;
                if (fu->reps > 1) {
                        if (!list_empty(&fu->prlist)) {
                                pr = list_last_entry(&fu->prlist,
index e216f72bf2cf1757a564a811b7edfc257dbbb1a0..e9fca0294185f4f73b5ba7da62f528da7d7da6bb 100644 (file)
 #include "strutils.h"
 #include "closestream.h"
 
-struct list_head fshead;                               /* head of format strings */
-ssize_t blocksize;                     /* data block size */
-int exitval;                           /* final exit value */
-ssize_t length = -1;                   /* max bytes to read */
-void hex_free(void);
-
-off_t skip;                            /* bytes to skip */
+void hex_free(struct hexdump *);
 
 int
-parse_args(int argc, char **argv)
+parse_args(int argc, char **argv, struct hexdump *hex)
 {
        int ch;
        char *hex_offt = "\"%07.7_Ax\n\"";
@@ -86,44 +80,44 @@ parse_args(int argc, char **argv)
        while ((ch = getopt_long(argc, argv, "bcCde:f:L::n:os:vxhV", longopts, NULL)) != -1) {
                switch (ch) {
                case 'b':
-                       add_fmt(hex_offt);
-                       add_fmt("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
+                       add_fmt(hex_offt, hex);
+                       add_fmt("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", hex);
                        break;
                case 'c':
-                       add_fmt(hex_offt);
-                       add_fmt("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
+                       add_fmt(hex_offt, hex);
+                       add_fmt("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", hex);
                        break;
                case 'C':
-                       add_fmt("\"%08.8_Ax\n\"");
-                       add_fmt("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
-                       add_fmt("\"  |\" 16/1 \"%_p\" \"|\\n\"");
+                       add_fmt("\"%08.8_Ax\n\"", hex);
+                       add_fmt("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ", hex);
+                       add_fmt("\"  |\" 16/1 \"%_p\" \"|\\n\"", hex);
                        break;
                case 'd':
-                       add_fmt(hex_offt);
-                       add_fmt("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
+                       add_fmt(hex_offt, hex);
+                       add_fmt("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"", hex);
                        break;
                case 'e':
-                       add_fmt(optarg);
+                       add_fmt(optarg, hex);
                        break;
                case 'f':
-                       addfile(optarg);
+                       addfile(optarg, hex);
                        break;
                case 'n':
-                       length = strtosize_or_err(optarg, _("failed to parse length"));
+                       hex->length = strtosize_or_err(optarg, _("failed to parse length"));
                        break;
                case 'o':
-                       add_fmt(hex_offt);
-                       add_fmt("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
+                       add_fmt(hex_offt, hex);
+                       add_fmt("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", hex);
                        break;
                case 's':
-                       skip = strtosize_or_err(optarg, _("failed to parse offset"));
+                       hex->skip = strtosize_or_err(optarg, _("failed to parse offset"));
                        break;
                case 'v':
                        vflag = ALL;
                        break;
                case 'x':
-                       add_fmt(hex_offt);
-                       add_fmt("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
+                       add_fmt(hex_offt, hex);
+                       add_fmt("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"", hex);
                        break;
                case 'h':
                        usage(stdout);
@@ -136,9 +130,9 @@ parse_args(int argc, char **argv)
                }
        }
 
-       if (list_empty(&fshead)) {
-               add_fmt(hex_offt);
-               add_fmt("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
+       if (list_empty(&hex->fshead)) {
+               add_fmt(hex_offt, hex);
+               add_fmt("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"", hex);
        }
        return optind;
 }
@@ -172,7 +166,10 @@ int main(int argc, char **argv)
        struct list_head *p;
        struct hexdump_fs *tfs;
        char *c;
-       INIT_LIST_HEAD(&fshead);
+
+       struct hexdump *hex = malloc (sizeof (struct hexdump));
+       hex->length = -1;
+       INIT_LIST_HEAD(&hex->fshead);
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
@@ -180,36 +177,36 @@ int main(int argc, char **argv)
        atexit(close_stdout);
 
        if (!(c = strrchr(argv[0], 'o')) || strcmp(c, "od")) {
-               argv += parse_args(argc, argv);
+               argv += parse_args(argc, argv, hex);
        } else
                errx(EXIT_FAILURE, _("calling hexdump as od has been deprecated "
                                     "in favour to GNU coreutils od."));
 
        /* figure out the data block size */
-       blocksize = 0;
-       list_for_each(p, &fshead) {
+       hex->blocksize = 0;
+       list_for_each(p, &hex->fshead) {
                tfs = list_entry(p, struct hexdump_fs, fslist);
-               if ((tfs->bcnt = block_size(tfs)) > blocksize)
-                       blocksize = tfs->bcnt;
+               if ((tfs->bcnt = block_size(tfs)) > hex->blocksize)
+                       hex->blocksize = tfs->bcnt;
        }
 
        /* rewrite the rules, do syntax checking */
-       list_for_each(p, &fshead)
-               rewrite_rules(list_entry(p, struct hexdump_fs, fslist));
+       list_for_each(p, &hex->fshead)
+               rewrite_rules(list_entry(p, struct hexdump_fs, fslist), hex);
 
-       next(argv);
-       display();
-       hex_free();
-       return exitval;
+       next(argv, hex);
+       display(hex);
+       hex_free(hex);
+       return hex->exitval;
 }
 
-void hex_free(void)
+void hex_free(struct hexdump *hex)
 {
        struct list_head *p, *pn, *q, *qn, *r, *rn;
        struct hexdump_fs *fs;
        struct hexdump_fu *fu;
        struct hexdump_pr *pr;
-       list_for_each_safe(p, pn, &fshead) {
+       list_for_each_safe(p, pn, &hex->fshead) {
                fs = list_entry(p, struct hexdump_fs, fslist);
                list_for_each_safe(q, qn, &fs->fulist) {
                        fu = list_entry(q, struct hexdump_fu, fulist);
@@ -223,4 +220,5 @@ void hex_free(void)
                }
                free(fs);
        }
+       free (hex);
 }
index 45c5531167cfe9ec91916ad78051342644578f6c..56f5dc55af80626c1a2ca55d75cc0255c95a94b9 100644 (file)
@@ -72,23 +72,26 @@ struct hexdump_fs {                 /* format strings */
        int bcnt;
 };
 
+struct hexdump {
+  struct list_head fshead;                             /* head of format strings */
+  ssize_t blocksize;                   /* data block size */
+  int exitval;                         /* final exit value */
+  ssize_t length;                      /* max bytes to read */
+  off_t skip;                          /* bytes to skip */
+};
+
 extern struct hexdump_fu *endfu;
-extern struct list_head fshead;                        /* head of format strings list */
-extern ssize_t blocksize;              /* data block size */
-extern int exitval;                    /* final exit value */
-extern ssize_t length;                 /* max bytes to read */
-extern off_t skip;                      /* bytes to skip */
 
 enum _vflag { ALL, DUP, FIRST, WAIT }; /* -v values */
 extern enum _vflag vflag;
 
 int block_size(struct hexdump_fs *);
-void add_fmt(const char *);
-void rewrite_rules(struct hexdump_fs *);
-void addfile(char *);
-void display(void);
+void add_fmt(const char *, struct hexdump *);
+void rewrite_rules(struct hexdump_fs *, struct hexdump *);
+void addfile(char *, struct hexdump *);
+void display(struct hexdump *);
 void __attribute__((__noreturn__)) usage(FILE *out);
 void conv_c(struct hexdump_pr *, u_char *);
 void conv_u(struct hexdump_pr *, u_char *);
-int  next(char **);
-int parse_args(int, char **);
+int  next(char **, struct hexdump *);
+int parse_args(int, char **, struct hexdump *);