git: 387ae6390534 - main - libc: Clean up *dir() code

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 02 Feb 2026 15:51:52 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=387ae6390534b6e9b48931840e7bc76eeb0b258d

commit 387ae6390534b6e9b48931840e7bc76eeb0b258d
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-02 15:46:53 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-02-02 15:46:53 +0000

    libc: Clean up *dir() code
    
    Fix style nits (mostly whitespace issues) and clean up the manual page.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D55024
---
 lib/libc/gen/closedir.c  |  1 -
 lib/libc/gen/directory.3 | 78 +++++++++++++++++++++++++-----------------------
 lib/libc/gen/dirfd.c     |  3 --
 lib/libc/gen/fdopendir.c |  1 +
 lib/libc/gen/opendir.c   |  1 -
 lib/libc/gen/opendir2.c  |  1 -
 lib/libc/gen/rewinddir.c |  1 -
 lib/libc/gen/scandir.c   |  2 --
 lib/libc/gen/telldir.c   |  4 +--
 9 files changed, 43 insertions(+), 49 deletions(-)

diff --git a/lib/libc/gen/closedir.c b/lib/libc/gen/closedir.c
index 6015114d6c47..73070d171cd1 100644
--- a/lib/libc/gen/closedir.c
+++ b/lib/libc/gen/closedir.c
@@ -68,6 +68,5 @@ fdclosedir(DIR *dirp)
 int
 closedir(DIR *dirp)
 {
-
 	return (_close(fdclosedir(dirp)));
 }
diff --git a/lib/libc/gen/directory.3 b/lib/libc/gen/directory.3
index 263dfdd6eb95..ccbc60b03776 100644
--- a/lib/libc/gen/directory.3
+++ b/lib/libc/gen/directory.3
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd August 1, 2020
+.Dd January 31, 2026
 .Dt DIRECTORY 3
 .Os
 .Sh NAME
@@ -86,13 +86,6 @@ and
 returns a pointer to be used to identify the
 .Em directory stream
 in subsequent operations.
-The pointer
-.Dv NULL
-is returned if
-.Fa filename
-cannot be accessed, or if it cannot
-.Xr malloc 3
-enough memory to hold the whole thing.
 .Pp
 The
 .Fn fdopendir
@@ -134,14 +127,6 @@ or
 .Fn closedir
 on the same
 .Em directory stream .
-The function returns
-.Dv NULL
-upon reaching the end of the directory or on error.
-In the event of an error,
-.Va errno
-may be set to any of the values documented for the
-.Xr getdirentries 2
-system call.
 .Pp
 The
 .Fn readdir_r
@@ -166,10 +151,6 @@ upon reaching the end of the directory
 .Fa result
 is set to
 .Dv NULL .
-The
-.Fn readdir_r
-function
-returns 0 on success or an error number to indicate failure.
 .Pp
 The
 .Fn telldir
@@ -179,12 +160,9 @@ returns a token representing the current location associated with the named
 Values returned by
 .Fn telldir
 are good only for the lifetime of the
-.Dv DIR
-pointer,
-.Fa dirp ,
+.Em directory stream
 from which they are derived.
-If the directory is closed and then
-reopened, prior values returned by
+If the directory is closed and then reopened, prior values returned by
 .Fn telldir
 will no longer be valid.
 Values returned by
@@ -217,28 +195,22 @@ The
 function
 closes the named
 .Em directory stream
-and frees the structure associated with the
-.Fa dirp
-pointer,
-returning 0 on success.
-On failure, \-1 is returned and the global variable
-.Va errno
-is set to indicate the error.
+and frees the structure associated with
+.Fa dirp .
 .Pp
 The
 .Fn fdclosedir
 function is equivalent to the
 .Fn closedir
-function except that this function returns directory file descriptor instead of
-closing it.
+function except that it returns the file descriptor associated with
+.Fa dirp
+instead of closing it.
 .Pp
 The
 .Fn dirfd
 function
-returns the integer file descriptor associated with the named
-.Em directory stream ,
-see
-.Xr open 2 .
+returns the file descriptor associated with
+.Fa dirp .
 .Sh EXAMPLES
 Sample code which searches a directory for entry ``name'' is:
 .Bd -literal -offset indent
@@ -255,6 +227,36 @@ while ((dp = readdir(dirp)) != NULL) {
 (void)closedir(dirp);
 return (NOT_FOUND);
 .Ed
+.Sh RETURN VALUES
+The
+.Fn opendir
+and
+.Fn fdopendir
+functions return a pointer to the new
+.Em directory stream
+on success and
+.Dv NULL
+on failure.
+.Pp
+The
+.Fn readdir
+function returns a pointer to a directory entry on success and
+.Dv NULL
+on failure.
+The
+.Fn readdir_r
+function returns 0 on success and an error number on failure.
+.Pp
+The
+.Fn telldir
+function returns a nonnegative value on success and -1 on failure.
+.Pp
+The
+.Fn closedir
+function returns 0 on success and -1 on failure.
+The
+.Fn fdclosedir
+function returns an open file descriptor on success and -1 on failure.
 .Sh ERRORS
 The
 .Fn opendir
diff --git a/lib/libc/gen/dirfd.c b/lib/libc/gen/dirfd.c
index 85090bd4da6c..ba8f52845b4e 100644
--- a/lib/libc/gen/dirfd.c
+++ b/lib/libc/gen/dirfd.c
@@ -27,8 +27,6 @@
  */
 
 #include "namespace.h"
-#include <sys/param.h>
-
 #include <dirent.h>
 #include "un-namespace.h"
 
@@ -37,6 +35,5 @@
 int
 dirfd(DIR *dirp)
 {
-
 	return (_dirfd(dirp));
 }
diff --git a/lib/libc/gen/fdopendir.c b/lib/libc/gen/fdopendir.c
index 9393cbe28f85..05e1a09fd00c 100644
--- a/lib/libc/gen/fdopendir.c
+++ b/lib/libc/gen/fdopendir.c
@@ -37,6 +37,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include "un-namespace.h"
 
 #include "gen-private.h"
diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c
index 08d9eb10eaa2..4c08b7156055 100644
--- a/lib/libc/gen/opendir.c
+++ b/lib/libc/gen/opendir.c
@@ -42,6 +42,5 @@
 DIR *
 opendir(const char *name)
 {
-
 	return (__opendir2(name, DTF_HIDEW | DTF_NODUP));
 }
diff --git a/lib/libc/gen/opendir2.c b/lib/libc/gen/opendir2.c
index c5c2e662efd8..321cb2261b8c 100644
--- a/lib/libc/gen/opendir2.c
+++ b/lib/libc/gen/opendir2.c
@@ -68,7 +68,6 @@ __opendir2(const char *name, int flags)
 static int
 opendir_compar(const void *p1, const void *p2)
 {
-
 	return (strcmp((*(const struct dirent * const *)p1)->d_name,
 	    (*(const struct dirent * const *)p2)->d_name));
 }
diff --git a/lib/libc/gen/rewinddir.c b/lib/libc/gen/rewinddir.c
index df829e98d138..5cead2adc6d4 100644
--- a/lib/libc/gen/rewinddir.c
+++ b/lib/libc/gen/rewinddir.c
@@ -43,7 +43,6 @@
 void
 rewinddir(DIR *dirp)
 {
-
 	if (__isthreaded)
 		_pthread_mutex_lock(&dirp->dd_lock);
 	dirp->dd_flags &= ~__DTF_SKIPREAD; /* current contents are invalid */
diff --git a/lib/libc/gen/scandir.c b/lib/libc/gen/scandir.c
index fb589f4b36b6..84b20fbafa03 100644
--- a/lib/libc/gen/scandir.c
+++ b/lib/libc/gen/scandir.c
@@ -232,14 +232,12 @@ scandirat(int dirfd, const char *dirname, struct dirent ***namelist,
 int
 alphasort(const struct dirent **d1, const struct dirent **d2)
 {
-
 	return (strcoll((*d1)->d_name, (*d2)->d_name));
 }
 
 int
 versionsort(const struct dirent **d1, const struct dirent **d2)
 {
-
 	return (strverscmp((*d1)->d_name, (*d2)->d_name));
 }
 
diff --git a/lib/libc/gen/telldir.c b/lib/libc/gen/telldir.c
index 1731cc4d7a2c..d71b7bed158c 100644
--- a/lib/libc/gen/telldir.c
+++ b/lib/libc/gen/telldir.c
@@ -53,7 +53,7 @@ telldir(DIR *dirp)
 
 	if (__isthreaded)
 		_pthread_mutex_lock(&dirp->dd_lock);
-	/* 
+	/*
 	 * Outline:
 	 * 1) If the directory position fits in a packed structure, return that.
 	 * 2) Otherwise, see if it's already been recorded in the linked list
@@ -95,7 +95,7 @@ telldir(DIR *dirp)
 			LIST_INSERT_HEAD(&dirp->dd_td->td_locq, lp, loc_lqe);
 	}
 	ddloc.i.is_packed = 0;
-	/* 
+	/*
 	 * Technically this assignment could overflow on 32-bit architectures,
 	 * but we would get ENOMEM long before that happens.
 	 */