From e8c421c51dea128a890d62bd091473aa61f3a357 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 3 Oct 2022 11:18:49 +0200 Subject: Backport from xs. --- xs_glob.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 xs_glob.h (limited to 'xs_glob.h') diff --git a/xs_glob.h b/xs_glob.h new file mode 100644 index 0000000..c5293dc --- /dev/null +++ b/xs_glob.h @@ -0,0 +1,56 @@ +/* copyright (c) 2022 grunfink - MIT license */ + +#ifndef _XS_GLOB_H + +#define _XS_GLOB_H + +d_char *xs_glob_n(const char *spec, int basename, int reverse, int max); +#define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, 0xfffffff) + + +#ifdef XS_IMPLEMENTATION + +#include + +d_char *xs_glob_n(const char *spec, int basename, int reverse, int max) +/* does a globbing and returns the found files */ +{ + glob_t globbuf; + d_char *list = xs_list_new(); + + if (glob(spec, 0, NULL, &globbuf) == 0) { + int n; + + if (max > globbuf.gl_pathc) + max = globbuf.gl_pathc; + + for (n = 0; n < max; n++) { + char *p; + + if (reverse) + p = globbuf.gl_pathv[globbuf.gl_pathc - n - 1]; + else + p = globbuf.gl_pathv[n]; + + if (p != NULL) { + if (basename) { + if ((p = strrchr(p, '/')) == NULL) + continue; + + p++; + } + + list = xs_list_append(list, p); + } + } + } + + globfree(&globbuf); + + return list; +} + + +#endif /* XS_IMPLEMENTATION */ + +#endif /* _XS_GLOB_H */ -- cgit v1.2.3