recurse: add symlink derefencing flags -H and -L

This commit is contained in:
Tai Chi Minh Ralph Eastwood
2015-02-09 19:53:24 +00:00
committed by sin
parent 5a13865385
commit 82bc92da51
7 changed files with 37 additions and 19 deletions

View File

@@ -11,14 +11,16 @@
#include "../util.h"
void
recurse(const char *path, void (*fn)(const char *))
recurse(const char *path, void (*fn)(const char *, char), char follow)
{
char buf[PATH_MAX];
struct dirent *d;
struct stat st;
struct stat lst, st;
DIR *dp;
if (lstat(path, &st) < 0 || !S_ISDIR(st.st_mode))
if (lstat(path, &lst) < 0 || stat(path, &st) < 0 ||
!(S_ISDIR(lst.st_mode) ||
(follow != 'P' && S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode))))
return;
if (!(dp = opendir(path)))
@@ -35,7 +37,7 @@ recurse(const char *path, void (*fn)(const char *))
eprintf("path too long\n");
if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
eprintf("path too long\n");
fn(buf);
fn(buf, follow == 'H' ? 'P' : follow);
}
closedir(dp);