[Toybox] Fwd: potential addition of sponge to POSIX
Rob Landley
rob at landley.net
Wed Jun 25 15:30:51 PDT 2025
On 6/25/25 08:09, enh wrote:
> On Tue, Jun 24, 2025 at 7:03 PM Rob Landley <rob at landley.net> wrote:
>>
>> Anybody have a strong opinion on this command? It's 5 minutes to add,
>> probably something vaguely like:
>>
>> USE_SPONGE(NEWTOY(sponge, ">1", TOYFLAG_USR|TOYFLAG_BIN))
>>
>> void sponge_main(void)
>> {
>> off_t len = 0;
>> char *blah = readfd(0, 0, &len);
>>
>> xwrite(*toys.optargs?xopen(*toys.optargs, O_CREATE|O_BINARY:1, 700),
>
> presumably O_TRUNC too?
>
>> blah, len);
>> }
>>
>> https://linux.die.net/man/1/sponge
>
> my opinion reading the coreutils thread was basically "i've never
> heard of this in all my decades of unixing, and even now i'm
> underwhelmed".
>
> (the arguments about what exactly the semantics should be also leaned
> me towards "this is not a convincingly good idea".)
Indeed. As far as I can tell, the reason it exits is because A) you
can't have embedded NUL bytes in a shell variable, B) there's no way to
tell bash NOT to eat trailing newlines:
$ X="$(echo $'abc\n\n\n')"; echo "$X" | hd
00000000 61 62 63 0a |abc.|
So the obvious "assign the output of a subshell to a variable" mangles
the data.
You'd think with bash syntax you could go:
sed <file.txt >|file.txt blah-blah-blah
And it would delete the old file before opening the new one like vi and
friends do (and the order of operations would open the old file before
writing the new one). But alas, that's not what >| does, it seems to be
a hack to work around a new "set -o noclobber" option that breaks stuff
and regularly needs an override to disable it.
If you're not bothered about memory efficiency, you could presumably do:
sponge() { X=$(base64); base64 -d <<<"$X" > $1;}
I tried to do > ${1:-&1} but bash created a file called "&1". I suppose
I could use /dev/stdin. And yes, maybe I should delete $1 before
recreating it, but then I'm not entirely sure what sponge is DEFINED to
do there. Does it break hardlinks? Will it write to a /dev node?
(Sigh, I should cc: this back to coreutils but... I do not have the spoons.)
Rob
More information about the Toybox
mailing list