On 12/13/25 1:51 AM, Grisha Levit wrote:
On Fri, Dec 12, 2025, 19:20 Robert Elz <[email protected] <mailto:[email protected]>> wrote:

 > I'm guessing that the difference between using awk, and most other
 > processes that might be used (echo, sed, even cat probably) inside the
 > cmdsub, is that awk (gawk) might be doing
 >
 >         freopen(filename, "r", stdin);
 >
 > or something similar, which it is perfectly entitled to do (ie: doing that
 > is certainly not, of itself, a bug).

What gawk had was essentially a call like:

     fcntl(2, F_SETFL, fcntl(2, F_GETFL) | O_APPEND);

This is/was the problem. That and the way cat checks for fd 0 and fd 1
being the same.


...and, it seems, any changes to the file offset or file flags associated
with inherited file descriptors.

So executing gawk, even in a subshell, would add O_APPEND to the
file status flags of FD 2 of the parent.

And it's fairly easy to write little C programs to: verify that adding
O_APPEND to fd 2's status flags reproduces the `cat' behavior; verify
that removing O_APPEND from fd 2's flags `fixes' cat; and verify that
adding O_APPEND to fd 2's flags also adds it to fd 1's flags.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [email protected]    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to