From: Christian Heller Date: Fri, 18 Apr 2014 12:43:20 +0000 (+0200) Subject: Make try_fgetc() prepend clearerr() for fgetc() versions that need it. X-Git-Tag: tce~754 X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Bdb.prefix%7D%7D/blog?a=commitdiff_plain;h=2690e30a5d476cdf8689af9203748e7ed0fa841d;p=plomrogue Make try_fgetc() prepend clearerr() for fgetc() versions that need it. --- diff --git a/src/common/readwrite.c b/src/common/readwrite.c index c2c970d..045bc4f 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -4,7 +4,7 @@ #include /* size_t */ #include /* uint8_t, uint16_t, uint32_t, UINT32_MAX */ #include /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(), - * fputc(), fwrite(), fclose(), fopen() + * fputc(), fwrite(), fclose(), fopen(), clearerr() */ #include /* strlen() */ #include /* 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; diff --git a/src/common/readwrite.h b/src/common/readwrite.h index 4aebdbc..b61230f 100644 --- a/src/common/readwrite.h +++ b/src/common/readwrite.h @@ -22,7 +22,9 @@ extern void try_fputc(uint8_t c, FILE * file, char * f); /* 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);