On Sat, Sep 16, 2017 at 10:25:19PM -0500, Scott Cheloha wrote: > Hi, > > The second example in the write(2) CAVEATS section is identical to > the corresponding example in the read(2) page: > > while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0) > > read(2) returns 0 on EOF, but that logic is inapplicable to write(2). > > I'm not totally sure what the correct code should be but attached is > my best guess. > > Also, if "nr" is for "Number Read", because this is the write(2) > page, would "nw" be better? > > -- > Scott Cheloha > > Index: lib/libc/sys/write.2 > =================================================================== > RCS file: /cvs/src/lib/libc/sys/write.2,v > retrieving revision 1.39 > diff -u -p -r1.39 write.2 > --- lib/libc/sys/write.2 5 Feb 2015 02:33:09 -0000 1.39 > +++ lib/libc/sys/write.2 17 Sep 2017 03:02:57 -0000 > @@ -311,7 +311,7 @@ function call appeared in > Error checks should explicitly test for \-1. > Code such as > .Bd -literal -offset indent > -while ((nr = write(fd, buf, sizeof(buf))) > 0) > +while ((nw = write(fd, buf, sizeof(buf))) > 0) > .Ed > .Pp > is not maximally portable, as some platforms allow for > @@ -325,5 +325,5 @@ and > may appear as a negative number distinct from \-1. > Proper loops should use > .Bd -literal -offset indent > -while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0) > +while ((nw = write(fd, buf, sizeof(buf))) != -1 && nw == sizeof(buf)) > .Ed
Well, that's not okay either, because nw == sizeof(buf) will only make sense for file-based fds. I'm for either scraping the example altogether or writing a proper one, which would mean embedding any version of safe_write in the man page.