From 784b4c9e35eb166161cfcfbb98515834bb053a75 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Thu, 2 Aug 2018 00:00:58 +1200 Subject: common: Make line counting more robust --- common.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/common.c b/common.c index fbc9ae2..62f0a2e 100644 --- a/common.c +++ b/common.c @@ -4,14 +4,20 @@ void indicate_file_area(FILE* fd, size_t line, size_t column, size_t span) { - char margin[] = " "; - /* FIXME use proper line counting, not this hack */ - char buf[1024]; + const char margin[] = " "; + + char buf[1024] = { '\0' }; + char *s = buf; char *line_start = buf; rewind(fd); - for (; line; line--) { - fgets(buf, sizeof(buf), fd); + while (line && !feof(fd) && fgets(buf, sizeof(buf), fd)) { + s = buf; + while (*s) { + if (*(s++) == '\n') { + line--; + } + } } while (*line_start == '\t' || *line_start == ' ') { -- cgit v1.1