fix failure to flush stderr when fflush(0) is called
[musl.git] / src / internal / shgetc.c
blobebd5fae707b4ad44abfaf21cff797d9bcd37e26e
1 #include "shgetc.h"
3 /* The shcnt field stores the number of bytes read so far, offset by
4 * the value of buf-rpos at the last function call (__shlim or __shgetc),
5 * so that between calls the inline shcnt macro can add rpos-buf to get
6 * the actual count. */
8 void __shlim(FILE *f, off_t lim)
10 f->shlim = lim;
11 f->shcnt = f->buf - f->rpos;
12 /* If lim is nonzero, rend must be a valid pointer. */
13 if (lim && f->rend - f->rpos > lim)
14 f->shend = f->rpos + lim;
15 else
16 f->shend = f->rend;
19 int __shgetc(FILE *f)
21 int c;
22 off_t cnt = shcnt(f);
23 if (f->shlim && cnt >= f->shlim || (c=__uflow(f)) < 0) {
24 f->shcnt = f->buf - f->rpos + cnt;
25 f->shend = 0;
26 return EOF;
28 cnt++;
29 if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
30 f->shend = f->rpos + (f->shlim - cnt);
31 else
32 f->shend = f->rend;
33 f->shcnt = f->buf - f->rpos + cnt;
34 if (f->rpos[-1] != c) f->rpos[-1] = c;
35 return c;