ed: Detect correctly end of file in gettxt() - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit beb6a2fa2aeaa9791738510097231e7e9da45f47
 (DIR) parent c63dd6d49941778ad99fb1f2834f97626a511ccb
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Mon,  8 Dec 2025 17:32:38 +0100
       
       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.
       
       Diffstat:
         M ed.c                                |       3 ++-
         A tests/0007-ed.sh                    |      13 +++++++++++++
       
       2 files changed, 15 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/ed.c b/ed.c
       @@ -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);
 (DIR) diff --git a/tests/0007-ed.sh b/tests/0007-ed.sh
       @@ -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