X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Ffgets.c;h=6171f398dacb4170a5a1227d515788d5457b252e;hb=7be59733d71ada3a32a98622507399253f1d5e48;hp=cf5b1039b50e7c6eafb6d792f605f91836d3b118;hpb=402611c3ba3be5b3b0486835d98e22ac7ced2722;p=musl diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index cf5b1039..6171f398 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -10,23 +10,27 @@ char *fgets(char *restrict s, int n, FILE *restrict f) size_t k; int c; + FLOCK(f); + if (n--<=1) { + f->mode |= f->mode-1; + FUNLOCK(f); if (n) return 0; *s = 0; return s; } - FLOCK(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;