Package: busybox
Version: master
Severity: bug

Hello all,

I found a bit of broken behavior when `busybox vi` is used on a file
that doesn't end in a newline. If you go to the last line of the file
and type "o", the new line that opens below it contains the last
character of the previous line. This shouldn't happen, and I attached
a demo text file that you can reproduce it on. The "A" command on that
line is also broken, as it desynchronizes the cursor from where text
appears.

I made a patch to fix it by adding a newline to the end of files that
don't already have one. This comes with the caveat that opening a
file, not making any edits, and saving it can append a newline. Vi,
vim, nano, and gedit already add this newline though, so this is
expected behavior.

Output of `make bloatcheck`:
function                                             old     new   delta
init_text_buffer                                     161     193     +32
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 32/0)               Total: 32 bytes
   text       data        bss        dec        hex    filename
1026178      16515       1808    1044501      ff015    busybox_old
1026210      16515       1808    1044533      ff035    busybox_unstripped

Thanks,
David
diff --git a/editors/vi.c b/editors/vi.c
index 3cc3d2a0b..7f99961d1 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2315,9 +2315,12 @@ 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
+	if (rc <= 0) {
+		// file doesn't exist or is empty
 		char_insert(text, '\n', NO_UNDO);
+	} else if (end[-1] != '\n') {
+		// file doesn't end with a newline
+		stupid_insert(end, '\n');
 	}
 
 	flush_undo_data();
Go to the beginning of the last line with your cursor, and press 'o'. It
will move the 1 onto its own line, which is improper behavior.

You can also type "A" to append to the line, and the cursor ends up getting
desynchronized from where characters appear.

If it doesn't show the problem, you might need to truncate the ending newline
character if one got added.

This line: 1
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to