]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: add ul_path_next_dirent()
authorKarel Zak <kzak@redhat.com>
Wed, 8 Sep 2021 11:44:20 +0000 (13:44 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:54 +0000 (11:01 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/path.h
lib/path.c

index 3ce70c40b2ad18213c5fbce4e5fe36ad286d702c..22cd54481503e26e87e69f5d4cefa159f85cfdef 100644 (file)
@@ -128,6 +128,8 @@ int ul_path_count_dirents(struct path_cxt *pc, const char *path);
 int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
                                __attribute__ ((__format__ (__printf__, 2, 3)));
 
+int ul_path_next_dirent(struct path_cxt *pc, DIR **sub, const char *dirname, struct dirent **d);
+
 FILE *ul_prefix_fopen(const char *prefix, const char *path, const char *mode);
 
 
index cf3a33e046f88f7c84ed4eec347c0ad34310c71b..0d357966f3024d18b7676eebdb02b94c4bdbc8ef 100644 (file)
@@ -968,6 +968,27 @@ int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
        return !p ? -errno : ul_path_count_dirents(pc, p);
 }
 
+/* first call (when @sub is NULL) opens the directory, last call closes the diretory */
+int ul_path_next_dirent(struct path_cxt *pc, DIR **sub, const char *dirname, struct dirent **d)
+{
+       if (!pc || !sub || !d)
+               return -EINVAL;
+
+       if (!*sub) {
+               *sub = ul_path_opendir(pc, dirname);
+               if (!*sub)
+                       return -errno;
+       }
+
+       *d = xreaddir(*sub);
+       if (*d)
+               return 0;
+
+       closedir(*sub);
+       *sub = NULL;
+       return 1;
+}
+
 /*
  * Like fopen() but, @path is always prefixed by @prefix. This function is
  * useful in case when ul_path_* API is overkill.