diff options
author | David Phillips <david@sighup.nz> | 2018-08-02 00:00:58 +1200 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2018-08-02 00:00:58 +1200 |
commit | 784b4c9e35eb166161cfcfbb98515834bb053a75 (patch) | |
tree | a902415376f0292a85316445df70c4d8dc9f54e8 | |
parent | 4b4f9d9aeeff996f216e228d900e1afdc8d22b32 (diff) | |
download | hence-784b4c9e35eb166161cfcfbb98515834bb053a75.tar.xz |
common: Make line counting more robust
-rw-r--r-- | common.c | 16 |
1 files changed, 11 insertions, 5 deletions
@@ -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 == ' ') { |