This is really a StackOverflow answer. But since I've voted for migration there …
What's happening is that you've made a novice mistake:
the
open
call returnsNo such file or directory
error
No, it did not. It returned the value 3, as you can easily see from the program output. That does not signify an error. Neither does the value 0 returned from close()
.
Only check and use the value of the errno
macro if a function has indicated that it has set it. If open()
had returned -1, indicating that it has set the value of errno
, you could have legitimately interpreted the value of the macro as the error from the call. But since it didn't, you cannot.
Put another way: These functions do not reset the value of the errno
macro upon success. They only set the value on failure.
The ENOENT
error was from a previous failing system call somewhere. strace
will probably tell you where. No doubt linking in that extra library causes initialization code to run before main()
.