2024-01-04 11:22:03 +03:00
|
|
|
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
|
2022-10-03 12:18:49 +03:00
|
|
|
|
|
|
|
#ifndef _XS_GLOB_H
|
|
|
|
|
|
|
|
#define _XS_GLOB_H
|
|
|
|
|
2023-01-28 19:49:02 +03:00
|
|
|
xs_list *xs_glob_n(const char *spec, int basename, int reverse, int max);
|
2022-11-24 10:47:02 +03:00
|
|
|
#define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, XS_ALL)
|
2022-10-03 12:18:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef XS_IMPLEMENTATION
|
|
|
|
|
|
|
|
#include <glob.h>
|
|
|
|
|
2023-01-28 19:49:02 +03:00
|
|
|
xs_list *xs_glob_n(const char *spec, int basename, int reverse, int max)
|
2022-10-03 12:18:49 +03:00
|
|
|
/* does a globbing and returns the found files */
|
|
|
|
{
|
|
|
|
glob_t globbuf;
|
2023-01-28 19:49:02 +03:00
|
|
|
xs_list *list = xs_list_new();
|
2022-10-03 12:18:49 +03:00
|
|
|
|
|
|
|
if (glob(spec, 0, NULL, &globbuf) == 0) {
|
|
|
|
int n;
|
|
|
|
|
2023-01-08 12:39:11 +03:00
|
|
|
if (max > (int) globbuf.gl_pathc)
|
2022-10-03 12:18:49 +03:00
|
|
|
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 */
|