1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
32 #include <stdlib.h> /* free() */
36 /* same conditions as recursive_action */
37 #define bb_need_name_too_long
38 #define BB_DECLARE_EXTERN
39 #include "../messages.c"
41 #undef DEBUG_RECURS_ACTION
45 * Walk down all the directories under the specified
46 * location, and do something (something specified
47 * by the fileAction and dirAction function pointers).
49 * Unfortunately, while nftw(3) could replace this and reduce
50 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
51 * and so isn't sufficiently portable to take over since glibc2.1
52 * is so stinking huge.
54 int recursive_action(const char *fileName,
55 int recurse, int followLinks, int depthFirst,
56 int (*fileAction) (const char *fileName,
57 struct stat * statbuf,
59 int (*dirAction) (const char *fileName,
60 struct stat * statbuf,
68 if (followLinks == TRUE)
69 status = stat(fileName, &statbuf);
71 status = lstat(fileName, &statbuf);
74 #ifdef DEBUG_RECURS_ACTION
76 "status=%d followLinks=%d TRUE=%d\n",
77 status, followLinks, TRUE);
79 perror_msg("%s", fileName);
83 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
84 if (fileAction == NULL)
87 return fileAction(fileName, &statbuf, userData);
90 if (recurse == FALSE) {
91 if (S_ISDIR(statbuf.st_mode)) {
92 if (dirAction != NULL)
93 return (dirAction(fileName, &statbuf, userData));
99 if (S_ISDIR(statbuf.st_mode)) {
102 if (dirAction != NULL && depthFirst == FALSE) {
103 status = dirAction(fileName, &statbuf, userData);
104 if (status == FALSE) {
105 perror_msg("%s", fileName);
107 } else if (status == SKIP)
110 dir = opendir(fileName);
112 perror_msg("%s", fileName);
116 while ((next = readdir(dir)) != NULL) {
119 if ((strcmp(next->d_name, "..") == 0)
120 || (strcmp(next->d_name, ".") == 0)) {
123 nextFile = concat_path_file(fileName, next->d_name);
124 if (recursive_action(nextFile, TRUE, followLinks, depthFirst,
125 fileAction, dirAction, userData) == FALSE) {
131 if (dirAction != NULL && depthFirst == TRUE) {
132 if (dirAction(fileName, &statbuf, userData) == FALSE) {
133 perror_msg("%s", fileName);
140 if (fileAction == NULL)
143 return fileAction(fileName, &statbuf, userData);
152 c-file-style: "linux"