/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ian Lance Taylor (ian@airs.com).
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA.
Modified by Ian Lanc Taylor for Taylor UUCP, June 1992. */
#else /* ! HAVE_DIRENT_H */
#endif /* ! HAVE_DIRENT_H */
#endif /* HAVE_OPENDIR */
#define PATH_MAX MAXPATHLEN
/* Traverse one level of a directory tree. */
ftw_dir (dirs
, level
, descriptors
, dir
, len
, func
)
int (*func
) P((const char *file
, const struct stat
*status
, int flag
));
while ((entry
= readdir (dirs
[level
])) != NULL
)
namlen
= strlen (entry
->d_name
);
if (entry
->d_name
[0] == '.'
(namlen
== 2 && entry
->d_name
[1] == '.')))
if (namlen
+ len
+ 1 > PATH_MAX
)
memcpy ((dir
+ len
+ 1), entry
->d_name
, namlen
+ 1);
else if (S_ISDIR (s
.st_mode
))
newlev
= (level
+ 1) % descriptors
;
if (dirs
[newlev
] != NULL
)
dirs
[newlev
] = opendir (dir
);
if (dirs
[newlev
] != NULL
)
ret
= (*func
) (dir
, &s
, flag
);
ret
= ftw_dir (dirs
, newlev
, descriptors
, dir
,
if (dirs
[newlev
] != NULL
)
dirs
[level
] = opendir (dir
);
if (readdir (dirs
[level
]) == NULL
)
return errno
== 0 ? 0 : -1;
return errno
== 0 ? 0 : -1;
/* Call a function on every element in a directory tree. */
ftw (dir
, func
, descriptors
)
int (*func
) P((const char *file
, const struct stat
*status
, int flag
));
dirs
= (DIR **) malloc (descriptors
* sizeof (DIR *));
memcpy (buf
, dir
, len
+ 1);
else if (S_ISDIR (s
.st_mode
))
ret
= (*func
) (buf
, &s
, flag
);
ret
= ftw_dir (dirs
, 0, descriptors
, buf
, len
, func
);