[toolchain] uclibc: backport FTS support
[openwrt/svn-archive/archive.git] / toolchain / uClibc / patches-0.9.32 / 970-uclibc_fts.patch
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.
5
6 It is required by libdwfl in elfutils package.
7
8 Signed-off-by: Salvatore Cro <salvatore.cro at st.com>
9 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
10 ---
11 diff --git a/Makefile.in b/Makefile.in
12 index 0c1f414..5e0bfcd 100644
13 --- a/Makefile.in
14 +++ b/Makefile.in
15 @@ -239,6 +239,7 @@ HEADERS_RM-$(UCLIBC_HAS_FLOATS) += complex.h fpu_control.h ieee754.
16 tgmath.h \
17 bits/math*.h
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 @@ -1898,6 +1898,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.
30
31 +config UCLIBC_HAS_FTS
32 + bool "Support the fts() interface (bsd-compat)"
33 + default n
34 + help
35 + The fts functions are provided for traversing UNIX file hierarchies.
36 +
37 + This interface is currently used by the elfutils and adds
38 + around 7.5k.
39 + You should port your application to use the POSIX nftw()
40 + interface.
41 +
42 + Unless you need to build/use elfutils, you should prolly answer N.
43
44 config UCLIBC_HAS_GLOB
45 bool "Support the glob() interface"
46 diff --git a/include/fts.h b/include/fts.h
47 new file mode 100644
48 index 0000000..0a070ba
49 --- a/dev/null
50 +++ b/include/fts.h
51 @@ -0,0 +1,131 @@
52 +/*
53 + * Copyright (c) 1989, 1993
54 + * The Regents of the University of California. All rights reserved.
55 + *
56 + * Redistribution and use in source and binary forms, with or without
57 + * modification, are permitted provided that the following conditions
58 + * are met:
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.
67 + *
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
78 + * SUCH DAMAGE.
79 + *
80 + * @(#)fts.h 8.3 (Berkeley) 8/14/94
81 + */
82 +
83 +#ifndef _FTS_H
84 +#define _FTS_H 1
85 +
86 +#include <features.h>
87 +#include <sys/types.h>
88 +
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"
93 +#endif
94 +
95 +
96 +typedef struct {
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 */
106 +
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 */
116 +
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 */
120 +} FTS;
121 +
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) */
134 +
135 + ino_t fts_ino; /* inode */
136 + dev_t fts_dev; /* device */
137 + nlink_t fts_nlink; /* link count */
138 +
139 +#define FTS_ROOTPARENTLEVEL -1
140 +#define FTS_ROOTLEVEL 0
141 + short fts_level; /* depth (-1 to N) */
142 +
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 */
158 +
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 */
162 +
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 */
168 +
169 + struct stat *fts_statp; /* stat(2) information */
170 + char fts_name[1]; /* file name */
171 +} FTSENT;
172 +
173 +__BEGIN_DECLS
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;
180 +__END_DECLS
181 +
182 +#endif /* fts.h */
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 @@ -16,6 +16,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
196 new file mode 100644
197 index 0000000..1361db3
198 --- a/dev/null
199 +++ b/libc/misc/fts/Makefile
200 @@ -0,0 +1,14 @@
201 +# Makefile for uClibc
202 +#
203 +# Copyright (C) 2009 STMicroelectronics Ltd.
204 +# Author: Salvatore Cro <salvatore.cro at st.com>
205 +#
206 +# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
207 +#
208 +
209 +top_srcdir=../../../
210 +top_builddir=../../../
211 +all: objs
212 +include $(top_builddir)Rules.mak
213 +include Makefile.in
214 +include $(top_srcdir)Makerules
215 diff --git a/libc/misc/fts/Makefile.in b/libc/misc/fts/Makefile.in
216 new file mode 100644
217 index 0000000..a1d0efa
218 --- a/dev/null
219 +++ b/libc/misc/fts/Makefile.in
220 @@ -0,0 +1,23 @@
221 +# FTS Makefile for uClibc
222 +#
223 +# Copyright (C) 2009 STMicroelectronics Ltd.
224 +# Author: Salvatore Cro <salvatore.cro at st.com>
225 +#
226 +# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
227 +#
228 +
229 +subdirs += libc/misc/fts
230 +CSRC := fts.c
231 +
232 +MISC_FTS_DIR := $(top_srcdir)libc/misc/fts
233 +MISC_FTS_OUT := $(top_builddir)libc/misc/fts
234 +
235 +MISC_FTS_SRC := $(patsubst %.c,$(MISC_FTS_DIR)/%.c,$(CSRC))
236 +MISC_FTS_OBJ := $(patsubst %.c,$(MISC_FTS_OUT)/%.o,$(CSRC))
237 +
238 +libc-$(UCLIBC_HAS_FTS) += $(MISC_FTS_OBJ)
239 +
240 +objclean-y += CLEAN_libc/misc/fts
241 +
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
245 new file mode 100644
246 index 0000000..ce5d158
247 --- a/dev/null
248 +++ b/libc/misc/fts/fts.c
249 @@ -0,0 +1,1145 @@
250 +/*-
251 + * Copyright (c) 1990, 1993, 1994
252 + * The Regents of the University of California. All rights reserved.
253 + *
254 + * Redistribution and use in source and binary forms, with or without
255 + * modification, are permitted provided that the following conditions
256 + * are met:
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.
265 + *
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
276 + * SUCH DAMAGE.
277 + */
278 +
279 +#include <sys/param.h>
280 +#include <sys/stat.h>
281 +#include <fcntl.h>
282 +#include <dirent.h>
283 +#include <errno.h>
284 +#include <fts.h>
285 +#include <stdlib.h>
286 +#include <string.h>
287 +#include <unistd.h>
288 +
289 +#ifdef __UCLIBC_HAS_LFS__
290 +# include <_lfs_64.h>
291 +#else
292 +# define stat64 stat
293 +# define fstat64 fstat
294 +#endif
295 +
296 +/* Largest alignment size needed, minus one.
297 + Usually long double is the worst case. */
298 +#ifndef ALIGNBYTES
299 +#define ALIGNBYTES (__alignof__ (long double) - 1)
300 +#endif
301 +/* Align P to that size. */
302 +#ifndef ALIGN
303 +#define ALIGN(p) (((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES)
304 +#endif
305 +
306 +
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 *)
317 + internal_function;
318 +
319 +#ifndef MAX
320 +#define MAX(a, b) ({ __typeof__ (a) _a = (a); \
321 + __typeof__ (b) _b = (b); \
322 + _a > _b ? _a : _b; })
323 +#endif
324 +
325 +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
326 +
327 +#define CLR(opt) (sp->fts_options &= ~(opt))
328 +#define ISSET(opt) (sp->fts_options & (opt))
329 +#define SET(opt) (sp->fts_options |= (opt))
330 +
331 +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
332 +
333 +/* fts_build flags */
334 +#define BCHILD 1 /* fts_children */
335 +#define BNAMES 2 /* fts_children, names only */
336 +#define BREAD 3 /* fts_read */
337 +
338 +FTS *
339 +fts_open(argv, options, compar)
340 + char * const *argv;
341 + register int options;
342 + int (*compar) (const FTSENT **, const FTSENT **);
343 +{
344 + register FTS *sp;
345 + register FTSENT *p, *root;
346 + register int nitems;
347 + FTSENT *parent = NULL;
348 + FTSENT *tmp = NULL;
349 +
350 + /* Options check. */
351 + if (options & ~FTS_OPTIONMASK) {
352 + __set_errno (EINVAL);
353 + return (NULL);
354 + }
355 +
356 + /* Allocate/initialize the stream */
357 + if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
358 + return (NULL);
359 + memset(sp, 0, sizeof(FTS));
360 + sp->fts_compar = (int (*) (const void *, const void *)) compar;
361 + sp->fts_options = options;
362 +
363 + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
364 + if (ISSET(FTS_LOGICAL))
365 + SET(FTS_NOCHDIR);
366 +
367 + /*
368 + * Start out with 1K of path space, and enough, in any case,
369 + * to hold the user's paths.
370 + */
371 +#ifndef MAXPATHLEN
372 +#define MAXPATHLEN 1024
373 +#endif
374 + size_t maxarglen = fts_maxarglen(argv);
375 + if (fts_palloc(sp, MAX(maxarglen, MAXPATHLEN)))
376 + goto mem1;
377 +
378 + /* Allocate/initialize root's parent. */
379 + if (*argv != NULL) {
380 + if ((parent = fts_alloc(sp, "", 0)) == NULL)
381 + goto mem2;
382 + parent->fts_level = FTS_ROOTPARENTLEVEL;
383 + }
384 +
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);
389 + if (len == 0) {
390 + __set_errno (ENOENT);
391 + goto mem3;
392 + }
393 +
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));
399 +
400 + /* Command-line "." and ".." are real directories. */
401 + if (p->fts_info == FTS_DOT)
402 + p->fts_info = FTS_D;
403 +
404 + /*
405 + * If comparison routine supplied, traverse in sorted
406 + * order; otherwise traverse in the order specified.
407 + */
408 + if (compar) {
409 + p->fts_link = root;
410 + root = p;
411 + } else {
412 + p->fts_link = NULL;
413 + if (root == NULL)
414 + tmp = root = p;
415 + else {
416 + tmp->fts_link = p;
417 + tmp = p;
418 + }
419 + }
420 + }
421 + if (compar && nitems > 1)
422 + root = fts_sort(sp, root, nitems);
423 +
424 + /*
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.
428 + */
429 + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
430 + goto mem3;
431 + sp->fts_cur->fts_link = root;
432 + sp->fts_cur->fts_info = FTS_INIT;
433 +
434 + /*
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.
440 + */
441 + if (!ISSET(FTS_NOCHDIR)
442 + && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
443 + SET(FTS_NOCHDIR);
444 +
445 + return (sp);
446 +
447 +mem3: fts_lfree(root);
448 + free(parent);
449 +mem2: free(sp->fts_path);
450 +mem1: free(sp);
451 + return (NULL);
452 +}
453 +
454 +static void
455 +internal_function
456 +fts_load(sp, p)
457 + FTS *sp;
458 + register FTSENT *p;
459 +{
460 + register int len;
461 + register char *cp;
462 +
463 + /*
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.
469 + */
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;
476 + }
477 + p->fts_accpath = p->fts_path = sp->fts_path;
478 + sp->fts_dev = p->fts_dev;
479 +}
480 +
481 +int
482 +fts_close(sp)
483 + FTS *sp;
484 +{
485 + register FTSENT *freep, *p;
486 + int saved_errno;
487 +
488 + /*
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.
492 + */
493 + if (sp->fts_cur) {
494 + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
495 + freep = p;
496 + p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
497 + free(freep);
498 + }
499 + free(p);
500 + }
501 +
502 + /* Free up child linked list, sort array, path buffer. */
503 + if (sp->fts_child)
504 + fts_lfree(sp->fts_child);
505 + free(sp->fts_array);
506 + free(sp->fts_path);
507 +
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);
512 +
513 + /* Set errno and return. */
514 + if (saved_errno != 0) {
515 + /* Free up the stream pointer. */
516 + free(sp);
517 + __set_errno (saved_errno);
518 + return (-1);
519 + }
520 + }
521 +
522 + /* Free up the stream pointer. */
523 + free(sp);
524 + return (0);
525 +}
526 +
527 +/*
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".
530 + */
531 +#define NAPPEND(p) \
532 + (p->fts_path[p->fts_pathlen - 1] == '/' \
533 + ? p->fts_pathlen - 1 : p->fts_pathlen)
534 +
535 +FTSENT *
536 +fts_read(sp)
537 + register FTS *sp;
538 +{
539 + register FTSENT *p, *tmp;
540 + register int instr;
541 + register char *t;
542 + int saved_errno;
543 +
544 + /* If finished or unrecoverable error, return NULL. */
545 + if (sp->fts_cur == NULL || ISSET(FTS_STOP))
546 + return (NULL);
547 +
548 + /* Set current node pointer. */
549 + p = sp->fts_cur;
550 +
551 + /* Save and zero out user instructions. */
552 + instr = p->fts_instr;
553 + p->fts_instr = FTS_NOINSTR;
554 +
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);
558 + return (p);
559 + }
560 +
561 + /*
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.
566 + */
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;
574 + } else
575 + p->fts_flags |= FTS_SYMFOLLOW;
576 + }
577 + return (p);
578 + }
579 +
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;
590 + }
591 + p->fts_info = FTS_DP;
592 + return (p);
593 + }
594 +
595 + /* Rebuild if only read the names and now traversing. */
596 + if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
597 + CLR(FTS_NAMEONLY);
598 + fts_lfree(sp->fts_child);
599 + sp->fts_child = NULL;
600 + }
601 +
602 + /*
603 + * Cd to the subdirectory.
604 + *
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.
610 + *
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.
613 + */
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;
619 + p = p->fts_link)
620 + p->fts_accpath =
621 + p->fts_parent->fts_accpath;
622 + }
623 + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
624 + if (ISSET(FTS_STOP))
625 + return (NULL);
626 + return (p);
627 + }
628 + p = sp->fts_child;
629 + sp->fts_child = NULL;
630 + sp->fts_cur = p;
631 + goto name;
632 + }
633 +
634 + /* Move to the next node on this level. */
635 +next: tmp = p;
636 + if ((p = p->fts_link) != NULL) {
637 + sp->fts_cur = p;
638 + free(tmp);
639 +
640 + /*
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.
643 + */
644 + if (p->fts_level == FTS_ROOTLEVEL) {
645 + if (FCHDIR(sp, sp->fts_rfd)) {
646 + SET(FTS_STOP);
647 + return (NULL);
648 + }
649 + fts_load(sp, p);
650 + return p;
651 + }
652 +
653 + /*
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.
657 + */
658 + if (p->fts_instr == FTS_SKIP)
659 + goto next;
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;
667 + } else
668 + p->fts_flags |= FTS_SYMFOLLOW;
669 + }
670 + p->fts_instr = FTS_NOINSTR;
671 + }
672 +
673 +name: t = sp->fts_path + NAPPEND(p->fts_parent);
674 + *t++ = '/';
675 + memmove(t, p->fts_name, p->fts_namelen + 1);
676 + return p;
677 + }
678 +
679 + /* Move up to the parent node. */
680 + p = tmp->fts_parent;
681 + sp->fts_cur = p;
682 + free(tmp);
683 +
684 + if (p->fts_level == FTS_ROOTPARENTLEVEL) {
685 + /*
686 + * Done; free everything up and set errno to 0 so the user
687 + * can distinguish between error and EOF.
688 + */
689 + free(p);
690 + __set_errno (0);
691 + return (sp->fts_cur = NULL);
692 + }
693 +
694 + /* NUL terminate the pathname. */
695 + sp->fts_path[p->fts_pathlen] = '\0';
696 +
697 + /*
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
700 + * one directory.
701 + */
702 + if (p->fts_level == FTS_ROOTLEVEL) {
703 + if (FCHDIR(sp, sp->fts_rfd)) {
704 + SET(FTS_STOP);
705 + return (NULL);
706 + }
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);
712 + SET(FTS_STOP);
713 + return (NULL);
714 + }
715 + (void)close(p->fts_symfd);
716 + } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
717 + fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
718 + SET(FTS_STOP);
719 + return (NULL);
720 + }
721 + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
722 + return p;
723 +}
724 +
725 +/*
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
729 + * reasons.
730 + */
731 +/* ARGSUSED */
732 +int
733 +fts_set(sp, p, instr)
734 + FTS *sp;
735 + FTSENT *p;
736 + int instr;
737 +{
738 + if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
739 + instr != FTS_NOINSTR && instr != FTS_SKIP) {
740 + __set_errno (EINVAL);
741 + return (1);
742 + }
743 + p->fts_instr = instr;
744 + return (0);
745 +}
746 +
747 +FTSENT *
748 +fts_children(sp, instr)
749 + register FTS *sp;
750 + int instr;
751 +{
752 + register FTSENT *p;
753 + int fd;
754 +
755 + if (instr != 0 && instr != FTS_NAMEONLY) {
756 + __set_errno (EINVAL);
757 + return (NULL);
758 + }
759 +
760 + /* Set current node pointer. */
761 + p = sp->fts_cur;
762 +
763 + /*
764 + * Errno set to 0 so user can distinguish empty directory from
765 + * an error.
766 + */
767 + __set_errno (0);
768 +
769 + /* Fatal errors stop here. */
770 + if (ISSET(FTS_STOP))
771 + return (NULL);
772 +
773 + /* Return logical hierarchy of user's arguments. */
774 + if (p->fts_info == FTS_INIT)
775 + return (p->fts_link);
776 +
777 + /*
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.
781 + */
782 + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
783 + return (NULL);
784 +
785 + /* Free up any previous child list. */
786 + if (sp->fts_child != NULL)
787 + fts_lfree(sp->fts_child);
788 +
789 + if (instr == FTS_NAMEONLY) {
790 + SET(FTS_NAMEONLY);
791 + instr = BNAMES;
792 + } else
793 + instr = BCHILD;
794 +
795 + /*
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.
801 + */
802 + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
803 + ISSET(FTS_NOCHDIR))
804 + return (sp->fts_child = fts_build(sp, instr));
805 +
806 + if ((fd = open(".", O_RDONLY, 0)) < 0)
807 + return (NULL);
808 + sp->fts_child = fts_build(sp, instr);
809 + if (fchdir(fd))
810 + return (NULL);
811 + (void)close(fd);
812 + return (sp->fts_child);
813 +}
814 +
815 +/*
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.
819 + *
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.
828 + */
829 +static FTSENT *
830 +internal_function
831 +fts_build(sp, type)
832 + register FTS *sp;
833 + int type;
834 +{
835 + register struct dirent *dp;
836 + register FTSENT *p, *head;
837 + register int nitems;
838 + FTSENT *cur, *tail;
839 + DIR *dirp;
840 + void *oldaddr;
841 + int cderrno, descend, len, level, nlinks, saved_errno,
842 + nostat, doadjust;
843 + size_t maxlen;
844 + char *cp;
845 +
846 + /* Set current node pointer. */
847 + cur = sp->fts_cur;
848 +
849 + /*
850 + * Open the directory for reading. If this fails, we're done.
851 + * If being called from fts_read, set the fts_info field.
852 + */
853 +#if defined FTS_WHITEOUT && 0
854 + if (ISSET(FTS_WHITEOUT))
855 + oflag = DTF_NODUP|DTF_REWIND;
856 + else
857 + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
858 +#else
859 +# define opendir2(path, flag) opendir(path)
860 +#endif
861 + if ((dirp = opendir2(cur->fts_accpath, oflag)) == NULL) {
862 + if (type == BREAD) {
863 + cur->fts_info = FTS_DNR;
864 + cur->fts_errno = errno;
865 + }
866 + return (NULL);
867 + }
868 +
869 + /*
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.
873 + */
874 + if (type == BNAMES) {
875 + nlinks = 0;
876 + /* Be quiet about nostat, GCC. */
877 + nostat = 0;
878 + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
879 + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
880 + nostat = 1;
881 + } else {
882 + nlinks = -1;
883 + nostat = 0;
884 + }
885 +
886 +#ifdef notdef
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));
890 +#endif
891 + /*
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.
905 + */
906 + cderrno = 0;
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;
912 + descend = 0;
913 + cderrno = errno;
914 + (void)closedir(dirp);
915 + dirp = NULL;
916 + } else
917 + descend = 1;
918 + } else
919 + descend = 0;
920 +
921 + /*
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.
927 + *
928 + * If not changing directories set a pointer so that can just append
929 + * each new name into the path.
930 + */
931 + len = NAPPEND(cur);
932 + if (ISSET(FTS_NOCHDIR)) {
933 + cp = sp->fts_path + len;
934 + *cp++ = '/';
935 + } else {
936 + /* GCC, you're too verbose. */
937 + cp = NULL;
938 + }
939 + len++;
940 + maxlen = sp->fts_pathlen - len;
941 +
942 + level = cur->fts_level + 1;
943 +
944 + /* Read the directory, attaching each entry to the `link' pointer. */
945 + doadjust = 0;
946 + for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
947 + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
948 + continue;
949 +
950 + if ((p = fts_alloc(sp, dp->d_name, _D_EXACT_NAMLEN (dp))) == NULL)
951 + goto mem1;
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)) {
955 + /*
956 + * No more memory for path or structures. Save
957 + * errno, free up the current structure and the
958 + * structures already allocated.
959 + */
960 +mem1: saved_errno = errno;
961 + free(p);
962 + fts_lfree(head);
963 + (void)closedir(dirp);
964 + cur->fts_info = FTS_ERR;
965 + SET(FTS_STOP);
966 + __set_errno (saved_errno);
967 + return (NULL);
968 + }
969 + /* Did realloc() change the pointer? */
970 + if (oldaddr != sp->fts_path) {
971 + doadjust = 1;
972 + if (ISSET(FTS_NOCHDIR))
973 + cp = sp->fts_path + len;
974 + }
975 + maxlen = sp->fts_pathlen - len;
976 + }
977 +
978 + if (len + _D_EXACT_NAMLEN (dp) >= USHRT_MAX) {
979 + /*
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.
984 + */
985 + free(p);
986 + fts_lfree(head);
987 + (void)closedir(dirp);
988 + cur->fts_info = FTS_ERR;
989 + SET(FTS_STOP);
990 + __set_errno (ENAMETOOLONG);
991 + return (NULL);
992 + }
993 + p->fts_level = level;
994 + p->fts_parent = sp->fts_cur;
995 + p->fts_pathlen = len + _D_EXACT_NAMLEN (dp);
996 +
997 +#if defined FTS_WHITEOUT && 0
998 + if (dp->d_type == DT_WHT)
999 + p->fts_flags |= FTS_ISW;
1000 +#endif
1001 +
1002 +#if 0
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. */
1006 + if (cderrno) {
1007 + if (nlinks) {
1008 + p->fts_info = FTS_NS;
1009 + p->fts_errno = cderrno;
1010 + } else
1011 + p->fts_info = FTS_NSOK;
1012 + p->fts_accpath = cur->fts_accpath;
1013 + } else
1014 +#endif
1015 + if (nlinks == 0
1016 +#if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
1017 + || (nostat &&
1018 + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
1019 +#endif
1020 + ) {
1021 + p->fts_accpath =
1022 + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
1023 + p->fts_info = FTS_NSOK;
1024 + } else {
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);
1029 + } else
1030 + p->fts_accpath = p->fts_name;
1031 + /* Stat it. */
1032 + p->fts_info = fts_stat(sp, p, 0);
1033 +
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))
1037 + --nlinks;
1038 + }
1039 +
1040 + /* We walk in directory order so "ls -f" doesn't get upset. */
1041 + p->fts_link = NULL;
1042 + if (head == NULL)
1043 + head = tail = p;
1044 + else {
1045 + tail->fts_link = p;
1046 + tail = p;
1047 + }
1048 + ++nitems;
1049 + }
1050 + if (dirp)
1051 + (void)closedir(dirp);
1052 +
1053 + /*
1054 + * If realloc() changed the address of the path, adjust the
1055 + * addresses for the rest of the tree and the dir list.
1056 + */
1057 + if (doadjust)
1058 + fts_padjust(sp, head);
1059 +
1060 + /*
1061 + * If not changing directories, reset the path back to original
1062 + * state.
1063 + */
1064 + if (ISSET(FTS_NOCHDIR)) {
1065 + if (len == sp->fts_pathlen || nitems == 0)
1066 + --cp;
1067 + *cp = '\0';
1068 + }
1069 +
1070 + /*
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.
1076 + */
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;
1082 + SET(FTS_STOP);
1083 + fts_lfree(head);
1084 + return (NULL);
1085 + }
1086 +
1087 + /* If didn't find anything, return NULL. */
1088 + if (!nitems) {
1089 + if (type == BREAD)
1090 + cur->fts_info = FTS_DP;
1091 + fts_lfree(head);
1092 + return (NULL);
1093 + }
1094 +
1095 + /* Sort the entries. */
1096 + if (sp->fts_compar && nitems > 1)
1097 + head = fts_sort(sp, head, nitems);
1098 + return (head);
1099 +}
1100 +
1101 +static u_short
1102 +internal_function
1103 +fts_stat(sp, p, follow)
1104 + FTS *sp;
1105 + register FTSENT *p;
1106 + int follow;
1107 +{
1108 + register FTSENT *t;
1109 + register dev_t dev;
1110 + register ino_t ino;
1111 + struct stat *sbp, sb;
1112 + int saved_errno;
1113 +
1114 + /* If user needs stat info, stat buffer already allocated. */
1115 + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
1116 +
1117 +#if defined FTS_WHITEOUT && 0
1118 + /* check for whiteout */
1119 + if (p->fts_flags & FTS_ISW) {
1120 + if (sbp != &sb) {
1121 + memset(sbp, '\0', sizeof (*sbp));
1122 + sbp->st_mode = S_IFWHT;
1123 + }
1124 + return (FTS_W);
1125 + }
1126 +#endif
1127 +
1128 + /*
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.
1132 + */
1133 + if (ISSET(FTS_LOGICAL) || follow) {
1134 + if (stat(p->fts_accpath, sbp)) {
1135 + saved_errno = errno;
1136 + if (!lstat(p->fts_accpath, sbp)) {
1137 + __set_errno (0);
1138 + return (FTS_SLNONE);
1139 + }
1140 + p->fts_errno = saved_errno;
1141 + goto err;
1142 + }
1143 + } else if (lstat(p->fts_accpath, sbp)) {
1144 + p->fts_errno = errno;
1145 +err: memset(sbp, 0, sizeof(struct stat));
1146 + return (FTS_NS);
1147 + }
1148 +
1149 + if (S_ISDIR(sbp->st_mode)) {
1150 + /*
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.
1156 + */
1157 + dev = p->fts_dev = sbp->st_dev;
1158 + ino = p->fts_ino = sbp->st_ino;
1159 + p->fts_nlink = sbp->st_nlink;
1160 +
1161 + if (ISDOT(p->fts_name))
1162 + return (FTS_DOT);
1163 +
1164 + /*
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.
1169 + */
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) {
1173 + p->fts_cycle = t;
1174 + return (FTS_DC);
1175 + }
1176 + return (FTS_D);
1177 + }
1178 + if (S_ISLNK(sbp->st_mode))
1179 + return (FTS_SL);
1180 + if (S_ISREG(sbp->st_mode))
1181 + return (FTS_F);
1182 + return (FTS_DEFAULT);
1183 +}
1184 +
1185 +static FTSENT *
1186 +internal_function
1187 +fts_sort(sp, head, nitems)
1188 + FTS *sp;
1189 + FTSENT *head;
1190 + register int nitems;
1191 +{
1192 + register FTSENT **ap, *p;
1193 +
1194 + /*
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.
1200 + */
1201 + if (nitems > sp->fts_nitems) {
1202 + struct _ftsent **a;
1203 +
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;
1210 + return (head);
1211 + }
1212 + sp->fts_array = a;
1213 + }
1214 + for (ap = sp->fts_array, p = head; p; p = p->fts_link)
1215 + *ap++ = p;
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;
1220 + return (head);
1221 +}
1222 +
1223 +static FTSENT *
1224 +internal_function
1225 +fts_alloc(sp, name, namelen)
1226 + FTS *sp;
1227 + const char *name;
1228 + size_t namelen;
1229 +{
1230 + register FTSENT *p;
1231 + size_t len;
1232 +
1233 + /*
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.
1240 + */
1241 + len = sizeof(FTSENT) + namelen;
1242 + if (!ISSET(FTS_NOSTAT))
1243 + len += sizeof(struct stat) + ALIGNBYTES;
1244 + if ((p = malloc(len)) == NULL)
1245 + return (NULL);
1246 +
1247 + /* Copy the name and guarantee NUL termination. */
1248 + memmove(p->fts_name, name, namelen);
1249 + p->fts_name[namelen] = '\0';
1250 +
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;
1255 + p->fts_errno = 0;
1256 + p->fts_flags = 0;
1257 + p->fts_instr = FTS_NOINSTR;
1258 + p->fts_number = 0;
1259 + p->fts_pointer = NULL;
1260 + return (p);
1261 +}
1262 +
1263 +static void
1264 +internal_function
1265 +fts_lfree(head)
1266 + register FTSENT *head;
1267 +{
1268 + register FTSENT *p;
1269 +
1270 + /* Free a linked list of structures. */
1271 + while ((p = head)) {
1272 + head = head->fts_link;
1273 + free(p);
1274 + }
1275 +}
1276 +
1277 +/*
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.
1282 + */
1283 +static int
1284 +internal_function
1285 +fts_palloc(sp, more)
1286 + FTS *sp;
1287 + size_t more;
1288 +{
1289 + char *p;
1290 +
1291 + sp->fts_pathlen += more + 256;
1292 + /*
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.
1296 + */
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);
1301 + return (1);
1302 + }
1303 + p = realloc(sp->fts_path, sp->fts_pathlen);
1304 + if (p == NULL) {
1305 + free(sp->fts_path);
1306 + sp->fts_path = NULL;
1307 + return 1;
1308 + }
1309 + sp->fts_path = p;
1310 + return 0;
1311 +}
1312 +
1313 +/*
1314 + * When the path is realloc'd, have to fix all of the pointers in structures
1315 + * already returned.
1316 + */
1317 +static void
1318 +internal_function
1319 +fts_padjust(sp, head)
1320 + FTS *sp;
1321 + FTSENT *head;
1322 +{
1323 + FTSENT *p;
1324 + char *addr = sp->fts_path;
1325 +
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); \
1330 + } \
1331 + (p)->fts_path = addr; \
1332 +} while (0)
1333 + /* Adjust the current set of children. */
1334 + for (p = sp->fts_child; p; p = p->fts_link)
1335 + ADJUST(p);
1336 +
1337 + /* Adjust the rest of the tree, including the current level. */
1338 + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
1339 + ADJUST(p);
1340 + p = p->fts_link ? p->fts_link : p->fts_parent;
1341 + }
1342 +}
1343 +
1344 +static size_t
1345 +internal_function
1346 +fts_maxarglen(argv)
1347 + char * const *argv;
1348 +{
1349 + size_t len, max;
1350 +
1351 + for (max = 0; *argv; ++argv)
1352 + if ((len = strlen(*argv)) > max)
1353 + max = len;
1354 + return (max + 1);
1355 +}
1356 +
1357 +/*
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.
1361 + */
1362 +static int
1363 +internal_function
1364 +fts_safe_changedir(sp, p, fd, path)
1365 + FTS *sp;
1366 + FTSENT *p;
1367 + int fd;
1368 + const char *path;
1369 +{
1370 + int ret, oerrno, newfd;
1371 + struct stat64 sb;
1372 +
1373 + newfd = fd;
1374 + if (ISSET(FTS_NOCHDIR))
1375 + return (0);
1376 + if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
1377 + return (-1);
1378 + if (fstat64(newfd, &sb)) {
1379 + ret = -1;
1380 + goto bail;
1381 + }
1382 + if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
1383 + __set_errno (ENOENT); /* disinformation */
1384 + ret = -1;
1385 + goto bail;
1386 + }
1387 + ret = fchdir(newfd);
1388 +bail:
1389 + oerrno = errno;
1390 + if (fd < 0)
1391 + (void)close(newfd);
1392 + __set_errno (oerrno);
1393 + return (ret);
1394 +}
1395 --
1396 cgit v0.9.0.1-2-gef13
1397