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.

Florian

diff --git a/src/xxd/xxd.c b/src/xxd/xxd.c
index 9f47fef..b042755 100644
--- a/src/xxd/xxd.c
+++ b/src/xxd/xxd.c
@@ -209,7 +209,7 @@ char osver[] = "";
 /* 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((FILE *, char *, int));
+static void xxdline __P((char *, 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 */
@@ -252,6 +252,16 @@ char *pname;
   exit(1);
 }
 
+static void
+die(pname, ret)
+char *pname;
+int ret;
+{
+  fprintf(stderr,"%s: ", pname);
+  perror(NULL);
+  exit(ret);
+}
+
 /*
  * Max. cols binary characters are decoded from the input stream per line.
  * Two adjacent garbage characters after evaluated data delimit valid data.
@@ -318,7 +328,8 @@ long base_off;
 
       if (base_off + want_off != have_off)
        {
-         fflush(fpo);
+         if (fflush(fpo))
+           die(pname, 3);
 #ifdef TRY_SEEK
          c = fseek(fpo, base_off + want_off - have_off, 1);
          if (c >= 0)
@@ -330,12 +341,14 @@ long base_off;
              return 5;
            }
          for (; have_off < base_off + want_off; have_off++)
-           putc(0, fpo);
+           if (putc(0, fpo) == EOF)
+             die(pname, 3);
        }
 
       if (n2 >= 0 && n1 >= 0)
        {
-         putc((n2 << 4) | n1, fpo);
+         if (putc((n2 << 4) | n1, fpo) == EOF)
+           die(pname, 3);
          have_off++;
          want_off++;
          n1 = -1;
@@ -345,6 +358,8 @@ long base_off;
              want_off = 0;
              while ((c = getc(fpi)) != '\n' && c != EOF)
                ;
+             if (c == EOF && ferror(fpi))
+               die(pname, 2);
              ign_garb = 1;
            }
        }
@@ -355,15 +370,20 @@ long base_off;
            want_off = 0;
          while ((c = getc(fpi)) != '\n' && c != EOF)
            ;
+         if (c == EOF && ferror(fpi))
+           die(pname, 2);
          ign_garb = 1;
        }
     }
-  fflush(fpo);
+  if (fflush(fpo))
+    die(pname, 3);
 #ifdef TRY_SEEK
   fseek(fpo, 0L, 2);
 #endif
-  fclose(fpo);
-  fclose(fpi);
+  if (fclose(fpo))
+    die(pname, 3);
+  if (fclose(fpi))
+    die(pname, 2);
   return 0;
 }
 
@@ -380,7 +400,8 @@ long base_off;
  * If nz is always positive, lines are never suppressed.
  */
 static void
-xxdline(fp, l, nz)
+xxdline(pname, fp, l, nz)
+char *pname;
 FILE *fp;
 char *l;
 int nz;
@@ -398,12 +419,15 @@ int nz;
          if (nz < 0)
            zero_seen--;
          if (zero_seen == 2)
-           fputs(z, fp);
+           if (fputs(z, fp) == EOF)
+             die(pname, 3);
          if (zero_seen > 2)
-           fputs("*\n", fp);
+           if (fputs("*\n", fp) == EOF)
+             die(pname, 3);
        }
       if (nz >= 0 || zero_seen > 0)
-       fputs(l, fp);
+       if (fputs(l, fp) == EOF)
+         die(pname, 3);
       if (nz)
        zero_seen = 0;
     }
@@ -664,7 +688,14 @@ char *argv[];
          long s = seekoff;
 
          while (s--)
-           (void)getc(fp);
+           if (getc(fp) == EOF)
+             if (ferror(fp))
+               {
+                 die(pname, 2);
+               } else {
+                 fprintf(stderr, "%s: sorry cannot seek.\n", pname);
+                 return 4;
+               }
        }
     }
 
@@ -672,54 +703,73 @@ char *argv[];
     {
       if (fp != stdin)
        {
-         fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : 
"");
+         if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" 
: "") < 0)
+           die(pname, 3);
          for (e = 0; (c = argv[1][e]) != 0; e++)
-           putc(isalnum(c) ? c : '_', fpo);
-         fputs("[] = {\n", fpo);
+           if (putc(isalnum(c) ? c : '_', fpo) == EOF)
+             die(pname, 3);
+         if (fputs("[] = {\n", fpo) == EOF)
+           die(pname, 3);
        }
 
       p = 0;
       while ((length < 0 || p < length) && (c = getc(fp)) != EOF)
        {
-         fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
-           (p % cols) ? ", " : ",\n  "+2*!p,  c);
+         if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
+           (p % cols) ? ", " : ",\n  "+2*!p,  c) < 0)
+           die(pname, 3);
          p++;
        }
 
       if (p)
-       fputs("\n};\n"+3*(fp == stdin), fpo);
+       if (fputs("\n};\n"+3*(fp == stdin), fpo) == EOF)
+         die(pname, 3);
 
       if (fp != stdin)
        {
-         fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "");
+         if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : 
"") < 0)
+           die(pname, 3);
          for (e = 0; (c = argv[1][e]) != 0; e++)
-           putc(isalnum(c) ? c : '_', fpo);
-         fprintf(fpo, "_len = %d;\n", p);
+           if (putc(isalnum(c) ? c : '_', fpo) == EOF)
+             die(pname, 3);
+         if (fprintf(fpo, "_len = %d;\n", p) < 0)
+           die(pname, 3);
        }
 
-      fclose(fp);
-      fclose(fpo);
+      if (fclose(fp))
+       die(pname, 2);
+      if (fclose(fpo))
+       die(pname, 3);
       return 0;
     }
 
   if (hextype == HEX_POSTSCRIPT)
     {
       p = cols;
+      e = 0;
       while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
        {
-         putc(hexx[(e >> 4) & 0xf], fpo);
-         putc(hexx[(e     ) & 0xf], fpo);
+         if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF)
+           die(pname, 3);
+         if (putc(hexx[(e     ) & 0xf], fpo) == EOF)
+           die(pname, 3);
          n++;
          if (!--p)
            {
-             putc('\n', fpo);
+             if (putc('\n', fpo) == EOF)
+               die(pname, 3);
              p = cols;
            }
        }
+      if (e == EOF && ferror(fp))
+       die(pname, 2);
       if (p < cols)
-       putc('\n', fpo);
-      fclose(fp);
-      fclose(fpo);
+       if (putc('\n', fpo) == EOF)
+         die(pname, 3);
+      if (fclose(fp))
+       die(pname,2 );
+      if (fclose(fpo))
+       die(pname, 3);
       return 0;
     }
 
@@ -730,6 +780,7 @@ char *argv[];
   else /* hextype == HEX_BITS */
     grplen = 8 * octspergrp + 1;
 
+  e = 0;
   while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
     {
       if (p == 0)
@@ -766,20 +817,24 @@ char *argv[];
       if (++p == cols)
        {
          l[c = (11 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = 
'\0';
-         xxdline(fpo, l, autoskip ? nonzero : 1);
+         xxdline(pname, fpo, l, autoskip ? nonzero : 1);
          nonzero = 0;
          p = 0;
        }
     }
+  if (e == EOF && ferror(fp))
+    die(pname, 2);
   if (p)
     {
       l[c = (11 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0';
-      xxdline(fpo, l, 1);
+      xxdline(pname, fpo, l, 1);
     }
   else if (autoskip)
-    xxdline(fpo, l, -1);       /* last chance to flush out suppressed lines */
+    xxdline(pname, fpo, l, -1);        /* last chance to flush out suppressed 
lines */
 
-  fclose(fp);
-  fclose(fpo);
+  if (fclose(fp))
+    die(pname, 2);
+  if (fclose(fpo))
+    die(pname, 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

Raspunde prin e-mail lui