fix broken shgetc limiter logic (wasn't working)
[musl] / src / internal / shgetc.c
1 #include "shgetc.h"
2
3 void __shlim(FILE *f, off_t lim)
4 {
5         f->shlim = lim;
6         f->shcnt = f->rend - f->rpos;
7         if (lim && f->rend - f->rpos > lim)
8                 f->shend = f->rpos + lim;
9         else
10                 f->shend = f->rend;
11 }
12
13 int __shgetc(FILE *f)
14 {
15         int c;
16         if (f->shlim && f->shcnt >= f->shlim) {
17                 f->shend = 0;
18                 return EOF;
19         }
20         c = __uflow(f);
21         if (f->shlim && f->rend - f->rpos > f->shlim - f->shcnt - 1)
22                 f->shend = f->rpos + (f->shlim - f->shcnt - 1);
23         else
24                 f->shend = f->rend;
25         if (f->rend) f->shcnt += f->rend - f->buf;
26         return c;
27 }