normalize routine bug; bug report net2/lib/4
[unix-history] / usr / src / lib / libc / gen / fts.c
index c8e3d75..d564d86 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fts.c      5.20 (Berkeley) %G%";
+static char sccsid[] = "@(#)fts.c      5.40 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -14,7 +14,7 @@ static char sccsid[] = "@(#)fts.c     5.20 (Berkeley) %G%";
 #include <fcntl.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <dirent.h>
 #include <errno.h>
-#include "fts.h"
+#include <fts.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -23,10 +23,14 @@ static FTSENT       *fts_alloc __P((FTS *, char *, int));
 static FTSENT  *fts_build __P((FTS *, int));
 static void     fts_lfree __P((FTSENT *));
 static void     fts_load __P((FTS *, FTSENT *));
 static FTSENT  *fts_build __P((FTS *, int));
 static void     fts_lfree __P((FTSENT *));
 static void     fts_load __P((FTS *, FTSENT *));
-static char    *fts_path __P((FTS *, int));
+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_sort __P((FTS *, FTSENT *, int));
 static u_short  fts_stat __P((FTS *, FTSENT *, int));
 
+#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        ISSET(opt)      (sp->fts_options & opt)
 #define        SET(opt)        (sp->fts_options |= opt)
 
@@ -34,8 +38,9 @@ static u_short         fts_stat __P((FTS *, FTSENT *, int));
 #define        FCHDIR(sp, fd)  (!ISSET(FTS_NOCHDIR) && fchdir(fd))
 
 /* fts_build flags */
 #define        FCHDIR(sp, fd)  (!ISSET(FTS_NOCHDIR) && fchdir(fd))
 
 /* fts_build flags */
-#define        BCHILD          1               /* from fts_children */
-#define        BREAD           2               /* from fts_read */
+#define        BCHILD          1               /* fts_children */
+#define        BNAMES          2               /* fts_children, names only */
+#define        BREAD           3               /* fts_read */
 
 FTS *
 fts_open(argv, options, compar)
 
 FTS *
 fts_open(argv, options, compar)
@@ -45,13 +50,19 @@ fts_open(argv, options, compar)
 {
        register FTS *sp;
        register FTSENT *p, *root;
 {
        register FTS *sp;
        register FTSENT *p, *root;
-       register int nitems, maxlen;
+       register int nitems;
        FTSENT *parent, *tmp;
        int len;
 
        FTSENT *parent, *tmp;
        int len;
 
+       /* Options check. */
+       if (options & ~FTS_OPTIONMASK) {
+               errno = EINVAL;
+               return (NULL);
+       }
+
        /* Allocate/initialize the stream */
        /* Allocate/initialize the stream */
-       if (!(sp = (FTS *)malloc((u_int)sizeof(FTS))))
-               return(NULL);
+       if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
+               return (NULL);
        bzero(sp, sizeof(FTS));
        sp->fts_compar = compar;
        sp->fts_options = options;
        bzero(sp, sizeof(FTS));
        sp->fts_compar = compar;
        sp->fts_options = options;
@@ -60,23 +71,36 @@ fts_open(argv, options, compar)
        if (ISSET(FTS_LOGICAL))
                SET(FTS_NOCHDIR);
 
        if (ISSET(FTS_LOGICAL))
                SET(FTS_NOCHDIR);
 
-       /* Allocate/initialize root's parent. */
-       if (!(parent = fts_alloc(sp, "", 0)))
+       /*
+        * Start out with 1K of path space, and enough, in any case,
+        * to hold the user's paths.
+        */
+       if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
                goto mem1;
                goto mem1;
+
+       /* Allocate/initialize root's parent. */
+       if ((parent = fts_alloc(sp, "", 0)) == NULL)
+               goto mem2;
        parent->fts_level = FTS_ROOTPARENTLEVEL;
 
        /* Allocate/initialize root(s). */
        parent->fts_level = FTS_ROOTPARENTLEVEL;
 
        /* Allocate/initialize root(s). */
-       maxlen = -1;
        for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
        for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
-               if (!(len = strlen(*argv))) {
+               /* Don't allow zero-length paths. */
+               if ((len = strlen(*argv)) == 0) {
                        errno = ENOENT;
                        errno = ENOENT;
-                       goto mem2;
+                       goto mem3;
                }
                }
-               if (maxlen < len)
-                       maxlen = len;
+
                p = fts_alloc(sp, *argv, len);
                p->fts_level = FTS_ROOTLEVEL;
                p->fts_parent = parent;
                p = fts_alloc(sp, *argv, len);
                p->fts_level = FTS_ROOTLEVEL;
                p->fts_parent = parent;
+               p->fts_accpath = p->fts_name;
+               p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
+
+               /* Command-line "." and ".." are real directories. */
+               if (p->fts_info == FTS_DOT)
+                       p->fts_info = FTS_D;
+
                /*
                 * If comparison routine supplied, traverse in sorted
                 * order; otherwise traverse in the order specified.
                /*
                 * If comparison routine supplied, traverse in sorted
                 * order; otherwise traverse in the order specified.
@@ -84,12 +108,9 @@ fts_open(argv, options, compar)
                if (compar) {
                        p->fts_link = root;
                        root = p;
                if (compar) {
                        p->fts_link = root;
                        root = p;
-                       p->fts_accpath = p->fts_name;
-                       if (!(options & FTS_NOSTAT))
-                               p->fts_info = fts_stat(sp, p, 0);
                } else {
                        p->fts_link = NULL;
                } else {
                        p->fts_link = NULL;
-                       if (!root)
+                       if (root == NULL)
                                tmp = root = p;
                        else {
                                tmp->fts_link = p;
                                tmp = root = p;
                        else {
                                tmp->fts_link = p;
@@ -102,17 +123,13 @@ fts_open(argv, options, compar)
 
        /*
         * Allocate a dummy pointer and make fts_read think that we've just
 
        /*
         * Allocate a dummy pointer and make fts_read think that we've just
-        * finished the node before the root(s); set p->fts_info to FTS_NS
+        * finished the node before the root(s); set p->fts_info to FTS_INIT
         * so that everything about the "current" node is ignored.
         */
         * so that everything about the "current" node is ignored.
         */
-       if (!(sp->fts_cur = fts_alloc(sp, "", 0)))
-               goto mem2;
-       sp->fts_cur->fts_link = root;
-       sp->fts_cur->fts_info = FTS_NS;
-
-       /* Start out with at least 1K+ of path space. */
-       if (!fts_path(sp, MAX(maxlen, MAXPATHLEN)))
+       if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
                goto mem3;
                goto mem3;
+       sp->fts_cur->fts_link = root;
+       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 insure
@@ -124,13 +141,13 @@ fts_open(argv, options, compar)
        if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
                SET(FTS_NOCHDIR);
 
        if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
                SET(FTS_NOCHDIR);
 
-       return(sp);
+       return (sp);
 
 
-mem3:  free(sp->fts_cur);
-mem2:  fts_lfree(root);
+mem3:  fts_lfree(root);
        free(parent);
        free(parent);
+mem2:  free(sp->fts_path);
 mem1:  free(sp);
 mem1:  free(sp);
-       return(NULL);
+       return (NULL);
 }
 
 static void
 }
 
 static void
@@ -145,7 +162,8 @@ fts_load(sp, p)
         * Load the stream structure for the next traversal.  Since we don't
         * actually enter the directory until after the preorder visit, set
         * the fts_accpath field specially so the chdir gets done to the right
         * Load the stream structure for the next traversal.  Since we don't
         * actually enter the directory until after the preorder visit, set
         * the fts_accpath field specially so the chdir gets done to the right
-        * place and the user can access the first node.
+        * place and the user can access the first node.  From fts_open it's
+        * known that the path will fit.
         */
        len = p->fts_pathlen = p->fts_namelen;
        bcopy(p->fts_name, sp->fts_path, len + 1);
         */
        len = p->fts_pathlen = p->fts_namelen;
        bcopy(p->fts_name, sp->fts_path, len + 1);
@@ -155,24 +173,23 @@ fts_load(sp, p)
                p->fts_namelen = len;
        }
        p->fts_accpath = p->fts_path = sp->fts_path;
                p->fts_namelen = len;
        }
        p->fts_accpath = p->fts_path = sp->fts_path;
-
-       p->fts_info = fts_stat(sp, p, 0);
-       sp->rdev = p->fts_statb.st_dev;
+       sp->fts_dev = p->fts_dev;
 }
 
 }
 
+int
 fts_close(sp)
        FTS *sp;
 {
        register FTSENT *freep, *p;
        int saved_errno;
 
 fts_close(sp)
        FTS *sp;
 {
        register FTSENT *freep, *p;
        int saved_errno;
 
+       /*
+        * This still works if we haven't read anything -- the dummy structure
+        * points to the root list, so we step through to the end of the root
+        * list which has a valid parent pointer.
+        */
        if (sp->fts_cur) {
        if (sp->fts_cur) {
-               /*
-                * This still works if we haven't read anything -- the dummy
-                * structure points to the root list, so we step through to
-                * the end of the root list which has a valid parent pointer.
-                */
-               for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
+               for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
                        freep = p;
                        p = p->fts_link ? p->fts_link : p->fts_parent;
                        free(freep);
                        freep = p;
                        p = p->fts_link ? p->fts_link : p->fts_parent;
                        free(freep);
@@ -199,17 +216,17 @@ fts_close(sp)
        /* Set errno and return. */
        if (!ISSET(FTS_NOCHDIR) && saved_errno) {
                errno = saved_errno;
        /* Set errno and return. */
        if (!ISSET(FTS_NOCHDIR) && saved_errno) {
                errno = saved_errno;
-               return(-1);
+               return (-1);
        }
        }
-       return(0);
+       return (0);
 }
 
 /*
 }
 
 /*
- * Special case a root of "/" so that slashes aren't appended causing
- * paths to be written as "//foo".
+ * Special case a root of "/" 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 && \
+#define        NAPPEND(p)                                                      \
+       (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 &&        \
            p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
 
 FTSENT *
            p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
 
 FTSENT *
@@ -219,10 +236,11 @@ fts_read(sp)
        register FTSENT *p, *tmp;
        register int instr;
        register char *t;
        register FTSENT *p, *tmp;
        register int instr;
        register char *t;
+       int saved_errno;
 
        /* If finished or unrecoverable error, return NULL. */
 
        /* If finished or unrecoverable error, return NULL. */
-       if (!sp->fts_cur || ISSET(FTS_STOP))
-               return(NULL);
+       if (sp->fts_cur == NULL || ISSET(FTS_STOP))
+               return (NULL);
 
        /* Set current node pointer. */
        p = sp->fts_cur;
 
        /* Set current node pointer. */
        p = sp->fts_cur;
@@ -231,75 +249,83 @@ fts_read(sp)
        instr = p->fts_instr;
        p->fts_instr = FTS_NOINSTR;
 
        instr = p->fts_instr;
        p->fts_instr = FTS_NOINSTR;
 
-       /* If used fts_link pointer for cycle detection, restore it. */
-       if (sp->fts_savelink) {
-               p->fts_link = sp->fts_savelink;
-               sp->fts_savelink = NULL;
-       }
-
        /* Any type of file may be re-visited; re-stat and re-turn. */
        if (instr == FTS_AGAIN) {
                p->fts_info = fts_stat(sp, p, 0);
        /* Any type of file may be re-visited; re-stat and re-turn. */
        if (instr == FTS_AGAIN) {
                p->fts_info = fts_stat(sp, p, 0);
-               return(p);
+               return (p);
        }
 
        /*
         * Following a symlink -- SLNONE test allows application to see
        }
 
        /*
         * Following a symlink -- SLNONE test allows application to see
-        * SLNONE and recover.
+        * SLNONE and recover.  If indirecting through a symlink, have
+        * keep a pointer to current location.  If unable to get that
+        * pointer, follow fails.
         */
        if (instr == FTS_FOLLOW &&
            (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
                p->fts_info = fts_stat(sp, p, 1);
         */
        if (instr == FTS_FOLLOW &&
            (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
                p->fts_info = fts_stat(sp, p, 1);
-               return(p);
+               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);
        }
 
        /* Directory in pre-order. */
        if (p->fts_info == FTS_D) {
                /* If skipped or crossed mount point, do post-order visit. */
                if (instr == FTS_SKIP ||
        }
 
        /* Directory in pre-order. */
        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_statb.st_dev != sp->rdev) {
+                   ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
+                       if (p->fts_flags & FTS_SYMFOLLOW)
+                               (void)close(p->fts_symfd);
                        if (sp->fts_child) {
                                fts_lfree(sp->fts_child);
                                sp->fts_child = NULL;
                        }
                        p->fts_info = FTS_DP;
                        if (sp->fts_child) {
                                fts_lfree(sp->fts_child);
                                sp->fts_child = NULL;
                        }
                        p->fts_info = FTS_DP;
-                       return(p);
+                       return (p);
                } 
 
                } 
 
+               /* Rebuild if only read the names and now traversing. */
+               if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) {
+                       sp->fts_options &= ~FTS_NAMEONLY;
+                       fts_lfree(sp->fts_child);
+                       sp->fts_child = NULL;
+               }
+
                /*
                /*
-                * Cd to the subdirectory, reading it if haven't already.  If
-                * the read fails for any reason, or the directory is empty,
-                * the fts_info field of the current node is set by fts_build.
+                * Cd to the subdirectory.
+                *
                 * If have already read and now fail to chdir, whack the list
                 * If have already read and now fail to chdir, whack the list
-                * to make the names come out right, and set the parent state
+                * to make the names come out right, and set the parent errno
                 * so the application will eventually get an error condition.
                 * so the application will eventually get an error condition.
-                * If haven't read and fail to chdir, check to see if we're
-                * at the root node -- if so, we have to get back or the root
-                * node may be inaccessible.
+                * Set the FTS_DONTCHDIR flag so that when we logically change
+                * directories back to the parent we don't do a chdir.
+                *
+                * 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) {
                        if (CHDIR(sp, p->fts_accpath)) {
-                               p->fts_parent->fts_cderr = errno;
+                               p->fts_errno = errno;
+                               p->fts_flags |= FTS_DONTCHDIR;
                                for (p = sp->fts_child; p; p = p->fts_link)
                                        p->fts_accpath =
                                            p->fts_parent->fts_accpath;
                        }
                                for (p = sp->fts_child; p; p = p->fts_link)
                                        p->fts_accpath =
                                            p->fts_parent->fts_accpath;
                        }
-               } else if (!(sp->fts_child = fts_build(sp, BREAD))) {
-                       if ISSET(FTS_STOP)
-                               return(NULL);
-                       if (p->fts_level == FTS_ROOTLEVEL &&
-                           FCHDIR(sp, sp->fts_rfd)) {
-                               SET(FTS_STOP);
-                               return(NULL);
-                       }
-                       return(p);
+               } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
+                       if (ISSET(FTS_STOP))
+                               return (NULL);
+                       return (p);
                }
                p = sp->fts_child;
                sp->fts_child = NULL;
                goto name;
        }
 
                }
                p = sp->fts_child;
                sp->fts_child = NULL;
                goto name;
        }
 
-       /* Move to next node on this level. */
+       /* Move to the next node on this level. */
 next:  tmp = p;
        if (p = p->fts_link) {
                free(tmp);
 next:  tmp = p;
        if (p = p->fts_link) {
                free(tmp);
@@ -307,21 +333,32 @@ next:     tmp = p;
                /* If reached the top, load the paths for the next root. */
                if (p->fts_level == FTS_ROOTLEVEL) {
                        fts_load(sp, p);
                /* If reached the top, load the paths for the next root. */
                if (p->fts_level == FTS_ROOTLEVEL) {
                        fts_load(sp, p);
-                       return(sp->fts_cur = p);
+                       return (sp->fts_cur = p);
                }
 
                }
 
-               /* User may have called fts_set on the node. */
+               /*
+                * User may have called fts_set on the node.  If skipped,
+                * ignore.  If followed, get a file descriptor so we can
+                * get back if necessary.
+                */
                if (p->fts_instr == FTS_SKIP)
                        goto next;
                if (p->fts_instr == FTS_FOLLOW) {
                        p->fts_info = fts_stat(sp, p, 1);
                if (p->fts_instr == FTS_SKIP)
                        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_symfd =
+                                   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);
                        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);
+               return (sp->fts_cur = p);
        }
 
        /* Move up to the parent node. */
        }
 
        /* Move up to the parent node. */
@@ -335,40 +372,39 @@ name:             t = sp->fts_path + NAPPEND(p->fts_parent);
                 */
                free(p);
                errno = 0;
                 */
                free(p);
                errno = 0;
-               return(sp->fts_cur = NULL);
+               return (sp->fts_cur = NULL);
        }
 
        }
 
+       /* Nul terminate the pathname. */
        sp->fts_path[p->fts_pathlen] = '\0';
 
        /*
        sp->fts_path[p->fts_pathlen] = '\0';
 
        /*
-        * Cd back up to the parent directory.  If at a root node, have to cd
-        * back to the original place, otherwise may not be able to access the
-        * original node on post-order.
+        * Return to the parent directory.  If at a root node or came through
+        * a symlink, go back through the file descriptor.  Otherwise, cd up
+        * one directory.
         */
        if (p->fts_level == FTS_ROOTLEVEL) {
         */
        if (p->fts_level == FTS_ROOTLEVEL) {
-               if (FCHDIR(sp, sp->fts_rfd)) {
+               if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) {
                        SET(FTS_STOP);
                        SET(FTS_STOP);
-                       return(NULL);
+                       return (NULL);
+               }
+       } else if (p->fts_flags & FTS_SYMFOLLOW) {
+               if (FCHDIR(sp, p->fts_symfd)) {
+                       saved_errno = errno;
+                       (void)close(p->fts_symfd);
+                       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);
                }
        }
                }
        }
-       else if (CHDIR(sp, "..")) {
-               SET(FTS_STOP);
-               return(NULL);
-       }
-
-       /* 
-        * If had a chdir error when trying to get into the directory, set the
-        * info field to reflect this, and restore errno.  The error indicator
-        * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
-        * works.
-        */
-       if (p->fts_cderr) {
-               errno = p->fts_cderr;
-               p->fts_cderr = 0;
-               p->fts_info = FTS_ERR;
-       } else
-               p->fts_info = FTS_DP;
-       return(sp->fts_cur = p);
+       p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
+       return (sp->fts_cur = p);
 }
 
 /*
 }
 
 /*
@@ -378,39 +414,69 @@ name:             t = sp->fts_path + NAPPEND(p->fts_parent);
  * reasons.
  */
 /* ARGSUSED */
  * reasons.
  */
 /* ARGSUSED */
+int
 fts_set(sp, p, instr)
        FTS *sp;
        FTSENT *p;
        int instr;
 {
 fts_set(sp, p, instr)
        FTS *sp;
        FTSENT *p;
        int instr;
 {
+       if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
+           instr != FTS_NOINSTR && instr != FTS_SKIP) {
+               errno = EINVAL;
+               return (1);
+       }
        p->fts_instr = instr;
        p->fts_instr = instr;
-       return(0);
+       return (0);
 }
 
 FTSENT *
 }
 
 FTSENT *
-fts_children(sp)
+fts_children(sp, instr)
        register FTS *sp;
        register FTS *sp;
+       int instr;
 {
        register FTSENT *p;
        int fd;
 
 {
        register FTSENT *p;
        int fd;
 
+       if (instr && instr != FTS_NAMEONLY) {
+               errno = EINVAL;
+               return (NULL);
+       }
+
        /* Set current node pointer. */
        p = sp->fts_cur;
 
        /*
        /* Set current node pointer. */
        p = sp->fts_cur;
 
        /*
-        * Set errno to 0 so that user can tell the difference between an
-        * error and a directory without entries.  If not a directory being
-        * visited in *pre-order*, or we've already had fatal errors, return
-        * immediately.
+        * Errno set to 0 so user can distinguish empty directory from
+        * an error.
         */
        errno = 0;
         */
        errno = 0;
-       if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
-               return(NULL);
+
+       /* Fatal errors stop here. */
+       if (ISSET(FTS_STOP))
+               return (NULL);
+
+       /* Return logical hierarchy of user's arguments. */
+       if (p->fts_info == FTS_INIT)
+               return (p->fts_link);
+
+       /*
+        * If not a directory being visited in pre-order, stop here.  Could
+        * allow FTS_DNR, assuming the user has fixed the problem, but the
+        * same effect is available with FTS_AGAIN.
+        */
+       if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
+               return (NULL);
 
        /* Free up any previous child list. */
        if (sp->fts_child)
                fts_lfree(sp->fts_child);
 
 
        /* Free up any previous child list. */
        if (sp->fts_child)
                fts_lfree(sp->fts_child);
 
+       if (instr == FTS_NAMEONLY) {
+               sp->fts_options |= FTS_NAMEONLY;
+               instr = BNAMES;
+       } else 
+               instr = BCHILD;
+
        /*
         * If using chdir on a relative path and called BEFORE fts_read does
         * its chdir to the root of a traversal, we can lose -- we need to
        /*
         * If using chdir on a relative path and called BEFORE fts_read does
         * its chdir to the root of a traversal, we can lose -- we need to
@@ -420,15 +486,15 @@ fts_children(sp)
         */
        if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
            ISSET(FTS_NOCHDIR))
         */
        if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
            ISSET(FTS_NOCHDIR))
-               return(sp->fts_child = fts_build(sp, BCHILD));
+               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, BCHILD);
+               return (NULL);
+       sp->fts_child = fts_build(sp, instr);
        if (fchdir(fd))
        if (fchdir(fd))
-               return(NULL);
+               return (NULL);
        (void)close(fd);
        (void)close(fd);
-       return(sp->fts_child);
+       return (sp->fts_child);
 }
 
 /*
 }
 
 /*
@@ -443,8 +509,6 @@ fts_children(sp)
  * directories and for any nodes after the directories in the parent node have
  * been found.  This empirically cuts the stat calls by about 2/3.
  */
  * directories and for any nodes after the directories in the parent node have
  * been found.  This empirically cuts the stat calls by about 2/3.
  */
-#define        ISDOT(a)        (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
-
 static FTSENT *
 fts_build(sp, type)
        register FTS *sp;
 static FTSENT *
 fts_build(sp, type)
        register FTS *sp;
@@ -453,9 +517,10 @@ fts_build(sp, type)
        register struct dirent *dp;
        register FTSENT *p, *head;
        register int nitems;
        register struct dirent *dp;
        register FTSENT *p, *head;
        register int nitems;
-       FTSENT *cur;
+       FTSENT *cur, *tail;
        DIR *dirp;
        DIR *dirp;
-       int cderr, descend, len, level, maxlen, nlinks, saved_errno;
+       void *adjaddr;
+       int cderrno, descend, len, level, maxlen, nlinks, saved_errno;
        char *cp;
 
        /* Set current node pointer. */
        char *cp;
 
        /* Set current node pointer. */
@@ -465,10 +530,12 @@ fts_build(sp, type)
         * Open the directory for reading.  If this fails, we're done.
         * If being called from fts_read, set the fts_info field.
         */
         * 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))) {
-               if (type == BREAD)
+       if ((dirp = opendir(cur->fts_accpath)) == NULL) {
+               if (type == BREAD) {
                        cur->fts_info = FTS_DNR;
                        cur->fts_info = FTS_DNR;
-               return(NULL);
+                       cur->fts_errno = errno;
+               }
+               return (NULL);
        }
 
        /*
        }
 
        /*
@@ -476,31 +543,43 @@ fts_build(sp, type)
         * 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.
         */
         * 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.
         */
-       nlinks =
-           ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
-           cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
+       if (type == BNAMES)
+               nlinks = 0;
+       else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL))
+               nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
+       else
+               nlinks = -1;
 
 
+#ifdef notdef
+       (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
+       (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
+           ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
+#endif
        /*
         * If we're going to need to stat anything or we want to descend
        /*
         * If we're going to need to stat anything or we want to descend
-        * and stay in the directory, chdir.  If this fails we keep going.
+        * and stay in the directory, chdir.  If this fails we keep going,
+        * but set a flag so we don't chdir after the post-order visit.
         * We won't be able to stat anything, but we can still return the
         * names themselves.  Note, that since fts_read won't be able to
         * chdir into the directory, it will have to return different path
         * names than before, i.e. "a/b" instead of "b".  Since the node
         * has already been visited in pre-order, have to wait until the
         * We won't be able to stat anything, but we can still return the
         * names themselves.  Note, that since fts_read won't be able to
         * chdir into the directory, it will have to return different path
         * names than before, i.e. "a/b" instead of "b".  Since the node
         * has already been visited in pre-order, have to wait until the
-        * post-order visit to return the error.  This is all fairly nasty.
-        * If a program needed sorted entries or stat information, they had
-        * better be checking FTS_NS on the returned nodes.
+        * post-order visit to return the error.  There is a special case
+        * here, if there was nothing to stat then it's not an error to
+        * not be able to stat.  This is all fairly nasty.  If a program
+        * needed sorted entries or stat information, they had better be
+        * checking FTS_NS on the returned nodes.
         */
        if (nlinks || type == BREAD)
                if (FCHDIR(sp, dirfd(dirp))) {
         */
        if (nlinks || type == BREAD)
                if (FCHDIR(sp, dirfd(dirp))) {
-                       if (type == BREAD)
-                               cur->fts_cderr = errno;
-                       descend = nlinks = 0;
-                       cderr = 1;
+                       if (nlinks && type == BREAD)
+                               cur->fts_errno = errno;
+                       cur->fts_flags |= FTS_DONTCHDIR;
+                       descend = 0;
+                       cderrno = errno;
                } else {
                        descend = 1;
                } else {
                        descend = 1;
-                       cderr = 0;
+                       cderrno = 0;
                }
        else
                descend = 0;
                }
        else
                descend = 0;
@@ -512,8 +591,8 @@ fts_build(sp, type)
         * could do them in fts_read before returning the path, but it's a
         * lot easier here since the length is part of the dirent structure.
         *
         * could do them in fts_read before returning the path, but it's a
         * lot easier here since the length is part of the dirent structure.
         *
-        * If not changing directories set a pointer so that we can just
-        * append each new name into the path.
+        * 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);
         */
        maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
        len = NAPPEND(cur);
@@ -525,14 +604,15 @@ fts_build(sp, type)
        level = cur->fts_level + 1;
 
        /* Read the directory, attaching each entry to the `link' pointer. */
        level = cur->fts_level + 1;
 
        /* Read the directory, attaching each entry to the `link' pointer. */
-       for (head = NULL, nitems = 0; dp = readdir(dirp);) {
+       adjaddr = NULL;
+       for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
                if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
                        continue;
 
                if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
                        continue;
 
-               if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
+               if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
                        goto mem1;
                if (dp->d_namlen > maxlen) {
                        goto mem1;
                if (dp->d_namlen > maxlen) {
-                       if (!fts_path(sp, (int)dp->d_namlen)) {
+                       if (fts_palloc(sp, (size_t)dp->d_namlen)) {
                                /*
                                 * No more memory for path or structures.  Save
                                 * errno, free up the current structure and the
                                /*
                                 * No more memory for path or structures.  Save
                                 * errno, free up the current structure and the
@@ -546,8 +626,9 @@ mem1:                               saved_errno = errno;
                                errno = saved_errno;
                                cur->fts_info = FTS_ERR;
                                SET(FTS_STOP);
                                errno = saved_errno;
                                cur->fts_info = FTS_ERR;
                                SET(FTS_STOP);
-                               return(NULL);
+                               return (NULL);
                        }
                        }
+                       adjaddr = sp->fts_path;
                        maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
                }
 
                        maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
                }
 
@@ -555,7 +636,14 @@ mem1:                              saved_errno = errno;
                p->fts_parent = sp->fts_cur;
                p->fts_level = level;
 
                p->fts_parent = sp->fts_cur;
                p->fts_level = level;
 
-               if (nlinks) {
+               if (cderrno) {
+                       if (nlinks) {
+                               p->fts_info = FTS_NS;
+                               p->fts_errno = cderrno;
+                       } else
+                               p->fts_info = FTS_NSOK;
+                       p->fts_accpath = cur->fts_accpath;
+               } else if (nlinks) {
                        /* Build a file name for fts_stat to stat. */
                        if (ISSET(FTS_NOCHDIR)) {
                                p->fts_accpath = p->fts_path;
                        /* Build a file name for fts_stat to stat. */
                        if (ISSET(FTS_NOCHDIR)) {
                                p->fts_accpath = p->fts_path;
@@ -563,23 +651,34 @@ mem1:                             saved_errno = errno;
                        } else
                                p->fts_accpath = p->fts_name;
                        p->fts_info = fts_stat(sp, p, 0);
                        } else
                                p->fts_accpath = p->fts_name;
                        p->fts_info = fts_stat(sp, p, 0);
-                       if (nlinks > 0 && p->fts_info == FTS_D)
+                       if (nlinks > 0 && (p->fts_info == FTS_D ||
+                           p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
                                --nlinks;
                                --nlinks;
-               } else if (cderr) {
-                       p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
-                       p->fts_accpath = cur->fts_accpath;
                } else {
                        p->fts_accpath =
                            ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
                        p->fts_info = FTS_NSOK;
                }
 
                } else {
                        p->fts_accpath =
                            ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
                        p->fts_info = FTS_NSOK;
                }
 
-               p->fts_link = head;
-               head = p;
+               /* We walk in directory order so "ls -f" doesn't get upset. */
+               p->fts_link = NULL;
+               if (head == NULL)
+                       head = tail = p;
+               else {
+                       tail->fts_link = p;
+                       tail = p;
+               }
                ++nitems;
        }
        (void)closedir(dirp);
 
                ++nitems;
        }
        (void)closedir(dirp);
 
+       /*
+        * If had to realloc the path, adjust the addresses for the rest
+        * of the tree.
+        */
+       if (adjaddr)
+               fts_padjust(sp, adjaddr);
+
        /*
         * If not changing directories, reset the path back to original
         * state.
        /*
         * If not changing directories, reset the path back to original
         * state.
@@ -593,25 +692,25 @@ mem1:                             saved_errno = errno;
        /*
         * If descended after called from fts_children or called from
         * fts_read and didn't find anything, get back.  If can't get
        /*
         * If descended after called from fts_children or called from
         * fts_read and didn't find anything, get back.  If can't get
-        * back, we're done.
+        * back, done.
         */
        if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
                cur->fts_info = FTS_ERR;
                SET(FTS_STOP);
         */
        if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
                cur->fts_info = FTS_ERR;
                SET(FTS_STOP);
-               return(NULL);
+               return (NULL);
        }
 
        }
 
-       /* If we didn't find anything, just do the post-order visit */
+       /* If didn't find anything, return NULL. */
        if (!nitems) {
                if (type == BREAD)
                        cur->fts_info = FTS_DP;
        if (!nitems) {
                if (type == BREAD)
                        cur->fts_info = FTS_DP;
-               return(NULL);
+               return (NULL);
        }
 
        /* Sort the entries. */
        if (sp->fts_compar && nitems > 1)
                head = fts_sort(sp, head, nitems);
        }
 
        /* Sort the entries. */
        if (sp->fts_compar && nitems > 1)
                head = fts_sort(sp, head, nitems);
-       return(head);
+       return (head);
 }
 
 static u_short
 }
 
 static u_short
@@ -620,62 +719,72 @@ fts_stat(sp, p, follow)
        register FTSENT *p;
        int follow;
 {
        register FTSENT *p;
        int follow;
 {
+       register FTSENT *t;
+       register dev_t dev;
+       register ino_t ino;
+       struct stat *sbp, sb;
        int saved_errno;
 
        int saved_errno;
 
+       /* If user needs stat info, stat buffer already allocated. */
+       sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
+       
        /*
         * If doing a logical walk, or application requested FTS_FOLLOW, do
         * a stat(2).  If that fails, check for a non-existent symlink.  If
        /*
         * If doing a logical walk, or application requested FTS_FOLLOW, do
         * a stat(2).  If that fails, check for a non-existent symlink.  If
-        * fail, return the errno from the stat call.
+        * fail, set the errno from the stat call.
         */
        if (ISSET(FTS_LOGICAL) || follow) {
         */
        if (ISSET(FTS_LOGICAL) || follow) {
-               if (stat(p->fts_accpath, &p->fts_statb)) {
+               if (stat(p->fts_accpath, sbp)) {
                        saved_errno = errno;
                        saved_errno = errno;
-                       if (!lstat(p->fts_accpath, &p->fts_statb)) {
+                       if (!lstat(p->fts_accpath, sbp)) {
                                errno = 0;
                                errno = 0;
-                               return(FTS_SLNONE);
+                               return (FTS_SLNONE);
                        } 
                        } 
-                       errno = saved_errno;
-                       bzero(&p->fts_statb, sizeof(struct stat));
-                       return(FTS_NS);
+                       p->fts_errno = saved_errno;
+                       goto err;
                }
                }
-       } else if (lstat(p->fts_accpath, &p->fts_statb)) {
-               bzero(&p->fts_statb, sizeof(struct stat));
-               return(FTS_NS);
+       } else if (lstat(p->fts_accpath, sbp)) {
+               p->fts_errno = errno;
+err:           bzero(sbp, sizeof(struct stat));
+               return (FTS_NS);
        }
 
        }
 
-       /*
-        * Cycle detection is done as soon as we find a directory.  Detection
-        * is by brute force; if the tree gets deep enough or the number of
-        * symbolic links to directories high enough something faster might
-        * be worthwhile.
-        */
-       if (S_ISDIR(p->fts_statb.st_mode)) {
-               register FTSENT *t;
-               register dev_t dev;
-               register ino_t ino;
-
-               dev = p->fts_statb.st_dev;
-               ino = p->fts_statb.st_ino;
-               for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL;
-                   t = t->fts_parent)
-                       if (ino == t->fts_statb.st_ino &&
-                           dev == t->fts_statb.st_dev) {
-                               sp->fts_savelink = p->fts_link;
-                               p->fts_link = t;
-                               return(FTS_DC);
+       if (S_ISDIR(sbp->st_mode)) {
+               /*
+                * Set the device/inode.  Used to find cycles and check for
+                * crossing mount points.  Also remember the link count, used
+                * in fts_build to limit the number of stat calls.  It is
+                * understood that these fields are only referenced if fts_info
+                * is set to FTS_D.
+                */
+               dev = p->fts_dev = sbp->st_dev;
+               ino = p->fts_ino = sbp->st_ino;
+               p->fts_nlink = sbp->st_nlink;
+
+               if (ISDOT(p->fts_name))
+                       return (FTS_DOT);
+
+               /*
+                * Cycle detection is done by brute force when the directory
+                * is first encountered.  If the tree gets deep enough or the
+                * number of symbolic links to directories is high enough,
+                * something faster might be worthwhile.
+                */
+               for (t = p->fts_parent;
+                   t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
+                       if (ino == t->fts_ino && dev == t->fts_dev) {
+                               p->fts_cycle = t;
+                               return (FTS_DC);
                        }
                        }
-               return(FTS_D);
+               return (FTS_D);
        }
        }
-       if (S_ISLNK(p->fts_statb.st_mode))
-               return(FTS_SL);
-       if (S_ISREG(p->fts_statb.st_mode))
-               return(FTS_F);
-       return(FTS_DEFAULT);
+       if (S_ISLNK(sbp->st_mode))
+               return (FTS_SL);
+       if (S_ISREG(sbp->st_mode))
+               return (FTS_F);
+       return (FTS_DEFAULT);
 }
 
 }
 
-#define        R(type, nelem, ptr) \
-       (type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
-
 static FTSENT *
 fts_sort(sp, head, nitems)
        FTS *sp;
 static FTSENT *
 fts_sort(sp, head, nitems)
        FTS *sp;
@@ -689,14 +798,14 @@ fts_sort(sp, head, nitems)
         * Reassemble the array in the order returned by qsort.  If unable to
         * sort for memory reasons, return the directory entries in their
         * current order.  Allocate enough space for the current needs plus
         * Reassemble the array in the order returned by qsort.  If unable to
         * sort for memory reasons, return the directory entries in their
         * current order.  Allocate enough space for the current needs plus
-        * 40 so we don't realloc one entry at a time.
+        * 40 so don't realloc one entry at a time.
         */
        if (nitems > sp->fts_nitems) {
                sp->fts_nitems = nitems + 40;
         */
        if (nitems > sp->fts_nitems) {
                sp->fts_nitems = nitems + 40;
-               if (!(sp->fts_array =
-                   R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
+               if ((sp->fts_array = realloc(sp->fts_array,
+                   (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
                        sp->fts_nitems = 0;
                        sp->fts_nitems = 0;
-                       return(head);
+                       return (head);
                }
        }
        for (ap = sp->fts_array, p = head; p; p = p->fts_link)
                }
        }
        for (ap = sp->fts_array, p = head; p; p = p->fts_link)
@@ -705,32 +814,45 @@ fts_sort(sp, head, nitems)
        for (head = *(ap = sp->fts_array); --nitems; ++ap)
                ap[0]->fts_link = ap[1];
        ap[0]->fts_link = NULL;
        for (head = *(ap = sp->fts_array); --nitems; ++ap)
                ap[0]->fts_link = ap[1];
        ap[0]->fts_link = NULL;
-       return(head);
+       return (head);
 }
 
 static FTSENT *
 }
 
 static FTSENT *
-fts_alloc(sp, name, len)
+fts_alloc(sp, name, namelen)
        FTS *sp;
        char *name;
        FTS *sp;
        char *name;
-       register int len;
+       register int namelen;
 {
        register FTSENT *p;
 {
        register FTSENT *p;
+       size_t len;
 
        /*
 
        /*
-        * Variable sized structures; the name is the last element so
-        * we allocate enough extra space after the structure to store
-        * it.
+        * The file name is a variable length array and no stat structure is
+        * necessary if the user has set the nostat bit.  Allocate the FTSENT
+        * structure, the file name and the stat structure in one chunk, but
+        * be careful that the stat structure is reasonably aligned.  Since the
+        * fts_name field is declared to be of size 1, the fts_name pointer is
+        * namelen + 2 before the first possible address of the stat structure.
         */
         */
-       if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
-               return(NULL);
-       bcopy(name, p->fts_name, len + 1);
-       p->fts_namelen = len;
+       len = sizeof(FTSENT) + namelen;
+       if (!ISSET(FTS_NOSTAT))
+               len += sizeof(struct stat) + ALIGNBYTES;
+       if ((p = malloc(len)) == NULL)
+               return (NULL);
+
+       /* Copy the name plus the trailing NULL. */
+       bcopy(name, p->fts_name, namelen + 1);
+
+       if (!ISSET(FTS_NOSTAT))
+               p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
+       p->fts_namelen = namelen;
        p->fts_path = sp->fts_path;
        p->fts_path = sp->fts_path;
+       p->fts_errno = 0;
+       p->fts_flags = 0;
        p->fts_instr = FTS_NOINSTR;
        p->fts_instr = FTS_NOINSTR;
-       p->fts_cderr = 0;
        p->fts_number = 0;
        p->fts_pointer = NULL;
        p->fts_number = 0;
        p->fts_pointer = NULL;
-       return(p);
+       return (p);
 }
 
 static void
 }
 
 static void
@@ -747,17 +869,55 @@ fts_lfree(head)
 }
 
 /*
 }
 
 /*
- * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
- * work on any tree.  Most systems will allow creation of paths much longer
- * than MAXPATHLEN, even though the kernel won't resolve them.  Add an extra
- * 128 bytes to the requested size so that we don't realloc the path 2 bytes
- * at a time.
+ * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
+ * Most systems will allow creation of paths much longer than MAXPATHLEN, even
+ * though the kernel won't resolve them.  Add the size (not just what's needed)
+ * plus 256 bytes so don't realloc the path 2 bytes at a time. 
  */
  */
-static char *
-fts_path(sp, size)
+static int
+fts_palloc(sp, more)
        FTS *sp;
        FTS *sp;
-       int size;
+       size_t more;
 {
 {
-       sp->fts_pathlen += size + 128;
-       return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
+       sp->fts_pathlen += more + 256;
+       sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
+       return (sp->fts_path == NULL);
+}
+
+/*
+ * When the path is realloc'd, have to fix all of the pointers in structures
+ * already returned.
+ */
+static void
+fts_padjust(sp, addr)
+       FTS *sp;
+       void *addr;
+{
+       FTSENT *p;
+
+#define        ADJUST(p) {                                                     \
+       (p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path);   \
+       (p)->fts_path = addr;                                           \
+}
+       /* 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(p);
+               p = p->fts_link ? p->fts_link : p->fts_parent;
+       }
+}
+
+static size_t
+fts_maxarglen(argv)
+       char * const *argv;
+{
+       size_t len, max;
+
+       for (max = 0; *argv; ++argv)
+               if ((len = strlen(*argv)) > max)
+                       max = len;
+       return (max);
 }
 }