git: b614c9893fa3 - stable/14 - libc: Clean up *dir() code
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 05 Feb 2026 15:27:32 UTC
The branch stable/14 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=b614c9893fa38771075d1f329763a0151b53b86d
commit b614c9893fa38771075d1f329763a0151b53b86d
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-05 14:48:36 +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
(cherry picked from commit 387ae6390534b6e9b48931840e7bc76eeb0b258d)
---
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 16f9bff452e4..18444bb984fb 100644
--- a/lib/libc/gen/closedir.c
+++ b/lib/libc/gen/closedir.c
@@ -70,6 +70,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 0314ab90ca24..3a121dcafb8c 100644
--- a/lib/libc/gen/directory.3
+++ b/lib/libc/gen/directory.3
@@ -27,7 +27,7 @@
.\"
.\" @(#)directory.3 8.1 (Berkeley) 6/4/93
.\"
-.Dd August 1, 2020
+.Dd January 31, 2026
.Dt DIRECTORY 3
.Os
.Sh NAME
@@ -88,13 +88,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
@@ -136,14 +129,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
@@ -168,10 +153,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
@@ -181,12 +162,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
@@ -219,28 +197,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
@@ -257,6 +229,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 af36e0077ef1..c38a5e753e6b 100644
--- a/lib/libc/gen/opendir.c
+++ b/lib/libc/gen/opendir.c
@@ -44,6 +44,5 @@ __SCCSID("@(#)opendir.c 8.8 (Berkeley) 5/1/95");
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 f85cdb9f6c75..7102974e7eca 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 bc072d2826ea..aaa7ffbeeed9 100644
--- a/lib/libc/gen/rewinddir.c
+++ b/lib/libc/gen/rewinddir.c
@@ -45,7 +45,6 @@ __SCCSID("@(#)rewinddir.c 8.1 (Berkeley) 6/8/93");
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 3f13248a3f2b..a223ef258569 100644
--- a/lib/libc/gen/scandir.c
+++ b/lib/libc/gen/scandir.c
@@ -234,14 +234,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 ac0e2e50f9b3..97df95ea8416 100644
--- a/lib/libc/gen/telldir.c
+++ b/lib/libc/gen/telldir.c
@@ -55,7 +55,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
@@ -97,7 +97,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.
*/