summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-15 15:44:45 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-15 15:44:45 -0400
commitc021ac07699a5fd6b176f793021e0c3136b0f6a2 (patch)
tree3c11c463d255c4175e5dc4eab2f8538ac7fd7f83
parent87d5824b904c5f015f6988d11b04dafda355f9af (diff)
remember to return NULL on empty read
-rw-r--r--src/stdio/fgets.c18
1 files changed, 10 insertions, 8 deletions
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;
}