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 +++++++++++ more.h | 2 ++ 2 files changed, 13 insertions(+) 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; diff --git a/more.h b/more.h index 8e6e723..27e7e7a 100644 --- a/more.h +++ b/more.h @@ -14,6 +14,8 @@ struct more_file { fpos_t *lines; size_t nlines; size_t mark[26]; + size_t nbytes; + size_t *bytepos; char *buf; size_t nbuf; }; -- cgit v1.2.1