bomstrip (9-3) 01_c_warnings.patch

Summary

 bomstrip.c |   23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

    
download this patch

Patch contents

Description: Make this program really work and fix some compiler warnings
 - make buf[] and utf8bom[] global to make sure they're zero-initialized
   and also to save stack space
 - use a sane size for buf[]
 - replace the fread() < 0 check with one for ferror()
 - check the return code of fwrite(), too
Forwarded: yes
Author: Peter Pentchev <roam@ringlet.net>
Last-Update: 2010-06-16

--- a/bomstrip.c
+++ b/bomstrip.c
@@ -3,19 +3,22 @@
 #include <unistd.h>
 #include <string.h>
 
-void
-usage(char *prog)
+char		 buf[BUFSIZ];
+const char	*utf8bom = "\xef\xbb\xbf";
+
+static void	 usage(const char *);
+
+static void
+usage(const char *prog)
 {
 	fprintf(stderr, "usage: %s\n", prog);
 	exit(1);
 }
 
 int
-main(int argc, char *argv[])
+main(int argc, const char * const argv[])
 {
 	size_t nread;
-	char buf[65536];
-	char *utf8bom = "\xef\xbb\xbf";
 
 	if (argc > 1)
 		usage(argv[0]);
@@ -24,14 +27,14 @@
 	if (nread == 0)
 		return 0;
 	if (strcmp(buf, utf8bom) != 0)
-		fwrite(buf, 1, nread, stdout);
+		if (fwrite(buf, 1, nread, stdout) < nread)
+			exit(1);
 	for (;;) {
 		nread = fread(buf, 1, sizeof buf, stdin);
-		if (nread < 0)
-			exit(1);
 		if (nread == 0)
-			return 0;
-		fwrite(buf, 1, nread, stdout);
+			return ferror(stdin) && 1;
+		if (fwrite(buf, 1, nread, stdout) < nread)
+			exit(1);
 	}
 	return 0;
 }