8

Possible Duplicate:
Is a return value of 0 from write(2) in C an error?

Assuming that count > 0 :

ret = write(fd, buf, count);  
if(ret == 0) {  
    // Should I try to write again
    // or treat this as error?
}

Are there any circumstances in which this condition is possible?

Community
  • 1
  • 1
Marek
  • 159
  • 1
  • 6

3 Answers3

12

Unless you explicitly passed a length of zero to write, this will never happen on a correct, POSIX conformant system. If you want to support all kinds of obscure broken legacy proprietary unices, you'll probably have to investigate what happens on each one, and whether the return value of zero is appearing in place of EINTR or in place of EWOULDBLOCK or some other error...

Personally in 2011 I would just assume it doesn't happen. There are a lot of other things which will break much worse trying to support such old broken junk..

Note, per POSIX:

If write() is interrupted by a signal before it writes any data, it shall return -1 with errno set to [EINTR].

http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Thanks. Also note that, for all of the other common error conditions you might encounter (connection closed, disk full, resource limit exceeded, ...) POSIX says `write` *shall fail*. The only way I can think that it could possibly get away with returning zero is under some other implementation-defined condition that's outside the scope of any of the specified mandatory error conditions, but that would be a big stretch. – R.. GitHub STOP HELPING ICE Apr 14 '11 at 00:21
  • 1
    "this will never happen on a correct, POSIX conformant system" -- could you please provide a link to prove that statement? – Oleg Andriyanov Jan 11 '16 at 16:43
  • Linux says that if the size is 0 and there isn't any error it will return 0. I looked up whether it could return 0 otherwise. (so even nonblocking won't return 0?) – Paul Stelian Mar 05 '19 at 15:57
  • 1
    @PaulStelian: Non-blocking returns -1 with `EAGAIN` if nothing could be written. Read the historical notes in the rationale on the above POSIX link. Long, long ago, pre-POSIX, some systems returned 0 instead of `EAGAIN`, but it's explicitly forbidden to do so. – R.. GitHub STOP HELPING ICE Mar 05 '19 at 16:15
  • @R.. So I see. Then in my code I will just put an error path if somehow 0 is returned (print something to standard error, then continue -- since I can't please the homework checker otherwise) – Paul Stelian Mar 05 '19 at 16:21
  • `assert(ret!=0)` would be reasonable if it accepts that. Or, if there's a possibility that you pass `n=0`, then `assert(n==0 || ret!=0)`. – R.. GitHub STOP HELPING ICE Mar 05 '19 at 16:38
2

The result of the write system call is the total number of bytes written by that system call. In all error cases, it will return -1. Therefore, if the function returns 0 we are in an undefined state (based on the generic docs). I would look for any platform specific reason to be returning 0 and handle it based on the results of that research. If you turn up no platform specific reason, I'd just exit. You're experiencing undefined behavior from a system call, that can't be good.

Man page: http://linux.die.net/man/2/write

Thebigcheeze
  • 3,408
  • 2
  • 22
  • 18
  • 1
    write can indeed return less than the count, but it cannot return 0 unless the count is 0 -- doing so would be a complete waste of time and would risk an infinite loop. Checking for returns of 0 and setting timers and all that would complicate every C program for absolutely no reason. – Jim Balter Apr 14 '11 at 00:13
  • The problem is that the behavior is undefined. The docs say it shouldn't happen but they also say that any error returns -1, thus if it somehow returns 0 then we are in an undefined state. Thus I would say that immediately exiting on undefined behavior from system calls is the best bet. – Thebigcheeze Apr 14 '11 at 15:36
  • 2
    User code that accounts for undefined behavior from system calls is very bad code. You might as well try to account for the system returning the national anthem instead of the contents of your file. – Jim Balter Apr 20 '11 at 15:17
-1

Your question has already been asked and discussed. :) Hope this page helps.

Community
  • 1
  • 1