From ab10a9c2176faa2f339d4e39ace58b811a6c05be Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Mon, 17 Apr 2023 18:37:39 -0400 Subject: [PATCH] Process last line --- src/lib/process.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/process.c b/src/lib/process.c index 61261d9..674fa59 100644 --- a/src/lib/process.c +++ b/src/lib/process.c @@ -28,7 +28,7 @@ void loadlines(struct filedata *f) { for (size_t i = 0; i < f->buflen; i++) { char c = f->buf[i]; if (c == '\n') { - if (offset < linelen) { + if (offset < linelen) { // shrink char *new_line = realloc(line, offset); if (new_line == NULL) die("realloc"); @@ -43,7 +43,6 @@ void loadlines(struct filedata *f) { line = malloc(linelen); if (line == NULL) die("malloc"); - } else { if (offset == linelen) { linelen *= 2; @@ -58,6 +57,19 @@ void loadlines(struct filedata *f) { line[offset++] = c; } } + + // capture last line + if (offset < linelen) { // shrink + char *new_line = realloc(line, offset); + if (new_line == NULL) + die("realloc"); + line = new_line; + } + + appendline(f, line, offset); + f->lc++; + + free(line); } char *linepad(int lc, int total) {