plan9front/sys/src/cmd/awk
Anthony Martin ef2fdb6fdd awk: fix truncated input after fflush
Before the "native" awk work, a call to the fflush function resulted
in one or more calls to the APE fflush(2).

Calling fflush on a stream open for reading has different behavior
based on the environment: within APE, it's a no-op¹; on OpenBSD, it's
an error²; in musl, it depends on whether or not the underlying file
descriptor is seekable³; etc. I'm sure glibc is subtly different.

Now that awk uses libbio, things are different: calling Bflush(2) on a
file open for reading simply discards any data in the buffer. This
explains why we're seeing truncated input. When awk attempts to read
in the next record, there's nothing in the buffer and no more data to
read so it gets EOF and exits normally. Note that this behavior is not
documented in bio(2). It was added in the second edition but I haven't
figured out why or what depends on it.

The simple fix is to have awk only call Bflush on files that were
opened for writing. You could argue that this is the only correct
behavior according to the awk(1) manual and it is, in fact, how GNU
awk behaves⁴.

1. /sys/src/ape/lib/ap/stdio/fflush.c
2. https://cvsweb.openbsd.org/src/lib/libc/stdio/fflush.c?rev=1.9
3. https://git.musl-libc.org/cgit/musl/tree/src/stdio/fflush.c
4. https://git.savannah.gnu.org/cgit/gawk.git/tree/io.c#n1492
2020-11-19 23:05:26 -08:00
..
awk.h awk: allow string as exit status 2017-08-12 21:34:06 +02:00
awkgram.y awk: allow string as exit status 2017-08-12 21:34:06 +02:00
lex.c awk: allow string as exit status 2017-08-12 21:34:06 +02:00
lib.c awk: make empty FS unicodely-correct. 2019-10-09 17:36:02 -07:00
main.c improve usage messages (thanks henesy) 2020-03-10 10:09:34 -07:00
maketab.c awk: handle bad/incomplete input in maketab (thanks kenji arisawa) 2017-06-02 19:03:37 +02:00
mkfile awk: fix race condition with sub-mk in mkfile 2020-05-24 16:00:45 +02:00
parse.c awk: allow string as exit status 2017-08-12 21:34:06 +02:00
popen.c remove ape regexp library, add utility for awk native port 2016-04-27 07:52:41 -05:00
proto.h awk: bring back ENVIRON[] support 2016-05-02 00:34:23 +02:00
re.c New libregexp and APE ported to native 2016-04-26 22:23:44 -05:00
run.c awk: fix truncated input after fflush 2020-11-19 23:05:26 -08:00
tran.c awk: bring back ENVIRON[] support 2016-05-02 00:34:23 +02:00