The below diff is an attempt to improve the errno documentation for the directory(3) functions. It probably needs some refinement.
Should the manual page also mention that these functions are POSIX-compliant? It seems they are. The opendir.c part of the diff sets a more accurate errno value. Regards, Tim Index: directory.3 =================================================================== RCS file: /cvs/src/lib/libc/gen/directory.3,v retrieving revision 1.19 diff -p -u directory.3 --- directory.3 23 Nov 2009 23:08:35 -0000 1.19 +++ directory.3 18 Jan 2010 19:04:38 -0000 @@ -66,11 +66,11 @@ function opens the directory named by .Fa filename , associates a directory stream with it, and returns a pointer to be used to identify the directory stream in subsequent operations. -A null pointer is returned if -.Fa filename -cannot be accessed, or if -.Xr malloc 3 -cannot allocate enough memory to hold the entire structure. +On failure, +.Dv NULL +is returned and +.Va errno +is set to indicate the error. .Pp The .Fn readdir @@ -114,6 +114,9 @@ The function returns the current location associated with the named directory stream .Fa dirp . +On failure, \-1 is returned and +.Va errno +is set to indicate the error. .Pp The .Fn seekdir @@ -177,6 +180,58 @@ if (dirp) { } return (NOT_FOUND); .Ed +.Sh ERRORS +The +.Fn opendir +function will fail if: +.Bl -tag -width Er +.It Bq Er ENOTDIR +The supplied +.Fa filename +is not a directory. +.El +.Pp +The +.Fn opendir +function may also fail and set +.Va errno +for any of the errors specified for the routines +.Xr fcntl 2 , +.Xr fstat 2 , +.Xr open 2 , +and +.Xr malloc 3 . +.Pp +The +.Fn readdir +and +.Fn readdir_r +functions may also fail and set +.Va errno +for any of the errors specified for the routine +.Xr getdirentries 2 . +.Pp +The +.Fn telldir +function may also fail and set +.Va errno +for any of the errors specified for the routine +.Xr realloc 3 . +.Pp +The +.Fn rewinddir +function may also fail and set +.Va errno +for any of the errors specified for the +.Fn telldir +function. +.Pp +The +.Fn closedir +function may also fail and set +.Va errno +for any of the errors specified for the routine +.Xr close 2 . .Sh SEE ALSO .Xr close 2 , .Xr getdirentries 2 , Index: opendir.c =================================================================== RCS file: /cvs/src/lib/libc/gen/opendir.c,v retrieving revision 1.19 diff -p -u opendir.c --- opendir.c 5 Jun 2007 18:11:48 -0000 1.19 +++ opendir.c 18 Jan 2010 19:04:38 -0000 @@ -62,8 +62,12 @@ __opendir2(const char *name, int flags) if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) return (NULL); - if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { + if (fstat(fd, &sb)) { close(fd); + return (NULL); + } + if (!S_ISDIR(sb.st_mode)) { + close(fd); errno = ENOTDIR; return (NULL); } @@ -89,7 +93,7 @@ __opendir2(const char *name, int flags) dirp->dd_buf = malloc((size_t)dirp->dd_len); if (dirp->dd_buf == NULL) { free(dirp); - close (fd); + close(fd); return (NULL); }