aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-06-09 22:25:49 -0500
committerRob Landley <rob@landley.net>2012-06-09 22:25:49 -0500
commit4af1e1d784fa757e1d50b3fa42bd27f0a9b7782c (patch)
treee6e247c6b617c1eb2d739c4a1baa8d49e8e9903a /lib
parent98356226c81109227faa89b721674e5fabf04b42 (diff)
downloadtoybox-4af1e1d784fa757e1d50b3fa42bd27f0a9b7782c.tar.gz
Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Diffstat (limited to 'lib')
-rw-r--r--lib/dirtree.c14
-rw-r--r--lib/lib.h4
2 files changed, 10 insertions, 8 deletions
diff --git a/lib/dirtree.c b/lib/dirtree.c
index 291f99a4..ff274275 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -10,7 +10,7 @@
// (This doesn't open directory filehandles yet so as not to exhaust the
// filehandle space on large trees. handle_callback() does that instead.)
-struct dirtree *dirtree_add_node(int dirfd, char *name)
+struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow)
{
struct dirtree *dt = NULL;
struct stat st;
@@ -18,7 +18,8 @@ struct dirtree *dirtree_add_node(int dirfd, char *name)
int len = 0, linklen = 0;
if (name) {
- if (fstatat(dirfd, name, &st, AT_SYMLINK_NOFOLLOW)) goto error;
+ if (fstatat(dirfd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW))
+ goto error;
if (S_ISLNK(st.st_mode)) {
if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error;
buf[linklen++]=0;
@@ -120,7 +121,7 @@ struct dirtree *handle_callback(struct dirtree *new,
if (dir) {
if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
- dirtree_recurse(new, callback);
+ dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW);
if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
} else close(new->data);
}
@@ -138,7 +139,7 @@ struct dirtree *handle_callback(struct dirtree *new,
// filtering through callback().
void dirtree_recurse(struct dirtree *node,
- int (*callback)(struct dirtree *node))
+ int (*callback)(struct dirtree *node), int symfollow)
{
struct dirtree *new, **ddt = &(node->child);
struct dirent *entry;
@@ -158,7 +159,8 @@ void dirtree_recurse(struct dirtree *node,
// The extra parentheses are to shut the stupid compiler up.
while ((entry = readdir(dir))) {
- if (!(new = dirtree_add_node(node->data, entry->d_name))) continue;
+ if (!(new = dirtree_add_node(node->data, entry->d_name, symfollow)))
+ continue;
new->parent = node;
new = handle_callback(new, callback);
if (new == DIRTREE_ABORTVAL) break;
@@ -179,7 +181,7 @@ void dirtree_recurse(struct dirtree *node,
struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
{
- struct dirtree *root = dirtree_add_node(AT_FDCWD, path);
+ struct dirtree *root = dirtree_add_node(AT_FDCWD, path, 0);
return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL;
}
diff --git a/lib/lib.h b/lib/lib.h
index add0c14e..c3aa15ce 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -71,14 +71,14 @@ struct dirtree {
char name[];
};
-struct dirtree *dirtree_add_node(int dirfd, char *name);
+struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow);
char *dirtree_path(struct dirtree *node, int *plen);
int dirtree_notdotdot(struct dirtree *catch);
int dirtree_opennode(struct dirtree *try);
struct dirtree *handle_callback(struct dirtree *new,
int (*callback)(struct dirtree *node));
void dirtree_recurse(struct dirtree *node,
- int (*callback)(struct dirtree *node));
+ int (*callback)(struct dirtree *node), int symfollow);
struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
// lib.c