correct locking in stdio functions that tried to be lock-free
[musl] / src / stdio / fgetln.c
1 #include "stdio_impl.h"
2
3 char *fgetln(FILE *f, size_t *plen)
4 {
5         char *ret = 0, *z;
6         ssize_t l;
7         FLOCK(f);
8         ungetc(getc_unlocked(f), f);
9         if ((z=memchr(f->rpos, '\n', f->rend - f->rpos))) {
10                 ret = (char *)f->rpos;
11                 *plen = ++z - ret;
12                 f->rpos = (void *)z;
13         } else if ((l = getline(&f->getln_buf, (size_t[]){0}, f)) > 0) {
14                 *plen = l;
15                 ret = f->getln_buf;
16         }
17         FUNLOCK(f);
18         return ret;
19 }