/*-
- * Copyright (c) 1990, 1993
+ * Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fts.c 8.2 (Berkeley) 1/2/94";
+static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
-#include <sys/stat.h>
+#include <include/sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#endif
-static FTSENT *fts_alloc __P((FTS *, const char *, int));
-static FTSENT *fts_build __P((FTS *, int));
-static void fts_lfree __P((FTSENT *));
-static void fts_load __P((FTS *, FTSENT *));
-static size_t fts_maxarglen __P((char * const *));
-static void fts_padjust __P((FTS *, void *));
-static int fts_palloc __P((FTS *, size_t));
-static FTSENT *fts_sort __P((FTS *, FTSENT *, int));
-static u_short fts_stat __P((FTS *, FTSENT *, int));
+static FTSENT *fts_alloc (FTS *, const char *, size_t) internal_function;
+static FTSENT *fts_build (FTS *, int) internal_function;
+static void fts_lfree (FTSENT *) internal_function;
+static void fts_load (FTS *, FTSENT *) internal_function;
+static size_t fts_maxarglen (char * const *) internal_function;
+static void fts_padjust (FTS *, FTSENT *) internal_function;
+static int fts_palloc (FTS *, size_t) internal_function;
+static FTSENT *fts_sort (FTS *, FTSENT *, int) internal_function;
+static u_short fts_stat (FTS *, FTSENT *, int) internal_function;
+static int fts_safe_changedir (FTS *, FTSENT *, int, const char *)
+ internal_function;
#ifndef MAX
#define MAX(a, b) ({ __typeof__ (a) _a = (a); \
_a > _b ? _a : _b; })
#endif
-#define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
+#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
-#define ISSET(opt) (sp->fts_options & opt)
-#define SET(opt) (sp->fts_options |= opt)
+#define CLR(opt) (sp->fts_options &= ~(opt))
+#define ISSET(opt) (sp->fts_options & (opt))
+#define SET(opt) (sp->fts_options |= (opt))
-#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
-#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
+#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && __fchdir(fd))
/* fts_build flags */
#define BCHILD 1 /* fts_children */
fts_open(argv, options, compar)
char * const *argv;
register int options;
- int (*compar) __P((const FTSENT **, const FTSENT **));
+ int (*compar) (const FTSENT **, const FTSENT **);
{
register FTS *sp;
register FTSENT *p, *root;
register int nitems;
- FTSENT *parent, *tmp;
- int len;
+ FTSENT *parent = NULL;
+ FTSENT *tmp;
/* Options check. */
if (options & ~FTS_OPTIONMASK) {
/* Allocate/initialize the stream */
if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
return (NULL);
- bzero(sp, sizeof(FTS));
- sp->fts_compar = (int (*) __P((const void *, const void *))) compar;
+ memset(sp, 0, sizeof(FTS));
+ sp->fts_compar = (int (*) (const void *, const void *)) compar;
sp->fts_options = options;
/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
- if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
+ size_t maxarglen = fts_maxarglen(argv);
+ if (fts_palloc(sp, MAX(maxarglen, MAXPATHLEN)))
goto mem1;
/* Allocate/initialize root's parent. */
- if ((parent = fts_alloc(sp, "", 0)) == NULL)
- goto mem2;
- parent->fts_level = FTS_ROOTPARENTLEVEL;
+ if (*argv != NULL) {
+ if ((parent = fts_alloc(sp, "", 0)) == NULL)
+ goto mem2;
+ parent->fts_level = FTS_ROOTPARENTLEVEL;
+ }
/* Allocate/initialize root(s). */
- for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
+ for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
/* Don't allow zero-length paths. */
- if ((len = strlen(*argv)) == 0) {
+ size_t len = strlen(*argv);
+ if (len == 0) {
__set_errno (ENOENT);
goto mem3;
}
sp->fts_cur->fts_info = FTS_INIT;
/*
- * If using chdir(2), grab a file descriptor pointing to dot to insure
+ * If using chdir(2), grab a file descriptor pointing to dot to ensure
* that we can get back here; this could be avoided for some paths,
* but almost certainly not worth the effort. Slashes, symbolic links,
* and ".." are all fairly nasty problems. Note, if we can't get the
* descriptor we run anyway, just more slowly.
*/
- if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
+ if (!ISSET(FTS_NOCHDIR)
+ && (sp->fts_rfd = __open(".", O_RDONLY, 0)) < 0)
SET(FTS_NOCHDIR);
return (sp);
}
static void
+internal_function
fts_load(sp, p)
FTS *sp;
register FTSENT *p;
* known that the path will fit.
*/
len = p->fts_pathlen = p->fts_namelen;
- bcopy(p->fts_name, sp->fts_path, len + 1);
- if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
+ memmove(sp->fts_path, p->fts_name, len + 1);
+ if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
len = strlen(++cp);
- bcopy(cp, p->fts_name, len + 1);
+ memmove(p->fts_name, cp, len + 1);
p->fts_namelen = len;
}
p->fts_accpath = p->fts_path = sp->fts_path;
if (sp->fts_cur) {
for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
freep = p;
- p = p->fts_link ? p->fts_link : p->fts_parent;
+ p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
free(freep);
}
free(p);
/* Free up child linked list, sort array, path buffer. */
if (sp->fts_child)
fts_lfree(sp->fts_child);
- if (sp->fts_array)
- free(sp->fts_array);
+ free(sp->fts_array);
free(sp->fts_path);
/* Return to original directory, save errno if necessary. */
if (!ISSET(FTS_NOCHDIR)) {
- saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
- (void)close(sp->fts_rfd);
+ saved_errno = __fchdir(sp->fts_rfd) ? errno : 0;
+ (void)__close(sp->fts_rfd);
+
+ /* Set errno and return. */
+ if (saved_errno != 0) {
+ /* Free up the stream pointer. */
+ free(sp);
+ __set_errno (saved_errno);
+ return (-1);
+ }
}
/* Free up the stream pointer. */
free(sp);
-
- /* Set errno and return. */
- if (!ISSET(FTS_NOCHDIR) && saved_errno) {
- __set_errno (saved_errno);
- return (-1);
- }
return (0);
}
/*
- * Special case a root of "/" so that slashes aren't appended which would
- * cause paths to be written as "//foo".
+ * Special case of "/" at the end of the path so that slashes aren't
+ * appended which would cause paths to be written as "....//foo".
*/
#define NAPPEND(p) \
- (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
- p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
+ (p->fts_path[p->fts_pathlen - 1] == '/' \
+ ? p->fts_pathlen - 1 : p->fts_pathlen)
FTSENT *
fts_read(sp)
if (instr == FTS_FOLLOW &&
(p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
p->fts_info = fts_stat(sp, p, 1);
- if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
- if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
+ if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
+ if ((p->fts_symfd = __open(".", O_RDONLY, 0)) < 0) {
p->fts_errno = errno;
p->fts_info = FTS_ERR;
} else
p->fts_flags |= FTS_SYMFOLLOW;
+ }
return (p);
}
if (p->fts_info == FTS_D) {
/* If skipped or crossed mount point, do post-order visit. */
if (instr == FTS_SKIP ||
- ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
+ (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
if (p->fts_flags & FTS_SYMFOLLOW)
- (void)close(p->fts_symfd);
+ (void)__close(p->fts_symfd);
if (sp->fts_child) {
fts_lfree(sp->fts_child);
sp->fts_child = NULL;
}
/* Rebuild if only read the names and now traversing. */
- if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) {
- sp->fts_options &= ~FTS_NAMEONLY;
+ if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
+ CLR(FTS_NAMEONLY);
fts_lfree(sp->fts_child);
sp->fts_child = NULL;
}
* If haven't read do so. If the read fails, fts_build sets
* FTS_STOP or the fts_info field of the node.
*/
- if (sp->fts_child) {
- if (CHDIR(sp, p->fts_accpath)) {
+ if (sp->fts_child != NULL) {
+ if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
p->fts_errno = errno;
p->fts_flags |= FTS_DONTCHDIR;
- for (p = sp->fts_child; p; p = p->fts_link)
+ for (p = sp->fts_child; p != NULL;
+ p = p->fts_link)
p->fts_accpath =
p->fts_parent->fts_accpath;
}
}
p = sp->fts_child;
sp->fts_child = NULL;
+ sp->fts_cur = p;
goto name;
}
/* Move to the next node on this level. */
next: tmp = p;
- if (p = p->fts_link) {
+ if ((p = p->fts_link) != NULL) {
+ sp->fts_cur = p;
free(tmp);
/*
- * If reached the top, return to the original directory, and
- * load the paths for the next root.
+ * If reached the top, return to the original directory (or
+ * the root of the tree), and load the paths for the next root.
*/
if (p->fts_level == FTS_ROOTLEVEL) {
- if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) {
+ if (FCHDIR(sp, sp->fts_rfd)) {
SET(FTS_STOP);
return (NULL);
}
fts_load(sp, p);
- return (sp->fts_cur = p);
+ return p;
}
/*
goto next;
if (p->fts_instr == FTS_FOLLOW) {
p->fts_info = fts_stat(sp, p, 1);
- if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
+ if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
if ((p->fts_symfd =
- open(".", O_RDONLY, 0)) < 0) {
+ __open(".", O_RDONLY, 0)) < 0) {
p->fts_errno = errno;
p->fts_info = FTS_ERR;
} else
p->fts_flags |= FTS_SYMFOLLOW;
+ }
p->fts_instr = FTS_NOINSTR;
}
name: t = sp->fts_path + NAPPEND(p->fts_parent);
*t++ = '/';
- bcopy(p->fts_name, t, p->fts_namelen + 1);
- return (sp->fts_cur = p);
+ memmove(t, p->fts_name, p->fts_namelen + 1);
+ return p;
}
/* Move up to the parent node. */
p = tmp->fts_parent;
+ sp->fts_cur = p;
free(tmp);
if (p->fts_level == FTS_ROOTPARENTLEVEL) {
return (sp->fts_cur = NULL);
}
- /* Nul terminate the pathname. */
+ /* NUL terminate the pathname. */
sp->fts_path[p->fts_pathlen] = '\0';
/*
* one directory.
*/
if (p->fts_level == FTS_ROOTLEVEL) {
- if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) {
+ if (FCHDIR(sp, sp->fts_rfd)) {
SET(FTS_STOP);
return (NULL);
}
} else if (p->fts_flags & FTS_SYMFOLLOW) {
if (FCHDIR(sp, p->fts_symfd)) {
saved_errno = errno;
- (void)close(p->fts_symfd);
+ (void)__close(p->fts_symfd);
__set_errno (saved_errno);
SET(FTS_STOP);
return (NULL);
}
- (void)close(p->fts_symfd);
- } else if (!(p->fts_flags & FTS_DONTCHDIR)) {
- if (CHDIR(sp, "..")) {
- SET(FTS_STOP);
- return (NULL);
- }
+ (void)__close(p->fts_symfd);
+ } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
+ fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
+ SET(FTS_STOP);
+ return (NULL);
}
p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
- return (sp->fts_cur = p);
+ return p;
}
/*
FTSENT *p;
int instr;
{
- if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
+ if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
instr != FTS_NOINSTR && instr != FTS_SKIP) {
__set_errno (EINVAL);
return (1);
register FTSENT *p;
int fd;
- if (instr && instr != FTS_NAMEONLY) {
+ if (instr != 0 && instr != FTS_NAMEONLY) {
__set_errno (EINVAL);
return (NULL);
}
return (NULL);
/* Free up any previous child list. */
- if (sp->fts_child)
+ if (sp->fts_child != NULL)
fts_lfree(sp->fts_child);
if (instr == FTS_NAMEONLY) {
- sp->fts_options |= FTS_NAMEONLY;
+ SET(FTS_NAMEONLY);
instr = BNAMES;
} else
instr = BCHILD;
ISSET(FTS_NOCHDIR))
return (sp->fts_child = fts_build(sp, instr));
- if ((fd = open(".", O_RDONLY, 0)) < 0)
+ if ((fd = __open(".", O_RDONLY, 0)) < 0)
return (NULL);
sp->fts_child = fts_build(sp, instr);
- if (fchdir(fd))
+ if (__fchdir(fd))
return (NULL);
- (void)close(fd);
+ (void)__close(fd);
return (sp->fts_child);
}
* been found, cutting the stat calls by about 2/3.
*/
static FTSENT *
+internal_function
fts_build(sp, type)
register FTS *sp;
int type;
{
- struct dirent *dp;
+ register struct dirent *dp;
register FTSENT *p, *head;
register int nitems;
FTSENT *cur, *tail;
DIR *dirp;
- void *adjaddr;
- int cderrno, descend, len, level, maxlen, nlinks, saved_errno;
+ void *oldaddr;
+ int cderrno, descend, len, level, nlinks, saved_errno,
+ nostat, doadjust;
+ size_t maxlen;
char *cp;
/* Set current node pointer. */
* Open the directory for reading. If this fails, we're done.
* If being called from fts_read, set the fts_info field.
*/
- if ((dirp = opendir(cur->fts_accpath)) == NULL) {
+#if defined FTS_WHITEOUT && 0
+ if (ISSET(FTS_WHITEOUT))
+ oflag = DTF_NODUP|DTF_REWIND;
+ else
+ oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
+#else
+# define __opendir2(path, flag) __opendir(path)
+#endif
+ if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
if (type == BREAD) {
cur->fts_info = FTS_DNR;
cur->fts_errno = errno;
* directory if we're cheating on stat calls, 0 if we're not doing
* any stat calls at all, -1 if we're doing stats on everything.
*/
- if (type == BNAMES)
+ if (type == BNAMES) {
nlinks = 0;
- else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL))
+ /* Be quiet about nostat, GCC. */
+ nostat = 0;
+ } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
- else
+ nostat = 1;
+ } else {
nlinks = -1;
+ nostat = 0;
+ }
#ifdef notdef
(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
* checking FTS_NS on the returned nodes.
*/
cderrno = 0;
- if (nlinks || type == BREAD)
- if (FCHDIR(sp, dirfd(dirp))) {
+ if (nlinks || type == BREAD) {
+ if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
if (nlinks && type == BREAD)
cur->fts_errno = errno;
cur->fts_flags |= FTS_DONTCHDIR;
descend = 0;
cderrno = errno;
+ (void)__closedir(dirp);
+ dirp = NULL;
} else
descend = 1;
- else
+ } else
descend = 0;
/*
* If not changing directories set a pointer so that can just append
* each new name into the path.
*/
- maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
len = NAPPEND(cur);
if (ISSET(FTS_NOCHDIR)) {
cp = sp->fts_path + len;
*cp++ = '/';
+ } else {
+ /* GCC, you're too verbose. */
+ cp = NULL;
}
+ len++;
+ maxlen = sp->fts_pathlen - len;
level = cur->fts_level + 1;
/* Read the directory, attaching each entry to the `link' pointer. */
- adjaddr = NULL;
- for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
- int namlen;
-
+ doadjust = 0;
+ for (head = tail = NULL, nitems = 0; dirp && (dp = __readdir(dirp));) {
if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
continue;
- namlen = _D_EXACT_NAMLEN (dp);
- if ((p = fts_alloc(sp, dp->d_name, namlen)) == NULL)
+ if ((p = fts_alloc(sp, dp->d_name, _D_EXACT_NAMLEN (dp))) == NULL)
goto mem1;
- if (namlen > maxlen) {
- if (fts_palloc(sp, (size_t)namlen)) {
+ if (_D_EXACT_NAMLEN (dp) >= maxlen) {/* include space for NUL */
+ oldaddr = sp->fts_path;
+ if (fts_palloc(sp, _D_EXACT_NAMLEN (dp) + len + 1)) {
/*
* No more memory for path or structures. Save
* errno, free up the current structure and the
* structures already allocated.
*/
mem1: saved_errno = errno;
- if (p)
- free(p);
+ free(p);
fts_lfree(head);
- (void)closedir(dirp);
- __set_errno (saved_errno);
+ (void)__closedir(dirp);
cur->fts_info = FTS_ERR;
SET(FTS_STOP);
+ __set_errno (saved_errno);
return (NULL);
}
- adjaddr = sp->fts_path;
- maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
+ /* Did realloc() change the pointer? */
+ if (oldaddr != sp->fts_path) {
+ doadjust = 1;
+ if (ISSET(FTS_NOCHDIR))
+ cp = sp->fts_path + len;
+ }
+ maxlen = sp->fts_pathlen - len;
}
- p->fts_pathlen = len + namlen + 1;
- p->fts_parent = sp->fts_cur;
+ if (len + _D_EXACT_NAMLEN (dp) >= USHRT_MAX) {
+ /*
+ * In an FTSENT, fts_pathlen is a u_short so it is
+ * possible to wraparound here. If we do, free up
+ * the current structure and the structures already
+ * allocated, then error out with ENAMETOOLONG.
+ */
+ free(p);
+ fts_lfree(head);
+ (void)__closedir(dirp);
+ cur->fts_info = FTS_ERR;
+ SET(FTS_STOP);
+ __set_errno (ENAMETOOLONG);
+ return (NULL);
+ }
p->fts_level = level;
+ p->fts_parent = sp->fts_cur;
+ p->fts_pathlen = len + _D_EXACT_NAMLEN (dp);
+
+#if defined FTS_WHITEOUT && 0
+ if (dp->d_type == DT_WHT)
+ p->fts_flags |= FTS_ISW;
+#endif
- if (cderrno) {
+ /* Unreachable code. cderrno is only ever set to a nonnull
+ value if dirp is closed at the same time. But then we
+ cannot enter this loop. */
+ if (0 && cderrno) {
if (nlinks) {
p->fts_info = FTS_NS;
p->fts_errno = cderrno;
p->fts_info = FTS_NSOK;
p->fts_accpath = cur->fts_accpath;
} else if (nlinks == 0
-#ifdef DT_DIR
- || nlinks > 0 &&
- dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN
+#if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
+ || (nostat &&
+ dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
#endif
) {
p->fts_accpath =
/* Build a file name for fts_stat to stat. */
if (ISSET(FTS_NOCHDIR)) {
p->fts_accpath = p->fts_path;
- bcopy(p->fts_name, cp, p->fts_namelen + 1);
+ memmove(cp, p->fts_name, p->fts_namelen + 1);
} else
p->fts_accpath = p->fts_name;
/* Stat it. */
}
++nitems;
}
- (void)closedir(dirp);
+ if (dirp)
+ (void)__closedir(dirp);
/*
- * If had to realloc the path, adjust the addresses for the rest
- * of the tree.
+ * If realloc() changed the address of the path, adjust the
+ * addresses for the rest of the tree and the dir list.
*/
- if (adjaddr)
- fts_padjust(sp, adjaddr);
+ if (doadjust)
+ fts_padjust(sp, head);
/*
* If not changing directories, reset the path back to original
* state.
*/
if (ISSET(FTS_NOCHDIR)) {
- if (cp - 1 > sp->fts_path)
+ if (len == sp->fts_pathlen || nitems == 0)
--cp;
*cp = '\0';
}
/*
- * If descended after called from fts_children or called from
- * fts_read and didn't find anything, get back. If can't get
- * back, done.
+ * If descended after called from fts_children or after called from
+ * fts_read and nothing found, get back. At the root level we use
+ * the saved fd; if one of fts_open()'s arguments is a relative path
+ * to an empty directory, we wind up here with no other way back. If
+ * can't get back, we're done.
*/
- if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
+ if (descend && (type == BCHILD || !nitems) &&
+ (cur->fts_level == FTS_ROOTLEVEL ?
+ FCHDIR(sp, sp->fts_rfd) :
+ fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
cur->fts_info = FTS_ERR;
SET(FTS_STOP);
+ fts_lfree(head);
return (NULL);
}
if (!nitems) {
if (type == BREAD)
cur->fts_info = FTS_DP;
+ fts_lfree(head);
return (NULL);
}
}
static u_short
+internal_function
fts_stat(sp, p, follow)
FTS *sp;
register FTSENT *p;
/* If user needs stat info, stat buffer already allocated. */
sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
+#if defined FTS_WHITEOUT && 0
+ /* check for whiteout */
+ if (p->fts_flags & FTS_ISW) {
+ if (sbp != &sb) {
+ memset(sbp, '\0', sizeof (*sbp));
+ sbp->st_mode = S_IFWHT;
+ }
+ return (FTS_W);
+ }
+#endif
+
/*
* If doing a logical walk, or application requested FTS_FOLLOW, do
* a stat(2). If that fails, check for a non-existent symlink. If
}
} else if (lstat(p->fts_accpath, sbp)) {
p->fts_errno = errno;
-err: bzero(sbp, sizeof(struct stat));
+err: memset(sbp, 0, sizeof(struct stat));
return (FTS_NS);
}
}
static FTSENT *
+internal_function
fts_sort(sp, head, nitems)
FTS *sp;
FTSENT *head;
* 40 so don't realloc one entry at a time.
*/
if (nitems > sp->fts_nitems) {
+ struct _ftsent **a;
+
sp->fts_nitems = nitems + 40;
- if ((sp->fts_array = realloc(sp->fts_array,
- (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
+ if ((a = realloc(sp->fts_array,
+ (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
+ free(sp->fts_array);
+ sp->fts_array = NULL;
sp->fts_nitems = 0;
return (head);
}
+ sp->fts_array = a;
}
for (ap = sp->fts_array, p = head; p; p = p->fts_link)
*ap++ = p;
}
static FTSENT *
+internal_function
fts_alloc(sp, name, namelen)
FTS *sp;
const char *name;
- register int namelen;
+ size_t namelen;
{
register FTSENT *p;
size_t len;
if ((p = malloc(len)) == NULL)
return (NULL);
- /* Copy the name plus the trailing NULL. */
- bcopy(name, p->fts_name, namelen + 1);
+ /* Copy the name and guarantee NUL termination. */
+ memmove(p->fts_name, name, namelen);
+ p->fts_name[namelen] = '\0';
if (!ISSET(FTS_NOSTAT))
p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
}
static void
+internal_function
fts_lfree(head)
register FTSENT *head;
{
register FTSENT *p;
/* Free a linked list of structures. */
- while (p = head) {
+ while ((p = head)) {
head = head->fts_link;
free(p);
}
* plus 256 bytes so don't realloc the path 2 bytes at a time.
*/
static int
+internal_function
fts_palloc(sp, more)
FTS *sp;
size_t more;
{
+ char *p;
+
sp->fts_pathlen += more + 256;
- sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
- return (sp->fts_path == NULL);
+ /*
+ * Check for possible wraparound. In an FTS, fts_pathlen is
+ * a signed int but in an FTSENT it is an unsigned short.
+ * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
+ */
+ if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) {
+ free(sp->fts_path);
+ sp->fts_path = NULL;
+ __set_errno (ENAMETOOLONG);
+ return (1);
+ }
+ p = realloc(sp->fts_path, sp->fts_pathlen);
+ if (p == NULL) {
+ free(sp->fts_path);
+ sp->fts_path = NULL;
+ return 1;
+ }
+ sp->fts_path = p;
+ return 0;
}
/*
* already returned.
*/
static void
-fts_padjust(sp, addr)
+internal_function
+fts_padjust(sp, head)
FTS *sp;
- void *addr;
+ FTSENT *head;
{
FTSENT *p;
+ char *addr = sp->fts_path;
-#define ADJUST(p) { \
- (p)->fts_accpath = \
- (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
+#define ADJUST(p) do { \
+ if ((p)->fts_accpath != (p)->fts_name) { \
+ (p)->fts_accpath = \
+ (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
+ } \
(p)->fts_path = addr; \
-}
+} while (0)
/* Adjust the current set of children. */
for (p = sp->fts_child; p; p = p->fts_link)
ADJUST(p);
- /* Adjust the rest of the tree. */
- for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
+ /* Adjust the rest of the tree, including the current level. */
+ for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
ADJUST(p);
p = p->fts_link ? p->fts_link : p->fts_parent;
}
}
static size_t
+internal_function
fts_maxarglen(argv)
char * const *argv;
{
for (max = 0; *argv; ++argv)
if ((len = strlen(*argv)) > max)
max = len;
- return (max);
+ return (max + 1);
+}
+
+/*
+ * Change to dir specified by fd or p->fts_accpath without getting
+ * tricked by someone changing the world out from underneath us.
+ * Assumes p->fts_dev and p->fts_ino are filled in.
+ */
+static int
+internal_function
+fts_safe_changedir(sp, p, fd, path)
+ FTS *sp;
+ FTSENT *p;
+ int fd;
+ const char *path;
+{
+ int ret, oerrno, newfd;
+ struct stat64 sb;
+
+ newfd = fd;
+ if (ISSET(FTS_NOCHDIR))
+ return (0);
+ if (fd < 0 && (newfd = __open(path, O_RDONLY, 0)) < 0)
+ return (-1);
+ if (__fxstat64(_STAT_VER, newfd, &sb)) {
+ ret = -1;
+ goto bail;
+ }
+ if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
+ __set_errno (ENOENT); /* disinformation */
+ ret = -1;
+ goto bail;
+ }
+ ret = __fchdir(newfd);
+bail:
+ oerrno = errno;
+ if (fd < 0)
+ (void)__close(newfd);
+ __set_errno (oerrno);
+ return (ret);
}