Hi, > > xxd so far for the most part just ignores any errors, happily aborting > > with success on I/O errors, chewing away on the CPU when the destination > > FS is full, and the like. The patch below (hopefully) fixes that. > > Thanks. Is there some way to simplify this? Passing pname in some > other way perhaps.
Like this maybe? The program certainly could use a major overhaul (it's also full of integer overflow bugs, for example), but for now I didn't want to inadvertently break anything and just fix the worst bugs in the least-intrusive way possible. > BTW: wouldn't it be nice to have exceptions? *g* BTW: http://woozle.org/~neale/papers/reply-to-still-harmful.html Florian diff --git a/src/xxd/xxd.c b/src/xxd/xxd.c index b042755..6be1a02 100644 --- a/src/xxd/xxd.c +++ b/src/xxd/xxd.c @@ -207,9 +207,9 @@ char osver[] = ""; /* Let's collect some prototypes */ /* CodeWarrior is really picky about missing prototypes */ -static void exit_with_usage __P((char *)); -static int huntype __P((FILE *, FILE *, FILE *, char *, int, int, long)); -static void xxdline __P((char *, FILE *, char *, int)); +static void exit_with_usage __P((void)); +static int huntype __P((FILE *, FILE *, FILE *, int, int, long)); +static void xxdline __P((FILE *, char *, int)); #define TRY_SEEK /* attempt to use lseek, or skip forward by reading */ #define COLS 256 /* change here, if you ever need more columns */ @@ -223,9 +223,10 @@ char hexxa[] = "0123456789abcdef0123456789ABCDEF", *hexx = hexxa; #define HEX_CINCLUDE 2 #define HEX_BITS 3 /* not hex a dump, but bits: 01111001 */ +static char *pname; + static void -exit_with_usage(pname) -char *pname; +exit_with_usage(void) { fprintf(stderr, "Usage:\n %s [options] [infile [outfile]]\n", pname); fprintf(stderr, " or\n %s -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]\n", pname); @@ -253,8 +254,7 @@ char *pname; } static void -die(pname, ret) -char *pname; +die(ret) int ret; { fprintf(stderr,"%s: ", pname); @@ -270,9 +270,8 @@ int ret; * The name is historic and came from 'undo type opt h'. */ static int -huntype(fpi, fpo, fperr, pname, cols, hextype, base_off) +huntype(fpi, fpo, fperr, cols, hextype, base_off) FILE *fpi, *fpo, *fperr; -char *pname; int cols, hextype; long base_off; { @@ -329,7 +328,7 @@ long base_off; if (base_off + want_off != have_off) { if (fflush(fpo)) - die(pname, 3); + die(3); #ifdef TRY_SEEK c = fseek(fpo, base_off + want_off - have_off, 1); if (c >= 0) @@ -342,13 +341,13 @@ long base_off; } for (; have_off < base_off + want_off; have_off++) if (putc(0, fpo) == EOF) - die(pname, 3); + die(3); } if (n2 >= 0 && n1 >= 0) { if (putc((n2 << 4) | n1, fpo) == EOF) - die(pname, 3); + die(3); have_off++; want_off++; n1 = -1; @@ -359,7 +358,7 @@ long base_off; while ((c = getc(fpi)) != '\n' && c != EOF) ; if (c == EOF && ferror(fpi)) - die(pname, 2); + die(2); ign_garb = 1; } } @@ -371,19 +370,19 @@ long base_off; while ((c = getc(fpi)) != '\n' && c != EOF) ; if (c == EOF && ferror(fpi)) - die(pname, 2); + die(2); ign_garb = 1; } } if (fflush(fpo)) - die(pname, 3); + die(3); #ifdef TRY_SEEK fseek(fpo, 0L, 2); #endif if (fclose(fpo)) - die(pname, 3); + die(3); if (fclose(fpi)) - die(pname, 2); + die(2); return 0; } @@ -400,8 +399,7 @@ long base_off; * If nz is always positive, lines are never suppressed. */ static void -xxdline(pname, fp, l, nz) -char *pname; +xxdline(fp, l, nz) FILE *fp; char *l; int nz; @@ -420,14 +418,14 @@ int nz; zero_seen--; if (zero_seen == 2) if (fputs(z, fp) == EOF) - die(pname, 3); + die(3); if (zero_seen > 2) if (fputs("*\n", fp) == EOF) - die(pname, 3); + die(3); } if (nz >= 0 || zero_seen > 0) if (fputs(l, fp) == EOF) - die(pname, 3); + die(3); if (nz) zero_seen = 0; } @@ -476,7 +474,7 @@ char *argv[]; int grplen; /* total chars per octet group */ long length = -1, n = 0, seekoff = 0; char l[LLEN+1]; - char *pname, *pp; + char *pp; #ifdef AMIGA /* This program doesn't work when started from the Workbench */ @@ -519,7 +517,7 @@ char *argv[]; else { if (!argv[2]) - exit_with_usage(pname); + exit_with_usage(); cols = (int)strtol(argv[2], NULL, 0); argv++; argc--; @@ -532,7 +530,7 @@ char *argv[]; else { if (!argv[2]) - exit_with_usage(pname); + exit_with_usage(); octspergrp = (int)strtol(argv[2], NULL, 0); argv++; argc--; @@ -555,7 +553,7 @@ char *argv[]; else { if (!argv[2]) - exit_with_usage(pname); + exit_with_usage(); #ifdef TRY_SEEK if (argv[2][0] == '+') relseek++; @@ -574,7 +572,7 @@ char *argv[]; else { if (!argv[2]) - exit_with_usage(pname); + exit_with_usage(); length = strtol(argv[2], (char **)NULL, 0); argv++; argc--; @@ -587,7 +585,7 @@ char *argv[]; break; } else if (pp[0] == '-' && pp[1]) /* unknown option */ - exit_with_usage(pname); + exit_with_usage(); else break; /* not an option */ @@ -626,7 +624,7 @@ char *argv[]; octspergrp = cols; if (argc > 3) - exit_with_usage(pname); + exit_with_usage(); if (argc == 1 || (argv[1][0] == '-' && !argv[1][1])) BIN_ASSIGN(fp = stdin, !revert); @@ -664,7 +662,7 @@ char *argv[]; fprintf(stderr, "%s: sorry, cannot revert this type of hexdump\n", pname); return -1; } - return huntype(fp, fpo, stderr, pname, cols, hextype, + return huntype(fp, fpo, stderr, cols, hextype, negseek ? -seekoff : seekoff); } @@ -691,7 +689,7 @@ char *argv[]; if (getc(fp) == EOF) if (ferror(fp)) { - die(pname, 2); + die(2); } else { fprintf(stderr, "%s: sorry cannot seek.\n", pname); return 4; @@ -704,12 +702,12 @@ char *argv[]; if (fp != stdin) { if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) - die(pname, 3); + die(3); for (e = 0; (c = argv[1][e]) != 0; e++) if (putc(isalnum(c) ? c : '_', fpo) == EOF) - die(pname, 3); + die(3); if (fputs("[] = {\n", fpo) == EOF) - die(pname, 3); + die(3); } p = 0; @@ -717,29 +715,29 @@ char *argv[]; { if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", (p % cols) ? ", " : ",\n "+2*!p, c) < 0) - die(pname, 3); + die(3); p++; } if (p) if (fputs("\n};\n"+3*(fp == stdin), fpo) == EOF) - die(pname, 3); + die(3); if (fp != stdin) { if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) - die(pname, 3); + die(3); for (e = 0; (c = argv[1][e]) != 0; e++) if (putc(isalnum(c) ? c : '_', fpo) == EOF) - die(pname, 3); + die(3); if (fprintf(fpo, "_len = %d;\n", p) < 0) - die(pname, 3); + die(3); } if (fclose(fp)) - die(pname, 2); + die(2); if (fclose(fpo)) - die(pname, 3); + die(3); return 0; } @@ -750,26 +748,26 @@ char *argv[]; while ((length < 0 || n < length) && (e = getc(fp)) != EOF) { if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF) - die(pname, 3); + die(3); if (putc(hexx[(e ) & 0xf], fpo) == EOF) - die(pname, 3); + die(3); n++; if (!--p) { if (putc('\n', fpo) == EOF) - die(pname, 3); + die(3); p = cols; } } if (e == EOF && ferror(fp)) - die(pname, 2); + die(2); if (p < cols) if (putc('\n', fpo) == EOF) - die(pname, 3); + die(3); if (fclose(fp)) - die(pname,2 ); + die(2); if (fclose(fpo)) - die(pname, 3); + die(3); return 0; } @@ -817,24 +815,24 @@ char *argv[]; if (++p == cols) { l[c = (11 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; - xxdline(pname, fpo, l, autoskip ? nonzero : 1); + xxdline(fpo, l, autoskip ? nonzero : 1); nonzero = 0; p = 0; } } if (e == EOF && ferror(fp)) - die(pname, 2); + die(2); if (p) { l[c = (11 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; - xxdline(pname, fpo, l, 1); + xxdline(fpo, l, 1); } else if (autoskip) - xxdline(pname, fpo, l, -1); /* last chance to flush out suppressed lines */ + xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ if (fclose(fp)) - die(pname, 2); + die(2); if (fclose(fpo)) - die(pname, 3); + die(3); return 0; } -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php