2011-05-24 23:24:33 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-06-04 13:30:54 +00:00
|
|
|
#include <sys/stat.h>
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2011-05-24 23:24:33 +00:00
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
recurse(const char *path, void (*fn)(const char *))
|
|
|
|
{
|
2011-05-26 03:17:06 +00:00
|
|
|
char *cwd;
|
2011-05-24 23:24:33 +00:00
|
|
|
struct dirent *d;
|
2011-06-04 13:30:54 +00:00
|
|
|
struct stat st;
|
2011-05-24 23:24:33 +00:00
|
|
|
DIR *dp;
|
|
|
|
|
2013-03-05 20:46:48 +00:00
|
|
|
if(lstat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
2011-06-04 13:30:54 +00:00
|
|
|
return;
|
2013-03-05 20:46:48 +00:00
|
|
|
} else if(!(dp = opendir(path))) {
|
2011-06-04 13:30:54 +00:00
|
|
|
eprintf("opendir %s:", path);
|
2013-03-05 20:46:48 +00:00
|
|
|
}
|
2011-06-04 13:30:54 +00:00
|
|
|
|
2011-05-26 03:17:06 +00:00
|
|
|
cwd = agetcwd();
|
2011-06-04 11:20:41 +00:00
|
|
|
if(chdir(path) == -1)
|
2011-05-24 23:24:33 +00:00
|
|
|
eprintf("chdir %s:", path);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
|
|
|
while((d = readdir(dp))) {
|
2011-05-24 23:24:33 +00:00
|
|
|
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
|
|
|
|
fn(d->d_name);
|
2013-03-05 20:46:48 +00:00
|
|
|
}
|
2011-05-24 23:24:33 +00:00
|
|
|
|
|
|
|
closedir(dp);
|
2011-06-04 11:20:41 +00:00
|
|
|
if(chdir(cwd) == -1)
|
2011-05-26 03:17:06 +00:00
|
|
|
eprintf("chdir %s:", cwd);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2011-05-26 03:17:06 +00:00
|
|
|
free(cwd);
|
2011-05-24 23:24:33 +00:00
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|