[hackers] [sbase] [PATCH] cksum: Skip files with read errors and continue

From: Wolfgang Corcoran-Mathe <first.lord.of.teal_AT_gmail.com>
Date: Thu, 11 Jun 2015 12:25:34 -0400

Previously, 'cksum *' would exit early if * contained any
directories or other files causing fread() errors.

Exit status is set to indicate an error has occurred.
---
 cksum.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/cksum.c b/cksum.c
index 3355b4c..179ab05 100644
--- a/cksum.c
+++ b/cksum.c
_AT_@ -59,7 +59,7 @@ static const unsigned long crctab[] = {         0x00000000,
 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
 };
 
-static void
+static int
 cksum(FILE *fp, const char *s)
 {
 	size_t len = 0, i, n;
_AT_@ -71,8 +71,10 @@ cksum(FILE *fp, const char *s)
 			ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
 		len += n;
 	}
-	if (ferror(fp))
-		eprintf("fread %s:", s ? s : "<stdin>");
+	if (ferror(fp)) {
+		weprintf("fread %s:", s ? s : "<stdin>");
+		return 1;
+	}
 
 	for (i = len; i; i >>= 8)
 		ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)];
_AT_@ -83,6 +85,7 @@ cksum(FILE *fp, const char *s)
 		fputs(s, stdout);
 	}
 	putchar('\n');
+	return 0;
 }
 
 int
_AT_@ -105,7 +108,7 @@ main(int argc, char *argv[])
 				ret = 1;
 				continue;
 			}
-			cksum(fp, *argv);
+			ret |= cksum(fp, *argv);
 			if (fp != stdin && fshut(fp, *argv))
 				ret = 1;
 		}
-- 
2.3.5
Received on Thu Jun 11 2015 - 18:25:34 CEST

This archive was generated by hypermail 2.3.0 : Thu Jun 18 2015 - 17:36:42 CEST