]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/configs: simplify suffix verification
authorKarel Zak <kzak@redhat.com>
Wed, 15 Oct 2025 08:32:26 +0000 (10:32 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 15 Oct 2025 08:32:26 +0000 (10:32 +0200)
Use ul_endswith() to simplify the code.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/configs.c

index 34eec455a49a146ded3b235486cce8ba52ef5b1c..0b800d2d07107de132e58dd0d608d846f913bdb1 100644 (file)
@@ -14,7 +14,9 @@
 #include <dirent.h>
 #endif
 
+#include "c.h"
 #include "configs.h"
+#include "strutils.h"
 #include "list.h"
 #include "fileutils.h"
 
@@ -103,10 +105,9 @@ static int filter(const struct dirent *d)
            d->d_type != DT_LNK)
                return 0;
 #endif
-       if (*d->d_name == '.')
+       if (is_dotdir_dirent(d))
                return 0;
 
-       /* Accept this */
        return 1;
 }
 
@@ -142,13 +143,12 @@ static int read_dir(struct list_head *file_list,
 
        for (i = 0; i < nfiles; i++) {
                struct dirent *d = namelist[i];
-               size_t namesz = strlen(d->d_name);
 
-               if (config_suffix && strlen(config_suffix) > 0 &&
-                   (!namesz || namesz < strlen(config_suffix) + 1 ||
-                    strcmp(d->d_name + (namesz - strlen(config_suffix)), config_suffix) != 0)) {
-                       /* filename does not have requested suffix */
-                       continue;
+               if (config_suffix) {
+                       const char *p = ul_endswith(d->d_name, config_suffix);
+
+                       if (!p || p == d->d_name || *(p - 1) != '.')
+                               continue;
                }
 
                if (asprintf(&filename, "%s/%s", dirname, d->d_name) < 0) {