In the man page for the system call write(2) -
ssize_t write(int fd, const void *buf, size_t count);
it says the following:
Return Value
On success, the number of bytes
written are returned (zero indicates
nothing was written). On error, -1 is
returned, and errno
is set
appropriately. If count is zero and
the file descriptor refers to a
regular file, 0 may be returned, or an
error could be detected. For a special
file, the results are not portable.
I would interpret this to mean that returning 0 simply means that nothing was written, for whatever arbitrary reason.
However, Stevens in UNP treats a return value of 0 as a fatal error when dealing with a file descriptor that is a TCP socket ( this is wrapped by another function which calls exit(1)
on a short count ):
ssize_t /* Write "n" bytes to a descriptor. */
writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call write() again */
else
return(-1); /* error */
}
nleft -= nwritten;
ptr += nwritten;
}
return(n);
}
He only treats 0 as a legit return value if the errno
indicates that the call to write was interrupted by the process receiving a signal.
Why?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…