aboutsummaryrefslogtreecommitdiffstats
path: root/lib/path.c
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2021-09-08 13:44:20 +0200
committerKarel Zak <kzak@redhat.com>2021-10-06 11:01:54 +0200
commitae838009d24b411203ee5e43c0a81d658dda2722 (patch)
treecc048abc3ef25a09c78f851e5aed074ac2a03cb2 /lib/path.c
parentdb9ad223d6f136d95254d73808c39786d80abf2e (diff)
downloadutil-linux-ae838009d24b411203ee5e43c0a81d658dda2722.tar.gz
lib/path: add ul_path_next_dirent()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/path.c')
-rw-r--r--lib/path.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/path.c b/lib/path.c
index cf3a33e046..0d357966f3 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -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.