From a10514072f27ff9f4a6ab308b3bccfd4628ef2aa Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 9 Feb 2023 15:03:45 +0000 Subject: Add sals, several refactors Signed-off-by: Laurent Bercot --- src/libstddjb/sals.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/libstddjb/sals.c (limited to 'src/libstddjb/sals.c') diff --git a/src/libstddjb/sals.c b/src/libstddjb/sals.c new file mode 100644 index 0000000..839df6d --- /dev/null +++ b/src/libstddjb/sals.c @@ -0,0 +1,44 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include + +int sals (char const *fn, stralloc *sa, size_t *x) +{ + int n = 0 ; + size_t sabase = sa->len ; + size_t maxlen = 0 ; + DIR *dir = opendir(fn) ; + if (dir) + { + for (;;) + { + direntry *d ; + size_t len ; + errno = 0 ; + d = readdir(dir) ; + if (!d) break ; + if (d->d_name[0] == '.') + if (((d->d_name[1] == '.') && !d->d_name[2]) || !d->d_name[1]) + continue ; + len = strlen(d->d_name) ; + if (len > maxlen) maxlen = len ; + if (!stralloc_catb(sa, d->d_name, len+1)) goto err ; + n++ ; + } + if (errno) goto err ; + dir_close(dir) ; + } + else if (errno != ENOENT) return -1 ; + if (x) *x = maxlen ; + return n ; + + err: + dir_close(dir) ; + sa->len = sabase ; + return -1 ; +} -- cgit v1.2.3