summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLuca Boccassi <bluca@debian.org>2022-08-03 14:20:41 +0100
committerLuca Boccassi <bluca@debian.org>2022-08-23 20:04:41 +0100
commitf52faaf923acfe6fe3f0955d1ad66840b13babfc (patch)
tree231492119bbb57956a9967841f99c6595333e649 /src/basic
parentd4f38ff036ce001081175292e87198ee4b19c364 (diff)
downloadsystemd-f52faaf923acfe6fe3f0955d1ad66840b13babfc.tar.gz
glob: add glob_first(), returns first match
Note that which match is returned depends on the system and is not guaranteed to be stable
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/glob-util.c15
-rw-r--r--src/basic/glob-util.h4
2 files changed, 16 insertions, 3 deletions
diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c
index e026b29478..fd60a6eda2 100644
--- a/src/basic/glob-util.c
+++ b/src/basic/glob-util.c
@@ -47,17 +47,28 @@ int safe_glob(const char *path, int flags, glob_t *pglob) {
return 0;
}
-int glob_exists(const char *path) {
+int glob_first(const char *path, char **ret_first) {
_cleanup_globfree_ glob_t g = {};
int k;
assert(path);
k = safe_glob(path, GLOB_NOSORT|GLOB_BRACE, &g);
- if (k == -ENOENT)
+ if (k == -ENOENT) {
+ if (ret_first)
+ *ret_first = NULL;
return false;
+ }
if (k < 0)
return k;
+
+ if (ret_first) {
+ char *first = strdup(g.gl_pathv[0]);
+ if (!first)
+ return log_oom_debug();
+ *ret_first = first;
+ }
+
return true;
}
diff --git a/src/basic/glob-util.h b/src/basic/glob-util.h
index fc86e990dd..7ca26cc27f 100644
--- a/src/basic/glob-util.h
+++ b/src/basic/glob-util.h
@@ -10,7 +10,9 @@
/* Note: this function modifies pglob to set various functions. */
int safe_glob(const char *path, int flags, glob_t *pglob);
-int glob_exists(const char *path);
+/* Note: which match is returned depends on the implementation/system and not guaranteed to be stable */
+int glob_first(const char *path, char **ret_first);
+#define glob_exists(path) glob_first(path, NULL)
int glob_extend(char ***strv, const char *path, int flags);
int glob_non_glob_prefix(const char *path, char **ret);