sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 5765a6e6b434ce692a15ed2e64bc2ea0d1c5a2e1
parent 08b8c73ddaf92727f8e9a93f2c0d60eb0183caa4
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Sat, 13 Dec 2025 12:05:39 +0100

ed: Implement non truncate writes

The trunc parameter of dowrite() was ignored, making no difference
between the w and W commands.

Diffstat:
Med.c | 4+++-
Atests/0018-ed.sh | 20++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/ed.c b/ed.c @@ -761,6 +761,7 @@ dowrite(const char *fname, int trunc) FILE *aux; static int sh; static FILE *fp; + char *mode; if (fp) { sh ? pclose(fp) : fclose(fp); @@ -774,7 +775,8 @@ dowrite(const char *fname, int trunc) error("bad exec"); } else { sh = 0; - if ((fp = fopen(fname, "w")) == NULL) + mode = (trunc) ? "w" : "a"; + if ((fp = fopen(fname, mode)) == NULL) error("cannot open input file"); } diff --git a/tests/0018-ed.sh b/tests/0018-ed.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +tmp=tmp.$$ + +trap 'rm -f $tmp' EXIT +trap 'rm -f $tmp; kill -KILL $$' HUP INT TERM + +touch $tmp +../ed -s $tmp <<EOF | (read a && test $a = 1) +a +1 +. +w +,c +2 +. +W +e +1p +EOF