#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() */
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;
/* Wrapper to calling fgetc() and fgets() from function "f". The return code is
* returned unless ferror() indicates an error (i.e. to signify an end of file,
- * fgetc() may return an EOF and fgets() a NULL).
+ * fgetc() may return an EOF and fgets() a NULL). try_fgetc() calls clearerr()
+ * on "file" before fgetc(), because some Unixes fgetc() remember old EOFs and
+ * only return those until explicitely cleared.
*/
extern int try_fgetc(FILE * file, char * f);
extern char * try_fgets(char * line, int size, FILE * file, char * f);