1 From a4aa01c128b04c7174d57a28f61656f966d2bd6c Mon Sep 17 00:00:00 2001
2 From: Salvatore Cro <salvatore.cro at st.com>
3 Date: Wed, 20 Apr 2011 10:49:25 +0000
4 Subject: Added fts support for traversing UNIX file hierarchies.
6 It is required by libdwfl in elfutils package.
8 Signed-off-by: Salvatore Cro <salvatore.cro at st.com>
9 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
11 diff --git a/Makefile.in b/Makefile.in
12 index 0c1f414..5e0bfcd 100644
15 @@ -239,6 +239,7 @@ HEADERS_RM-$(UCLIBC_HAS_FLOATS) += complex.h fpu_control.h ieee754.
18 HEADERS_RM-$(findstring y,$(UCLIBC_HAS_FTW)$(UCLIBC_HAS_NFTW)) += ftw.h
19 +HEADERS_RM-$(UCLIBC_HAS_FTS) += fts.h
20 HEADERS_RM-$(UCLIBC_HAS_GETTEXT_AWARENESS) += libintl.h
21 HEADERS_RM-$(UCLIBC_HAS_GLIBC_CUSTOM_PRINTF) += printf.h
22 HEADERS_RM-$(UCLIBC_HAS_GLOB) += glob.h
23 diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
24 index 8628f28..ca51aa0 100644
25 --- a/extra/Configs/Config.in
26 +++ b/extra/Configs/Config.in
27 @@ -1904,6 +1904,18 @@ config UCLIBC_HAS_FTW
28 This interface is rarely used, and adds around 4.5k. Unless you have
29 a pressing need for ftw(), you should probably answer N.
31 +config UCLIBC_HAS_FTS
32 + bool "Support the fts() interface (bsd-compat)"
35 + The fts functions are provided for traversing UNIX file hierarchies.
37 + This interface is currently used by the elfutils and adds
39 + You should port your application to use the POSIX nftw()
42 + Unless you need to build/use elfutils, you should prolly answer N.
44 config UCLIBC_HAS_GLOB
45 bool "Support the glob() interface"
46 diff --git a/include/fts.h b/include/fts.h
48 index 0000000..0a070ba
53 + * Copyright (c) 1989, 1993
54 + * The Regents of the University of California. All rights reserved.
56 + * Redistribution and use in source and binary forms, with or without
57 + * modification, are permitted provided that the following conditions
59 + * 1. Redistributions of source code must retain the above copyright
60 + * notice, this list of conditions and the following disclaimer.
61 + * 2. Redistributions in binary form must reproduce the above copyright
62 + * notice, this list of conditions and the following disclaimer in the
63 + * documentation and/or other materials provided with the distribution.
64 + * 4. Neither the name of the University nor the names of its contributors
65 + * may be used to endorse or promote products derived from this software
66 + * without specific prior written permission.
68 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 + * @(#)fts.h 8.3 (Berkeley) 8/14/94
86 +#include <features.h>
87 +#include <sys/types.h>
89 +/* The fts interface is incompatible with the LFS interface which
90 + transparently uses the 64-bit file access functions. */
91 +#ifdef __USE_FILE_OFFSET64
92 +# error "<fts.h> cannot be used with -D_FILE_OFFSET_BITS==64"
97 + struct _ftsent *fts_cur; /* current node */
98 + struct _ftsent *fts_child; /* linked list of children */
99 + struct _ftsent **fts_array; /* sort array */
100 + dev_t fts_dev; /* starting device # */
101 + char *fts_path; /* path for this descent */
102 + int fts_rfd; /* fd for root */
103 + int fts_pathlen; /* sizeof(path) */
104 + int fts_nitems; /* elements in the sort array */
105 + int (*fts_compar) (const void *, const void *); /* compare fn */
107 +#define FTS_COMFOLLOW 0x0001 /* follow command line symlinks */
108 +#define FTS_LOGICAL 0x0002 /* logical walk */
109 +#define FTS_NOCHDIR 0x0004 /* don't change directories */
110 +#define FTS_NOSTAT 0x0008 /* don't get stat info */
111 +#define FTS_PHYSICAL 0x0010 /* physical walk */
112 +#define FTS_SEEDOT 0x0020 /* return dot and dot-dot */
113 +#define FTS_XDEV 0x0040 /* don't cross devices */
114 +#define FTS_WHITEOUT 0x0080 /* return whiteout information */
115 +#define FTS_OPTIONMASK 0x00ff /* valid user option mask */
117 +#define FTS_NAMEONLY 0x0100 /* (private) child names only */
118 +#define FTS_STOP 0x0200 /* (private) unrecoverable error */
119 + int fts_options; /* fts_open options, global flags */
122 +typedef struct _ftsent {
123 + struct _ftsent *fts_cycle; /* cycle node */
124 + struct _ftsent *fts_parent; /* parent directory */
125 + struct _ftsent *fts_link; /* next file in directory */
126 + long fts_number; /* local numeric value */
127 + void *fts_pointer; /* local address value */
128 + char *fts_accpath; /* access path */
129 + char *fts_path; /* root path */
130 + int fts_errno; /* errno for this node */
131 + int fts_symfd; /* fd for symlink */
132 + u_short fts_pathlen; /* strlen(fts_path) */
133 + u_short fts_namelen; /* strlen(fts_name) */
135 + ino_t fts_ino; /* inode */
136 + dev_t fts_dev; /* device */
137 + nlink_t fts_nlink; /* link count */
139 +#define FTS_ROOTPARENTLEVEL -1
140 +#define FTS_ROOTLEVEL 0
141 + short fts_level; /* depth (-1 to N) */
143 +#define FTS_D 1 /* preorder directory */
144 +#define FTS_DC 2 /* directory that causes cycles */
145 +#define FTS_DEFAULT 3 /* none of the above */
146 +#define FTS_DNR 4 /* unreadable directory */
147 +#define FTS_DOT 5 /* dot or dot-dot */
148 +#define FTS_DP 6 /* postorder directory */
149 +#define FTS_ERR 7 /* error; errno is set */
150 +#define FTS_F 8 /* regular file */
151 +#define FTS_INIT 9 /* initialized only */
152 +#define FTS_NS 10 /* stat(2) failed */
153 +#define FTS_NSOK 11 /* no stat(2) requested */
154 +#define FTS_SL 12 /* symbolic link */
155 +#define FTS_SLNONE 13 /* symbolic link without target */
156 +#define FTS_W 14 /* whiteout object */
157 + u_short fts_info; /* user flags for FTSENT structure */
159 +#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */
160 +#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */
161 + u_short fts_flags; /* private flags for FTSENT structure */
163 +#define FTS_AGAIN 1 /* read node again */
164 +#define FTS_FOLLOW 2 /* follow symbolic link */
165 +#define FTS_NOINSTR 3 /* no instructions */
166 +#define FTS_SKIP 4 /* discard node */
167 + u_short fts_instr; /* fts_set() instructions */
169 + struct stat *fts_statp; /* stat(2) information */
170 + char fts_name[1]; /* file name */
174 +FTSENT *fts_children (FTS *, int);
175 +int fts_close (FTS *);
176 +FTS *fts_open (char * const *, int,
177 + int (*)(const FTSENT **, const FTSENT **));
178 +FTSENT *fts_read (FTS *);
179 +int fts_set (FTS *, FTSENT *, int) __THROW;
183 diff --git a/libc/misc/Makefile.in b/libc/misc/Makefile.in
184 index 6c09d31..e01b3dc 100644
185 --- a/libc/misc/Makefile.in
186 +++ b/libc/misc/Makefile.in
187 @@ -18,6 +18,7 @@ @@ include $(top_srcdir)libc/misc/elf/Makefile.in
188 include $(top_srcdir)libc/misc/file/Makefile.in
189 include $(top_srcdir)libc/misc/fnmatch/Makefile.in
190 include $(top_srcdir)libc/misc/ftw/Makefile.in
191 +include $(top_srcdir)libc/misc/fts/Makefile.in
192 include $(top_srcdir)libc/misc/glob/Makefile.in
193 include $(top_srcdir)libc/misc/gnu/Makefile.in
194 include $(top_srcdir)libc/misc/internals/Makefile.in
195 diff --git a/libc/misc/fts/Makefile b/libc/misc/fts/Makefile
197 index 0000000..1361db3
198 --- a/libc/misc/fts/Makefile
199 +++ b/libc/misc/fts/Makefile
201 +# Makefile for uClibc
203 +# Copyright (C) 2009 STMicroelectronics Ltd.
204 +# Author: Salvatore Cro <salvatore.cro at st.com>
206 +# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
209 +top_srcdir=../../../
210 +top_builddir=../../../
212 +include $(top_builddir)Rules.mak
214 +include $(top_srcdir)Makerules
215 diff --git a/libc/misc/fts/Makefile.in b/libc/misc/fts/Makefile.in
217 index 0000000..a1d0efa
218 --- a/libc/misc/fts/Makefile.in
219 +++ b/libc/misc/fts/Makefile.in
221 +# FTS Makefile for uClibc
223 +# Copyright (C) 2009 STMicroelectronics Ltd.
224 +# Author: Salvatore Cro <salvatore.cro at st.com>
226 +# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
229 +subdirs += libc/misc/fts
232 +MISC_FTS_DIR := $(top_srcdir)libc/misc/fts
233 +MISC_FTS_OUT := $(top_builddir)libc/misc/fts
235 +MISC_FTS_SRC := $(patsubst %.c,$(MISC_FTS_DIR)/%.c,$(CSRC))
236 +MISC_FTS_OBJ := $(patsubst %.c,$(MISC_FTS_OUT)/%.o,$(CSRC))
238 +libc-$(UCLIBC_HAS_FTS) += $(MISC_FTS_OBJ)
240 +objclean-y += CLEAN_libc/misc/fts
242 +CLEAN_libc/misc/fts:
243 + $(do_rm) $(addprefix $(MISC_FTS_OUT)/*., o os)
244 diff --git a/libc/misc/fts/fts.c b/libc/misc/fts/fts.c
246 index 0000000..ce5d158
247 --- a/libc/misc/fts/fts.c
248 +++ b/libc/misc/fts/fts.c
251 + * Copyright (c) 1990, 1993, 1994
252 + * The Regents of the University of California. All rights reserved.
254 + * Redistribution and use in source and binary forms, with or without
255 + * modification, are permitted provided that the following conditions
257 + * 1. Redistributions of source code must retain the above copyright
258 + * notice, this list of conditions and the following disclaimer.
259 + * 2. Redistributions in binary form must reproduce the above copyright
260 + * notice, this list of conditions and the following disclaimer in the
261 + * documentation and/or other materials provided with the distribution.
262 + * 4. Neither the name of the University nor the names of its contributors
263 + * may be used to endorse or promote products derived from this software
264 + * without specific prior written permission.
266 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
267 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
269 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
270 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
272 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
273 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
275 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279 +#include <sys/param.h>
280 +#include <sys/stat.h>
289 +#ifdef __UCLIBC_HAS_LFS__
290 +# include <_lfs_64.h>
292 +# define stat64 stat
293 +# define fstat64 fstat
296 +/* Largest alignment size needed, minus one.
297 + Usually long double is the worst case. */
299 +#define ALIGNBYTES (__alignof__ (long double) - 1)
301 +/* Align P to that size. */
303 +#define ALIGN(p) (((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES)
307 +static FTSENT *fts_alloc (FTS *, const char *, size_t) internal_function;
308 +static FTSENT *fts_build (FTS *, int) internal_function;
309 +static void fts_lfree (FTSENT *) internal_function;
310 +static void fts_load (FTS *, FTSENT *) internal_function;
311 +static size_t fts_maxarglen (char * const *) internal_function;
312 +static void fts_padjust (FTS *, FTSENT *) internal_function;
313 +static int fts_palloc (FTS *, size_t) internal_function;
314 +static FTSENT *fts_sort (FTS *, FTSENT *, int) internal_function;
315 +static u_short fts_stat (FTS *, FTSENT *, int) internal_function;
316 +static int fts_safe_changedir (FTS *, FTSENT *, int, const char *)
320 +#define MAX(a, b) ({ __typeof__ (a) _a = (a); \
321 + __typeof__ (b) _b = (b); \
322 + _a > _b ? _a : _b; })
325 +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
327 +#define CLR(opt) (sp->fts_options &= ~(opt))
328 +#define ISSET(opt) (sp->fts_options & (opt))
329 +#define SET(opt) (sp->fts_options |= (opt))
331 +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
333 +/* fts_build flags */
334 +#define BCHILD 1 /* fts_children */
335 +#define BNAMES 2 /* fts_children, names only */
336 +#define BREAD 3 /* fts_read */
339 +fts_open(argv, options, compar)
340 + char * const *argv;
341 + register int options;
342 + int (*compar) (const FTSENT **, const FTSENT **);
345 + register FTSENT *p, *root;
346 + register int nitems;
347 + FTSENT *parent = NULL;
348 + FTSENT *tmp = NULL;
350 + /* Options check. */
351 + if (options & ~FTS_OPTIONMASK) {
352 + __set_errno (EINVAL);
356 + /* Allocate/initialize the stream */
357 + if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
359 + memset(sp, 0, sizeof(FTS));
360 + sp->fts_compar = (int (*) (const void *, const void *)) compar;
361 + sp->fts_options = options;
363 + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
364 + if (ISSET(FTS_LOGICAL))
368 + * Start out with 1K of path space, and enough, in any case,
369 + * to hold the user's paths.
372 +#define MAXPATHLEN 1024
374 + size_t maxarglen = fts_maxarglen(argv);
375 + if (fts_palloc(sp, MAX(maxarglen, MAXPATHLEN)))
378 + /* Allocate/initialize root's parent. */
379 + if (*argv != NULL) {
380 + if ((parent = fts_alloc(sp, "", 0)) == NULL)
382 + parent->fts_level = FTS_ROOTPARENTLEVEL;
385 + /* Allocate/initialize root(s). */
386 + for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
387 + /* Don't allow zero-length paths. */
388 + size_t len = strlen(*argv);
390 + __set_errno (ENOENT);
394 + p = fts_alloc(sp, *argv, len);
395 + p->fts_level = FTS_ROOTLEVEL;
396 + p->fts_parent = parent;
397 + p->fts_accpath = p->fts_name;
398 + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
400 + /* Command-line "." and ".." are real directories. */
401 + if (p->fts_info == FTS_DOT)
402 + p->fts_info = FTS_D;
405 + * If comparison routine supplied, traverse in sorted
406 + * order; otherwise traverse in the order specified.
409 + p->fts_link = root;
412 + p->fts_link = NULL;
421 + if (compar && nitems > 1)
422 + root = fts_sort(sp, root, nitems);
425 + * Allocate a dummy pointer and make fts_read think that we've just
426 + * finished the node before the root(s); set p->fts_info to FTS_INIT
427 + * so that everything about the "current" node is ignored.
429 + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
431 + sp->fts_cur->fts_link = root;
432 + sp->fts_cur->fts_info = FTS_INIT;
435 + * If using chdir(2), grab a file descriptor pointing to dot to ensure
436 + * that we can get back here; this could be avoided for some paths,
437 + * but almost certainly not worth the effort. Slashes, symbolic links,
438 + * and ".." are all fairly nasty problems. Note, if we can't get the
439 + * descriptor we run anyway, just more slowly.
441 + if (!ISSET(FTS_NOCHDIR)
442 + && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
447 +mem3: fts_lfree(root);
449 +mem2: free(sp->fts_path);
458 + register FTSENT *p;
464 + * Load the stream structure for the next traversal. Since we don't
465 + * actually enter the directory until after the preorder visit, set
466 + * the fts_accpath field specially so the chdir gets done to the right
467 + * place and the user can access the first node. From fts_open it's
468 + * known that the path will fit.
470 + len = p->fts_pathlen = p->fts_namelen;
471 + memmove(sp->fts_path, p->fts_name, len + 1);
472 + if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
473 + len = strlen(++cp);
474 + memmove(p->fts_name, cp, len + 1);
475 + p->fts_namelen = len;
477 + p->fts_accpath = p->fts_path = sp->fts_path;
478 + sp->fts_dev = p->fts_dev;
485 + register FTSENT *freep, *p;
489 + * This still works if we haven't read anything -- the dummy structure
490 + * points to the root list, so we step through to the end of the root
491 + * list which has a valid parent pointer.
494 + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
496 + p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
502 + /* Free up child linked list, sort array, path buffer. */
504 + fts_lfree(sp->fts_child);
505 + free(sp->fts_array);
506 + free(sp->fts_path);
508 + /* Return to original directory, save errno if necessary. */
509 + if (!ISSET(FTS_NOCHDIR)) {
510 + saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
511 + (void)close(sp->fts_rfd);
513 + /* Set errno and return. */
514 + if (saved_errno != 0) {
515 + /* Free up the stream pointer. */
517 + __set_errno (saved_errno);
522 + /* Free up the stream pointer. */
528 + * Special case of "/" at the end of the path so that slashes aren't
529 + * appended which would cause paths to be written as "....//foo".
531 +#define NAPPEND(p) \
532 + (p->fts_path[p->fts_pathlen - 1] == '/' \
533 + ? p->fts_pathlen - 1 : p->fts_pathlen)
539 + register FTSENT *p, *tmp;
540 + register int instr;
544 + /* If finished or unrecoverable error, return NULL. */
545 + if (sp->fts_cur == NULL || ISSET(FTS_STOP))
548 + /* Set current node pointer. */
551 + /* Save and zero out user instructions. */
552 + instr = p->fts_instr;
553 + p->fts_instr = FTS_NOINSTR;
555 + /* Any type of file may be re-visited; re-stat and re-turn. */
556 + if (instr == FTS_AGAIN) {
557 + p->fts_info = fts_stat(sp, p, 0);
562 + * Following a symlink -- SLNONE test allows application to see
563 + * SLNONE and recover. If indirecting through a symlink, have
564 + * keep a pointer to current location. If unable to get that
565 + * pointer, follow fails.
567 + if (instr == FTS_FOLLOW &&
568 + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
569 + p->fts_info = fts_stat(sp, p, 1);
570 + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
571 + if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
572 + p->fts_errno = errno;
573 + p->fts_info = FTS_ERR;
575 + p->fts_flags |= FTS_SYMFOLLOW;
580 + /* Directory in pre-order. */
581 + if (p->fts_info == FTS_D) {
582 + /* If skipped or crossed mount point, do post-order visit. */
583 + if (instr == FTS_SKIP ||
584 + (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
585 + if (p->fts_flags & FTS_SYMFOLLOW)
586 + (void)close(p->fts_symfd);
587 + if (sp->fts_child) {
588 + fts_lfree(sp->fts_child);
589 + sp->fts_child = NULL;
591 + p->fts_info = FTS_DP;
595 + /* Rebuild if only read the names and now traversing. */
596 + if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
598 + fts_lfree(sp->fts_child);
599 + sp->fts_child = NULL;
603 + * Cd to the subdirectory.
605 + * If have already read and now fail to chdir, whack the list
606 + * to make the names come out right, and set the parent errno
607 + * so the application will eventually get an error condition.
608 + * Set the FTS_DONTCHDIR flag so that when we logically change
609 + * directories back to the parent we don't do a chdir.
611 + * If haven't read do so. If the read fails, fts_build sets
612 + * FTS_STOP or the fts_info field of the node.
614 + if (sp->fts_child != NULL) {
615 + if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
616 + p->fts_errno = errno;
617 + p->fts_flags |= FTS_DONTCHDIR;
618 + for (p = sp->fts_child; p != NULL;
621 + p->fts_parent->fts_accpath;
623 + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
624 + if (ISSET(FTS_STOP))
629 + sp->fts_child = NULL;
634 + /* Move to the next node on this level. */
636 + if ((p = p->fts_link) != NULL) {
641 + * If reached the top, return to the original directory (or
642 + * the root of the tree), and load the paths for the next root.
644 + if (p->fts_level == FTS_ROOTLEVEL) {
645 + if (FCHDIR(sp, sp->fts_rfd)) {
654 + * User may have called fts_set on the node. If skipped,
655 + * ignore. If followed, get a file descriptor so we can
656 + * get back if necessary.
658 + if (p->fts_instr == FTS_SKIP)
660 + if (p->fts_instr == FTS_FOLLOW) {
661 + p->fts_info = fts_stat(sp, p, 1);
662 + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
663 + if ((p->fts_symfd =
664 + open(".", O_RDONLY, 0)) < 0) {
665 + p->fts_errno = errno;
666 + p->fts_info = FTS_ERR;
668 + p->fts_flags |= FTS_SYMFOLLOW;
670 + p->fts_instr = FTS_NOINSTR;
673 +name: t = sp->fts_path + NAPPEND(p->fts_parent);
675 + memmove(t, p->fts_name, p->fts_namelen + 1);
679 + /* Move up to the parent node. */
680 + p = tmp->fts_parent;
684 + if (p->fts_level == FTS_ROOTPARENTLEVEL) {
686 + * Done; free everything up and set errno to 0 so the user
687 + * can distinguish between error and EOF.
691 + return (sp->fts_cur = NULL);
694 + /* NUL terminate the pathname. */
695 + sp->fts_path[p->fts_pathlen] = '\0';
698 + * Return to the parent directory. If at a root node or came through
699 + * a symlink, go back through the file descriptor. Otherwise, cd up
702 + if (p->fts_level == FTS_ROOTLEVEL) {
703 + if (FCHDIR(sp, sp->fts_rfd)) {
707 + } else if (p->fts_flags & FTS_SYMFOLLOW) {
708 + if (FCHDIR(sp, p->fts_symfd)) {
709 + saved_errno = errno;
710 + (void)close(p->fts_symfd);
711 + __set_errno (saved_errno);
715 + (void)close(p->fts_symfd);
716 + } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
717 + fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
721 + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
726 + * Fts_set takes the stream as an argument although it's not used in this
727 + * implementation; it would be necessary if anyone wanted to add global
728 + * semantics to fts using fts_set. An error return is allowed for similar
733 +fts_set(sp, p, instr)
738 + if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
739 + instr != FTS_NOINSTR && instr != FTS_SKIP) {
740 + __set_errno (EINVAL);
743 + p->fts_instr = instr;
748 +fts_children(sp, instr)
752 + register FTSENT *p;
755 + if (instr != 0 && instr != FTS_NAMEONLY) {
756 + __set_errno (EINVAL);
760 + /* Set current node pointer. */
764 + * Errno set to 0 so user can distinguish empty directory from
769 + /* Fatal errors stop here. */
770 + if (ISSET(FTS_STOP))
773 + /* Return logical hierarchy of user's arguments. */
774 + if (p->fts_info == FTS_INIT)
775 + return (p->fts_link);
778 + * If not a directory being visited in pre-order, stop here. Could
779 + * allow FTS_DNR, assuming the user has fixed the problem, but the
780 + * same effect is available with FTS_AGAIN.
782 + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
785 + /* Free up any previous child list. */
786 + if (sp->fts_child != NULL)
787 + fts_lfree(sp->fts_child);
789 + if (instr == FTS_NAMEONLY) {
796 + * If using chdir on a relative path and called BEFORE fts_read does
797 + * its chdir to the root of a traversal, we can lose -- we need to
798 + * chdir into the subdirectory, and we don't know where the current
799 + * directory is, so we can't get back so that the upcoming chdir by
800 + * fts_read will work.
802 + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
803 + ISSET(FTS_NOCHDIR))
804 + return (sp->fts_child = fts_build(sp, instr));
806 + if ((fd = open(".", O_RDONLY, 0)) < 0)
808 + sp->fts_child = fts_build(sp, instr);
812 + return (sp->fts_child);
816 + * This is the tricky part -- do not casually change *anything* in here. The
817 + * idea is to build the linked list of entries that are used by fts_children
818 + * and fts_read. There are lots of special cases.
820 + * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
821 + * set and it's a physical walk (so that symbolic links can't be directories),
822 + * we can do things quickly. First, if it's a 4.4BSD file system, the type
823 + * of the file is in the directory entry. Otherwise, we assume that the number
824 + * of subdirectories in a node is equal to the number of links to the parent.
825 + * The former skips all stat calls. The latter skips stat calls in any leaf
826 + * directories and for any files after the subdirectories in the directory have
827 + * been found, cutting the stat calls by about 2/3.
835 + register struct dirent *dp;
836 + register FTSENT *p, *head;
837 + register int nitems;
838 + FTSENT *cur, *tail;
841 + int cderrno, descend, len, level, nlinks, saved_errno,
846 + /* Set current node pointer. */
850 + * Open the directory for reading. If this fails, we're done.
851 + * If being called from fts_read, set the fts_info field.
853 +#if defined FTS_WHITEOUT && 0
854 + if (ISSET(FTS_WHITEOUT))
855 + oflag = DTF_NODUP|DTF_REWIND;
857 + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
859 +# define opendir2(path, flag) opendir(path)
861 + if ((dirp = opendir2(cur->fts_accpath, oflag)) == NULL) {
862 + if (type == BREAD) {
863 + cur->fts_info = FTS_DNR;
864 + cur->fts_errno = errno;
870 + * Nlinks is the number of possible entries of type directory in the
871 + * directory if we're cheating on stat calls, 0 if we're not doing
872 + * any stat calls at all, -1 if we're doing stats on everything.
874 + if (type == BNAMES) {
876 + /* Be quiet about nostat, GCC. */
878 + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
879 + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
887 + (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
888 + (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
889 + ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
892 + * If we're going to need to stat anything or we want to descend
893 + * and stay in the directory, chdir. If this fails we keep going,
894 + * but set a flag so we don't chdir after the post-order visit.
895 + * We won't be able to stat anything, but we can still return the
896 + * names themselves. Note, that since fts_read won't be able to
897 + * chdir into the directory, it will have to return different path
898 + * names than before, i.e. "a/b" instead of "b". Since the node
899 + * has already been visited in pre-order, have to wait until the
900 + * post-order visit to return the error. There is a special case
901 + * here, if there was nothing to stat then it's not an error to
902 + * not be able to stat. This is all fairly nasty. If a program
903 + * needed sorted entries or stat information, they had better be
904 + * checking FTS_NS on the returned nodes.
907 + if (nlinks || type == BREAD) {
908 + if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
909 + if (nlinks && type == BREAD)
910 + cur->fts_errno = errno;
911 + cur->fts_flags |= FTS_DONTCHDIR;
914 + (void)closedir(dirp);
922 + * Figure out the max file name length that can be stored in the
923 + * current path -- the inner loop allocates more path as necessary.
924 + * We really wouldn't have to do the maxlen calculations here, we
925 + * could do them in fts_read before returning the path, but it's a
926 + * lot easier here since the length is part of the dirent structure.
928 + * If not changing directories set a pointer so that can just append
929 + * each new name into the path.
931 + len = NAPPEND(cur);
932 + if (ISSET(FTS_NOCHDIR)) {
933 + cp = sp->fts_path + len;
936 + /* GCC, you're too verbose. */
940 + maxlen = sp->fts_pathlen - len;
942 + level = cur->fts_level + 1;
944 + /* Read the directory, attaching each entry to the `link' pointer. */
946 + for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
947 + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
950 + if ((p = fts_alloc(sp, dp->d_name, _D_EXACT_NAMLEN (dp))) == NULL)
952 + if (_D_EXACT_NAMLEN (dp) >= maxlen) {/* include space for NUL */
953 + oldaddr = sp->fts_path;
954 + if (fts_palloc(sp, _D_EXACT_NAMLEN (dp) + len + 1)) {
956 + * No more memory for path or structures. Save
957 + * errno, free up the current structure and the
958 + * structures already allocated.
960 +mem1: saved_errno = errno;
963 + (void)closedir(dirp);
964 + cur->fts_info = FTS_ERR;
966 + __set_errno (saved_errno);
969 + /* Did realloc() change the pointer? */
970 + if (oldaddr != sp->fts_path) {
972 + if (ISSET(FTS_NOCHDIR))
973 + cp = sp->fts_path + len;
975 + maxlen = sp->fts_pathlen - len;
978 + if (len + _D_EXACT_NAMLEN (dp) >= USHRT_MAX) {
980 + * In an FTSENT, fts_pathlen is a u_short so it is
981 + * possible to wraparound here. If we do, free up
982 + * the current structure and the structures already
983 + * allocated, then error out with ENAMETOOLONG.
987 + (void)closedir(dirp);
988 + cur->fts_info = FTS_ERR;
990 + __set_errno (ENAMETOOLONG);
993 + p->fts_level = level;
994 + p->fts_parent = sp->fts_cur;
995 + p->fts_pathlen = len + _D_EXACT_NAMLEN (dp);
997 +#if defined FTS_WHITEOUT && 0
998 + if (dp->d_type == DT_WHT)
999 + p->fts_flags |= FTS_ISW;
1003 + /* Unreachable code. cderrno is only ever set to a nonnull
1004 + value if dirp is closed at the same time. But then we
1005 + cannot enter this loop. */
1008 + p->fts_info = FTS_NS;
1009 + p->fts_errno = cderrno;
1011 + p->fts_info = FTS_NSOK;
1012 + p->fts_accpath = cur->fts_accpath;
1016 +#if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
1018 + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
1022 + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
1023 + p->fts_info = FTS_NSOK;
1025 + /* Build a file name for fts_stat to stat. */
1026 + if (ISSET(FTS_NOCHDIR)) {
1027 + p->fts_accpath = p->fts_path;
1028 + memmove(cp, p->fts_name, p->fts_namelen + 1);
1030 + p->fts_accpath = p->fts_name;
1032 + p->fts_info = fts_stat(sp, p, 0);
1034 + /* Decrement link count if applicable. */
1035 + if (nlinks > 0 && (p->fts_info == FTS_D ||
1036 + p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
1040 + /* We walk in directory order so "ls -f" doesn't get upset. */
1041 + p->fts_link = NULL;
1045 + tail->fts_link = p;
1051 + (void)closedir(dirp);
1054 + * If realloc() changed the address of the path, adjust the
1055 + * addresses for the rest of the tree and the dir list.
1058 + fts_padjust(sp, head);
1061 + * If not changing directories, reset the path back to original
1064 + if (ISSET(FTS_NOCHDIR)) {
1065 + if (len == sp->fts_pathlen || nitems == 0)
1071 + * If descended after called from fts_children or after called from
1072 + * fts_read and nothing found, get back. At the root level we use
1073 + * the saved fd; if one of fts_open()'s arguments is a relative path
1074 + * to an empty directory, we wind up here with no other way back. If
1075 + * can't get back, we're done.
1077 + if (descend && (type == BCHILD || !nitems) &&
1078 + (cur->fts_level == FTS_ROOTLEVEL ?
1079 + FCHDIR(sp, sp->fts_rfd) :
1080 + fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
1081 + cur->fts_info = FTS_ERR;
1087 + /* If didn't find anything, return NULL. */
1089 + if (type == BREAD)
1090 + cur->fts_info = FTS_DP;
1095 + /* Sort the entries. */
1096 + if (sp->fts_compar && nitems > 1)
1097 + head = fts_sort(sp, head, nitems);
1103 +fts_stat(sp, p, follow)
1105 + register FTSENT *p;
1108 + register FTSENT *t;
1109 + register dev_t dev;
1110 + register ino_t ino;
1111 + struct stat *sbp, sb;
1114 + /* If user needs stat info, stat buffer already allocated. */
1115 + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
1117 +#if defined FTS_WHITEOUT && 0
1118 + /* check for whiteout */
1119 + if (p->fts_flags & FTS_ISW) {
1121 + memset(sbp, '\0', sizeof (*sbp));
1122 + sbp->st_mode = S_IFWHT;
1129 + * If doing a logical walk, or application requested FTS_FOLLOW, do
1130 + * a stat(2). If that fails, check for a non-existent symlink. If
1131 + * fail, set the errno from the stat call.
1133 + if (ISSET(FTS_LOGICAL) || follow) {
1134 + if (stat(p->fts_accpath, sbp)) {
1135 + saved_errno = errno;
1136 + if (!lstat(p->fts_accpath, sbp)) {
1138 + return (FTS_SLNONE);
1140 + p->fts_errno = saved_errno;
1143 + } else if (lstat(p->fts_accpath, sbp)) {
1144 + p->fts_errno = errno;
1145 +err: memset(sbp, 0, sizeof(struct stat));
1149 + if (S_ISDIR(sbp->st_mode)) {
1151 + * Set the device/inode. Used to find cycles and check for
1152 + * crossing mount points. Also remember the link count, used
1153 + * in fts_build to limit the number of stat calls. It is
1154 + * understood that these fields are only referenced if fts_info
1155 + * is set to FTS_D.
1157 + dev = p->fts_dev = sbp->st_dev;
1158 + ino = p->fts_ino = sbp->st_ino;
1159 + p->fts_nlink = sbp->st_nlink;
1161 + if (ISDOT(p->fts_name))
1165 + * Cycle detection is done by brute force when the directory
1166 + * is first encountered. If the tree gets deep enough or the
1167 + * number of symbolic links to directories is high enough,
1168 + * something faster might be worthwhile.
1170 + for (t = p->fts_parent;
1171 + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
1172 + if (ino == t->fts_ino && dev == t->fts_dev) {
1178 + if (S_ISLNK(sbp->st_mode))
1180 + if (S_ISREG(sbp->st_mode))
1182 + return (FTS_DEFAULT);
1187 +fts_sort(sp, head, nitems)
1190 + register int nitems;
1192 + register FTSENT **ap, *p;
1195 + * Construct an array of pointers to the structures and call qsort(3).
1196 + * Reassemble the array in the order returned by qsort. If unable to
1197 + * sort for memory reasons, return the directory entries in their
1198 + * current order. Allocate enough space for the current needs plus
1199 + * 40 so don't realloc one entry at a time.
1201 + if (nitems > sp->fts_nitems) {
1202 + struct _ftsent **a;
1204 + sp->fts_nitems = nitems + 40;
1205 + if ((a = realloc(sp->fts_array,
1206 + (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
1207 + free(sp->fts_array);
1208 + sp->fts_array = NULL;
1209 + sp->fts_nitems = 0;
1212 + sp->fts_array = a;
1214 + for (ap = sp->fts_array, p = head; p; p = p->fts_link)
1216 + qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
1217 + for (head = *(ap = sp->fts_array); --nitems; ++ap)
1218 + ap[0]->fts_link = ap[1];
1219 + ap[0]->fts_link = NULL;
1225 +fts_alloc(sp, name, namelen)
1230 + register FTSENT *p;
1234 + * The file name is a variable length array and no stat structure is
1235 + * necessary if the user has set the nostat bit. Allocate the FTSENT
1236 + * structure, the file name and the stat structure in one chunk, but
1237 + * be careful that the stat structure is reasonably aligned. Since the
1238 + * fts_name field is declared to be of size 1, the fts_name pointer is
1239 + * namelen + 2 before the first possible address of the stat structure.
1241 + len = sizeof(FTSENT) + namelen;
1242 + if (!ISSET(FTS_NOSTAT))
1243 + len += sizeof(struct stat) + ALIGNBYTES;
1244 + if ((p = malloc(len)) == NULL)
1247 + /* Copy the name and guarantee NUL termination. */
1248 + memmove(p->fts_name, name, namelen);
1249 + p->fts_name[namelen] = '\0';
1251 + if (!ISSET(FTS_NOSTAT))
1252 + p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
1253 + p->fts_namelen = namelen;
1254 + p->fts_path = sp->fts_path;
1257 + p->fts_instr = FTS_NOINSTR;
1258 + p->fts_number = 0;
1259 + p->fts_pointer = NULL;
1266 + register FTSENT *head;
1268 + register FTSENT *p;
1270 + /* Free a linked list of structures. */
1271 + while ((p = head)) {
1272 + head = head->fts_link;
1278 + * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
1279 + * Most systems will allow creation of paths much longer than MAXPATHLEN, even
1280 + * though the kernel won't resolve them. Add the size (not just what's needed)
1281 + * plus 256 bytes so don't realloc the path 2 bytes at a time.
1285 +fts_palloc(sp, more)
1291 + sp->fts_pathlen += more + 256;
1293 + * Check for possible wraparound. In an FTS, fts_pathlen is
1294 + * a signed int but in an FTSENT it is an unsigned short.
1295 + * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
1297 + if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) {
1298 + free(sp->fts_path);
1299 + sp->fts_path = NULL;
1300 + __set_errno (ENAMETOOLONG);
1303 + p = realloc(sp->fts_path, sp->fts_pathlen);
1305 + free(sp->fts_path);
1306 + sp->fts_path = NULL;
1314 + * When the path is realloc'd, have to fix all of the pointers in structures
1315 + * already returned.
1319 +fts_padjust(sp, head)
1324 + char *addr = sp->fts_path;
1326 +#define ADJUST(p) do { \
1327 + if ((p)->fts_accpath != (p)->fts_name) { \
1328 + (p)->fts_accpath = \
1329 + (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
1331 + (p)->fts_path = addr; \
1333 + /* Adjust the current set of children. */
1334 + for (p = sp->fts_child; p; p = p->fts_link)
1337 + /* Adjust the rest of the tree, including the current level. */
1338 + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
1340 + p = p->fts_link ? p->fts_link : p->fts_parent;
1346 +fts_maxarglen(argv)
1347 + char * const *argv;
1351 + for (max = 0; *argv; ++argv)
1352 + if ((len = strlen(*argv)) > max)
1358 + * Change to dir specified by fd or p->fts_accpath without getting
1359 + * tricked by someone changing the world out from underneath us.
1360 + * Assumes p->fts_dev and p->fts_ino are filled in.
1364 +fts_safe_changedir(sp, p, fd, path)
1370 + int ret, oerrno, newfd;
1374 + if (ISSET(FTS_NOCHDIR))
1376 + if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
1378 + if (fstat64(newfd, &sb)) {
1382 + if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
1383 + __set_errno (ENOENT); /* disinformation */
1387 + ret = fchdir(newfd);
1391 + (void)close(newfd);
1392 + __set_errno (oerrno);
1396 cgit v0.9.0.1-2-gef13