From 987d50eba64ff6720db47b21fd09af7a6d486ded Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 6 Jul 2017 10:57:34 +0200 Subject: [PATCH] Improve prototype of fgets so that it handles better EOL. Return true in case of something has been read. --- m-string.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/m-string.h b/m-string.h index 80bebb29..939a602f 100644 --- a/m-string.h +++ b/m-string.h @@ -532,7 +532,7 @@ string_cat_printf (string_t v, const char format[], ...) return size; } -static inline void +static inline bool string_fgets(string_t v, FILE *f, string_fgets_t arg) { STRING_CONTRACT(v); @@ -540,8 +540,9 @@ string_fgets(string_t v, FILE *f, string_fgets_t arg) M_ASSUME(v->ptr != NULL); v->size = 0; v->ptr[0] = 0; - while (!feof (f) && !ferror(f)) { - fgets(&v->ptr[v->size], v->alloc - v->size, f); + bool retcode = false; + while (fgets(&v->ptr[v->size], v->alloc - v->size, f) != NULL) { + retcode = true; char *p = strchr(&v->ptr[v->size], '\n'); v->size += strlen(&v->ptr[v->size]); STRING_CONTRACT(v); @@ -551,7 +552,7 @@ string_fgets(string_t v, FILE *f, string_fgets_t arg) v->size --; } STRING_CONTRACT(v); - return; + return retcode; } else if (p == NULL && !feof(f)) { /* The string buffer is not big enough: increase it and continue reading */ @@ -559,6 +560,7 @@ string_fgets(string_t v, FILE *f, string_fgets_t arg) } } STRING_CONTRACT (v); + return retcode; } static inline void