home · contact · privacy
Make try_fgetc() prepend clearerr() for fgetc() versions that need it.
[plomrogue] / src / common / readwrite.c
index 3dcd2a2d90ec307d86a6c23fe99a36048725e143..045bc4fbbc89f80ff2044bc971e98251488d1681 100644 (file)
@@ -4,7 +4,7 @@
 #include <stddef.h> /* size_t */
 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
-                    * fputc(), fwrite(), fclose(), fopen()
+                    * fputc(), fwrite(), fclose(), fopen(), clearerr()
                     */
 #include <string.h> /* strlen() */
 #include <unistd.h> /* for access(), unlink() */
@@ -53,6 +53,7 @@ extern void try_fputc(uint8_t c, FILE * file, char * f)
 
 extern int try_fgetc(FILE * file, char * f)
 {
+    clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
     int test = fgetc(file);
     exit_trouble(EOF == test && ferror(file), f, "fgetc()");
     return test;
@@ -108,8 +109,8 @@ extern uint32_t textfile_width(FILE * file)
         {
             break;
         }
-        exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
         c_count++;
+        exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
         if ('\n' == c)
         {
             if (c_count > linemax)