0
0
mirror of https://github.com/vim/vim.git synced 2025-10-06 05:44:14 -04:00

patch 8.2.3680: repeated code in xxd

Problem:    Repeated code in xxd.
Solution:   Change exit_on_ferror() to getc_or_die(). (closes #9226)
This commit is contained in:
DungSaga
2021-11-26 13:59:27 +00:00
committed by Bram Moolenaar
parent 7329cfab36
commit d1d8a595bd
2 changed files with 13 additions and 14 deletions

View File

@@ -757,6 +757,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
3680,
/**/ /**/
3679, 3679,
/**/ /**/

View File

@@ -252,11 +252,13 @@ error_exit(int ret, char *msg)
exit(ret); exit(ret);
} }
static void static int
exit_on_ferror(int c, FILE *fpi) getc_or_die(FILE *fpi)
{ {
int c = getc(fpi);
if (c == EOF && ferror(fpi)) if (c == EOF && ferror(fpi))
perror_exit(2); perror_exit(2);
return c;
} }
static void static void
@@ -311,8 +313,7 @@ parse_hex_digit(int c)
skip_to_eol(FILE *fpi, int c) skip_to_eol(FILE *fpi, int c)
{ {
while (c != '\n' && c != EOF) while (c != '\n' && c != EOF)
c = getc(fpi); c = getc_or_die(fpi);
exit_on_ferror(c, fpi);
return c; return c;
} }
@@ -736,9 +737,8 @@ main(int argc, char *argv[])
long s = seekoff; long s = seekoff;
while (s--) while (s--)
if ((c = getc(fp)) == EOF) if ((c = getc_or_die(fp)) == EOF)
{ {
exit_on_ferror(c, fp);
error_exit(4, "sorry cannot seek."); error_exit(4, "sorry cannot seek.");
} }
} }
@@ -756,20 +756,19 @@ main(int argc, char *argv[])
p = 0; p = 0;
c = 0; c = 0;
while ((length < 0 || p < length) && (c = getc(fp)) != EOF) while ((length < 0 || p < length) && (c = getc_or_die(fp)) != EOF)
{ {
fprintf_or_die(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", fprintf_or_die(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
(p % cols) ? ", " : &",\n "[2*!p], c); (p % cols) ? ", " : (!p ? " " : ",\n "), c);
p++; p++;
} }
exit_on_ferror(c, fp);
if (p) if (p)
fputs_or_die("\n", fpo); fputs_or_die("\n", fpo);
fputs_or_die(&"};\n"[3 * (fp == stdin)], fpo);
if (fp != stdin) if (fp != stdin)
{ {
fputs_or_die("};\n", fpo);
fprintf_or_die(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "", 0); fprintf_or_die(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "", 0);
for (e = 0; (c = argv[1][e]) != 0; e++) for (e = 0; (c = argv[1][e]) != 0; e++)
putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
@@ -784,7 +783,7 @@ main(int argc, char *argv[])
{ {
p = cols; p = cols;
e = 0; e = 0;
while ((length < 0 || n < length) && (e = getc(fp)) != EOF) while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF)
{ {
putc_or_die(hexx[(e >> 4) & 0xf], fpo); putc_or_die(hexx[(e >> 4) & 0xf], fpo);
putc_or_die(hexx[e & 0xf], fpo); putc_or_die(hexx[e & 0xf], fpo);
@@ -795,7 +794,6 @@ main(int argc, char *argv[])
p = cols; p = cols;
} }
} }
exit_on_ferror(e, fp);
if (p < cols) if (p < cols)
putc_or_die('\n', fpo); putc_or_die('\n', fpo);
fclose_or_die(fp, fpo); fclose_or_die(fp, fpo);
@@ -810,7 +808,7 @@ main(int argc, char *argv[])
grplen = 8 * octspergrp + 1; grplen = 8 * octspergrp + 1;
e = 0; e = 0;
while ((length < 0 || n < length) && (e = getc(fp)) != EOF) while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF)
{ {
int x; int x;
@@ -856,7 +854,6 @@ main(int argc, char *argv[])
p = 0; p = 0;
} }
} }
exit_on_ferror(e, fp);
if (p) if (p)
{ {
l[c] = '\n'; l[c] = '\n';