1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
11 #include <sys/types.h>
13 #include "dirent-util.h"
14 #include "glob-util.h"
16 #include "path-util.h"
19 static void closedir_wrapper(void* v
) {
23 int safe_glob(const char *path
, int flags
, glob_t
*pglob
) {
26 /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */
27 assert(!(flags
& GLOB_ALTDIRFUNC
));
29 if (!pglob
->gl_closedir
)
30 pglob
->gl_closedir
= closedir_wrapper
;
31 if (!pglob
->gl_readdir
)
32 pglob
->gl_readdir
= (struct dirent
*(*)(void *)) readdir_no_dot
;
33 if (!pglob
->gl_opendir
)
34 pglob
->gl_opendir
= (void *(*)(const char *)) opendir
;
36 pglob
->gl_lstat
= lstat
;
38 pglob
->gl_stat
= stat
;
41 k
= glob(path
, flags
| GLOB_ALTDIRFUNC
, NULL
, pglob
);
43 if (k
== GLOB_NOMATCH
)
45 if (k
== GLOB_NOSPACE
)
48 return errno
> 0 ? -errno
: -EIO
;
49 if (strv_isempty(pglob
->gl_pathv
))
55 int glob_exists(const char *path
) {
56 _cleanup_globfree_ glob_t g
= {};
61 k
= safe_glob(path
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
69 int glob_extend(char ***strv
, const char *path
) {
70 _cleanup_globfree_ glob_t g
= {};
73 k
= safe_glob(path
, GLOB_NOSORT
|GLOB_BRACE
, &g
);
77 return strv_extend_strv(strv
, g
.gl_pathv
, false);