From 408d8ecbd73cf8dcb4d9af0e3e965534c1aff03f Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 15 Jul 2020 12:38:28 -0400 Subject: add byte counts for use by =/^G --- io.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'io.c') diff --git a/io.c b/io.c index 2715e1b..a253792 100644 --- a/io.c +++ b/io.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "more.h" @@ -16,10 +18,16 @@ ssize_t more_getline(struct more_file *mf, size_t lineno) while (mf->nlines <= lineno) { mf->nlines++; mf->lines = realloc(mf->lines, mf->nlines * sizeof(*mf->lines)); + mf->bytepos = realloc(mf->bytepos, mf->nlines * sizeof(*mf->bytepos)); fgetpos(mf->f, &(mf->lines[mf->nlines - 1])); getline(&(mf->buf), &(mf->nbuf), mf->f); + if (mf->nlines > 1) { + mf->bytepos[mf->nlines - 1] = mf->bytepos[mf->nlines - 2] + strlen(mf->buf); + } else { + mf->bytepos[0] = 0; + } if (mf->backing != mf->f) { fgetpos(mf->backing, &(mf->lines[mf->nlines - 1])); @@ -50,6 +58,9 @@ struct more_file more_open(const char *path) mf.backing = tmpfile(); } else { mf.backing = mf.f; + struct stat st; + fstat(fileno(mf.f), &st); + mf.nbytes = st.st_size; } return mf; -- cgit v1.2.1