1 // Copyright (2013) Jann Horn <jann@thejh.net>
2 // This code is licensed under the AGPLv3.
4 HEADER #include <stdio.h>
5 HEADER #include <sys/types.h>
6 HEADER #include <dirent.h>
10 // nonzero iterator exit code means cancel
11 HEADER typedef int dir_iterator(struct dirent *dent, void *data);
12 PUBLIC_FN int fdir_foreach(DIR *d, dir_iterator *iter, void *data) {
16 int rd_res = readdir_r(d, &de, &res);
17 if (rd_res) return rd_res;
18 if (res == NULL) return 0;
19 int iter_res = iter(&de, data);
20 if (iter_res) return iter_res;
24 PUBLIC_FN int dir_foreach(char *path, dir_iterator *iter, void *data) {
25 DIR *d = opendir(path);
26 if (d == NULL) return 1;
27 int feach_res = fdir_foreach(d, iter, data);