summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--io.c11
-rw-r--r--more.h2
2 files changed, 13 insertions, 0 deletions
diff --git a/io.c b/io.c
index 2715e1b..a253792 100644
--- a/io.c
+++ b/io.c
@@ -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;
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;
};