diff -urN gnulib.orig/lib/ftw.c gnulib/lib/ftw.c --- gnulib.orig/lib/ftw.c 1970-01-01 01:00:00.000000000 +0100 +++ gnulib/lib/ftw.c 2005-04-13 13:10:26.000000000 +0200 @@ -0,0 +1,113 @@ +/* Copyright (C) 2005 Free Software Foundation, Inc. + * Written by Yoann Vandoorselaere + * + * The file is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this file; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include +#include +#include +#include + +#include "ftw_.h" +#include "pathmax.h" + + +static int get_path_infos(const char *path, struct stat *st, int *flag) +{ + int ret; + + ret = stat(path, st); + if ( ret < 0 ) { + *flag = FTW_NS; + return -1; + } + + if ( S_ISREG(st->st_mode) ) + *flag = FTW_F; + + else if ( S_ISDIR(st->st_mode) ) + *flag = FTW_D; + + else if ( S_ISLNK(st->st_mode) ) + *flag = FTW_SL; + + return ret; +} + + + +int ftw(const char *dir, + int (*fn)(const char *file, const struct stat *sb, int flag), int nopenfd) +{ + DIR *d; + size_t len; + struct stat st; + struct dirent *de; + int flag, ret = 0; + char filename[PATH_MAX]; + + ret = get_path_infos(dir, &st, &flag); + if ( ret < 0 ) + return -1; + + d = opendir(dir); + if ( ! d ) + return (errno == EACCES) ? fn(dir, &st, FTW_DNR) : -1; + + ret = fn(dir, &st, flag); + if ( ret < 0 ) { + closedir(d); + return ret; + } + + while ( (de = readdir(d)) ) { + + len = snprintf(filename, sizeof(filename), "%s/%s", dir, de->d_name); + if ( len < 0 || len >= sizeof(filename) ) { + errno = ENAMETOOLONG; + return -1; + } + + ret = get_path_infos(filename, &st, &flag); + if ( ret < 0 ) + break; + + if ( flag == FTW_D ) { + if ( strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, ".") == 0 ) + continue; + + ret = ftw(filename, fn, nopenfd); + if ( ret < 0 ) + break; + + continue; + } + + ret = fn(filename, (flag == FTW_NS) ? NULL : &st, flag); + if ( ret < 0 ) + break; + } + + closedir(d); + + return ret; +} diff -urN gnulib.orig/lib/ftw_.h gnulib/lib/ftw_.h --- gnulib.orig/lib/ftw_.h 1970-01-01 01:00:00.000000000 +0100 +++ gnulib/lib/ftw_.h 2005-04-13 13:06:57.000000000 +0200 @@ -0,0 +1,36 @@ +/* Copyright (C) 2005 Free Software Foundation, Inc. + * Written by Yoann Vandoorselaere + * + * The file is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this file; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#include +#include +#include + +#if HAVE_FTW + #include +#else + +#define FTW_F 1 +#define FTW_D 2 +#define FTW_DNR 3 +#define FTW_SL 4 +#define FTW_NS 5 + +extern int ftw(const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag), int nopenfd); + +#endif diff -urN gnulib.orig/m4/ftw.m4 gnulib/m4/ftw.m4 --- gnulib.orig/m4/ftw.m4 1970-01-01 01:00:00.000000000 +0100 +++ gnulib/m4/ftw.m4 2005-04-13 13:11:53.000000000 +0200 @@ -0,0 +1,14 @@ +# strsep.m4 serial 3 +dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_FTW], +[ + AC_REPLACE_FUNCS(ftw) + gl_PREREQ_FTW +]) + +# Prerequisites of lib/ftw.c. +AC_DEFUN([gl_PREREQ_FTW], [:]) diff -urN gnulib.orig/modules/ftw gnulib/modules/ftw --- gnulib.orig/modules/ftw 1970-01-01 01:00:00.000000000 +0100 +++ gnulib/modules/ftw 2005-04-13 13:12:39.000000000 +0200 @@ -0,0 +1,26 @@ +Description: +ftw() walks through the directory tree starting from the indicated directory dir. + +Files: +lib/ftw_.h +lib/ftw.c +m4/ftw.m4 + +Depends-on: +pathmax +snprintf + +configure.ac: +gl_FUNC_FTW + +Makefile.am: +lib_SOURCES += ftw.h + +Include: +"ftw.h" + +License: +LGPL + +Maintainer: +Yoann Vandoorselaere