1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <stdlib.h> /* free() */
29 #undef DEBUG_RECURS_ACTION
33 * Walk down all the directories under the specified
34 * location, and do something (something specified
35 * by the fileAction and dirAction function pointers).
37 * Unfortunately, while nftw(3) could replace this and reduce
38 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
39 * and so isn't sufficiently portable to take over since glibc2.1
40 * is so stinking huge.
42 int recursive_action(const char *fileName,
43 int recurse, int followLinks, int depthFirst,
44 int (*fileAction) (const char *fileName,
45 struct stat * statbuf,
47 int (*dirAction) (const char *fileName,
48 struct stat * statbuf,
56 if (followLinks == TRUE)
57 status = stat(fileName, &statbuf);
59 status = lstat(fileName, &statbuf);
62 #ifdef DEBUG_RECURS_ACTION
64 "status=%d followLinks=%d TRUE=%d\n",
65 status, followLinks, TRUE);
67 perror_msg("%s", fileName);
71 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
72 if (fileAction == NULL)
75 return fileAction(fileName, &statbuf, userData);
78 if (recurse == FALSE) {
79 if (S_ISDIR(statbuf.st_mode)) {
80 if (dirAction != NULL)
81 return (dirAction(fileName, &statbuf, userData));
87 if (S_ISDIR(statbuf.st_mode)) {
90 if (dirAction != NULL && depthFirst == FALSE) {
91 status = dirAction(fileName, &statbuf, userData);
92 if (status == FALSE) {
93 perror_msg("%s", fileName);
95 } else if (status == SKIP)
98 dir = opendir(fileName);
100 perror_msg("%s", fileName);
104 while ((next = readdir(dir)) != NULL) {
107 if ((strcmp(next->d_name, "..") == 0)
108 || (strcmp(next->d_name, ".") == 0)) {
111 nextFile = concat_path_file(fileName, next->d_name);
112 if (recursive_action(nextFile, TRUE, followLinks, depthFirst,
113 fileAction, dirAction, userData) == FALSE) {
119 if (dirAction != NULL && depthFirst == TRUE) {
120 if (dirAction(fileName, &statbuf, userData) == FALSE) {
121 perror_msg("%s", fileName);
128 if (fileAction == NULL)
131 return fileAction(fileName, &statbuf, userData);
140 c-file-style: "linux"