| Syren Baran 2008-01-04, 7:38 am |
| William Pursell schrieb:
> If the function is intended to always "return the number of
> bytes read", then it should return 0 if an error
> occurs on the first read. However, that is not the
> intended behavior, nor is it the behavior of the read(2)
> system call. From the read man page:
>
> On success, the number of bytes read is returned (zero indicates end
> of file)...
> On error, -1 is returned
>
> Steven's function returns the number of bytes read if there
> is no error. It returns -1 if there is an error, regardless
> of the number of bytes that were read. That is correct
> behavior.
Unluckily only partially true. The return values may be the same for
both functions, yet there is a subtle difference. If bytes were read and
a further call to read returns an error you would have to insert a
statement such as "fseek(fd,nleft-n,SEEK_CUR);" in line 15.
Provoking such an error should be easy, by setting fd into nonblocking
mode checking what happens when EAGAIN arrives.
|