diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-07-15 12:38:28 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-07-15 12:38:28 -0400 |
commit | 408d8ecbd73cf8dcb4d9af0e3e965534c1aff03f (patch) | |
tree | 6eee8c854c6ac26343bdd39c00fb63c1501fb52d | |
parent | 707da08aacb29be4b1b7677768b0206301cf50a0 (diff) |
add byte counts for use by =/^G
-rw-r--r-- | io.c | 11 | ||||
-rw-r--r-- | more.h | 2 |
2 files changed, 13 insertions, 0 deletions
@@ -3,6 +3,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> +#include <unistd.h> #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; @@ -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; }; |