d5ab53e
--- acl-2.2.39/getfacl/getfacl.c.walk	2006-06-20 08:51:25.000000000 +0200
71326ac
+++ acl-2.2.39/getfacl/getfacl.c	2007-03-21 10:52:07.000000000 +0100
d5ab53e
@@ -34,7 +34,6 @@
d5ab53e
 #include <dirent.h>
d5ab53e
 #include <libgen.h>
d5ab53e
 #include <getopt.h>
d5ab53e
-#include <ftw.h>
d5ab53e
 #include <locale.h>
d5ab53e
 #include "config.h"
d5ab53e
 #include "user_group.h"
d5ab53e
@@ -70,9 +69,9 @@
d5ab53e
 const char *progname;
d5ab53e
 const char *cmd_line_options;
d5ab53e
 
d5ab53e
-int opt_recursive;  /* recurse into sub-directories? */
d5ab53e
-int opt_walk_logical;  /* always follow symbolic links */
d5ab53e
-int opt_walk_physical;  /* never follow symbolic links */
d5ab53e
+int opt_recursive = 0;  /* recurse into sub-directories? */
d5ab53e
+int opt_walk_logical = 0;  /* always follow symbolic links */
d5ab53e
+int opt_walk_physical = 0;  /* never follow symbolic links */
d5ab53e
 int opt_print_acl = 0;
d5ab53e
 int opt_print_default_acl = 0;
d5ab53e
 int opt_strip_leading_slash = 1;
71326ac
@@ -562,71 +561,140 @@
d5ab53e
 
d5ab53e
 
d5ab53e
 static int __errors;
d5ab53e
-int __do_print(const char *file, const struct stat *stat,
d5ab53e
-               int flag, struct FTW *ftw)
d5ab53e
+
d5ab53e
+int walk_tree(const char *file)
d5ab53e
 {
d5ab53e
-	int saved_errno = errno;
d5ab53e
+	static int level = 0;
d5ab53e
+	static int link_count = 0;
d5ab53e
+	DIR *dir;
d5ab53e
+	struct dirent *entry;
d5ab53e
+	struct stat buf;
71326ac
+	char path[FILENAME_MAX];
71326ac
+	char path2[FILENAME_MAX];
71326ac
+	char path3[FILENAME_MAX];
71326ac
+	char *dir_name;
71326ac
+	size_t len;
71326ac
+	ssize_t slen;
71326ac
+	int res;
d5ab53e
 
d5ab53e
 	/* Process the target of a symbolic link, and traverse the link,
d5ab53e
            only if doing a logical walk, or if the symbolic link was
d5ab53e
            specified on the command line. Always skip symbolic links if
d5ab53e
            doing a physical walk. */
d5ab53e
 
d5ab53e
-	if (S_ISLNK(stat->st_mode) &&
d5ab53e
-	    (opt_walk_physical || (ftw->level > 0 && !opt_walk_logical)))
71326ac
+	len = strlen(file);
71326ac
+	/* check for FILENAME_MAX */
71326ac
+	if (len >= FILENAME_MAX) {
71326ac
+		fprintf(stderr, "%s: %s: %s\n", progname, xquote(file),
71326ac
+			strerror(ENAMETOOLONG));
71326ac
+		__errors++;
d5ab53e
 		return 0;
71326ac
+	}
71326ac
+	/* string ends with '/', remove it and restart */
71326ac
+	if (len > 1 && file[len-1] == '/') {
71326ac
+		strncpy(path, file, len);
71326ac
+		path[len-1] = '\0'; /* overwrite slash */
71326ac
+		return walk_tree(path);
71326ac
+	}
d5ab53e
 
d5ab53e
-	if (do_print(file, stat))
71326ac
-		__errors++;
71326ac
+	if (level > 0 && !opt_recursive)
71326ac
+		return 0;
71326ac
 
d5ab53e
-	if (flag == FTW_DNR && opt_recursive) {
d5ab53e
-		/* Item is a directory which can't be read. */
d5ab53e
-		fprintf(stderr, "%s: %s: %s\n",
d5ab53e
-			progname, file, strerror(saved_errno));
71326ac
+	if (lstat(file, &buf) != 0) {
71326ac
+		fprintf(stderr, "%s: %s: %s\n", progname, xquote(file),
71326ac
+			strerror(errno));
71326ac
+		__errors++;
d5ab53e
 		return 0;
d5ab53e
 	}
d5ab53e
 
d5ab53e
-	/* We also get here in non-recursive mode. In that case,
d5ab53e
-	   return something != 0 to abort nftw. */
71326ac
+	if (S_ISLNK(buf.st_mode)) {
71326ac
+		/* physical means: no links at all */
71326ac
+		if (opt_walk_physical)
71326ac
+			return 1;
71326ac
+
71326ac
+		/* logical: show information or walk if points to directory
71326ac
+		 * also for symbolic link arguments on level 0 */
71326ac
+		if (opt_walk_logical || level == 0) {
71326ac
+			/* copy and append terminating '\0' */
71326ac
+			strncpy(path2, file, len+1);
71326ac
+
71326ac
+			/* get directory name */
71326ac
+			dir_name = dirname(path2);
71326ac
+
71326ac
+			/* get link target */
71326ac
+			slen = readlink(file, path, FILENAME_MAX-1);
71326ac
+			if (slen < 0) {
71326ac
+				fprintf(stderr, "%s: %s: %s\n", progname,
71326ac
+					xquote(file), strerror(errno));
71326ac
+				__errors++;
71326ac
+				return 0;
71326ac
+			}
71326ac
+			path[slen] = '\0';
71326ac
 
d5ab53e
-	if (!opt_recursive)
71326ac
-		return 1;
71326ac
+			if (slen == 0 || path[0] == '/') {
71326ac
+				/* absolute:
71326ac
+				 * copy and append terminating '\0' */
71326ac
+				strncpy(path3, path, slen+1);
71326ac
+			} else
71326ac
+				/* relative */
71326ac
+				snprintf(path3, FILENAME_MAX, "%s/%s",
71326ac
+					 dir_name, path);
71326ac
+			
71326ac
+			if (lstat(path3, &buf) != 0) {
71326ac
+				fprintf(stderr, "%s: %s: %s\n", progname,
71326ac
+					xquote(path), strerror(errno));
71326ac
+				__errors++;
71326ac
+				return 0;
71326ac
+			}
d5ab53e
 
d5ab53e
-	return 0;
d5ab53e
-}
71326ac
+			if ((S_ISDIR(buf.st_mode) && opt_recursive && 
71326ac
+			     link_count < 1) || S_ISLNK(buf.st_mode)) {
71326ac
+				/* walk directory or follow symlink on level
71326ac
+				 * 0 */
71326ac
+				link_count++;
71326ac
+				res = walk_tree(path3);
71326ac
+				link_count--;
71326ac
+				if (res != 1)
71326ac
+					return 0;
71326ac
+			} else
71326ac
+				if (do_print(path3, &buf))
71326ac
+					__errors++;
71326ac
 
d5ab53e
-char *resolve_symlinks(const char *file)
d5ab53e
-{
d5ab53e
-	static char buffer[4096];
d5ab53e
-	char *path = NULL;
d5ab53e
-	ssize_t len;
d5ab53e
-
d5ab53e
-	len = readlink(file, buffer, sizeof(buffer)-1);
d5ab53e
-	if (len < 0) {
d5ab53e
-		if (errno == EINVAL)	/* not a symlink, use given path */
d5ab53e
-			path = (char *)file;
d5ab53e
-	} else {
d5ab53e
-		buffer[len+1] = '\0';
d5ab53e
-		path = buffer;
d5ab53e
+			return 1;
d5ab53e
+		}
d5ab53e
 	}
d5ab53e
-	return path;
d5ab53e
-}
d5ab53e
-
d5ab53e
-int walk_tree(const char *file)
d5ab53e
-{
d5ab53e
-	const char *p;
d5ab53e
 
d5ab53e
-	__errors = 0;
d5ab53e
-	if ((p = resolve_symlinks(file)) == NULL) {
d5ab53e
-		fprintf(stderr, "%s: %s: %s\n", progname,
d5ab53e
-			xquote(file), strerror(errno));
d5ab53e
-		__errors++;
d5ab53e
-	} else if (nftw(p, __do_print, 0, opt_walk_logical? 0 : FTW_PHYS) < 0) {
d5ab53e
-		fprintf(stderr, "%s: %s: %s\n", progname, xquote(file),
d5ab53e
-			strerror(errno));
d5ab53e
+	if (do_print(file, &buf))
d5ab53e
 		__errors++;
d5ab53e
+
71326ac
+	/* it is a directory, walk */
d5ab53e
+	if (S_ISDIR(buf.st_mode)) {
d5ab53e
+		dir = opendir(file);
d5ab53e
+		if (!dir) {
d5ab53e
+			fprintf(stderr, "%s: %s: %s\n", progname,
d5ab53e
+				xquote(file), strerror(errno));
d5ab53e
+			__errors++;
d5ab53e
+			return 0;
d5ab53e
+		}
d5ab53e
+
71326ac
+		level++;
d5ab53e
+		while ((entry = readdir(dir)) != NULL) {
d5ab53e
+			if (! strcmp(entry->d_name, ".") || 
d5ab53e
+			    ! strcmp(entry->d_name, ".."))
d5ab53e
+				continue;
d5ab53e
+
71326ac
+			snprintf(path, FILENAME_MAX, "%s/%s", file,
71326ac
+				 entry->d_name);
d5ab53e
+
71326ac
+			/* ignore result, walk every entry */
71326ac
+			res = walk_tree(path);
d5ab53e
+		}
d5ab53e
+		level--;
71326ac
+
71326ac
+		closedir(dir);
d5ab53e
 	}
d5ab53e
-	return __errors;
d5ab53e
+
d5ab53e
+	return 1;
d5ab53e
 }
d5ab53e
 
d5ab53e
 int main(int argc, char *argv[])
71326ac
@@ -762,15 +830,22 @@
71326ac
 				if (*line == '\0')
71326ac
 					continue;
71326ac
 
71326ac
-				had_errors += walk_tree(line);
71326ac
+				/* ignore result of walk_tree, use __errors */
71326ac
+				__errors = 0;
71326ac
+				walk_tree(line);
71326ac
+				had_errors += __errors;
71326ac
 			}
71326ac
 			if (!feof(stdin)) {
71326ac
 				fprintf(stderr, _("%s: Standard input: %s\n"),
71326ac
 				        progname, strerror(errno));
71326ac
 				had_errors++;
71326ac
 			}
71326ac
-		} else
71326ac
-			had_errors += walk_tree(argv[optind]);
71326ac
+		} else {
71326ac
+			/* ignore result of walk_tree, use __errors */
71326ac
+			__errors = 0;
71326ac
+			walk_tree(argv[optind]);
71326ac
+			had_errors += __errors;
71326ac
+		}
71326ac
 		optind++;
71326ac
 	} while (optind < argc);
71326ac