[Toybox] [PATCH 2/5] lib: Add dirtree_for_each().

Georgi Chorbadzhiyski gf at unixsol.org
Wed Mar 7 00:15:57 PST 2012


---
 lib/dirtree.c |   32 ++++++++++++++++++++++++++++++++
 lib/lib.h     |    1 +
 2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/lib/dirtree.c b/lib/dirtree.c
index efdb2e6..9260731 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -87,4 +87,36 @@ struct dirtree *dirtree_read(char *path, struct dirtree *parent, void *cb_param,
 	return dtroot;
 }
 
+static int dirtree_node(char *path, struct dirtree *node, void *cb_param)
+{
+	char *s = path + strlen(path);
+	struct dirtree *n = node;
+	int (*callback)(char *path) = cb_param;
+
+	for ( ; ; n = n->parent) {
+		while (s!=path) {
+			if (*(--s) == '/') break;
+		}
+		if (!n) break;
+	}
+	if (s != path) s++;
+
+	callback(s);
 
+	return 0;
+}
+
+void dirtree_for_each(char *path, int (*callback)(char *path))
+{
+	struct stat sb;
+	if (stat(path, &sb) == -1) {
+		perror_msg("%s", path);
+		return;
+	}
+	callback(path);
+	if (S_ISDIR(sb.st_mode)) {
+		strncpy(toybuf, path, sizeof(toybuf) - 1);
+		toybuf[sizeof(toybuf) - 1] = 0;
+		dirtree_read(toybuf, NULL, callback, dirtree_node);
+	}
+}
diff --git a/lib/lib.h b/lib/lib.h
index f0d15fc..be5543f 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -51,6 +51,7 @@ struct dirtree {
 struct dirtree *dirtree_add_node(char *path);
 struct dirtree *dirtree_read(char *path, struct dirtree *parent, void *cb_param,
                     int (*callback)(char *path, struct dirtree *node, void *param));
+void dirtree_for_each(char *path, int (*callback)(char *path));
 
 // lib.c
 void xstrcpy(char *dest, char *src, size_t size);
-- 
1.7.5.1


 1331108157.0


More information about the Toybox mailing list