Debian Codegraph @

src/OVAL/fts_sun.c

raw symbols SCIP
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29 #if defined (OS_SOLARIS)
30
31 #include <config.h>
32
33 #if !defined(HAVE_FTS_OPEN)
34
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94";
37 #endif /* LIBC_SCCS and not lint */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include "fts_sun.h"
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49
50 /* Largest alignment size needed, minus one.
51 Usually long double is the worst case. */
52 #ifndef ALIGNBYTES
53 #define ALIGNBYTES (__alignof__ (long double) - 1)
54 #endif
55 /* Align P to that size. */
56 #ifndef ALIGN
57 #define ALIGN(p) (((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES)
58 #endif
59
60 #if defined (OS_SOLARIS)
61 #define USHRT_MAX 65535 /* max of "unsigned short int" */
62 #define __set_errno(val) (errno = (val))
63 #define __fchdir fchdir
64 #define __opendir opendir
65 #define __closedir closedir
66 #define __readdir readdir
67 #define __open open
68 #define __close close
69 #endif
70
71 static FTSENT *fts_alloc (FTS *, const char *, size_t);
72 static FTSENT *fts_build (FTS *, int);
73 static void fts_lfree (FTSENT *);
74 static void fts_load (FTS *, FTSENT *);
75 static size_t fts_maxarglen (char * const *);
76 static void fts_padjust (FTS *, FTSENT *);
77 static int fts_palloc (FTS *, size_t);
78 static FTSENT *fts_sort (FTS *, FTSENT *, int);
79 static u_short fts_stat (FTS *, FTSENT *, int);
80 static int fts_safe_changedir (FTS *, FTSENT *, int, const char *);
81
82 #ifndef MAX
83 #define MAX(a, b) ({ __typeof__ (a) _a = (a); \
84 __typeof__ (b) _b = (b); \
85 _a > _b ? _a : _b; })
86 #endif
87
88 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
89
90 #define CLR(opt) (sp->fts_options &= ~(opt))
91 #define ISSET(opt) (sp->fts_options & (opt))
92 #define SET(opt) (sp->fts_options |= (opt))
93
94 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && __fchdir(fd))
95
96 /* fts_build flags */
97 #define BCHILD 1 /* fts_children */
98 #define BNAMES 2 /* fts_children, names only */
99 #define BREAD 3 /* fts_read */
100
101 int
102 dirfd (dirp)
103 DIR *dirp;
104 {
105 __set_errno(ENOSYS);
106 return -1;
107 }
108
109 FTS *
110 fts_open(argv, options, compar)
111 char * const *argv;
112 register int options;
113 int (*compar) (const FTSENT **, const FTSENT **);
114 {
115 register FTS *sp;
116 register FTSENT *p, *root;
117 register int nitems;
118 FTSENT *parent = NULL;
119 FTSENT *tmp;
120
121 /* Options check. */
122 if (options & ~FTS_OPTIONMASK) {
123 __set_errno (EINVAL);
124 return (NULL);
125 }
126
127 /* Allocate/initialize the stream */
128 if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
129 return (NULL);
130 memset(sp, 0, sizeof(FTS));
131 sp->fts_compar = (int (*) (const void *, const void *)) compar;
132 sp->fts_options = options;
133
134 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
135 if (ISSET(FTS_LOGICAL))
136 SET(FTS_NOCHDIR);
137
138 /*
139 * Start out with 1K of path space, and enough, in any case,
140 * to hold the user's paths.
141 */
142 #ifndef MAXPATHLEN
143 #define MAXPATHLEN 1024
144 #endif
145 size_t maxarglen = fts_maxarglen(argv);
146 if (fts_palloc(sp, MAX(maxarglen, MAXPATHLEN)))
147 goto mem1;
148
149 /* Allocate/initialize root's parent. */
150 if (*argv != NULL) {
151 if ((parent = fts_alloc(sp, "", 0)) == NULL)
152 goto mem2;
153 parent->fts_level = FTS_ROOTPARENTLEVEL;
154 }
155
156 /* Allocate/initialize root(s). */
157 for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
158 /* Don't allow zero-length paths. */
159 size_t len = strlen(*argv);
160 if (len == 0) {
161 __set_errno (ENOENT);
162 goto mem3;
163 }
164
165 p = fts_alloc(sp, *argv, len);
166 p->fts_level = FTS_ROOTLEVEL;
167 p->fts_parent = parent;
168 p->fts_accpath = p->fts_name;
169 p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
170
171 /* Command-line "." and ".." are real directories. */
172 if (p->fts_info == FTS_DOT)
173 p->fts_info = FTS_D;
174
175 /*
176 * If comparison routine supplied, traverse in sorted
177 * order; otherwise traverse in the order specified.
178 */
179 if (compar) {
180 p->fts_link = root;
181 root = p;
182 } else {
183 p->fts_link = NULL;
184 if (root == NULL)
185 tmp = root = p;
186 else {
187 tmp->fts_link = p;
188 tmp = p;
189 }
190 }
191 }
192 if (compar && nitems > 1)
193 root = fts_sort(sp, root, nitems);
194
195 /*
196 * Allocate a dummy pointer and make fts_read think that we've just
197 * finished the node before the root(s); set p->fts_info to FTS_INIT
198 * so that everything about the "current" node is ignored.
199 */
200 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
201 goto mem3;
202 sp->fts_cur->fts_link = root;
203 sp->fts_cur->fts_info = FTS_INIT;
204
205 /*
206 * If using chdir(2), grab a file descriptor pointing to dot to ensure
207 * that we can get back here; this could be avoided for some paths,
208 * but almost certainly not worth the effort. Slashes, symbolic links,
209 * and ".." are all fairly nasty problems. Note, if we can't get the
210 * descriptor we run anyway, just more slowly.
211 */
212 if (!ISSET(FTS_NOCHDIR)
213 && (sp->fts_rfd = __open(".", O_RDONLY, 0)) < 0)
214 SET(FTS_NOCHDIR);
215
216 return (sp);
217
218 mem3: fts_lfree(root);
219 free(parent);
220 mem2: free(sp->fts_path);
221 mem1: free(sp);
222 return (NULL);
223 }
224
225 static void
226 fts_load(sp, p)
227 FTS *sp;
228 register FTSENT *p;
229 {
230 register int len;
231 register char *cp;
232
233 /*
234 * Load the stream structure for the next traversal. Since we don't
235 * actually enter the directory until after the preorder visit, set
236 * the fts_accpath field specially so the chdir gets done to the right
237 * place and the user can access the first node. From fts_open it's
238 * known that the path will fit.
239 */
240 len = p->fts_pathlen = p->fts_namelen;
241 memmove(sp->fts_path, p->fts_name, len + 1);
242 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
243 len = strlen(++cp);
244 memmove(p->fts_name, cp, len + 1);
245 p->fts_namelen = len;
246 }
247 p->fts_accpath = p->fts_path = sp->fts_path;
248 sp->fts_dev = p->fts_dev;
249 }
250
251 int
252 fts_close(sp)
253 FTS *sp;
254 {
255 register FTSENT *freep, *p;
256 int saved_errno;
257
258 /*
259 * This still works if we haven't read anything -- the dummy structure
260 * points to the root list, so we step through to the end of the root
261 * list which has a valid parent pointer.
262 */
263 if (sp->fts_cur) {
264 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
265 freep = p;
266 p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
267 free(freep);
268 }
269 free(p);
270 }
271
272 /* Free up child linked list, sort array, path buffer. */
273 if (sp->fts_child)
274 fts_lfree(sp->fts_child);
275 free(sp->fts_array);
276 free(sp->fts_path);
277
278 /* Return to original directory, save errno if necessary. */
279 if (!ISSET(FTS_NOCHDIR)) {
280 saved_errno = __fchdir(sp->fts_rfd) ? errno : 0;
281 (void)__close(sp->fts_rfd);
282
283 /* Set errno and return. */
284 if (saved_errno != 0) {
285 /* Free up the stream pointer. */
286 free(sp);
287 __set_errno (saved_errno);
288 return (-1);
289 }
290 }
291
292 /* Free up the stream pointer. */
293 free(sp);
294 return (0);
295 }
296
297 /*
298 * Special case of "/" at the end of the path so that slashes aren't
299 * appended which would cause paths to be written as "....//foo".
300 */
301 #define NAPPEND(p) \
302 (p->fts_path[p->fts_pathlen - 1] == '/' \
303 ? p->fts_pathlen - 1 : p->fts_pathlen)
304
305 FTSENT *
306 fts_read(sp)
307 register FTS *sp;
308 {
309 register FTSENT *p, *tmp;
310 register int instr;
311 register char *t;
312 int saved_errno;
313
314 /* If finished or unrecoverable error, return NULL. */
315 if (sp->fts_cur == NULL || ISSET(FTS_STOP))
316 return (NULL);
317
318 /* Set current node pointer. */
319 p = sp->fts_cur;
320
321 /* Save and zero out user instructions. */
322 instr = p->fts_instr;
323 p->fts_instr = FTS_NOINSTR;
324
325 /* Any type of file may be re-visited; re-stat and re-turn. */
326 if (instr == FTS_AGAIN) {
327 p->fts_info = fts_stat(sp, p, 0);
328 return (p);
329 }
330
331 /*
332 * Following a symlink -- SLNONE test allows application to see
333 * SLNONE and recover. If indirecting through a symlink, have
334 * keep a pointer to current location. If unable to get that
335 * pointer, follow fails.
336 */
337 if (instr == FTS_FOLLOW &&
338 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
339 p->fts_info = fts_stat(sp, p, 1);
340 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
341 if ((p->fts_symfd = __open(".", O_RDONLY, 0)) < 0) {
342 p->fts_errno = errno;
343 p->fts_info = FTS_ERR;
344 } else
345 p->fts_flags |= FTS_SYMFOLLOW;
346 }
347 return (p);
348 }
349
350 /* Directory in pre-order. */
351 if (p->fts_info == FTS_D) {
352 /* If skipped or crossed mount point, do post-order visit. */
353 if (instr == FTS_SKIP ||
354 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
355 if (p->fts_flags & FTS_SYMFOLLOW)
356 (void)__close(p->fts_symfd);
357 if (sp->fts_child) {
358 fts_lfree(sp->fts_child);
359 sp->fts_child = NULL;
360 }
361 p->fts_info = FTS_DP;
362 return (p);
363 }
364
365 /* Rebuild if only read the names and now traversing. */
366 if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
367 CLR(FTS_NAMEONLY);
368 fts_lfree(sp->fts_child);
369 sp->fts_child = NULL;
370 }
371
372 /*
373 * Cd to the subdirectory.
374 *
375 * If have already read and now fail to chdir, whack the list
376 * to make the names come out right, and set the parent errno
377 * so the application will eventually get an error condition.
378 * Set the FTS_DONTCHDIR flag so that when we logically change
379 * directories back to the parent we don't do a chdir.
380 *
381 * If haven't read do so. If the read fails, fts_build sets
382 * FTS_STOP or the fts_info field of the node.
383 */
384 if (sp->fts_child != NULL) {
385 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
386 p->fts_errno = errno;
387 p->fts_flags |= FTS_DONTCHDIR;
388 for (p = sp->fts_child; p != NULL;
389 p = p->fts_link)
390 p->fts_accpath =
391 p->fts_parent->fts_accpath;
392 }
393 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
394 if (ISSET(FTS_STOP))
395 return (NULL);
396 return (p);
397 }
398 p = sp->fts_child;
399 sp->fts_child = NULL;
400 sp->fts_cur = p;
401 goto name;
402 }
403
404 /* Move to the next node on this level. */
405 next: tmp = p;
406 if ((p = p->fts_link) != NULL) {
407 sp->fts_cur = p;
408 free(tmp);
409
410 /*
411 * If reached the top, return to the original directory (or
412 * the root of the tree), and load the paths for the next root.
413 */
414 if (p->fts_level == FTS_ROOTLEVEL) {
415 if (FCHDIR(sp, sp->fts_rfd)) {
416 SET(FTS_STOP);
417 return (NULL);
418 }
419 fts_load(sp, p);
420 return p;
421 }
422
423 /*
424 * User may have called fts_set on the node. If skipped,
425 * ignore. If followed, get a file descriptor so we can
426 * get back if necessary.
427 */
428 if (p->fts_instr == FTS_SKIP)
429 goto next;
430 if (p->fts_instr == FTS_FOLLOW) {
431 p->fts_info = fts_stat(sp, p, 1);
432 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
433 if ((p->fts_symfd =
434 __open(".", O_RDONLY, 0)) < 0) {
435 p->fts_errno = errno;
436 p->fts_info = FTS_ERR;
437 } else
438 p->fts_flags |= FTS_SYMFOLLOW;
439 }
440 p->fts_instr = FTS_NOINSTR;
441 }
442
443 name: t = sp->fts_path + NAPPEND(p->fts_parent);
444 *t++ = '/';
445 memmove(t, p->fts_name, p->fts_namelen + 1);
446 return p;
447 }
448
449 /* Move up to the parent node. */
450 p = tmp->fts_parent;
451 sp->fts_cur = p;
452 free(tmp);
453
454 if (p->fts_level == FTS_ROOTPARENTLEVEL) {
455 /*
456 * Done; free everything up and set errno to 0 so the user
457 * can distinguish between error and EOF.
458 */
459 free(p);
460 __set_errno (0);
461 return (sp->fts_cur = NULL);
462 }
463
464 /* NUL terminate the pathname. */
465 sp->fts_path[p->fts_pathlen] = '\0';
466
467 /*
468 * Return to the parent directory. If at a root node or came through
469 * a symlink, go back through the file descriptor. Otherwise, cd up
470 * one directory.
471 */
472 if (p->fts_level == FTS_ROOTLEVEL) {
473 if (FCHDIR(sp, sp->fts_rfd)) {
474 SET(FTS_STOP);
475 return (NULL);
476 }
477 } else if (p->fts_flags & FTS_SYMFOLLOW) {
478 if (FCHDIR(sp, p->fts_symfd)) {
479 saved_errno = errno;
480 (void)__close(p->fts_symfd);
481 __set_errno (saved_errno);
482 SET(FTS_STOP);
483 return (NULL);
484 }
485 (void)__close(p->fts_symfd);
486 } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
487 fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
488 SET(FTS_STOP);
489 return (NULL);
490 }
491 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
492 return p;
493 }
494
495 /*
496 * Fts_set takes the stream as an argument although it's not used in this
497 * implementation; it would be necessary if anyone wanted to add global
498 * semantics to fts using fts_set. An error return is allowed for similar
499 * reasons.
500 */
501 /* ARGSUSED */
502 int
503 fts_set(sp, p, instr)
504 FTS *sp;
505 FTSENT *p;
506 int instr;
507 {
508 if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
509 instr != FTS_NOINSTR && instr != FTS_SKIP) {
510 __set_errno (EINVAL);
511 return (1);
512 }
513 p->fts_instr = instr;
514 return (0);
515 }
516
517 FTSENT *
518 fts_children(sp, instr)
519 register FTS *sp;
520 int instr;
521 {
522 register FTSENT *p;
523 int fd;
524
525 if (instr != 0 && instr != FTS_NAMEONLY) {
526 __set_errno (EINVAL);
527 return (NULL);
528 }
529
530 /* Set current node pointer. */
531 p = sp->fts_cur;
532
533 /*
534 * Errno set to 0 so user can distinguish empty directory from
535 * an error.
536 */
537 __set_errno (0);
538
539 /* Fatal errors stop here. */
540 if (ISSET(FTS_STOP))
541 return (NULL);
542
543 /* Return logical hierarchy of user's arguments. */
544 if (p->fts_info == FTS_INIT)
545 return (p->fts_link);
546
547 /*
548 * If not a directory being visited in pre-order, stop here. Could
549 * allow FTS_DNR, assuming the user has fixed the problem, but the
550 * same effect is available with FTS_AGAIN.
551 */
552 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
553 return (NULL);
554
555 /* Free up any previous child list. */
556 if (sp->fts_child != NULL)
557 fts_lfree(sp->fts_child);
558
559 if (instr == FTS_NAMEONLY) {
560 SET(FTS_NAMEONLY);
561 instr = BNAMES;
562 } else
563 instr = BCHILD;
564
565 /*
566 * If using chdir on a relative path and called BEFORE fts_read does
567 * its chdir to the root of a traversal, we can lose -- we need to
568 * chdir into the subdirectory, and we don't know where the current
569 * directory is, so we can't get back so that the upcoming chdir by
570 * fts_read will work.
571 */
572 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
573 ISSET(FTS_NOCHDIR))
574 return (sp->fts_child = fts_build(sp, instr));
575
576 if ((fd = __open(".", O_RDONLY, 0)) < 0)
577 return (NULL);
578 sp->fts_child = fts_build(sp, instr);
579 if (__fchdir(fd))
580 return (NULL);
581 (void)__close(fd);
582 return (sp->fts_child);
583 }
584
585 /*
586 * This is the tricky part -- do not casually change *anything* in here. The
587 * idea is to build the linked list of entries that are used by fts_children
588 * and fts_read. There are lots of special cases.
589 *
590 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
591 * set and it's a physical walk (so that symbolic links can't be directories),
592 * we can do things quickly. First, if it's a 4.4BSD file system, the type
593 * of the file is in the directory entry. Otherwise, we assume that the number
594 * of subdirectories in a node is equal to the number of links to the parent.
595 * The former skips all stat calls. The latter skips stat calls in any leaf
596 * directories and for any files after the subdirectories in the directory have
597 * been found, cutting the stat calls by about 2/3.
598 */
599 static FTSENT *
600 fts_build(sp, type)
601 register FTS *sp;
602 int type;
603 {
604 register struct dirent *dp;
605 register FTSENT *p, *head;
606 register int nitems;
607 FTSENT *cur, *tail;
608 DIR *dirp;
609 void *oldaddr;
610 int cderrno, descend, len, level, nlinks, saved_errno,
611 nostat, doadjust;
612 size_t maxlen;
613 char *cp;
614
615 /* Set current node pointer. */
616 cur = sp->fts_cur;
617
618 /*
619 * Open the directory for reading. If this fails, we're done.
620 * If being called from fts_read, set the fts_info field.
621 */
622 #if defined FTS_WHITEOUT && 0
623 if (ISSET(FTS_WHITEOUT))
624 oflag = DTF_NODUP|DTF_REWIND;
625 else
626 oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
627 #else
628 # define __opendir2(path, flag) __opendir(path)
629 #endif
630 if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
631 if (type == BREAD) {
632 cur->fts_info = FTS_DNR;
633 cur->fts_errno = errno;
634 }
635 return (NULL);
636 }
637
638 /*
639 * Nlinks is the number of possible entries of type directory in the
640 * directory if we're cheating on stat calls, 0 if we're not doing
641 * any stat calls at all, -1 if we're doing stats on everything.
642 */
643 if (type == BNAMES) {
644 nlinks = 0;
645 /* Be quiet about nostat, GCC. */
646 nostat = 0;
647 } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
648 nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
649 nostat = 1;
650 } else {
651 nlinks = -1;
652 nostat = 0;
653 }
654
655 #ifdef notdef
656 (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
657 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
658 ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
659 #endif
660 /*
661 * If we're going to need to stat anything or we want to descend
662 * and stay in the directory, chdir. If this fails we keep going,
663 * but set a flag so we don't chdir after the post-order visit.
664 * We won't be able to stat anything, but we can still return the
665 * names themselves. Note, that since fts_read won't be able to
666 * chdir into the directory, it will have to return different path
667 * names than before, i.e. "a/b" instead of "b". Since the node
668 * has already been visited in pre-order, have to wait until the
669 * post-order visit to return the error. There is a special case
670 * here, if there was nothing to stat then it's not an error to
671 * not be able to stat. This is all fairly nasty. If a program
672 * needed sorted entries or stat information, they had better be
673 * checking FTS_NS on the returned nodes.
674 */
675 cderrno = 0;
676 if (nlinks || type == BREAD) {
677 if (fts_safe_changedir(sp, cur, dirfd(dirp), cur->fts_accpath)) {
678 if (nlinks && type == BREAD)
679 cur->fts_errno = errno;
680 cur->fts_flags |= FTS_DONTCHDIR;
681 descend = 0;
682 cderrno = errno;
683 (void)__closedir(dirp);
684 dirp = NULL;
685 } else
686 descend = 1;
687 } else
688 descend = 0;
689
690 /*
691 * Figure out the max file name length that can be stored in the
692 * current path -- the inner loop allocates more path as necessary.
693 * We really wouldn't have to do the maxlen calculations here, we
694 * could do them in fts_read before returning the path, but it's a
695 * lot easier here since the length is part of the dirent structure.
696 *
697 * If not changing directories set a pointer so that can just append
698 * each new name into the path.
699 */
700 len = NAPPEND(cur);
701 if (ISSET(FTS_NOCHDIR)) {
702 cp = sp->fts_path + len;
703 *cp++ = '/';
704 } else {
705 /* GCC, you're too verbose. */
706 cp = NULL;
707 }
708 len++;
709 maxlen = sp->fts_pathlen - len;
710
711 level = cur->fts_level + 1;
712
713 /* Read the directory, attaching each entry to the `link' pointer. */
714 doadjust = 0;
715 for (head = tail = NULL, nitems = 0; dirp && (dp = __readdir(dirp));) {
716 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
717 continue;
718
719 if ((p = fts_alloc(sp, dp->d_name, _D_EXACT_NAMLEN (dp))) == NULL)
720 goto mem1;
721 if (_D_EXACT_NAMLEN (dp) >= maxlen) {/* include space for NUL */
722 oldaddr = sp->fts_path;
723 if (fts_palloc(sp, _D_EXACT_NAMLEN (dp) + len + 1)) {
724 /*
725 * No more memory for path or structures. Save
726 * errno, free up the current structure and the
727 * structures already allocated.
728 */
729 mem1: saved_errno = errno;
730 free(p);
731 fts_lfree(head);
732 (void)__closedir(dirp);
733 cur->fts_info = FTS_ERR;
734 SET(FTS_STOP);
735 __set_errno (saved_errno);
736 return (NULL);
737 }
738 /* Did realloc() change the pointer? */
739 if (oldaddr != sp->fts_path) {
740 doadjust = 1;
741 if (ISSET(FTS_NOCHDIR))
742 cp = sp->fts_path + len;
743 }
744 maxlen = sp->fts_pathlen - len;
745 }
746
747 if (len + _D_EXACT_NAMLEN (dp) >= USHRT_MAX) {
748 /*
749 * In an FTSENT, fts_pathlen is a u_short so it is
750 * possible to wraparound here. If we do, free up
751 * the current structure and the structures already
752 * allocated, then error out with ENAMETOOLONG.
753 */
754 free(p);
755 fts_lfree(head);
756 (void)__closedir(dirp);
757 cur->fts_info = FTS_ERR;
758 SET(FTS_STOP);
759 __set_errno (ENAMETOOLONG);
760 return (NULL);
761 }
762 p->fts_level = level;
763 p->fts_parent = sp->fts_cur;
764 p->fts_pathlen = len + _D_EXACT_NAMLEN (dp);
765
766 #if defined FTS_WHITEOUT && 0
767 if (dp->d_type == DT_WHT)
768 p->fts_flags |= FTS_ISW;
769 #endif
770
771 #if 0
772 /* Unreachable code. cderrno is only ever set to a nonnull
773 value if dirp is closed at the same time. But then we
774 cannot enter this loop. */
775 if (cderrno) {
776 if (nlinks) {
777 p->fts_info = FTS_NS;
778 p->fts_errno = cderrno;
779 } else
780 p->fts_info = FTS_NSOK;
781 p->fts_accpath = cur->fts_accpath;
782 } else
783 #endif
784 if (nlinks == 0
785 #if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
786 || (nostat &&
787 dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
788 #endif
789 ) {
790 p->fts_accpath =
791 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
792 p->fts_info = FTS_NSOK;
793 } else {
794 /* Build a file name for fts_stat to stat. */
795 if (ISSET(FTS_NOCHDIR)) {
796 p->fts_accpath = p->fts_path;
797 memmove(cp, p->fts_name, p->fts_namelen + 1);
798 } else
799 p->fts_accpath = p->fts_name;
800 /* Stat it. */
801 p->fts_info = fts_stat(sp, p, 0);
802
803 /* Decrement link count if applicable. */
804 if (nlinks > 0 && (p->fts_info == FTS_D ||
805 p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
806 --nlinks;
807 }
808
809 /* We walk in directory order so "ls -f" doesn't get upset. */
810 p->fts_link = NULL;
811 if (head == NULL)
812 head = tail = p;
813 else {
814 tail->fts_link = p;
815 tail = p;
816 }
817 ++nitems;
818 }
819 if (dirp)
820 (void)__closedir(dirp);
821
822 /*
823 * If realloc() changed the address of the path, adjust the
824 * addresses for the rest of the tree and the dir list.
825 */
826 if (doadjust)
827 fts_padjust(sp, head);
828
829 /*
830 * If not changing directories, reset the path back to original
831 * state.
832 */
833 if (ISSET(FTS_NOCHDIR)) {
834 if (len == sp->fts_pathlen || nitems == 0)
835 --cp;
836 *cp = '\0';
837 }
838
839 /*
840 * If descended after called from fts_children or after called from
841 * fts_read and nothing found, get back. At the root level we use
842 * the saved fd; if one of fts_open()'s arguments is a relative path
843 * to an empty directory, we wind up here with no other way back. If
844 * can't get back, we're done.
845 */
846 if (descend && (type == BCHILD || !nitems) &&
847 (cur->fts_level == FTS_ROOTLEVEL ?
848 FCHDIR(sp, sp->fts_rfd) :
849 fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
850 cur->fts_info = FTS_ERR;
851 SET(FTS_STOP);
852 fts_lfree(head);
853 return (NULL);
854 }
855
856 /* If didn't find anything, return NULL. */
857 if (!nitems) {
858 if (type == BREAD)
859 cur->fts_info = FTS_DP;
860 fts_lfree(head);
861 return (NULL);
862 }
863
864 /* Sort the entries. */
865 if (sp->fts_compar && nitems > 1)
866 head = fts_sort(sp, head, nitems);
867 return (head);
868 }
869
870 static u_short
871 fts_stat(sp, p, follow)
872 FTS *sp;
873 register FTSENT *p;
874 int follow;
875 {
876 register FTSENT *t;
877 register dev_t dev;
878 register ino_t ino;
879 struct stat *sbp, sb;
880 int saved_errno;
881
882 /* If user needs stat info, stat buffer already allocated. */
883 sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
884
885 #if defined FTS_WHITEOUT && 0
886 /* check for whiteout */
887 if (p->fts_flags & FTS_ISW) {
888 if (sbp != &sb) {
889 memset(sbp, '\0', sizeof (*sbp));
890 sbp->st_mode = S_IFWHT;
891 }
892 return (FTS_W);
893 }
894 #endif
895
896 /*
897 * If doing a logical walk, or application requested FTS_FOLLOW, do
898 * a stat(2). If that fails, check for a non-existent symlink. If
899 * fail, set the errno from the stat call.
900 */
901 if (ISSET(FTS_LOGICAL) || follow) {
902 if (stat(p->fts_accpath, sbp)) {
903 saved_errno = errno;
904 if (!lstat(p->fts_accpath, sbp)) {
905 __set_errno (0);
906 return (FTS_SLNONE);
907 }
908 p->fts_errno = saved_errno;
909 goto err;
910 }
911 } else if (lstat(p->fts_accpath, sbp)) {
912 p->fts_errno = errno;
913 err: memset(sbp, 0, sizeof(struct stat));
914 return (FTS_NS);
915 }
916
917 if (S_ISDIR(sbp->st_mode)) {
918 /*
919 * Set the device/inode. Used to find cycles and check for
920 * crossing mount points. Also remember the link count, used
921 * in fts_build to limit the number of stat calls. It is
922 * understood that these fields are only referenced if fts_info
923 * is set to FTS_D.
924 */
925 dev = p->fts_dev = sbp->st_dev;
926 ino = p->fts_ino = sbp->st_ino;
927 p->fts_nlink = sbp->st_nlink;
928
929 if (ISDOT(p->fts_name))
930 return (FTS_DOT);
931
932 /*
933 * Cycle detection is done by brute force when the directory
934 * is first encountered. If the tree gets deep enough or the
935 * number of symbolic links to directories is high enough,
936 * something faster might be worthwhile.
937 */
938 for (t = p->fts_parent;
939 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
940 if (ino == t->fts_ino && dev == t->fts_dev) {
941 p->fts_cycle = t;
942 return (FTS_DC);
943 }
944 return (FTS_D);
945 }
946 if (S_ISLNK(sbp->st_mode))
947 return (FTS_SL);
948 if (S_ISREG(sbp->st_mode))
949 return (FTS_F);
950 return (FTS_DEFAULT);
951 }
952
953 static FTSENT *
954 fts_sort(sp, head, nitems)
955 FTS *sp;
956 FTSENT *head;
957 register int nitems;
958 {
959 register FTSENT **ap, *p;
960
961 /*
962 * Construct an array of pointers to the structures and call qsort(3).
963 * Reassemble the array in the order returned by qsort. If unable to
964 * sort for memory reasons, return the directory entries in their
965 * current order. Allocate enough space for the current needs plus
966 * 40 so don't realloc one entry at a time.
967 */
968 if (nitems > sp->fts_nitems) {
969 struct _ftsent **a;
970
971 sp->fts_nitems = nitems + 40;
972 if ((a = realloc(sp->fts_array,
973 (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
974 free(sp->fts_array);
975 sp->fts_array = NULL;
976 sp->fts_nitems = 0;
977 return (head);
978 }
979 sp->fts_array = a;
980 }
981 for (ap = sp->fts_array, p = head; p; p = p->fts_link)
982 *ap++ = p;
983 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
984 for (head = *(ap = sp->fts_array); --nitems; ++ap)
985 ap[0]->fts_link = ap[1];
986 ap[0]->fts_link = NULL;
987 return (head);
988 }
989
990 static FTSENT *
991 fts_alloc(sp, name, namelen)
992 FTS *sp;
993 const char *name;
994 size_t namelen;
995 {
996 register FTSENT *p;
997 size_t len;
998
999 /*
1000 * The file name is a variable length array and no stat structure is
1001 * necessary if the user has set the nostat bit. Allocate the FTSENT
1002 * structure, the file name and the stat structure in one chunk, but
1003 * be careful that the stat structure is reasonably aligned. Since the
1004 * fts_name field is declared to be of size 1, the fts_name pointer is
1005 * namelen + 2 before the first possible address of the stat structure.
1006 */
1007 len = sizeof(FTSENT) + namelen;
1008 if (!ISSET(FTS_NOSTAT))
1009 len += sizeof(struct stat) + ALIGNBYTES;
1010 if ((p = malloc(len)) == NULL)
1011 return (NULL);
1012
1013 /* Copy the name and guarantee NUL termination. */
1014 memmove(p->fts_name, name, namelen);
1015 p->fts_name[namelen] = '\0';
1016
1017 if (!ISSET(FTS_NOSTAT))
1018 p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
1019 p->fts_namelen = namelen;
1020 p->fts_path = sp->fts_path;
1021 p->fts_errno = 0;
1022 p->fts_flags = 0;
1023 p->fts_instr = FTS_NOINSTR;
1024 p->fts_number = 0;
1025 p->fts_pointer = NULL;
1026 #if defined(OS_SOLARIS)
1027 if (!ISSET(FTS_NOSTAT))
1028 p->fts_statp->st_fstype[0] = '\0';
1029 #endif
1030 return (p);
1031 }
1032
1033 static void
1034 fts_lfree(head)
1035 register FTSENT *head;
1036 {
1037 register FTSENT *p;
1038
1039 /* Free a linked list of structures. */
1040 while ((p = head)) {
1041 head = head->fts_link;
1042 free(p);
1043 }
1044 }
1045
1046 /*
1047 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
1048 * Most systems will allow creation of paths much longer than MAXPATHLEN, even
1049 * though the kernel won't resolve them. Add the size (not just what's needed)
1050 * plus 256 bytes so don't realloc the path 2 bytes at a time.
1051 */
1052 static int
1053 fts_palloc(sp, more)
1054 FTS *sp;
1055 size_t more;
1056 {
1057 char *p;
1058
1059 sp->fts_pathlen += more + 256;
1060 /*
1061 * Check for possible wraparound. In an FTS, fts_pathlen is
1062 * a signed int but in an FTSENT it is an unsigned short.
1063 * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
1064 */
1065 if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) {
1066 free(sp->fts_path);
1067 sp->fts_path = NULL;
1068 __set_errno (ENAMETOOLONG);
1069 return (1);
1070 }
1071 p = realloc(sp->fts_path, sp->fts_pathlen);
1072 if (p == NULL) {
1073 free(sp->fts_path);
1074 sp->fts_path = NULL;
1075 return 1;
1076 }
1077 sp->fts_path = p;
1078 return 0;
1079 }
1080
1081 /*
1082 * When the path is realloc'd, have to fix all of the pointers in structures
1083 * already returned.
1084 */
1085 static void
1086 fts_padjust(sp, head)
1087 FTS *sp;
1088 FTSENT *head;
1089 {
1090 FTSENT *p;
1091 char *addr = sp->fts_path;
1092
1093 #define ADJUST(p) do { \
1094 if ((p)->fts_accpath != (p)->fts_name) { \
1095 (p)->fts_accpath = \
1096 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
1097 } \
1098 (p)->fts_path = addr; \
1099 } while (0)
1100 /* Adjust the current set of children. */
1101 for (p = sp->fts_child; p; p = p->fts_link)
1102 ADJUST(p);
1103
1104 /* Adjust the rest of the tree, including the current level. */
1105 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
1106 ADJUST(p);
1107 p = p->fts_link ? p->fts_link : p->fts_parent;
1108 }
1109 }
1110
1111 static size_t
1112 fts_maxarglen(argv)
1113 char * const *argv;
1114 {
1115 size_t len, max;
1116
1117 for (max = 0; *argv; ++argv)
1118 if ((len = strlen(*argv)) > max)
1119 max = len;
1120 return (max + 1);
1121 }
1122
1123 /*
1124 * Change to dir specified by fd or p->fts_accpath without getting
1125 * tricked by someone changing the world out from underneath us.
1126 * Assumes p->fts_dev and p->fts_ino are filled in.
1127 */
1128 static int
1129 fts_safe_changedir(sp, p, fd, path)
1130 FTS *sp;
1131 FTSENT *p;
1132 int fd;
1133 const char *path;
1134 {
1135 int ret, oerrno, newfd;
1136 struct stat sb;
1137
1138 newfd = fd;
1139 if (ISSET(FTS_NOCHDIR))
1140 return (0);
1141 if (fd < 0 && (newfd = __open(path, O_RDONLY, 0)) < 0)
1142 return (-1);
1143 if (fstat(newfd, &sb)) {
1144 ret = -1;
1145 goto bail;
1146 }
1147 if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
1148 __set_errno (ENOENT); /* disinformation */
1149 ret = -1;
1150 goto bail;
1151 }
1152 ret = __fchdir(newfd);
1153 bail:
1154 oerrno = errno;
1155 if (fd < 0)
1156 (void)__close(newfd);
1157 __set_errno (oerrno);
1158 return (ret);
1159 }
1160
1161 #endif /* HAVE_FTS_OPEN */
1162 #endif