[hackers] [sbase] getlines: last line of file should always have a newline || Jakob Kramer
commit de6f2b2896abcb57e314180e4d26f748ec5fe750
Author: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Wed Feb 11 00:30:05 2015 +0100
getlines: last line of file should always have a newline
This is a useful behavior if you want to reorder the lines,
because otherwise you might end up with originally two lines
on one, e.g.
$ echo -ne "foo\nbar" | sort
barfoo
diff --git a/libutil/getlines.c b/libutil/getlines.c
index 55e9bf4..8d4fc44 100644
--- a/libutil/getlines.c
+++ b/libutil/getlines.c
_AT_@ -24,4 +24,9 @@ getlines(FILE *fp, struct linebuf *b)
memcpy(b->lines[b->nlines-1], line, linelen);
}
free(line);
+ if (strchr(b->lines[b->nlines-1], '\n') == NULL) {
+ b->lines[b->nlines-1] = erealloc(b->lines[b->nlines-1], linelen + 1);
+ b->lines[b->nlines-1][linelen-1] = '\n';
+ b->lines[b->nlines-1][linelen] = '\0';
+ }
}
Received on Wed Feb 11 2015 - 01:29:13 CET
This archive was generated by hypermail 2.3.0
: Wed Feb 11 2015 - 01:36:19 CET