X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Finternal%2Fshgetc.c;h=a4a9c633dc976033622003c3cc7e5068466076bd;hb=107d68ad1737929a3e815bd28514cb56e4bedd57;hp=7c4e58c1e7579effa325ef1e3c0ec5eabbed7fb5;hpb=2162541f38d3f642f5a643010548d62220d55a4d;p=musl diff --git a/src/internal/shgetc.c b/src/internal/shgetc.c index 7c4e58c1..a4a9c633 100644 --- a/src/internal/shgetc.c +++ b/src/internal/shgetc.c @@ -1,9 +1,15 @@ #include "shgetc.h" +/* The shcnt field stores the number of bytes read so far, offset by + * the value of buf-rpos at the last function call (__shlim or __shgetc), + * so that between calls the inline shcnt macro can add rpos-buf to get + * the actual count. */ + void __shlim(FILE *f, off_t lim) { f->shlim = lim; - f->shcnt = f->rend ? f->rend - f->buf : 0; + f->shcnt = f->buf - f->rpos; + /* If lim is nonzero, rend must be a valid pointer. */ if (lim && f->rend - f->rpos > lim) f->shend = f->rpos + lim; else @@ -13,12 +19,19 @@ void __shlim(FILE *f, off_t lim) int __shgetc(FILE *f) { int c; - if (f->shcnt >= f->shlim) return EOF; - c = __uflow(f); - if (f->shlim && f->rend - f->rpos > f->shlim - f->shcnt - 1) - f->shend = f->rpos + (f->shlim - f->shcnt - 1); + off_t cnt = shcnt(f); + if (f->shlim && cnt >= f->shlim || (c=__uflow(f)) < 0) { + f->shcnt = f->buf - f->rpos + cnt; + f->shend = f->rpos; + f->shlim = -1; + return EOF; + } + cnt++; + if (f->shlim && f->rend - f->rpos > f->shlim - cnt) + f->shend = f->rpos + (f->shlim - cnt); else f->shend = f->rend; - if (f->rend) f->shcnt += f->rend - f->buf; + f->shcnt = f->buf - f->rpos + cnt; + if (f->rpos[-1] != c) f->rpos[-1] = c; return c; }