sbase/util/recurse.c

34 lines
636 B
C
Raw Normal View History

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>
#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;
DIR *dp;
if(!(dp = opendir(path))) {
if(errno == ENOTDIR)
return;
else
eprintf("opendir %s:", path);
}
2011-05-26 03:17:06 +00:00
cwd = agetcwd();
2011-05-24 23:24:33 +00:00
if(chdir(path) != 0)
eprintf("chdir %s:", path);
while((d = readdir(dp)))
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
fn(d->d_name);
closedir(dp);
2011-05-26 03:17:06 +00:00
if(chdir(cwd) != 0)
eprintf("chdir %s:", cwd);
free(cwd);
2011-05-24 23:24:33 +00:00
}