Hi tech@,

I am reading write(2) manual, and come across the following example:

for (off = 0; off < bsz; off += nw)
        if ((nw = write(d, buf + off, bsz - off)) == 0 || nw == -1)
                err(1, "write");

I am just wondering when the write(2) will return 0? If in some cases,
it will indeed return 0, according to the manual:

> Upon successful completion the number of bytes which were written is
returned. Otherwise, a -1 is returned and the global variable errno is
set to indicate the error.

Because the errno is only set when return value is -1, if write(2)
returns 0, the errno should be an undefined value, and "err(1,
"write");" also won't print correct information.

If write(2) won't return 0, my following patch fixes the example code:

diff --git write.2 write.2
index c1686b1a910..db134959002 100644
--- write.2
+++ write.2
@@ -164,7 +164,7 @@ ssize_t nw;
 int d;

 for (off = 0; off < bsz; off += nw)
-       if ((nw = write(d, buf + off, bsz - off)) == 0 || nw == -1)
+       if ((nw = write(d, buf + off, bsz - off)) == -1)
                err(1, "write");
 .Ed
 .Sh ERRORS

Thanks!

-- 
Best Regards
Nan Xiao

Reply via email to