For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2007 > write() return value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author write() return value
gio

2007-04-20, 8:06 am

read() return:
x > 0 on succes
0 if read EOF (on file read) or if the peer close connection (on
socket read)
-1 if an error occur.

write() return:
x>0 on succes
-1 if an error occur
Is possible that write() on file or socket return 0?
thanks

Rainer Temme

2007-04-20, 8:06 am

gio wrote:
> write() return:
> x>0 on succes
> -1 if an error occur
> Is possible that write() on file or socket return 0?


From a Linux man-page ...
....
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.
....
So it seems that a return value of zero
can happen.

you might try
....
rc=write(fd,buffer,0);
printf("write(fd,buffer,0)=%d\n",rc);
....

Regards ... Rainer

gio

2007-04-20, 8:06 am

> So it seems that a return value of zero
> can happen.
>
> you might try
> ...
> rc=write(fd,buffer,0);
> printf("write(fd,buffer,0)=%d\n",rc);
> ...
>
> Regards ... Rainer


In my case I will never do something like:
....
rc=write(fd,buffer,0);
....

I will always do:
...
rc=write(fd,buffer,dim); //dim > 0
....

Is possible that in this case write return 0?

Jens Thoms Toerring

2007-04-20, 8:06 am

gio <giorginooo.rossi@yahoo.it> wrote:
> Rainer Temme <Rainer_Temme@nospam.hotmail_dot_com> wrote:
[color=darkred]
> In my case I will never do something like:
> ...
> rc=write(fd,buffer,0);


> I will always do:
> ..
> rc=write(fd,buffer,dim); //dim > 0


> Is possible that in this case write return 0?


I don't think so - the only occasion I could imagine this to happen
would be for a write to something that was opened in non-blocking
mode and where nothing could be written immediately. But then write()
will return -1 and errno is set to EAGAIN. A return value of 0 now-
adays seems to be reserved for the case that the number of bytes to
be written is also 0, which can be used to check if write() would
result in an error. But take care that this seems to a POSIX re-
quirement while it looks as if historcal implementations did return
0 when one tried to write() to something opened in non-blocking mode
and nothing could be written immediately. See

http://www.opengroup.org/onlinepubs...ions/write.html

for all the details.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Hallvard B Furuseth

2007-04-20, 8:06 am

gio <giorginooo.rossi@yahoo.it> writes:
> Is possible that write() on file or socket return 0?


Yes. If the descriptor's O_NDELAY flag is set, a write() which
would block returns 0. Which is why O_NONBLOCK is easier to deal
with: With that flag it returns -1.

Also write(,,size 0) returns 0, as someone mentioned.

--
Hallvard
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com