The conventional way to use write(),
is to verify that the return value matches the number of bytes.
But what could possibly go wrong,
if the return value is ignored and errno is used..
errno = 0;
while() write()
if errno
The conventional way to use write(),
is to verify that the return value matches the number of bytes.
But what could possibly go wrong,
if the return value is ignored and errno is used..
errno = 0;
while() write()
if errno
errno is set only if write() returns -1.
man 2 write
Handling write() correctly is far more complex than that. It has partial writes and retryable errors.
I consider assigning errno bad form. But that might be my own hang-up.
Is there a possibility that write() is successful and errno may still be set,I consider assigning errno bad form.
errno is a global..
Last edited by kodax; 11-11-2022 at 10:33 PM.
It is nuanced, and the 'code' you have provided in the original post is lacking. I can't compile it, I can't test it, I have to make assumptions and read your mind - as you so quickly point out, we here are not mind readers.
Success and failure are ill-defined terms. If half of the data is written, and errno not set, is that success or failure?
In general the write() function should act as the documentation says it will. It will only set errno if absolutely no data can be written.
If you have an actual bit of code, then post it and we can comment.
If you use errno without write() indicating an error then it is poor code.
If you do not handle E_AGAIN and E_INTR properly then it is poor code.
If you do not handle write() writing some, but not all, of the requested data then it is poor code.
Absolutely, it is poor code, it goes against the documentation,If you use errno without write() indicating an error then it is poor code.
write() returns a value for a reason.
The idea was to use write() without checking it's return value,
and relying on errno instead, and true it is irregular.
Great, so we're all agreed then.
Using write() without checking the return result is a dumb idea.
Case closed then.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
There's also a possibility that write() fails,
and errno is 0.
Circular argument troll.
Take a timeout.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.