utf8bomtoutf8.c - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       utf8bomtoutf8.c (684B)
       ---
            1 /* strip UTF-8 BOM marker from data, cat the rest */
            2 #include <stdio.h>
            3 #include <string.h>
            4 
            5 int
            6 main(void)
            7 {
            8         const char *s = "\xef\xbb\xbf"; /* UTF-8 BOM */
            9         char buf[16384]; /* must be atleast strlen(s) */
           10         int i, len;
           11 
           12         /* strip prefix */
           13         len = strlen(s);
           14         i = fread(buf, 1, len, stdin);
           15         if (!(i == len && !memcmp(buf, s, len)))
           16                 fwrite(buf, 1, i, stdout);
           17 
           18         /* cat */
           19         while (!feof(stdin) && !ferror(stdin)) {
           20                 i = fread(buf, 1, sizeof(buf), stdin);
           21                 if (fwrite(buf, 1, i, stdout) != i)
           22                         break;
           23         }
           24         if (ferror(stdin)) {
           25                 fputs("read error\n", stderr);
           26                 return 1;
           27         }
           28         if (fflush(stdout) || ferror(stdout)) {
           29                 fputs("write error\n", stderr);
           30                 return 1;
           31         }
           32 
           33         return 0;
           34 }