diff options
author | Mike Frysinger | 2005-04-24 05:07:59 +0000 |
---|---|---|
committer | Mike Frysinger | 2005-04-24 05:07:59 +0000 |
commit | d89e629e52c3b525424be958de238ed1dc5a0361 (patch) | |
tree | 9aec1ca3d0a479bef39bd63c14f4292061d34675 /e2fsprogs/e2p/iod.c | |
parent | 408ae211ceaf33bf758207cd5329903c39e8880c (diff) | |
download | busybox-d89e629e52c3b525424be958de238ed1dc5a0361.zip busybox-d89e629e52c3b525424be958de238ed1dc5a0361.tar.gz |
add new subdir for e2fsprogs
Diffstat (limited to 'e2fsprogs/e2p/iod.c')
-rw-r--r-- | e2fsprogs/e2p/iod.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/e2fsprogs/e2p/iod.c b/e2fsprogs/e2p/iod.c new file mode 100644 index 0000000..808d3a3 --- /dev/null +++ b/e2fsprogs/e2p/iod.c @@ -0,0 +1,72 @@ +/* + * iod.c - Iterate a function on each entry of a directory + * + * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> + * Laboratoire MASI, Institut Blaise Pascal + * Universite Pierre et Marie Curie (Paris VI) + * + * This file can be redistributed under the terms of the GNU Library General + * Public License + */ + +/* + * History: + * 93/10/30 - Creation + */ + +#include "e2p.h" +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <stdlib.h> +#include <string.h> + +int iterate_on_dir (const char * dir_name, + int (*func) (const char *, struct dirent *, void *), + void * private) +{ + DIR * dir; + struct dirent *de, *dep; + int max_len = -1, len; + +#if HAVE_PATHCONF && defined(_PC_NAME_MAX) + max_len = pathconf(dir_name, _PC_NAME_MAX); +#endif + if (max_len == -1) { +#ifdef _POSIX_NAME_MAX + max_len = _POSIX_NAME_MAX; +#else +#ifdef NAME_MAX + max_len = NAME_MAX; +#else + max_len = 256; +#endif /* NAME_MAX */ +#endif /* _POSIX_NAME_MAX */ + } + max_len += sizeof(struct dirent); + + de = malloc(max_len+1); + if (!de) + return -1; + memset(de, 0, max_len+1); + + dir = opendir (dir_name); + if (dir == NULL) { + free(de); + return -1; + } + while ((dep = readdir (dir))) { + len = sizeof(struct dirent); +#ifdef HAVE_RECLEN_DIRENT + if (len < dep->d_reclen) + len = dep->d_reclen; + if (len > max_len) + len = max_len; +#endif + memcpy(de, dep, len); + (*func) (dir_name, de, private); + } + free(de); + closedir(dir); + return 0; +} |