From 6f792631bc15b4724efe100cf0c7310cdd6a3aec Mon Sep 17 00:00:00 2001 From: default Date: Mon, 3 Oct 2022 11:08:11 +0200 Subject: [PATCH] Use xs_glob() in _timeline_list(). --- data.c | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/data.c b/data.c index 0753725..6b87a90 100644 --- a/data.c +++ b/data.c @@ -152,12 +152,19 @@ d_char *xs_glob_n(const char *spec, int basename, int reverse, int max) if (glob(spec, 0, NULL, &globbuf) == 0) { int n; - char *p; - if (reverse) { - } - else { - for (n = 0; n < max && (p = globbuf.gl_pathv[n]) != NULL; 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; @@ -379,36 +386,17 @@ d_char *timeline_get(snac *snac, char *fn) d_char *_timeline_list(snac *snac, char *directory, int max) /* returns a list of the timeline filenames */ { - d_char *list; xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory); - glob_t globbuf; int c_max; /* maximum number of items in the timeline */ c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries")); + /* never more timeline entries than the configured maximum */ if (max > c_max) max = c_max; - list = xs_list_new(); - - /* get the list in reverse order */ - 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 *fn = globbuf.gl_pathv[globbuf.gl_pathc - n - 1]; - - list = xs_list_append(list, fn); - } - } - - globfree(&globbuf); - - return list; + return xs_glob_n(spec, 0, 1, max); }