When inserting multiple lines with the insert or change command the
lines get reordered when the file is empty before inserting. e.g.
following ed script produces incorrect output (apply to a file with
three lines in it).
1,3c
x
y
z
.
The expected order of lines would be, x, y, z but patch produces y, z, x
instead. The first line is inserted at the correct position, but then
the other lines are inserted before the first one. The following diff
solves that problem by retaining the logic for the first line, but then
switch to append mode to insert the remaining lines after that one.
natano
Index: ed.c
===================================================================
RCS file: /cvs/src/usr.bin/patch/ed.c,v
retrieving revision 1.1
diff -u -p -r1.1 ed.c
--- ed.c 16 Oct 2015 07:33:47 -0000 1.1
+++ ed.c 21 Feb 2016 17:43:47 -0000
@@ -131,12 +131,13 @@ do_ed_script(void)
line_count++;
} else if (fsm == FSM_I) {
nline = create_line(linepos);
- if (cline == NULL) {
+ if (cline == NULL)
LIST_INSERT_HEAD(&head, nline, entries);
- cline = nline;
- } else
+ else
LIST_INSERT_BEFORE(cline, nline, entries);
+ cline = nline;
line_count++;
+ fsm = FSM_A;
}
}