]> sourceware.org Git - glibc.git/blobdiff - io/fts.c
Linux: Add ppoll fortify symbol for 64 bit time_t (BZ# 29746)
[glibc.git] / io / fts.c
index cf52d9e299b3a28c9886673b7e1128adc566466c..5a0a47a360697217c0fb48fe8148b3dfd63be17b 100644 (file)
--- a/io/fts.c
+++ b/io/fts.c
@@ -1,3 +1,21 @@
+/* File tree traversal functions.
+   Copyright (C) 1994-2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
 /*-
  * Copyright (c) 1990, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  * 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.
@@ -41,6 +55,7 @@ static char sccsid[] = "@(#)fts.c     8.6 (Berkeley) 8/14/94";
 #include <dirent.h>
 #include <errno.h>
 #include <fts.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -53,20 +68,35 @@ static char sccsid[] = "@(#)fts.c   8.6 (Berkeley) 8/14/94";
 #endif
 /* Align P to that size.  */
 #ifndef ALIGN
-#define        ALIGN(p)        (((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES)
+#define        ALIGN(p)        (((uintptr_t) (p) + ALIGNBYTES) & ~ALIGNBYTES)
 #endif
 
 
-static FTSENT  *fts_alloc __P((FTS *, const char *, int)) internal_function;
-static FTSENT  *fts_build __P((FTS *, int)) internal_function;
-static void     fts_lfree __P((FTSENT *)) internal_function;
-static void     fts_load __P((FTS *, FTSENT *));
-static size_t   fts_maxarglen __P((char * const *)) internal_function;
-static void     fts_padjust __P((FTS *, void *)) internal_function;
-static int      fts_palloc __P((FTS *, size_t)) internal_function;
-static FTSENT  *fts_sort __P((FTS *, FTSENT *, int)) internal_function;
-static u_short  fts_stat __P((FTS *, struct dirent *, FTSENT *, int))
-     internal_function;
+/* Support for the LFS API version.  */
+#ifndef FTS_OPEN
+#define FTS_OPEN fts_open
+#define FTS_CLOSE fts_close
+#define FTS_READ fts_read
+#define FTS_SET fts_set
+#define FTS_CHILDREN fts_children
+# define FTSOBJ FTS
+# define FTSENTRY FTSENT
+# define INO_T ino_t
+# define STRUCT_STAT stat
+# define STAT __stat
+# define LSTAT __lstat
+#endif
+
+static FTSENTRY        *fts_alloc (FTSOBJ *, const char *, size_t);
+static FTSENTRY        *fts_build (FTSOBJ *, int);
+static void     fts_lfree (FTSENTRY *);
+static void     fts_load (FTSOBJ *, FTSENTRY *);
+static size_t   fts_maxarglen (char * const *);
+static void     fts_padjust (FTSOBJ *, FTSENTRY *);
+static int      fts_palloc (FTSOBJ *, size_t);
+static FTSENTRY        *fts_sort (FTSOBJ *, FTSENTRY *, int);
+static u_short  fts_stat (FTSOBJ *, FTSENTRY *, int);
+static int      fts_safe_changedir (FTSOBJ *, FTSENTRY *, int, const char *);
 
 #ifndef MAX
 #define MAX(a, b)      ({ __typeof__ (a) _a = (a); \
@@ -76,10 +106,10 @@ static u_short      fts_stat __P((FTS *, struct dirent *, 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 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))
 
 /* fts_build flags */
@@ -87,17 +117,15 @@ static u_short      fts_stat __P((FTS *, struct dirent *, FTSENT *, int))
 #define        BNAMES          2               /* fts_children, names only */
 #define        BREAD           3               /* fts_read */
 
-FTS *
-fts_open(argv, options, compar)
-       char * const *argv;
-       register int options;
-       int (*compar) __P((const FTSENT **, const FTSENT **));
+FTSOBJ *
+FTS_OPEN (char * const *argv, int options,
+         int (*compar) (const FTSENTRY **, const FTSENTRY **))
 {
-       register FTS *sp;
-       register FTSENT *p, *root;
-       register int nitems;
-       FTSENT *parent, *tmp;
-       int len;
+       FTSOBJ *sp;
+       FTSENTRY *p, *root;
+       int nitems;
+       FTSENTRY *parent = NULL;
+       FTSENTRY *tmp;
 
        /* Options check. */
        if (options & ~FTS_OPTIONMASK) {
@@ -106,10 +134,10 @@ fts_open(argv, options, compar)
        }
 
        /* Allocate/initialize the stream */
-       if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
+       if ((sp = malloc((u_int)sizeof(FTSOBJ))) == NULL)
                return (NULL);
-       __bzero(sp, sizeof(FTS));
-       sp->fts_compar = (int (*) __P((const void *, const void *))) compar;
+       memset(sp, 0, sizeof(FTSOBJ));
+       sp->fts_compar = (int (*) (const void *, const void *)) compar;
        sp->fts_options = options;
 
        /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
@@ -123,18 +151,22 @@ fts_open(argv, options, compar)
 #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;
                }
@@ -143,7 +175,7 @@ fts_open(argv, options, compar)
                p->fts_level = FTS_ROOTLEVEL;
                p->fts_parent = parent;
                p->fts_accpath = p->fts_name;
-               p->fts_info = fts_stat(sp, NULL, p, ISSET(FTS_COMFOLLOW));
+               p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
 
                /* Command-line "." and ".." are real directories. */
                if (p->fts_info == FTS_DOT)
@@ -180,7 +212,7 @@ fts_open(argv, options, compar)
        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
@@ -200,12 +232,10 @@ mem1:     free(sp);
 }
 
 static void
-fts_load(sp, p)
-       FTS *sp;
-       register FTSENT *p;
+fts_load (FTSOBJ *sp, FTSENTRY *p)
 {
-       register int len;
-       register char *cp;
+       int len;
+       char *cp;
 
        /*
         * Load the stream structure for the next traversal.  Since we don't
@@ -215,10 +245,10 @@ fts_load(sp, 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;
@@ -226,12 +256,10 @@ fts_load(sp, p)
 }
 
 int
-fts_close(sp)
-       FTS *sp;
+FTS_CLOSE (FTSOBJ *sp)
 {
-       register FTSENT *freep, *p;
+       FTSENTRY *freep, *p;
        int saved_errno;
-       int retval = 0;
 
        /*
         * This still works if we haven't read anything -- the dummy structure
@@ -241,7 +269,7 @@ fts_close(sp)
        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);
@@ -250,43 +278,42 @@ fts_close(sp)
        /* 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);
-       }
 
-       /* Set errno and return. */
-       if (!ISSET(FTS_NOCHDIR) && saved_errno) {
-               __set_errno (saved_errno);
-               retval = -1;
+               /* 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);
-
-       return retval;
+       free(sp);
+       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)
-       register FTS *sp;
+FTSENTRY *
+FTS_READ (FTSOBJ *sp)
 {
-       register FTSENT *p, *tmp;
-       register int instr;
-       register char *t;
+       FTSENTRY *p, *tmp;
+       int instr;
+       char *t;
        int saved_errno;
 
        /* If finished or unrecoverable error, return NULL. */
@@ -302,7 +329,7 @@ fts_read(sp)
 
        /* Any type of file may be re-visited; re-stat and re-turn. */
        if (instr == FTS_AGAIN) {
-               p->fts_info = fts_stat(sp, NULL, p, 0);
+               p->fts_info = fts_stat(sp, p, 0);
                return (p);
        }
 
@@ -314,7 +341,7 @@ fts_read(sp)
         */
        if (instr == FTS_FOLLOW &&
            (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
-               p->fts_info = fts_stat(sp, NULL, p, 1);
+               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;
@@ -341,8 +368,8 @@ fts_read(sp)
                }
 
                /* 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;
                }
@@ -359,11 +386,12 @@ fts_read(sp)
                 * 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;
                        }
@@ -374,25 +402,27 @@ fts_read(sp)
                }
                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;
                }
 
                /*
@@ -403,7 +433,7 @@ next:       tmp = p;
                if (p->fts_instr == FTS_SKIP)
                        goto next;
                if (p->fts_instr == FTS_FOLLOW) {
-                       p->fts_info = fts_stat(sp, NULL, p, 1);
+                       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) {
@@ -417,12 +447,13 @@ next:     tmp = p;
 
 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) {
@@ -435,7 +466,7 @@ name:               t = sp->fts_path + NAPPEND(p->fts_parent);
                return (sp->fts_cur = NULL);
        }
 
-       /* Nul terminate the pathname. */
+       /* NUL terminate the pathname. */
        sp->fts_path[p->fts_pathlen] = '\0';
 
        /*
@@ -444,7 +475,7 @@ name:               t = sp->fts_path + NAPPEND(p->fts_parent);
         * 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);
                }
@@ -457,14 +488,13 @@ name:             t = sp->fts_path + NAPPEND(p->fts_parent);
                        return (NULL);
                }
                (void)__close(p->fts_symfd);
-       } else if (!(p->fts_flags & FTS_DONTCHDIR)) {
-               if (CHDIR(sp, "..")) {
-                       SET(FTS_STOP);
-                       return (NULL);
-               }
+       } 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;
 }
 
 /*
@@ -475,12 +505,9 @@ name:              t = sp->fts_path + NAPPEND(p->fts_parent);
  */
 /* ARGSUSED */
 int
-fts_set(sp, p, instr)
-       FTS *sp;
-       FTSENT *p;
-       int instr;
+FTS_SET (FTSOBJ *sp, FTSENTRY *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);
@@ -489,15 +516,13 @@ fts_set(sp, p, instr)
        return (0);
 }
 
-FTSENT *
-fts_children(sp, instr)
-       register FTS *sp;
-       int instr;
+FTSENTRY *
+FTS_CHILDREN(FTSOBJ *sp, int instr)
 {
-       register FTSENT *p;
+       FTSENTRY *p;
        int fd;
 
-       if (instr && instr != FTS_NAMEONLY) {
+       if (instr != 0 && instr != FTS_NAMEONLY) {
                __set_errno (EINVAL);
                return (NULL);
        }
@@ -528,11 +553,11 @@ fts_children(sp, instr)
                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;
@@ -557,6 +582,16 @@ fts_children(sp, instr)
        return (sp->fts_child);
 }
 
+static inline int
+dirent_not_directory(const struct dirent *dp)
+{
+#if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
+        return dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN;
+#else
+        return 0;
+#endif
+}
+
 /*
  * This is the tricky part -- do not casually change *anything* in here.  The
  * idea is to build the linked list of entries that are used by fts_children
@@ -571,23 +606,19 @@ fts_children(sp, instr)
  * directories and for any files after the subdirectories in the directory have
  * been found, cutting the stat calls by about 2/3.
  */
-static FTSENT *
-internal_function
-fts_build(sp, type)
-       register FTS *sp;
-       int type;
+static FTSENTRY *
+fts_build (FTSOBJ *sp, int type)
 {
        struct dirent *dp;
-       register FTSENT *p, *head;
-       register int nitems;
-       FTSENT *cur, *tail;
+       FTSENTRY *p, *head;
+       int nitems;
+       FTSENTRY *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;
-#ifdef DTF_HIDEW
-       int oflag;
-#endif
 
        /* Set current node pointer. */
        cur = sp->fts_cur;
@@ -596,15 +627,15 @@ 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.
         */
-#ifdef DTF_HIDEW
-       if (ISSET (FTS_WHITEOUT))
+#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 ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
                if (type == BREAD) {
                        cur->fts_info = FTS_DNR;
                        cur->fts_errno = errno;
@@ -617,12 +648,17 @@ 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.
         */
-       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);
@@ -646,12 +682,14 @@ fts_build(sp, type)
         */
        cderrno = 0;
        if (nlinks || type == BREAD) {
-               if (FCHDIR(sp, dirfd(dirp))) {
+               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
@@ -667,54 +705,81 @@ fts_build(sp, type)
         * 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;
-       head = tail = NULL;
-       nitems = 0;
-       while((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);
                                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 (cderrno) {
+#if defined FTS_WHITEOUT && 0
+               if (dp->d_type == DT_WHT)
+                       p->fts_flags |= FTS_ISW;
+#endif
+
+               /* 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;
@@ -722,11 +787,7 @@ mem1:                              saved_errno = errno;
                                p->fts_info = FTS_NSOK;
                        p->fts_accpath = cur->fts_accpath;
                } else if (nlinks == 0
-#if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
-                   || (nlinks > 0 &&
-                       dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
-#endif
-                   ) {
+                           || (nostat && dirent_not_directory(dp))) {
                        p->fts_accpath =
                            ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
                        p->fts_info = FTS_NSOK;
@@ -734,11 +795,11 @@ mem1:                             saved_errno = errno;
                        /* 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. */
-                       p->fts_info = fts_stat(sp, dp, p, 0);
+                       p->fts_info = fts_stat(sp, p, 0);
 
                        /* Decrement link count if applicable. */
                        if (nlinks > 0 && (p->fts_info == FTS_D ||
@@ -756,21 +817,22 @@ mem1:                             saved_errno = errno;
                }
                ++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';
        }
@@ -784,9 +846,11 @@ mem1:                              saved_errno = errno;
         */
        if (descend && (type == BCHILD || !nitems) &&
            (cur->fts_level == FTS_ROOTLEVEL ?
-            FCHDIR (sp, sp->fts_rfd) : CHDIR (sp, ".."))) {
+            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);
        }
 
@@ -794,6 +858,7 @@ mem1:                               saved_errno = errno;
        if (!nitems) {
                if (type == BREAD)
                        cur->fts_info = FTS_DP;
+               fts_lfree(head);
                return (NULL);
        }
 
@@ -804,31 +869,24 @@ mem1:                             saved_errno = errno;
 }
 
 static u_short
-internal_function
-fts_stat(sp, dp, p, follow)
-       FTS *sp;
-       register FTSENT *p;
-       struct dirent *dp;
-       int follow;
+fts_stat (FTSOBJ *sp, FTSENTRY *p, int follow)
 {
-       register FTSENT *t;
-       register dev_t dev;
-       register ino_t ino;
-       struct stat *sbp, sb;
+       FTSENTRY *t;
+       dev_t dev;
+       INO_T ino;
+       struct STRUCT_STAT *sbp, sb;
        int saved_errno;
 
        /* If user needs stat info, stat buffer already allocated. */
        sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
 
-#ifdef DT_WHT
-       /*
-        * Whited-out files don't really exist.  However, there's stat(2) file
-        * mask for them, so we set it so that programs (i.e., find) don't have
-        * to test FTS_W separately from other file types.
-        */
-       if (dp != NULL && dp->d_type == DT_WHT) {
-               memset(sbp, 0, sizeof(struct stat));
-               sbp->st_mode = S_IFWHT;
+#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
@@ -839,18 +897,18 @@ fts_stat(sp, dp, p, follow)
         * fail, set the errno from the stat call.
         */
        if (ISSET(FTS_LOGICAL) || follow) {
-               if (stat(p->fts_accpath, sbp)) {
+               if (STAT(p->fts_accpath, sbp)) {
                        saved_errno = errno;
-                       if (!lstat(p->fts_accpath, sbp)) {
+                       if (!LSTAT(p->fts_accpath, sbp)) {
                                __set_errno (0);
                                return (FTS_SLNONE);
                        }
                        p->fts_errno = saved_errno;
                        goto err;
                }
-       } else if (lstat(p->fts_accpath, sbp)) {
+       } else if (LSTAT(p->fts_accpath, sbp)) {
                p->fts_errno = errno;
-err:           __bzero(sbp, sizeof(struct stat));
+err:           memset(sbp, 0, sizeof(struct STRUCT_STAT));
                return (FTS_NS);
        }
 
@@ -890,14 +948,10 @@ err:              __bzero(sbp, sizeof(struct stat));
        return (FTS_DEFAULT);
 }
 
-static FTSENT *
-internal_function
-fts_sort(sp, head, nitems)
-       FTS *sp;
-       FTSENT *head;
-       register int nitems;
+static FTSENTRY *
+fts_sort (FTSOBJ *sp, FTSENTRY *head, int nitems)
 {
-       register FTSENT **ap, *p;
+       FTSENTRY **ap, *p;
 
        /*
         * Construct an array of pointers to the structures and call qsort(3).
@@ -907,30 +961,31 @@ fts_sort(sp, head, nitems)
         * 40 so don't realloc one entry at a time.
         */
        if (nitems > sp->fts_nitems) {
+               FTSENTRY **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(FTSENTRY *)))) == 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;
-       qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
+       qsort((void *)sp->fts_array, nitems, sizeof(FTSENTRY *), sp->fts_compar);
        for (head = *(ap = sp->fts_array); --nitems; ++ap)
                ap[0]->fts_link = ap[1];
        ap[0]->fts_link = NULL;
        return (head);
 }
 
-static FTSENT *
-internal_function
-fts_alloc(sp, name, namelen)
-       FTS *sp;
-       const char *name;
-       register int namelen;
+static FTSENTRY *
+fts_alloc (FTSOBJ *sp, const char *name, size_t namelen)
 {
-       register FTSENT *p;
+       FTSENTRY *p;
        size_t len;
 
        /*
@@ -941,17 +996,18 @@ fts_alloc(sp, name, namelen)
         * 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.
         */
-       len = sizeof(FTSENT) + namelen;
+       len = sizeof(FTSENTRY) + namelen;
        if (!ISSET(FTS_NOSTAT))
-               len += sizeof(struct stat) + ALIGNBYTES;
+               len += sizeof(struct STRUCT_STAT) + ALIGNBYTES;
        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);
+               p->fts_statp = (struct STRUCT_STAT *)ALIGN(p->fts_name + namelen + 2);
        p->fts_namelen = namelen;
        p->fts_path = sp->fts_path;
        p->fts_errno = 0;
@@ -963,11 +1019,9 @@ fts_alloc(sp, name, namelen)
 }
 
 static void
-internal_function
-fts_lfree(head)
-       register FTSENT *head;
+fts_lfree (FTSENTRY *head)
 {
-       register FTSENT *p;
+       FTSENTRY *p;
 
        /* Free a linked list of structures. */
        while ((p = head)) {
@@ -983,14 +1037,30 @@ fts_lfree(head)
  * 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;
+fts_palloc (FTSOBJ *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;
 }
 
 /*
@@ -998,38 +1068,70 @@ fts_palloc(sp, more)
  * already returned.
  */
 static void
-internal_function
-fts_padjust(sp, addr)
-       FTS *sp;
-       void *addr;
+fts_padjust (FTSOBJ *sp, FTSENTRY *head)
 {
-       FTSENT *p;
-
-#define        ADJUST(p) {                                                     \
-       (p)->fts_accpath =                                              \
-           (char *)addr + ((p)->fts_accpath - (p)->fts_path);          \
+       FTSENTRY *p;
+       char *addr = sp->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;
+fts_maxarglen (char * const *argv)
 {
        size_t len, max;
 
        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
+fts_safe_changedir (FTSOBJ *sp, FTSENTRY *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 (__fstat64(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);
 }
This page took 0.058213 seconds and 5 git commands to generate.