sponge can “soak up” stdin and write it atomically to a file, enabling one to do
cat f|sponge a. I want to know how exactly it accomplishes this. How does it know when the input is finished?
strace or similar will show the system calls used by
sponge, which is probably to
write(2) the input
read(2) from standard input out to a temporary file, and then to
rename(2) that temporary file to the desired output filename when the input ends. The input ends when a
read(2) call fails or returns
0 (which indicates end-of-file) at which point
sponge can do the rename.