summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-08-02 00:00:58 +1200
committerDavid Phillips <david@sighup.nz>2018-08-02 00:00:58 +1200
commit784b4c9e35eb166161cfcfbb98515834bb053a75 (patch)
treea902415376f0292a85316445df70c4d8dc9f54e8
parent4b4f9d9aeeff996f216e228d900e1afdc8d22b32 (diff)
downloadhence-784b4c9e35eb166161cfcfbb98515834bb053a75.tar.xz
common: Make line counting more robust
-rw-r--r--common.c16
1 files 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 == ' ') {