capture the offset at the start of a string and use that for printing.
previous code was incorrect for a string with any multibyte runes in
its first min-length characters.
---
 strings.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/strings.c b/strings.c
index 8f5a154..da25f9f 100644
--- a/strings.c
+++ b/strings.c
@@ -14,17 +14,18 @@ strings(FILE *fp, const char *fname, size_t min)
 {
        Rune r, *rbuf;
        size_t i, bread;
-       off_t off;
+       off_t off, start;
 
        rbuf = ereallocarray(NULL, min, sizeof(*rbuf));
 
-       for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
+       for (off = 0, start = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
                off += bread;
                if (r == Runeerror)
                        continue;
                if (!isprintrune(r)) {
                        if (i == min)
                                putchar('\n');
+                       start = off;
                        i = 0;
                        continue;
                }
@@ -35,7 +36,7 @@ strings(FILE *fp, const char *fname, size_t min)
                rbuf[i++] = r;
                if (i < min)
                        continue;
-               printf(format, (long)off - i);
+               printf(format, (long)start);
                for (i = 0; i < min; i++)
                        efputrune(rbuf + i, stdout, "<stdout>");
        }
-- 
2.31.1


Reply via email to