1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <stdlib.h> /* free() */
17 #undef DEBUG_RECURS_ACTION
21 * Walk down all the directories under the specified
22 * location, and do something (something specified
23 * by the fileAction and dirAction function pointers).
25 * Unfortunately, while nftw(3) could replace this and reduce
26 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
27 * and so isn't sufficiently portable to take over since glibc2.1
28 * is so stinking huge.
30 int recursive_action(const char *fileName,
31 int recurse, int followLinks, int depthFirst,
32 int (*fileAction) (const char *fileName,
33 struct stat * statbuf,
35 int (*dirAction) (const char *fileName,
36 struct stat * statbuf,
45 status = stat(fileName, &statbuf);
47 status = lstat(fileName, &statbuf);
50 #ifdef DEBUG_RECURS_ACTION
51 bb_error_msg("status=%d followLinks=%d TRUE=%d",
52 status, followLinks, TRUE);
54 bb_perror_msg("%s", fileName);
58 if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
59 if (fileAction == NULL)
62 return fileAction(fileName, &statbuf, userData);
66 if (S_ISDIR(statbuf.st_mode)) {
67 if (dirAction != NULL)
68 return (dirAction(fileName, &statbuf, userData));
74 if (S_ISDIR(statbuf.st_mode)) {
77 if (dirAction != NULL && ! depthFirst) {
78 status = dirAction(fileName, &statbuf, userData);
80 bb_perror_msg("%s", fileName);
82 } else if (status == SKIP)
85 dir = bb_opendir(fileName);
90 while ((next = readdir(dir)) != NULL) {
93 nextFile = concat_subpath_file(fileName, next->d_name);
96 if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
97 fileAction, dirAction, userData)) {
103 if (dirAction != NULL && depthFirst) {
104 if (! dirAction(fileName, &statbuf, userData)) {
105 bb_perror_msg("%s", fileName);
112 if (fileAction == NULL)
115 return fileAction(fileName, &statbuf, userData);