normalize routine bug; bug report net2/lib/4
[unix-history] / usr / src / lib / libc / gen / fts.c
index 5a86dcd..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.26 (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>
@@ -23,8 +23,9 @@ 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 size_t   fts_maxarglen __P((char * const *));
 static void     fts_padjust __P((FTS *, void *));
 static void     fts_padjust __P((FTS *, void *));
-static int      fts_palloc __P((FTS *, int));
+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));
 
@@ -37,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)
@@ -48,10 +50,16 @@ 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 */
        if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
                return (NULL);
        /* Allocate/initialize the stream */
        if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
                return (NULL);
@@ -63,28 +71,33 @@ fts_open(argv, options, compar)
        if (ISSET(FTS_LOGICAL))
                SET(FTS_NOCHDIR);
 
        if (ISSET(FTS_LOGICAL))
                SET(FTS_NOCHDIR);
 
+       /*
+        * 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;
+
        /* Allocate/initialize root's parent. */
        if ((parent = fts_alloc(sp, "", 0)) == NULL)
        /* Allocate/initialize root's parent. */
        if ((parent = fts_alloc(sp, "", 0)) == NULL)
-               goto mem1;
+               goto mem2;
        parent->fts_level = FTS_ROOTPARENTLEVEL;
 
        /* Allocate/initialize root(s). */
        parent->fts_level = FTS_ROOTPARENTLEVEL;
 
        /* Allocate/initialize root(s). */
-       for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) {
+       for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
                /* Don't allow zero-length paths. */
                if ((len = strlen(*argv)) == 0) {
                        errno = ENOENT;
                /* Don't allow zero-length paths. */
                if ((len = strlen(*argv)) == 0) {
                        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_info = fts_stat(sp, p, 0);
+               p->fts_accpath = p->fts_name;
+               p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
 
                /* Command-line "." and ".." are real directories. */
 
                /* Command-line "." and ".." are real directories. */
-               p->fts_accpath = p->fts_name;
                if (p->fts_info == FTS_DOT)
                        p->fts_info = FTS_D;
 
                if (p->fts_info == FTS_DOT)
                        p->fts_info = FTS_D;
 
@@ -114,17 +127,10 @@ fts_open(argv, options, compar)
         * so that everything about the "current" node is ignored.
         */
        if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
         * so that everything about the "current" node is ignored.
         */
        if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
-               goto mem2;
+               goto mem3;
        sp->fts_cur->fts_link = root;
        sp->fts_cur->fts_info = FTS_INIT;
 
        sp->fts_cur->fts_link = root;
        sp->fts_cur->fts_info = FTS_INIT;
 
-       /*
-        * Start out with more than 1K of path space, and enough, in any
-        * case, to hold the user's paths.
-        */
-       if (fts_palloc(sp, MAX(maxlen, MAXPATHLEN)))
-               goto mem3;
-
        /*
         * If using chdir(2), grab a file descriptor pointing to dot to insure
         * that we can get back here; this could be avoided for some paths,
        /*
         * If using chdir(2), grab a file descriptor pointing to dot to insure
         * that we can get back here; this could be avoided for some paths,
@@ -137,9 +143,9 @@ fts_open(argv, options, compar)
 
        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);
        return (NULL);
 }
 mem1:  free(sp);
        return (NULL);
 }
@@ -219,8 +225,8 @@ fts_close(sp)
  * Special case a root of "/" so that slashes aren't appended which would
  * cause 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 *
@@ -230,6 +236,7 @@ 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 (sp->fts_cur == NULL || ISSET(FTS_STOP))
 
        /* If finished or unrecoverable error, return NULL. */
        if (sp->fts_cur == NULL || ISSET(FTS_STOP))
@@ -250,11 +257,19 @@ fts_read(sp)
 
        /*
         * 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);
+               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);
        }
 
                return (p);
        }
 
@@ -263,6 +278,8 @@ fts_read(sp)
                /* If skipped or crossed mount point, do post-order visit. */
                if (instr == FTS_SKIP ||
                    ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
                /* If skipped or crossed mount point, do post-order visit. */
                if (instr == FTS_SKIP ||
                    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;
                        if (sp->fts_child) {
                                fts_lfree(sp->fts_child);
                                sp->fts_child = NULL;
@@ -271,32 +288,36 @@ fts_read(sp)
                        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_errno = 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;
                        }
                } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
                                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)) == NULL) {
-                       if ISSET(FTS_STOP)
+                       if (ISSET(FTS_STOP))
                                return (NULL);
                                return (NULL);
-                       if (p->fts_level == FTS_ROOTLEVEL &&
-                           FCHDIR(sp, sp->fts_rfd)) {
-                               SET(FTS_STOP);
-                               return (NULL);
-                       }
                        return (p);
                }
                p = sp->fts_child;
                        return (p);
                }
                p = sp->fts_child;
@@ -304,7 +325,7 @@ fts_read(sp)
                goto name;
        }
 
                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);
@@ -315,11 +336,22 @@ next:     tmp = 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;
                }
 
                        p->fts_instr = FTS_NOINSTR;
                }
 
@@ -343,36 +375,35 @@ name:             t = sp->fts_path + NAPPEND(p->fts_parent);
                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);
+                       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);
                }
        }
                        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_errno) {
-               errno = p->fts_errno;
-               p->fts_errno = 0;
-               p->fts_info = FTS_ERR;
-       } else
-               p->fts_info = FTS_DP;
+       p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
        return (sp->fts_cur = p);
 }
 
        return (sp->fts_cur = p);
 }
 
@@ -389,17 +420,28 @@ fts_set(sp, p, instr)
        FTSENT *p;
        int instr;
 {
        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;
        return (0);
 }
 
 FTSENT *
        p->fts_instr = instr;
        return (0);
 }
 
 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;
 
@@ -417,14 +459,24 @@ fts_children(sp)
        if (p->fts_info == FTS_INIT)
                return (p->fts_link);
 
        if (p->fts_info == FTS_INIT)
                return (p->fts_link);
 
-        /* If not a directory being visited in pre-order, stop here. */
-       if (p->fts_info != FTS_D && p->fts_info != FTS_DNR)
+       /*
+        * 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);
 
                return (NULL);
 
        /* 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
@@ -434,11 +486,11 @@ 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)
                return (NULL);
 
        if ((fd = open(".", O_RDONLY, 0)) < 0)
                return (NULL);
-       sp->fts_child = fts_build(sp, BCHILD);
+       sp->fts_child = fts_build(sp, instr);
        if (fchdir(fd))
                return (NULL);
        (void)close(fd);
        if (fchdir(fd))
                return (NULL);
        (void)close(fd);
@@ -465,10 +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;
        void *adjaddr;
        DIR *dirp;
        void *adjaddr;
-       int cderr, descend, len, level, maxlen, nlinks, saved_errno;
+       int cderrno, descend, len, level, maxlen, nlinks, saved_errno;
        char *cp;
 
        /* Set current node pointer. */
        char *cp;
 
        /* Set current node pointer. */
@@ -491,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_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)
+                       if (nlinks && type == BREAD)
                                cur->fts_errno = errno;
                                cur->fts_errno = errno;
-                       descend = nlinks = 0;
-                       cderr = 1;
+                       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;
@@ -541,14 +605,14 @@ fts_build(sp, type)
 
        /* Read the directory, attaching each entry to the `link' pointer. */
        adjaddr = NULL;
 
        /* Read the directory, attaching each entry to the `link' pointer. */
        adjaddr = NULL;
-       for (head = NULL, nitems = 0; dp = readdir(dirp);) {
+       for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
                if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
                        continue;
 
                if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
                        goto mem1;
                if (dp->d_namlen > maxlen) {
                if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
                        continue;
 
                if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
                        goto mem1;
                if (dp->d_namlen > maxlen) {
-                       if (fts_palloc(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
@@ -572,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;
@@ -583,17 +654,20 @@ mem1:                             saved_errno = errno;
                        if (nlinks > 0 && (p->fts_info == FTS_D ||
                            p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
                                --nlinks;
                        if (nlinks > 0 && (p->fts_info == FTS_D ||
                            p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
                                --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);
@@ -626,7 +700,7 @@ mem1:                               saved_errno = errno;
                return (NULL);
        }
 
                return (NULL);
        }
 
-       /* If 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;
@@ -676,8 +750,6 @@ err:                bzero(sbp, sizeof(struct stat));
        }
 
        if (S_ISDIR(sbp->st_mode)) {
        }
 
        if (S_ISDIR(sbp->st_mode)) {
-               if (ISDOT(p->fts_name))
-                       return (FTS_DOT);
                /*
                 * Set the device/inode.  Used to find cycles and check for
                 * crossing mount points.  Also remember the link count, used
                /*
                 * Set the device/inode.  Used to find cycles and check for
                 * crossing mount points.  Also remember the link count, used
@@ -689,6 +761,9 @@ err:                bzero(sbp, sizeof(struct stat));
                ino = p->fts_ino = sbp->st_ino;
                p->fts_nlink = sbp->st_nlink;
 
                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
                /*
                 * Cycle detection is done by brute force when the directory
                 * is first encountered.  If the tree gets deep enough or the
@@ -743,33 +818,40 @@ fts_sort(sp, head, nitems)
 }
 
 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;
-       int needstat;
+       size_t len;
 
        /*
 
        /*
-        * Variable sized structures.  The stat structure isn't necessary
-        * if the user doesn't need it, and the name is variable length.
-        * Allocate enough extra space after the structure to store them.
+        * 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.
         */
         */
-       needstat = ISSET(FTS_NOSTAT) ? 0 : sizeof(struct stat);
-       if ((p = malloc((size_t)(sizeof(FTSENT) + len + 1 + needstat))) == NULL)
+       len = sizeof(FTSENT) + namelen;
+       if (!ISSET(FTS_NOSTAT))
+               len += sizeof(struct stat) + ALIGNBYTES;
+       if ((p = malloc(len)) == NULL)
                return (NULL);
                return (NULL);
-       bcopy(name, p->fts_name, len + 1);
-       if (needstat)
-               p->fts_statp = (struct stat *)(p->fts_name + len + 1);
-       p->fts_namelen = len;
+
+       /* 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_instr = FTS_NOINSTR;
        p->fts_errno = 0;
        p->fts_errno = 0;
+       p->fts_flags = 0;
+       p->fts_instr = FTS_NOINSTR;
        p->fts_number = 0;
        p->fts_number = 0;
-#ifdef NOT_NECESSARY
        p->fts_pointer = NULL;
        p->fts_pointer = NULL;
-#endif
        return (p);
 }
 
        return (p);
 }
 
@@ -795,9 +877,8 @@ fts_lfree(head)
 static int
 fts_palloc(sp, more)
        FTS *sp;
 static int
 fts_palloc(sp, more)
        FTS *sp;
-       int more;
+       size_t more;
 {
 {
-
        sp->fts_pathlen += more + 256;
        sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
        return (sp->fts_path == NULL);
        sp->fts_pathlen += more + 256;
        sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
        return (sp->fts_path == NULL);
@@ -814,9 +895,9 @@ fts_padjust(sp, addr)
 {
        FTSENT *p;
 
 {
        FTSENT *p;
 
-#define        ADJUST(p) { \
-       (p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \
-       (p)->fts_path = addr; \
+#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 the current set of children. */
        for (p = sp->fts_child; p; p = p->fts_link)
@@ -828,3 +909,15 @@ fts_padjust(sp, addr)
                p = p->fts_link ? p->fts_link : p->fts_parent;
        }
 }
                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);
+}