Replace deprecated readdir_r() with readdir()
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 30 Dec 2025 08:54:15 +0000 (09:54 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 31 Dec 2025 11:11:25 +0000 (12:11 +0100)
Original warnings:
src/filefunc.cpp:318:21: error: 'int readdir_r(DIR*, dirent*, dirent**)' is deprecated [-Werror=deprecated-declarations]
src/filefunc.cpp:360:21: error: 'int readdir_r(DIR*, dirent*, dirent**)' is deprecated [-Werror=deprecated-declarations]
src/filefunc.cpp:362:27: error: the address of 'dirent::d_name' will never be NULL [-Werror=address]

The readdir_r() function is deprecated in favor of readdir(), which is
thread-safe on Linux with glibc since many years. I checked the glibc 2.17 source code
and it uses a lock internally (and also traced it back to at least 2012).

Also removed the unnecessary NULL check for d_name since it is never NULL per POSIX.

src/filefunc.cpp

index 9764521..fbb8903 100644 (file)
@@ -314,8 +314,8 @@ bool get_dir(
     {
         return false;
     }
-    struct dirent store, *entry = NULL;
-    while (readdir_r(dir, &store, &entry) == 0 && entry != NULL)
+    struct dirent *entry = NULL;
+    while ((entry = readdir(dir)) != NULL)
     {
         std::string name( entry->d_name );
         if (! include_dot_names && (name[0] == '.') )
@@ -356,12 +356,10 @@ int get_dir_count(const std::string& path, bool include_dot_names)
     DIR* dir = ::opendir( path.c_str());
     if (!dir)
         return -1;
-    struct dirent store, *entry = NULL;
-    while (readdir_r(dir, &store, &entry) == 0 && entry != NULL)
+    struct dirent *entry = NULL;
+    while ((entry = readdir(dir)) != NULL)
     {
-        if (entry->d_name == NULL)
-            continue;    // should not happen
-        else if (! include_dot_names && (entry->d_name)[0] == '.')
+        if (! include_dot_names && (entry->d_name)[0] == '.')
             continue;
         ++result;
     }