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
>From 6a2b31c06be91a64fe6ea7acea65c00db3a336e0 Mon Sep 17 00:00:00 2001
From: Jakob Kramer <[email protected]>
Date: Wed, 11 Feb 2015 00:30:05 +0100
Subject: [PATCH 2/2] 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
---
libutil/getlines.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libutil/getlines.c b/libutil/getlines.c
index 55e9bf4..8d4fc44 100644
--- a/libutil/getlines.c
+++ b/libutil/getlines.c
@@ -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';
+ }
}
--
1.9.1