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
This commit is contained in:
Jakob Kramer 2015-02-11 00:30:05 +01:00 committed by sin
parent c1e6ecb41b
commit 66a5ea722d

View File

@ -24,4 +24,9 @@ getlines(FILE *fp, struct linebuf *b)
memcpy(b->lines[b->nlines-1], line, linelen); memcpy(b->lines[b->nlines-1], line, linelen);
} }
free(line); 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';
}
} }