closedir(3) handling NULL

Bruce Evans brde at
Fri Jan 24 20:28:24 UTC 2014

On Fri, 24 Jan 2014, Garrett Wollman wrote:

> <<On Sat, 25 Jan 2014 06:00:08 +1100 (EST), Bruce Evans <brde at> said:
>> I don't know how the fd can be invalid for a (necessarily valid) stream.
>> Maybe because the fd for a stdio stream is not private, and POSIX actually
>> allows closing it directly.  At least this says "shall fail" instead of
>> "may fail".  I think the "may fail" for closedir() is just buggy wording.
>> The "may" is for the implementation not being required to use fd's at all.
>> But when it uses them, errors from them should be "shall fail" like they
>> are for fclose().
> "may fail" has a very specific meaning in the "ERRORS" section: if the
> implementation detects the condition described, it must use the
> specified error number.

That doesn't quite do it.  Detection of the error for closing a closed fd
is still not required, unlike for fclose().

I could only find the above implied indirectly, and not completely.

% 435              RETURN VALUE
% 436                        This section indicates the possible return values, if any.
% 437                        If the implementation can detect errors, ``successful completion'' means that no error
% 438                        has been detected during execution of the function. If the implementation does detect
% 439                        an error, the error is indicated.

So if an error is detected, that error is "indicated".  I think the indication
must be in the usual way, by storing in errno (except for these unsual APIs
where it is returned).  This is already inconsistent with returning a
specific error.  I think nothing prevents detection of a different error
(one not even listed in the ERRORS section) and nothing prevents returning
that error, while the above requires it.

% 440                        For functions where no errors are defined, ``successful completion'' means that if the
% 441                        implementation checks for errors, no error has been detected. If the implementation can
% 442                        detect errors, and an error is detected, the indicated return value is returned and errno
% 443                        may be set.

The only thing that is clear is that if an error is detected, the function
cannot succeed.


More information about the freebsd-standards mailing list