summaryrefslogtreecommitdiff
path: root/src/internal/shgetc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/internal/shgetc.c')
-rw-r--r--src/internal/shgetc.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/internal/shgetc.c b/src/internal/shgetc.c
index e878b00a..ebd5fae7 100644
--- a/src/internal/shgetc.c
+++ b/src/internal/shgetc.c
@@ -1,10 +1,16 @@
#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->rpos;
- if (lim && f->shcnt > lim)
+ 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
f->shend = f->rend;
@@ -13,15 +19,18 @@ void __shlim(FILE *f, off_t lim)
int __shgetc(FILE *f)
{
int c;
- if (f->shlim && f->shcnt >= f->shlim || (c=__uflow(f)) < 0) {
+ off_t cnt = shcnt(f);
+ if (f->shlim && cnt >= f->shlim || (c=__uflow(f)) < 0) {
+ f->shcnt = f->buf - f->rpos + cnt;
f->shend = 0;
return EOF;
}
- if (f->shlim && f->rend - f->rpos > f->shlim - f->shcnt - 1)
- f->shend = f->rpos + (f->shlim - f->shcnt - 1);
+ 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->rpos + 1;
+ f->shcnt = f->buf - f->rpos + cnt;
if (f->rpos[-1] != c) f->rpos[-1] = c;
return c;
}