33 #if defined(LIBC_SCCS) && !defined(lint) 34 static char sccsid[] =
"@(#)fts.c 8.6 (Berkeley) 8/14/94";
38 #include <sys/param.h> 39 #include <include/sys/stat.h> 48 #if defined(__UCLIBC__) 49 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 51 #if defined(hpux) || defined(__hpux) 52 # define _INCLUDE_POSIX_SOURCE 53 # define __errno_location() (&errno) 54 # define dirfd(dirp) -1 57 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 58 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen) 60 #if defined(sun) || defined(RPM_OS_UNIXWARE) 61 # define __errno_location() (&errno) 62 # define dirfd(dirp) -1 64 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 66 #if defined(__APPLE__) 67 # include <sys/stat.h> 68 # define __errno_location() (__error()) 69 #ifndef __DARWIN_STRUCT_STAT64 73 #ifndef __DARWIN_STRUCT_STAT64 74 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 76 # define __fxstat64(_stat_ver, _fd, _sbp) fstat64((_fd), (_sbp)) 79 #if defined(__CYGWIN__) || defined(__MINGW32__) 80 # include <sys/stat.h> 81 #if defined(__CYGWIN__) 82 # define __errno_location() (__errno()) 84 # define __errno_location() (_errno()) 86 # define __errno_location() (&errno) 90 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 92 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) 93 # define __errno_location() (&errno) 95 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 96 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen) 99 # define __errno_location() (&errno) 100 # define dirfd(dirp) -1 103 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 104 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen) 106 #if defined(RPM_OS_IRIX) 107 # define __errno_location() (&errno) 108 # define dirfd(dirp) -1 109 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 110 # define _D_EXACT_NAMLEN(d) ((d)->d_reclen) 112 #if defined(RPM_OS_AIX) 113 # define __errno_location() (&errno) 114 # define dirfd(dirp) ((dirp)->dd_fd) 116 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 117 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen) 119 #if defined(RPM_OS_NTOQNX) 120 # define __errno_location() (&errno) 123 # define dirfd(dirp) -1 124 # define __fxstat64(_stat_ver, _fd, _sbp) fstat((_fd), (_sbp)) 127 #if !defined(_D_EXACT_NAMLEN) 128 # define _D_EXACT_NAMLEN(d) (strlen((d)->d_name)) 138 # define __set_errno(val) (*__errno_location ()) = (val) 140 # define __close close 141 # define __fchdir fchdir 144 #if !defined(USHRT_MAX) 145 #define USHRT_MAX 65535 151 #if defined __GNUC__ && __GNUC__ >= 2 152 # define alignof(TYPE) __alignof__ (TYPE) 154 # define alignof(TYPE) \ 155 ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2) 157 #define ALIGNBYTES (alignof(long double) - 1) 161 #define ALIGN(p) (((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES) 193 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '/' && !a[2]) || (a[1] == '.' && (!a[2] || (a[2] == '/' && !a[3]))))) 195 #define CLR(opt) (sp->fts_options &= ~(opt)) 196 #define ISSET(opt) (sp->fts_options & (opt)) 197 #define SET(opt) (sp->fts_options |= (opt)) 199 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && __fchdir(fd)) 211 register FTSENT *p, *root;
219 fprintf(stderr,
"--> Fts_open(%p, 0x%x, %p) av[0] %s\n", argv, options, compar, argv[0]);
231 if ((sp = (
FTS *) malloc((u_int)
sizeof(*sp))) == NULL)
233 memset(sp, 0,
sizeof(*sp));
234 sp->
fts_compar = (int (*) (
const void *,
const void *)) compar;
251 #define MAXPATHLEN 1024 261 if ((parent =
fts_alloc(sp,
"", 0)) == NULL)
267 for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++
nitems) {
269 if ((len = strlen(*argv)) == 0) {
322 if (compar && nitems > 1)
382 register FTSENT *freep, *p;
386 fprintf(stderr,
"--> Fts_close(%p)\n", sp);
418 if (saved_errno != 0) {
453 if (!(fts_info >= 1 && fts_info <= 14))
455 return ftsInfoStrings[ fts_info ];
463 (p->fts_path[p->fts_pathlen - 1] == '/' \ 464 ? p->fts_pathlen - 1 : p->fts_pathlen) 476 fprintf(stderr,
"--> Fts_read(%p)\n", sp);
604 __open(
".", O_RDONLY, 0)) < 0) {
671 fprintf(stderr,
"<-- Fts_read(%p) ret %p", sp, p);
676 fprintf(stderr,
"\n");
692 fprintf(stderr,
"--> Fts_set(%p, %p, 0x%x)\n", sp, p, instr);
712 fprintf(stderr,
"--> Fts_children(%p, 0x%x)\n", sp, instr);
766 if ((fd =
__open(
".", O_RDONLY, 0)) < 0)
792 register struct dirent *dp;
793 register FTSENT *p, *head;
798 int cderrno, descend, len, level, nlinks, saved_errno,
810 #if defined FTS_WHITEOUT && 0 812 oflag = DTF_NODUP|DTF_REWIND;
814 oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
816 # define __opendir2(path, flag) (*sp->fts_opendir) (path) 844 (void)printf(
"nlinks == %d (cur: %d)\n", nlinks, cur->
fts_nlink);
845 (void)printf(
"NOSTAT %d PHYSICAL %d SEEDOT %d\n",
864 if (nlinks || type ==
BREAD) {
868 if (nlinks && type ==
BREAD)
905 for (head = tail = NULL, nitems = 0;
921 mem1: saved_errno =
errno;
959 #if defined FTS_WHITEOUT && 0 960 if (dp->d_type == DT_WHT)
980 #
if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
982 dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
1042 if (descend && (type ==
BCHILD || !nitems) &&
1072 struct stat *sbp, sb;
1078 #if defined FTS_WHITEOUT && 0 1082 memset(sbp,
'\0',
sizeof (*sbp));
1083 sbp->st_mode = S_IFWHT;
1096 saved_errno =
errno;
1106 err: memset(sbp, 0,
sizeof(*sbp));
1110 if (S_ISDIR(sbp->st_mode)) {
1118 dev = p->
fts_dev = sbp->st_dev;
1119 ino = p->
fts_ino = sbp->st_ino;
1141 if (S_ISREG(sbp->st_mode))
1149 register FTSENT **ap, *p;
1176 for (head = *(ap = sp->
fts_array); --nitems; ++ap)
1196 len =
sizeof(*p) + namelen;
1199 if ((p = (
FTSENT *) malloc(len)) == NULL)
1201 memset(p, 0,
sizeof(*p));
1205 memmove(p->
fts_name, name, namelen);
1226 while ((p = head)) {
1276 #define ADJUST(p) do { \ 1277 if ((p)->fts_accpath != (p)->fts_name) { \ 1278 (p)->fts_accpath = \ 1279 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ 1281 (p)->fts_path = addr; \ 1299 for (max = 0; *argv; ++argv)
1300 if ((len = strlen(*argv)) > max)
1313 int ret, oerrno, newfd;
1323 {
const char * lpath = NULL;
1324 int ut =
urlPath(path, &lpath);
1328 if (fd < 0 && (newfd =
__open(path, O_RDONLY, 0)) < 0)
1331 if (__fxstat64(_STAT_VER, newfd, &sb)) {
struct _ftsent * fts_cycle
static void fts_padjust(FTS *sp, FTSENT *head)
static FTSENT * fts_sort(FTS *sp, FTSENT *head, int nitems)
FTS * Fts_open(char *const *argv, int options, int(*compar)(const FTSENT **, const FTSENT **))
Create a handle for file hierarchy traversal.
FTSENT * Fts_children(FTS *sp, int instr)
Return list of children of the current node.
static int fts_safe_changedir(FTS *sp, FTSENT *p, int fd, const char *path)
urltype urlIsURL(const char *url)
Return type of URL.
int(* fts_closedir)(DIR *dir)
int Stat(const char *path, struct stat *st)
stat(2) clone.
static FTSENT * fts_alloc(FTS *sp, const char *name, int namelen)
struct _ftsent * fts_parent
#define __opendir2(path, flag)
static u_short fts_stat(FTS *sp, FTSENT *p, int follow)
#define _D_EXACT_NAMLEN(d)
static int fts_palloc(FTS *sp, size_t more)
static void fts_lfree(FTSENT *head)
struct dirent *(* fts_readdir)(DIR *dir)
int Lstat(const char *path, struct stat *st)
lstat(2) clone.
int(* fts_lstat)(const char *path, struct stat *st)
static const char * ftsInfoStrings[]
int(* fts_compar)(const void *, const void *)
static size_t fts_maxarglen(char *const *argv)
FTSENT * Fts_read(FTS *sp)
Return next node in the file hierarchy traversal.
static void fts_load(FTS *sp, FTSENT *p)
int Fts_set(FTS *sp, FTSENT *p, int instr)
Modify the traversal for a file set member.
static const char * ftsInfoStr(int fts_info)
urltype urlPath(const char *url, const char **pathp)
Return path component of URL.
int Fts_close(FTS *sp)
Destroy a file hierarchy traversal handle.
int(* fts_stat)(const char *path, struct stat *st)
DIR *(* fts_opendir)(const char *path)
struct _ftsent * fts_child
struct _ftsent ** fts_array
struct _ftsent * fts_link
#define FTS_ROOTPARENTLEVEL
static FTSENT * fts_build(FTS *sp, int type)