/* This file is part of limb https://lila.oss/limb
* Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <limb/direntry.h>
int
sa_scandirat(stralloc *sa, int bfd, const char *name, sa_scande fn, void *ctx)
{
size_t salen = sa->len;
int ret = 0;
DIR *dir = opendirat(bfd, name);
if (!dir) return -1;
direntry *de;
while ((errno = 0, de = readdir(dir))) {
if (ISDOTDOT(de)) continue;
int r = fn(sa, de, dirfd(dir), ctx);
if (r < 0) break;
if (r) ++ret;
}
if (errno) {
ret = -1;
sa->len = salen;
}
dir_close(dir);
return ret;
}