author | Rich Felker <dalias@aerifal.cx> | 2018年09月16日 13:46:46 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2018年09月16日 14:37:22 -0400 |
commit | 849e7603e9004fd292a93df64dd3524025f2987a (patch) | |
tree | 4a8e8b168be59045998b430f7654464025affeaf /src/stdio/fgets.c | |
parent | 5cd309f0cc3c92f3fabbaa499652a8329137c4de (diff) | |
download | musl-849e7603e9004fd292a93df64dd3524025f2987a.tar.gz |
-rw-r--r-- | src/stdio/fgets.c | 18 |
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; |