X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Ffgets.c;h=d3f9819e82eec3a5707805d5ae5b661a78adec30;hb=3fe595de83e398dbc3cdbe303cacaf8485c9ae08;hp=ee0ac30e83bc40a914859fbb770526590a27bfac;hpb=400c5e5c8307a2ebe44ef1f203f5a15669f20347;p=musl diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index ee0ac30e..d3f9819e 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -1,4 +1,5 @@ #include "stdio_impl.h" +#include #define MIN(a,b) ((a)<(b) ? (a) : (b)) @@ -9,14 +10,16 @@ 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; @@ -33,7 +36,7 @@ char *fgets(char *restrict s, int n, FILE *restrict f) n--; if ((*p++ = c) == '\n') break; } - *p = 0; + if (s) *p = 0; FUNLOCK(f);