aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-10-04 02:04:10 -0500
committerRob Landley <rob@landley.net>2007-10-04 02:04:10 -0500
commit103b7e031c4de0f9753e02d2217aa81819242278 (patch)
tree884c3216b7912b8c79a8ff1bd76c27a54ee37b23 /lib
parentdf27a6052e79a854db0e1337d28768f7c013d4f9 (diff)
downloadtoybox-103b7e031c4de0f9753e02d2217aa81819242278.tar.gz
Break out dirtree.c and let it call a function instead of returning the data.
Diffstat (limited to 'lib')
-rw-r--r--lib/dirtree.c79
-rw-r--r--lib/lib.c62
-rw-r--r--lib/lib.h13
3 files changed, 87 insertions, 67 deletions
diff --git a/lib/dirtree.c b/lib/dirtree.c
new file mode 100644
index 00000000..b64dfaf5
--- /dev/null
+++ b/lib/dirtree.c
@@ -0,0 +1,79 @@
+/* vi: set sw=4 ts=4 :*/
+/* dirtree.c - Functions for dealing with directory trees.
+ *
+ * Copyright 2007 Rob Landley <rob@landley.net>
+ */
+
+#include "toys.h"
+
+// Create a dirtree node from a path.
+
+struct dirtree *dirtree_add_node(char *path)
+{
+ struct dirtree *dt;
+ char *name;
+
+ // Find last chunk of name.
+
+ for (;;) {
+ name = strrchr(path, '/');
+
+ if (!name) name = path;
+ else {
+ if (*(name+1)) name++;
+ else {
+ *name=0;
+ continue;
+ }
+ }
+ break;
+ }
+
+ dt = xzalloc(sizeof(struct dirtree)+strlen(name)+1);
+ xstat(path, &(dt->st));
+ strcpy(dt->name, name);
+
+ return dt;
+}
+
+// Given a directory (in a writeable PATH_MAX buffer), recursively read in a
+// directory tree.
+//
+// If callback==NULL, allocate tree of struct dirtree and
+// 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)(struct dirtree *node))
+{
+ struct dirtree *dt = NULL, **ddt = &dt;
+ DIR *dir;
+ int len = strlen(path);
+
+ if (!(dir = opendir(path))) perror_msg("No %s", path);
+
+ for (;;) {
+ struct dirent *entry = readdir(dir);
+ if (!entry) break;
+
+ // Skip "." and ".."
+ if (entry->d_name[0]=='.') {
+ if (!entry->d_name[1]) continue;
+ if (entry->d_name[1]=='.' && !entry->d_name[2]) continue;
+ }
+
+ snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name);
+ *ddt = dirtree_add_node(path);
+ (*ddt)->parent = parent;
+ if (callback) callback(*ddt);
+ if (entry->d_type == DT_DIR)
+ (*ddt)->child = dirtree_read(path, *ddt, callback);
+ if (callback) free(*ddt);
+ else ddt = &((*ddt)->next);
+ path[len]=0;
+ }
+
+ return dt;
+}
+
+
diff --git a/lib/lib.c b/lib/lib.c
index 64c85698..7cb29f8f 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -549,65 +549,3 @@ void xpidfile(char *name)
xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
close(fd);
}
-
-// Create a dirtree node from a path.
-
-struct dirtree *read_dirtree_node(char *path)
-{
- struct dirtree *dt;
- char *name;
-
- // Find last chunk of name.
-
- for (;;) {
- name = strrchr(path, '/');
-
- if (!name) name = path;
- else {
- if (*(name+1)) name++;
- else {
- *name=0;
- continue;
- }
- }
- break;
- }
-
- dt = xzalloc(sizeof(struct dirtree)+strlen(name)+1);
- xstat(path, &(dt->st));
- strcpy(dt->name, name);
-
- return dt;
-}
-
-// Given a directory (in a writeable PATH_MAX buffer), recursively read in a
-// directory tree.
-
-struct dirtree *read_dirtree(char *path, struct dirtree *parent)
-{
- struct dirtree *dt = NULL, **ddt = &dt;
- DIR *dir;
- int len = strlen(path);
-
- if (!(dir = opendir(path))) perror_msg("No %s", path);
-
- for (;;) {
- struct dirent *entry = readdir(dir);
- if (!entry) break;
-
- // Skip "." and ".."
- if (entry->d_name[0]=='.') {
- if (!entry->d_name[1]) continue;
- if (entry->d_name[1]=='.' && !entry->d_name[2]) continue;
- }
-
- snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name);
- *ddt = read_dirtree_node(path);
- (*ddt)->parent = parent;
- if (entry->d_type == DT_DIR) (*ddt)->child = read_dirtree(path, *ddt);
- ddt = &((*ddt)->next);
- path[len]=0;
- }
-
- return dt;
-}
diff --git a/lib/lib.h b/lib/lib.h
index 37a94a53..4cc2dac4 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -21,16 +21,21 @@ struct arg_list {
char *arg;
};
+// args.c
+void get_optflags(void);
+
+// dirtree.c
struct dirtree {
struct dirtree *next, *child, *parent;
struct stat st;
char name[];
};
-// args.c
-void get_optflags(void);
+struct dirtree *dirtree_add_node(char *path);
+struct dirtree *dirtree_read(char *path, struct dirtree *parent,
+ int (*callback)(struct dirtree *node));
-// functions.c
+// lib.c
#if !defined(__UCLIBC__) && !defined(__KLIBC__)
void strlcpy(char *dest, char *src, size_t size);
#endif
@@ -72,8 +77,6 @@ char *itoa(int n);
long atolx(char *c);
off_t fdlength(int fd);
char *xreadlink(char *name);
-struct dirtree *read_dirtree_node(char *path);
-struct dirtree *read_dirtree(char *path, struct dirtree *parent);
// getmountlist.c
struct mtab_list {