From c021ac07699a5fd6b176f793021e0c3136b0f6a2 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 15 Aug 2020 15:44:45 -0400 Subject: remember to return NULL on empty read --- src/stdio/fgets.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/stdio') diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index b562cb31..cae9c887 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -4,30 +4,32 @@ /** read a string of characters from a file stream **/ char * fgets(char * restrict s, int n, FILE * restrict stream) { - flockfile(stream); - int i = 0; if (feof(stream)) { return NULL; } - while (i < n-1) { + flockfile(stream); + for (i = 0; i < n-1; i++) { s[i] = fgetc(stream); if (s[i] == '\n') { s[i+1] = '\0'; - i = n; - } else if (s[i] == EOF && feof(stream)) { + break; + } else if (s[i] == EOF) { s[i] = '\0'; - i = n; + break; } - i++; } - funlockfile(stream); + /* RETURN_SUCCESS(ARGUMENT(s)); RETURN_FAILURE(CONSTANT(NULL)); */ + if (s[0] == '\0') { + return NULL; + } + return s; } -- cgit v1.2.1