mirror of
https://github.com/vim/vim.git
synced 2025-09-29 04:34:16 -04:00
patch 8.2.3554: xxd has various way to exit
Problem: Xxd has various way to exit. Solution: Add function to print error and exit. (closes #9035)
This commit is contained in:
@@ -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 */
|
||||||
|
/**/
|
||||||
|
3554,
|
||||||
/**/
|
/**/
|
||||||
3553,
|
3553,
|
||||||
/**/
|
/**/
|
||||||
|
108
src/xxd/xxd.c
108
src/xxd/xxd.c
@@ -188,13 +188,6 @@ char osver[] = "";
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Let's collect some prototypes */
|
|
||||||
/* CodeWarrior is really picky about missing prototypes */
|
|
||||||
static void exit_with_usage __P((void));
|
|
||||||
static void die __P((int));
|
|
||||||
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 TRY_SEEK /* attempt to use lseek, or skip forward by reading */
|
||||||
#define COLS 256 /* change here, if you ever need more columns */
|
#define COLS 256 /* change here, if you ever need more columns */
|
||||||
#define LLEN ((2*(int)sizeof(unsigned long)) + 4 + (9*COLS-1) + COLS + 2)
|
#define LLEN ((2*(int)sizeof(unsigned long)) + 4 + (9*COLS-1) + COLS + 2)
|
||||||
@@ -245,13 +238,20 @@ exit_with_usage(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
die(int ret)
|
perror_exit(int ret)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: ", pname);
|
fprintf(stderr, "%s: ", pname);
|
||||||
perror(NULL);
|
perror(NULL);
|
||||||
exit(ret);
|
exit(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
error_exit(int ret, char *msg)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s\n", pname, msg);
|
||||||
|
exit(ret);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Max. cols binary characters are decoded from the input stream per line.
|
* Max. cols binary characters are decoded from the input stream per line.
|
||||||
* Two adjacent garbage characters after evaluated data delimit valid data.
|
* Two adjacent garbage characters after evaluated data delimit valid data.
|
||||||
@@ -263,7 +263,6 @@ die(int ret)
|
|||||||
huntype(
|
huntype(
|
||||||
FILE *fpi,
|
FILE *fpi,
|
||||||
FILE *fpo,
|
FILE *fpo,
|
||||||
FILE *fperr,
|
|
||||||
int cols,
|
int cols,
|
||||||
int hextype,
|
int hextype,
|
||||||
long base_off)
|
long base_off)
|
||||||
@@ -316,25 +315,22 @@ huntype(
|
|||||||
if (base_off + want_off != have_off)
|
if (base_off + want_off != have_off)
|
||||||
{
|
{
|
||||||
if (fflush(fpo) != 0)
|
if (fflush(fpo) != 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
#ifdef TRY_SEEK
|
#ifdef TRY_SEEK
|
||||||
if (fseek(fpo, base_off + want_off - have_off, 1) >= 0)
|
if (fseek(fpo, base_off + want_off - have_off, 1) >= 0)
|
||||||
have_off = base_off + want_off;
|
have_off = base_off + want_off;
|
||||||
#endif
|
#endif
|
||||||
if (base_off + want_off < have_off)
|
if (base_off + want_off < have_off)
|
||||||
{
|
error_exit(5, "sorry, cannot seek backwards.");
|
||||||
fprintf(fperr, "%s: sorry, cannot seek backwards.\n", pname);
|
|
||||||
return 5;
|
|
||||||
}
|
|
||||||
for (; have_off < base_off + want_off; have_off++)
|
for (; have_off < base_off + want_off; have_off++)
|
||||||
if (putc(0, fpo) == EOF)
|
if (putc(0, fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n2 >= 0 && n1 >= 0)
|
if (n2 >= 0 && n1 >= 0)
|
||||||
{
|
{
|
||||||
if (putc((n2 << 4) | n1, fpo) == EOF)
|
if (putc((n2 << 4) | n1, fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
have_off++;
|
have_off++;
|
||||||
want_off++;
|
want_off++;
|
||||||
n1 = -1;
|
n1 = -1;
|
||||||
@@ -351,7 +347,7 @@ huntype(
|
|||||||
while (c != '\n' && c != EOF)
|
while (c != '\n' && c != EOF)
|
||||||
c = getc(fpi);
|
c = getc(fpi);
|
||||||
if (c == EOF && ferror(fpi))
|
if (c == EOF && ferror(fpi))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
}
|
}
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
{
|
{
|
||||||
@@ -362,14 +358,14 @@ huntype(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (fflush(fpo) != 0)
|
if (fflush(fpo) != 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
#ifdef TRY_SEEK
|
#ifdef TRY_SEEK
|
||||||
fseek(fpo, 0L, 2);
|
fseek(fpo, 0L, 2);
|
||||||
#endif
|
#endif
|
||||||
if (fclose(fpo) != 0)
|
if (fclose(fpo) != 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (fclose(fpi) != 0)
|
if (fclose(fpi) != 0)
|
||||||
die(2);
|
perror_exit(2);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,14 +398,14 @@ xxdline(FILE *fp, char *l, int nz)
|
|||||||
zero_seen--;
|
zero_seen--;
|
||||||
if (zero_seen == 2)
|
if (zero_seen == 2)
|
||||||
if (fputs(z, fp) == EOF)
|
if (fputs(z, fp) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (zero_seen > 2)
|
if (zero_seen > 2)
|
||||||
if (fputs("*\n", fp) == EOF)
|
if (fputs("*\n", fp) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
}
|
}
|
||||||
if (nz >= 0 || zero_seen > 0)
|
if (nz >= 0 || zero_seen > 0)
|
||||||
if (fputs(l, fp) == EOF)
|
if (fputs(l, fp) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (nz)
|
if (nz)
|
||||||
zero_seen = 0;
|
zero_seen = 0;
|
||||||
}
|
}
|
||||||
@@ -640,12 +636,7 @@ main(int argc, char *argv[])
|
|||||||
if (octspergrp < 1 || octspergrp > cols)
|
if (octspergrp < 1 || octspergrp > cols)
|
||||||
octspergrp = cols;
|
octspergrp = cols;
|
||||||
else if (hextype == HEX_LITTLEENDIAN && (octspergrp & (octspergrp-1)))
|
else if (hextype == HEX_LITTLEENDIAN && (octspergrp & (octspergrp-1)))
|
||||||
{
|
error_exit(1, "number of octets per group must be a power of 2 with -e.");
|
||||||
fprintf(stderr,
|
|
||||||
"%s: number of octets per group must be a power of 2 with -e.\n",
|
|
||||||
pname);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc > 3)
|
if (argc > 3)
|
||||||
exit_with_usage();
|
exit_with_usage();
|
||||||
@@ -682,11 +673,8 @@ main(int argc, char *argv[])
|
|||||||
if (revert)
|
if (revert)
|
||||||
{
|
{
|
||||||
if (hextype && (hextype != HEX_POSTSCRIPT))
|
if (hextype && (hextype != HEX_POSTSCRIPT))
|
||||||
{
|
error_exit(-1, "sorry, cannot revert this type of hexdump");
|
||||||
fprintf(stderr, "%s: sorry, cannot revert this type of hexdump\n", pname);
|
return huntype(fp, fpo, cols, hextype,
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return huntype(fp, fpo, stderr, cols, hextype,
|
|
||||||
negseek ? -seekoff : seekoff);
|
negseek ? -seekoff : seekoff);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -698,10 +686,7 @@ main(int argc, char *argv[])
|
|||||||
else
|
else
|
||||||
e = fseek(fp, negseek ? -seekoff : seekoff, negseek ? 2 : 0);
|
e = fseek(fp, negseek ? -seekoff : seekoff, negseek ? 2 : 0);
|
||||||
if (e < 0 && negseek)
|
if (e < 0 && negseek)
|
||||||
{
|
error_exit(4, "sorry cannot seek.");
|
||||||
fprintf(stderr, "%s: sorry cannot seek.\n", pname);
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
if (e >= 0)
|
if (e >= 0)
|
||||||
seekoff = ftell(fp);
|
seekoff = ftell(fp);
|
||||||
else
|
else
|
||||||
@@ -714,12 +699,11 @@ main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
if (ferror(fp))
|
if (ferror(fp))
|
||||||
{
|
{
|
||||||
die(2);
|
perror_exit(2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: sorry cannot seek.\n", pname);
|
error_exit(4, "sorry cannot seek.");
|
||||||
return 4;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -730,12 +714,12 @@ main(int argc, char *argv[])
|
|||||||
if (fp != stdin)
|
if (fp != stdin)
|
||||||
{
|
{
|
||||||
if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0)
|
if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
for (e = 0; (c = argv[1][e]) != 0; e++)
|
for (e = 0; (c = argv[1][e]) != 0; e++)
|
||||||
if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF)
|
if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (fputs("[] = {\n", fpo) == EOF)
|
if (fputs("[] = {\n", fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
p = 0;
|
p = 0;
|
||||||
@@ -744,32 +728,32 @@ main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
|
if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
|
||||||
(p % cols) ? ", " : &",\n "[2*!p], c) < 0)
|
(p % cols) ? ", " : &",\n "[2*!p], c) < 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
if (c == EOF && ferror(fp))
|
if (c == EOF && ferror(fp))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
|
|
||||||
if (p && fputs("\n", fpo) == EOF)
|
if (p && fputs("\n", fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF)
|
if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
|
|
||||||
if (fp != stdin)
|
if (fp != stdin)
|
||||||
{
|
{
|
||||||
if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0)
|
if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
for (e = 0; (c = argv[1][e]) != 0; e++)
|
for (e = 0; (c = argv[1][e]) != 0; e++)
|
||||||
if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF)
|
if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0)
|
if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(fp))
|
if (fclose(fp))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
if (fclose(fpo))
|
if (fclose(fpo))
|
||||||
die(3);
|
perror_exit(3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -781,24 +765,24 @@ main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF
|
if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF
|
||||||
|| putc(hexx[e & 0xf], fpo) == EOF)
|
|| putc(hexx[e & 0xf], fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
n++;
|
n++;
|
||||||
if (!--p)
|
if (!--p)
|
||||||
{
|
{
|
||||||
if (putc('\n', fpo) == EOF)
|
if (putc('\n', fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
p = cols;
|
p = cols;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (e == EOF && ferror(fp))
|
if (e == EOF && ferror(fp))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
if (p < cols)
|
if (p < cols)
|
||||||
if (putc('\n', fpo) == EOF)
|
if (putc('\n', fpo) == EOF)
|
||||||
die(3);
|
perror_exit(3);
|
||||||
if (fclose(fp))
|
if (fclose(fp))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
if (fclose(fpo))
|
if (fclose(fpo))
|
||||||
die(3);
|
perror_exit(3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -863,7 +847,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (e == EOF && ferror(fp))
|
if (e == EOF && ferror(fp))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
if (p)
|
if (p)
|
||||||
{
|
{
|
||||||
l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0';
|
l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0';
|
||||||
@@ -873,9 +857,9 @@ main(int argc, char *argv[])
|
|||||||
xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */
|
xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */
|
||||||
|
|
||||||
if (fclose(fp))
|
if (fclose(fp))
|
||||||
die(2);
|
perror_exit(2);
|
||||||
if (fclose(fpo))
|
if (fclose(fpo))
|
||||||
die(3);
|
perror_exit(3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user