errno is a global variable for your process (or thread if you are threaded).
Even if YOUR code (the open and write calls) didn’t produce any error (and thus generate a value in errno) you have no idea whether some library call in the open/write routines did in fact encounter an error (or other condition) and thus fill out errno based on THEIR return calls (it may be that your file didn’t exist and that was detected and returned in errno to some library call which then generated calls to create the file vs overwrite. Or perhaps is a status return based on checking if you have permission to create a file in that directory etc).
Basically errno is undefined if the calls you make (open/write) return successfully.
Yes, well after all these years I still learn things! (or maybe re-remember them)
Noting that errno is global, and set to 0 immediately prior to the write() call, it seemed strange to me that regardless of what else may be happening (such as in another thread) and what the “docs” say, the value is consistently changed to 3 even though nothing actually went wrong.
I accept that it’s best that errno not be checked unless a -1 is returned, but if there is no error anywhere why would it be changed away from 0 at all?