Patch 8.2.3670
Problem:    Error checks repeated several times.
Solution:   Move the checks to functions. (closes #9213)
Files:      src/xxd/xxd.c


*** ../vim-8.2.3669/src/xxd/xxd.c       2021-11-24 11:18:03.742223158 +0000
--- src/xxd/xxd.c       2021-11-25 11:14:33.161485429 +0000
***************
*** 252,257 ****
--- 252,294 ----
    exit(ret);
  }
  
+   static void
+ exit_on_ferror(char c, FILE *fpi)
+ {
+   if (c == EOF && ferror(fpi))
+     perror_exit(2);
+ }
+ 
+   static void
+ putc_or_die(char c, FILE *fpo)
+ {
+   if (putc(c, fpo) == EOF)
+     perror_exit(3);
+ }
+ 
+   static void
+ fputs_or_die(char *s, FILE *fpo)
+ {
+   if (fputs(s, fpo) == EOF)
+     perror_exit(3);
+ }
+ 
+   static void
+ fprintf_or_die(FILE *fpo, char *format, char *s, int d)
+ {
+   if (fprintf(fpo, format, s, d) < 0)
+     perror_exit(3);
+ }
+ 
+   static void
+ fclose_or_die(FILE *fpi, FILE *fpo)
+ {
+   if (fclose(fpo) != 0)
+     perror_exit(3);
+   if (fclose(fpi) != 0)
+     perror_exit(2);
+ }
+ 
  /*
   * If "c" is a hex digit, return the value.
   * Otherwise return -1.
***************
*** 275,282 ****
  {
    while (c != '\n' && c != EOF)
      c = getc(fpi);
!   if (c == EOF && ferror(fpi))
!     perror_exit(2);
    return c;
  }
  
--- 312,318 ----
  {
    while (c != '\n' && c != EOF)
      c = getc(fpi);
!   exit_on_ferror(c, fpi);
    return c;
  }
  
***************
*** 342,355 ****
          if (base_off + want_off < have_off)
            error_exit(5, "sorry, cannot seek backwards.");
          for (; have_off < base_off + want_off; have_off++)
!           if (putc(0, fpo) == EOF)
!             perror_exit(3);
        }
  
        if (n2 >= 0 && n1 >= 0)
        {
!         if (putc((n2 << 4) | n1, fpo) == EOF)
!           perror_exit(3);
          have_off++;
          want_off++;
          n1 = -1;
--- 378,389 ----
          if (base_off + want_off < have_off)
            error_exit(5, "sorry, cannot seek backwards.");
          for (; have_off < base_off + want_off; have_off++)
!           putc_or_die(0, fpo);
        }
  
        if (n2 >= 0 && n1 >= 0)
        {
!         putc_or_die((n2 << 4) | n1, fpo);
          have_off++;
          want_off++;
          n1 = -1;
***************
*** 374,383 ****
  #ifdef TRY_SEEK
    fseek(fpo, 0L, SEEK_END);
  #endif
!   if (fclose(fpo) != 0)
!     perror_exit(3);
!   if (fclose(fpi) != 0)
!     perror_exit(2);
    return 0;
  }
  
--- 408,414 ----
  #ifdef TRY_SEEK
    fseek(fpo, 0L, SEEK_END);
  #endif
!   fclose_or_die(fpi, fpo);
    return 0;
  }
  
***************
*** 409,423 ****
          if (nz < 0)
            zero_seen--;
          if (zero_seen == 2)
!           if (fputs(z, fp) == EOF)
!             perror_exit(3);
          if (zero_seen > 2)
!           if (fputs("*\n", fp) == EOF)
!             perror_exit(3);
        }
        if (nz >= 0 || zero_seen > 0)
!       if (fputs(l, fp) == EOF)
!         perror_exit(3);
        if (nz)
        zero_seen = 0;
      }
--- 440,451 ----
          if (nz < 0)
            zero_seen--;
          if (zero_seen == 2)
!           fputs_or_die(z, fp);
          if (zero_seen > 2)
!           fputs_or_die("*\n", fp);
        }
        if (nz >= 0 || zero_seen > 0)
!       fputs_or_die(l, fp);
        if (nz)
        zero_seen = 0;
      }
***************
*** 708,723 ****
          long s = seekoff;
  
          while (s--)
!           if (getc(fp) == EOF)
            {
!             if (ferror(fp))
!               {
!                 perror_exit(2);
!               }
!             else
!               {
!                 error_exit(4, "sorry cannot seek.");
!               }
            }
        }
      }
--- 736,745 ----
          long s = seekoff;
  
          while (s--)
!           if ((c = getc(fp)) == EOF)
            {
!             exit_on_ferror(c, fp);
!             error_exit(4, "sorry cannot seek.");
            }
        }
      }
***************
*** 726,772 ****
      {
        if (fp != stdin)
        {
!         if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" 
: "") < 0)
!           perror_exit(3);
          for (e = 0; (c = argv[1][e]) != 0; e++)
!           if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF)
!             perror_exit(3);
!         if (fputs("[] = {\n", fpo) == EOF)
!           perror_exit(3);
        }
  
        p = 0;
        c = 0;
        while ((length < 0 || p < length) && (c = getc(fp)) != EOF)
        {
!         if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
!               (p % cols) ? ", " : &",\n  "[2*!p],  c) < 0)
!           perror_exit(3);
          p++;
        }
!       if (c == EOF && ferror(fp))
!       perror_exit(2);
  
!       if (p && fputs("\n", fpo) == EOF)
!       perror_exit(3);
!       if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF)
!       perror_exit(3);
  
        if (fp != stdin)
        {
!         if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : 
"") < 0)
!           perror_exit(3);
          for (e = 0; (c = argv[1][e]) != 0; e++)
!         if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF)
!             perror_exit(3);
!         if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0)
!           perror_exit(3);
        }
  
!       if (fclose(fp))
!       perror_exit(2);
!       if (fclose(fpo))
!       perror_exit(3);
        return 0;
      }
  
--- 748,782 ----
      {
        if (fp != stdin)
        {
!         fprintf_or_die(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? 
"__" : "", 0);
          for (e = 0; (c = argv[1][e]) != 0; e++)
!           putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
!         fputs_or_die("[] = {\n", fpo);
        }
  
        p = 0;
        c = 0;
        while ((length < 0 || p < length) && (c = getc(fp)) != EOF)
        {
!         fprintf_or_die(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
!               (p % cols) ? ", " : &",\n  "[2*!p],  c);
          p++;
        }
!       exit_on_ferror(c, fp);
  
!       if (p)
!       fputs_or_die("\n", fpo);
!       fputs_or_die(&"};\n"[3 * (fp == stdin)], fpo);
  
        if (fp != stdin)
        {
!         fprintf_or_die(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? 
"__" : "", 0);
          for (e = 0; (c = argv[1][e]) != 0; e++)
!           putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
!         fprintf_or_die(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p);
        }
  
!       fclose_or_die(fp, fpo);
        return 0;
      }
  
***************
*** 776,801 ****
        e = 0;
        while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
        {
!         if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF
!                 || putc(hexx[e & 0xf], fpo) == EOF)
!           perror_exit(3);
          n++;
          if (!--p)
            {
!             if (putc('\n', fpo) == EOF)
!               perror_exit(3);
              p = cols;
            }
        }
!       if (e == EOF && ferror(fp))
!       perror_exit(2);
        if (p < cols)
!       if (putc('\n', fpo) == EOF)
!         perror_exit(3);
!       if (fclose(fp))
!       perror_exit(2);
!       if (fclose(fpo))
!       perror_exit(3);
        return 0;
      }
  
--- 786,804 ----
        e = 0;
        while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
        {
!         putc_or_die(hexx[(e >> 4) & 0xf], fpo);
!         putc_or_die(hexx[e & 0xf], fpo);
          n++;
          if (!--p)
            {
!             putc_or_die('\n', fpo);
              p = cols;
            }
        }
!       exit_on_ferror(e, fp);
        if (p < cols)
!       putc_or_die('\n', fpo);
!       fclose_or_die(fp, fpo);
        return 0;
      }
  
***************
*** 853,860 ****
          p = 0;
        }
      }
!   if (e == EOF && ferror(fp))
!     perror_exit(2);
    if (p)
      {
        l[c] = '\n';
--- 856,862 ----
          p = 0;
        }
      }
!   exit_on_ferror(e, fp);
    if (p)
      {
        l[c] = '\n';
***************
*** 864,873 ****
    else if (autoskip)
      xxdline(fpo, l, -1);      /* last chance to flush out suppressed lines */
  
!   if (fclose(fp))
!     perror_exit(2);
!   if (fclose(fpo))
!     perror_exit(3);
    return 0;
  }
  
--- 866,872 ----
    else if (autoskip)
      xxdline(fpo, l, -1);      /* last chance to flush out suppressed lines */
  
!   fclose_or_die(fp, fpo);
    return 0;
  }
  
*** ../vim-8.2.3669/src/version.c       2021-11-25 10:50:09.178844306 +0000
--- src/version.c       2021-11-25 11:15:42.733395724 +0000
***************
*** 759,760 ****
--- 759,762 ----
  {   /* Add new patch number below this line */
+ /**/
+     3670,
  /**/

-- 
A special cleaning ordinance bans housewives from hiding dirt and dust under a
rug in a dwelling.
                [real standing law in Pennsylvania, United States of America]

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
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

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20211125111756.9DB6E1C0A05%40moolenaar.net.

Raspunde prin e-mail lui