From 0bdc395a81fbf8729b124137b1b9386adb21fa52 Mon Sep 17 00:00:00 2001 From: tg Date: Sun, 28 Jun 2015 14:57:25 +0000 Subject: [PATCH] =?UTF-8?q?permit=20read=20-N=20n=20(n=20=E2=89=A0=20-1)?= =?UTF-8?q?=20and=20read=20-t=20to=20return=20partial=20reads=20with=20$?= =?UTF-8?q?=3F=20=3D=3D=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit issue spotted by carstenh, could have been a documentation ambiguity issue (as -N was designed to read and return exactly n bytes), but this resolves it in a way both backwards-compatible and user-pleasing --- funcs.c | 10 ++++++---- mksh.1 | 10 +++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/funcs.c b/funcs.c index bd0124a..177530a 100644 --- a/funcs.c +++ b/funcs.c @@ -38,7 +38,7 @@ #endif #endif -__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.272 2015/05/01 23:16:29 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.273 2015/06/28 14:57:24 tg Exp $"); #if HAVE_KILLPG /* @@ -1964,8 +1964,9 @@ c_read(const char **wp) break; case 0: /* timeout expired for this call */ - rv = 1; - goto c_read_out; + bytesread = 0; + /* fake EOF read; all cases return 1 */ + goto c_read_didread; default: bi_errorf("%s: %s", Tselect, cstrerror(errno)); rv = 2; @@ -1990,6 +1991,7 @@ c_read(const char **wp) goto c_read_readloop; } + c_read_didread: switch (readmode) { case READALL: if (bytesread == 0) { @@ -2012,7 +2014,7 @@ c_read(const char **wp) if (bytesread == 0) { /* end of file reached */ rv = 1; - xp = Xstring(xs, xp); + /* may be partial read: $? = 1, but content */ goto c_read_readdone; } xp += bytesread; diff --git a/mksh.1 b/mksh.1 index 6f2c257..adcc969 100644 --- a/mksh.1 +++ b/mksh.1 @@ -1,4 +1,4 @@ -.\" $MirOS: src/bin/mksh/mksh.1,v 1.367 2015/05/23 17:43:20 tg Exp $ +.\" $MirOS: src/bin/mksh/mksh.1,v 1.368 2015/06/28 14:57:25 tg Exp $ .\" $OpenBSD: ksh.1,v 1.159 2015/03/25 12:10:52 jca Exp $ .\"- .\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, @@ -74,7 +74,7 @@ .\" with -mandoc, it might implement .Mx itself, but we want to .\" use our own definition. And .Dd must come *first*, always. .\" -.Dd $Mdocdate: May 23 2015 $ +.Dd $Mdocdate: June 28 2015 $ .\" .\" Check which macro package we use, and do other -mdoc setup. .\" @@ -3832,7 +3832,8 @@ if empty, instead of the ASCII newline character as input line delimiter. .It Fl N Ar z Instead of reading till end-of-line, read exactly .Ar z -bytes; less if EOF or a timeout occurs. +bytes. +If EOF or a timeout occurs, a partial read is returned with exit status 1. .It Fl n Ar z Instead of reading till end-of-line, read up to .Ar z @@ -3851,6 +3852,9 @@ The argument must immediately follow the option character. Interrupt reading after .Ar n seconds (specified as positive decimal value with an optional fractional part). +The exit status of +.Nm read +is 1 if the timeout occurred, but partial reads may still be returned. .It Fl r Normally, the ASCII backslash character escapes the special meaning of the following character and is stripped from the input;