From 05e5d6a38157cabc733fc30acf3646a099cf139c Mon Sep 17 00:00:00 2001 From: Petja Patjas Date: Mon, 17 Apr 2023 16:28:53 +0300 Subject: vi: Ensure that the edit buffer ends in a newline Currently vi assumes that the edit buffer ends in a newline. This may not be the case. For example: $ printf test > test $ vi test We fix this by inserting a newline to the end during initialization. Signed-off-by: Petja Patjas Signed-off-by: Denys Vlasenko --- editors/vi.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/editors/vi.c b/editors/vi.c index 3cc3d2a0b..34932f60c 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -2315,9 +2315,10 @@ static int init_text_buffer(char *fn) update_filename(fn); rc = file_insert(fn, text, 1); - if (rc < 0) { - // file doesnt exist. Start empty buf with dummy line - char_insert(text, '\n', NO_UNDO); + if (rc <= 0 || *(end - 1) != '\n') { + // file doesn't exist or doesn't end in a newline. + // insert a newline to the end + char_insert(end, '\n', NO_UNDO); } flush_undo_data(); -- cgit v1.2.3-55-g6feb