[Toybox] [PATCH 1/5] lib: Add void *cb_param param to dirtree_read().

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


This parameter is passed to callback() function.
---
 lib/dirtree.c |    8 ++++----
 lib/lib.h     |    4 ++--
 toys/cp.c     |    4 ++--
 toys/mdev.c   |    6 +++---
 toys/mke2fs.c |    2 +-
 5 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/lib/dirtree.c b/lib/dirtree.c
index 1993d00..efdb2e6 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -49,8 +49,8 @@ struct dirtree *dirtree_add_node(char *path)
 // return root of tree.  Otherwise call callback(node) on each hit, free
 // structures after use, and return NULL.
 
-struct dirtree *dirtree_read(char *path, struct dirtree *parent,
-					int (*callback)(char *path, struct dirtree *node))
+struct dirtree *dirtree_read(char *path, struct dirtree *parent, void *cb_param,
+					int (*callback)(char *path, struct dirtree *node, void *param))
 {
 	struct dirtree *dtroot = NULL, *this, **ddt = &dtroot;
 	DIR *dir;
@@ -76,9 +76,9 @@ struct dirtree *dirtree_read(char *path, struct dirtree *parent,
 		if (!this) continue;
 		this->parent = parent;
 		this->depth = parent ? parent->depth + 1 : 1;
-		if (callback) norecurse = callback(path, this);
+		if (callback) norecurse = callback(path, this, cb_param);
 		if (!norecurse && S_ISDIR(this->st.st_mode))
-			this->child = dirtree_read(path, this, callback);
+			this->child = dirtree_read(path, this, cb_param, callback);
 		if (callback) free(this);
 		else ddt = &(this->next);
 		path[len]=0;
diff --git a/lib/lib.h b/lib/lib.h
index ab10eb7..f0d15fc 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -49,8 +49,8 @@ struct dirtree {
 };
 
 struct dirtree *dirtree_add_node(char *path);
-struct dirtree *dirtree_read(char *path, struct dirtree *parent,
-                    int (*callback)(char *path, struct dirtree *node));
+struct dirtree *dirtree_read(char *path, struct dirtree *parent, void *cb_param,
+                    int (*callback)(char *path, struct dirtree *node, void *param));
 
 // lib.c
 void xstrcpy(char *dest, char *src, size_t size);
diff --git a/toys/cp.c b/toys/cp.c
index c3153e0..5e22b03 100644
--- a/toys/cp.c
+++ b/toys/cp.c
@@ -128,7 +128,7 @@ void cp_file(char *src, char *dst, struct stat *srcst)
 
 // Callback from dirtree_read() for each file/directory under a source dir.
 
-int cp_node(char *path, struct dirtree *node)
+int cp_node(char *path, struct dirtree *node, void *cb_param)
 {
 	char *s = path+strlen(path);
 	struct dirtree *n;
@@ -209,7 +209,7 @@ void cp_main(void)
 				TT.keep_symlinks++;
 				strncpy(toybuf, src, sizeof(toybuf)-1);
 				toybuf[sizeof(toybuf)-1]=0;
-				dirtree_read(toybuf, NULL, cp_node);
+				dirtree_read(toybuf, NULL, NULL, cp_node);
 			} else error_msg("Skipped dir '%s'", src);
 		} else cp_file(src, dst, &st);
 		if (TT.destisdir) free(dst);
diff --git a/toys/mdev.c b/toys/mdev.c
index b644408..2b8c489 100644
--- a/toys/mdev.c
+++ b/toys/mdev.c
@@ -173,7 +173,7 @@ found_device:
 	if (CFG_MDEV_CONF) mode=chown(temp, uid, gid);
 }
 
-static int callback(char *path, struct dirtree *node)
+static int callback(char *path, struct dirtree *node, void *cb_param)
 {
 	// Entries in /sys/class/block aren't char devices, so skip 'em.  (We'll
 	// get block devices out of /sys/block.)
@@ -199,9 +199,9 @@ void mdev_main(void)
 	if (toys.optflags) {
 		xchdir("/sys/class");
 		strcpy(toybuf, "/sys/class");
-		dirtree_read(toybuf, NULL, callback);
+		dirtree_read(toybuf, NULL, NULL, callback);
 		strcpy(toybuf+5, "block");
-		dirtree_read(toybuf, NULL, callback);
+		dirtree_read(toybuf, NULL, NULL, callback);
 	}
 //	if (toys.optflags) {
 //		strcpy(toybuf, "/sys/block");
diff --git a/toys/mke2fs.c b/toys/mke2fs.c
index cf31342..c3b05da 100644
--- a/toys/mke2fs.c
+++ b/toys/mke2fs.c
@@ -469,7 +469,7 @@ void mke2fs_main(void)
 
 	if (TT.gendir) {
 		strncpy(toybuf, TT.gendir, sizeof(toybuf));
-		dti = dirtree_read(toybuf, NULL, NULL);
+		dti = dirtree_read(toybuf, NULL, NULL, NULL);
 	} else {
 		dti = xzalloc(sizeof(struct dirtree)+11);
 		strcpy(dti->name, "lost+found");
-- 
1.7.5.1




More information about the Toybox mailing list