From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 Date: Thu, 23 Jan 2014 22:27:42 -0500 Message-ID: From: cherry To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: multipart/alternative; boundary=001a1134890ad9ace704f0aef01e Subject: [9fans] echo -n and zero-length write(2) Topicbox-Message-UUID: b45fb15e-ead8-11e9-9d60-3106f5b1d025 --001a1134890ad9ace704f0aef01e Content-Type: text/plain; charset=ISO-8859-1 Hello Fans, When echo(1) is invoked with -n and no or empty arguments, it calls write(2) with zero length. When writing to a pipe, the other end of the pipe read()'s zero byte, which causes it thinks it's got to the end and close the pipe, and commands after the "echo -n" would fail to write. I start thinking whether this is the desired behavior. On one hand, it is an easy way to do a zero-length write() through echo -n, also it is consistent for echo(1): to do a write(2). On the other hand, it makes rc script behaves differently if sending to a pipe, and probably some people would expect empty echo -n is just a no-op. Which should be better? The context is, as posted on the werc list, it's template.awk generates rc script with echo -n, and the output of the script is sent to a pipe. When the argument is empty it fails. My last post was wrapping echo as fn echo { builtin echo $* | cat } But invoking cat for every echo hurts the performance. Perhaps the most efficient way is to write an ad hoc echo that does not do zero-length write(2)? Any suggestion? Thanks, - cherry --001a1134890ad9ace704f0aef01e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hello Fans,

When echo(1) is invoked with -n and no = or empty arguments, it calls write(2) with zero length. When writing to a p= ipe, the other end of the pipe read()'s zero byte, which causes it thin= ks it's got to the end and close the pipe, and commands after the "= ;echo -n" would fail to write. I start thinking whether this is the de= sired=20 behavior. On one hand, it is an easy way to do a zero-length write() throug= h echo -n, also it is consistent for echo(1): to do a write(2). On the other hand, it makes rc script behaves differently if sending to a=20 pipe, and probably some people would expect empty echo -n is just a no-op. = Which should be better?

The context is, as posted on the werc list, = it's template.awk generates rc script with echo -n, and the output of t= he script is sent to a pipe. When the argument is empty it fails. My last p= ost was wrapping echo as

fn echo { builtin echo $* | cat }

But invoking cat for every ech= o hurts the performance. Perhaps the most efficient way is to write an ad h= oc echo that does not do zero-length write(2)? Any suggestion?

Thank= s,
- cherry

--001a1134890ad9ace704f0aef01e--