1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
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;
}
|