ed: Detect correctly end of file in gettxt()

The function gettxt() is basically a repeat loop where it loops
until it arrives to the end of the file when it doesn't find  a
newline, but the condition to detect the end of file was wrong
and it looped forever in a file without newline.
This commit is contained in:
Roberto E. Vargas Caballero 2025-12-08 17:32:38 +01:00
parent c63dd6d499
commit beb6a2fa2a
2 changed files with 15 additions and 1 deletions

3
ed.c
View File

@ -244,6 +244,7 @@ gettxt(int line)
return addchar('\0', &text);
repeat:
chksignals();
if (!csize || off < lasto || off - lasto >= csize) {
block = off & ~(CACHESIZ-1);
if (lseek(scratch, block, SEEK_SET) < 0 ||
@ -257,7 +258,7 @@ repeat:
++off;
addchar(*p, &text);
}
if (csize && p == buf + csize)
if (csize == CACHESIZ && p == buf + csize)
goto repeat;
addchar('\n', &text);

13
tests/0007-ed.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
set -e
tmp=tmp.$$
trap 'rm -f $tmp' EXIT
trap 'rm -f $tmp; kill -KILL $$' HUP INT TERM
printf 'something important' > $tmp
ed $tmp <<EOF 2>/dev/null | grep something | diff -w $tmp -
1p
EOF