about summary refs log tree commit diff
path: root/src/stdio/fgets.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdio/fgets.c')
-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 d3f9819e..6171f398 100644
--- a/src/stdio/fgets.c
+++ b/src/stdio/fgets.c
@@ -21,14 +21,16 @@ char *fgets(char *restrict s, int n, FILE *restrict f)
 	}
 
 	while (n) {
-		z = memchr(f->rpos, '\n', f->rend - f->rpos);
-		k = z ? z - f->rpos + 1 : f->rend - f->rpos;
-		k = MIN(k, n);
-		memcpy(p, f->rpos, k);
-		f->rpos += k;
-		p += k;
-		n -= k;
-		if (z || !n) break;
+		if (f->rpos != f->rend) {
+			z = memchr(f->rpos, '\n', f->rend - f->rpos);
+			k = z ? z - f->rpos + 1 : f->rend - f->rpos;
+			k = MIN(k, n);
+			memcpy(p, f->rpos, k);
+			f->rpos += k;
+			p += k;
+			n -= k;
+			if (z || !n) break;
+		}
 		if ((c = getc_unlocked(f)) < 0) {
 			if (p==s || !feof(f)) s = 0;
 			break;