changes for whiteouts and union filesystem
[unix-history] / usr / src / lib / libc / gen / fts.c
CommitLineData
df589a0d 1/*-
3679b670 2 * Copyright (c) 1990, 1993, 1994
74155b62 3 * The Regents of the University of California. All rights reserved.
6f8dfca7 4 *
df589a0d 5 * %sccs.include.redist.c%
6f8dfca7
KB
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
88edbcd4 9static char sccsid[] = "@(#)fts.c 8.5 (Berkeley) %G%";
6f8dfca7
KB
10#endif /* LIBC_SCCS and not lint */
11
12#include <sys/param.h>
13#include <sys/stat.h>
3679b670 14
6f8dfca7
KB
15#include <dirent.h>
16#include <errno.h>
3679b670 17#include <fcntl.h>
a1297096 18#include <fts.h>
ccfce475 19#include <stdlib.h>
10f860f8 20#include <string.h>
2c9e6184 21#include <unistd.h>
6f8dfca7 22
feb226de
KB
23static FTSENT *fts_alloc __P((FTS *, char *, int));
24static FTSENT *fts_build __P((FTS *, int));
25static void fts_lfree __P((FTSENT *));
26static void fts_load __P((FTS *, FTSENT *));
dd92c531 27static size_t fts_maxarglen __P((char * const *));
a1297096 28static void fts_padjust __P((FTS *, void *));
0f7a6938 29static int fts_palloc __P((FTS *, size_t));
feb226de
KB
30static FTSENT *fts_sort __P((FTS *, FTSENT *, int));
31static u_short fts_stat __P((FTS *, FTSENT *, int));
6f8dfca7 32
a1297096
KB
33#define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
34
9f27273b
KB
35#define ISSET(opt) (sp->fts_options & opt)
36#define SET(opt) (sp->fts_options |= opt)
6f8dfca7 37
9f27273b
KB
38#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
39#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
6f8dfca7 40
c8f49d0a 41/* fts_build flags */
5dc08f4e
KB
42#define BCHILD 1 /* fts_children */
43#define BNAMES 2 /* fts_children, names only */
44#define BREAD 3 /* fts_read */
c8f49d0a 45
6f8dfca7 46FTS *
9f27273b 47fts_open(argv, options, compar)
2c9e6184 48 char * const *argv;
6f8dfca7 49 register int options;
10f860f8 50 int (*compar)();
6f8dfca7
KB
51{
52 register FTS *sp;
53 register FTSENT *p, *root;
dd92c531 54 register int nitems;
6f8dfca7 55 FTSENT *parent, *tmp;
10f860f8 56 int len;
6f8dfca7 57
5dc08f4e
KB
58 /* Options check. */
59 if (options & ~FTS_OPTIONMASK) {
60 errno = EINVAL;
61 return (NULL);
62 }
63
9f27273b 64 /* Allocate/initialize the stream */
42ed89ca
KB
65 if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
66 return (NULL);
0e3758ac 67 memset(sp, 0, sizeof(FTS));
df589a0d
KB
68 sp->fts_compar = compar;
69 sp->fts_options = options;
6f8dfca7 70
9f27273b
KB
71 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
72 if (ISSET(FTS_LOGICAL))
73 SET(FTS_NOCHDIR);
74
0f7a6938 75 /*
dd92c531
KB
76 * Start out with 1K of path space, and enough, in any case,
77 * to hold the user's paths.
0f7a6938 78 */
dd92c531 79 if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
0f7a6938
KB
80 goto mem1;
81
9f27273b 82 /* Allocate/initialize root's parent. */
82448bce 83 if ((parent = fts_alloc(sp, "", 0)) == NULL)
0f7a6938 84 goto mem2;
6cfdcbf9 85 parent->fts_level = FTS_ROOTPARENTLEVEL;
6f8dfca7 86
9f27273b 87 /* Allocate/initialize root(s). */
dd92c531 88 for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
63fabffe 89 /* Don't allow zero-length paths. */
a1297096 90 if ((len = strlen(*argv)) == 0) {
10f860f8 91 errno = ENOENT;
0f7a6938 92 goto mem3;
10f860f8 93 }
a1297096 94
10f860f8 95 p = fts_alloc(sp, *argv, len);
1724ec6f
KB
96 p->fts_level = FTS_ROOTLEVEL;
97 p->fts_parent = parent;
24552002 98 p->fts_accpath = p->fts_name;
a9f467bc 99 p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
a1297096
KB
100
101 /* Command-line "." and ".." are real directories. */
a1297096
KB
102 if (p->fts_info == FTS_DOT)
103 p->fts_info = FTS_D;
104
9a0aa1ee 105 /*
9f27273b 106 * If comparison routine supplied, traverse in sorted
9a0aa1ee
KB
107 * order; otherwise traverse in the order specified.
108 */
109 if (compar) {
110 p->fts_link = root;
111 root = p;
9a0aa1ee
KB
112 } else {
113 p->fts_link = NULL;
82448bce 114 if (root == NULL)
9a0aa1ee
KB
115 tmp = root = p;
116 else {
117 tmp->fts_link = p;
118 tmp = p;
6f8dfca7 119 }
6f8dfca7 120 }
6f8dfca7 121 }
9a0aa1ee 122 if (compar && nitems > 1)
9f27273b 123 root = fts_sort(sp, root, nitems);
6f8dfca7 124
6f8dfca7 125 /*
9f27273b 126 * Allocate a dummy pointer and make fts_read think that we've just
63fabffe 127 * finished the node before the root(s); set p->fts_info to FTS_INIT
ccfce475 128 * so that everything about the "current" node is ignored.
6f8dfca7 129 */
82448bce 130 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
0f7a6938 131 goto mem3;
ccfce475 132 sp->fts_cur->fts_link = root;
63fabffe 133 sp->fts_cur->fts_info = FTS_INIT;
ccfce475 134
6f8dfca7 135 /*
9f27273b 136 * If using chdir(2), grab a file descriptor pointing to dot to insure
c8f49d0a
KB
137 * that we can get back here; this could be avoided for some paths,
138 * but almost certainly not worth the effort. Slashes, symbolic links,
139 * and ".." are all fairly nasty problems. Note, if we can't get the
140 * descriptor we run anyway, just more slowly.
6f8dfca7 141 */
10f860f8 142 if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
9f27273b 143 SET(FTS_NOCHDIR);
6f8dfca7 144
42ed89ca 145 return (sp);
6f8dfca7 146
0f7a6938 147mem3: fts_lfree(root);
10f860f8 148 free(parent);
0f7a6938 149mem2: free(sp->fts_path);
10f860f8 150mem1: free(sp);
42ed89ca 151 return (NULL);
6f8dfca7
KB
152}
153
0a60cc84 154static void
9f27273b
KB
155fts_load(sp, p)
156 FTS *sp;
6f8dfca7
KB
157 register FTSENT *p;
158{
159 register int len;
160 register char *cp;
161
162 /*
0548d806
KB
163 * Load the stream structure for the next traversal. Since we don't
164 * actually enter the directory until after the preorder visit, set
165 * the fts_accpath field specially so the chdir gets done to the right
a1297096
KB
166 * place and the user can access the first node. From fts_open it's
167 * known that the path will fit.
6f8dfca7 168 */
df589a0d 169 len = p->fts_pathlen = p->fts_namelen;
0e3758ac
KB
170 memmove(sp->fts_path, p->fts_name, len + 1);
171 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
6f8dfca7 172 len = strlen(++cp);
0e3758ac 173 memmove(p->fts_name, cp, len + 1);
df589a0d 174 p->fts_namelen = len;
6f8dfca7 175 }
9f27273b 176 p->fts_accpath = p->fts_path = sp->fts_path;
a1297096 177 sp->fts_dev = p->fts_dev;
6f8dfca7
KB
178}
179
a1297096 180int
9f27273b 181fts_close(sp)
6f8dfca7
KB
182 FTS *sp;
183{
184 register FTSENT *freep, *p;
185 int saved_errno;
186
a1297096
KB
187 /*
188 * This still works if we haven't read anything -- the dummy structure
189 * points to the root list, so we step through to the end of the root
190 * list which has a valid parent pointer.
191 */
ccfce475 192 if (sp->fts_cur) {
a1297096 193 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
ccfce475
KB
194 freep = p;
195 p = p->fts_link ? p->fts_link : p->fts_parent;
10f860f8 196 free(freep);
6f8dfca7 197 }
10f860f8 198 free(p);
ccfce475 199 }
6f8dfca7 200
9f27273b 201 /* Free up child linked list, sort array, path buffer. */
df589a0d
KB
202 if (sp->fts_child)
203 fts_lfree(sp->fts_child);
204 if (sp->fts_array)
10f860f8
KB
205 free(sp->fts_array);
206 free(sp->fts_path);
6f8dfca7 207
10f860f8 208 /* Return to original directory, save errno if necessary. */
9f27273b 209 if (!ISSET(FTS_NOCHDIR)) {
10f860f8
KB
210 saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
211 (void)close(sp->fts_rfd);
6f8dfca7
KB
212 }
213
9f27273b 214 /* Free up the stream pointer. */
10f860f8 215 free(sp);
6f8dfca7 216
9f27273b
KB
217 /* Set errno and return. */
218 if (!ISSET(FTS_NOCHDIR) && saved_errno) {
6f8dfca7 219 errno = saved_errno;
42ed89ca 220 return (-1);
6f8dfca7 221 }
42ed89ca 222 return (0);
6f8dfca7
KB
223}
224
10f860f8 225/*
63fabffe
KB
226 * Special case a root of "/" so that slashes aren't appended which would
227 * cause paths to be written as "//foo".
10f860f8 228 */
dd92c531
KB
229#define NAPPEND(p) \
230 (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
10f860f8
KB
231 p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
232
6f8dfca7 233FTSENT *
9f27273b 234fts_read(sp)
6f8dfca7
KB
235 register FTS *sp;
236{
c8f49d0a 237 register FTSENT *p, *tmp;
6f8dfca7 238 register int instr;
10f860f8 239 register char *t;
f5e5bbf1 240 int saved_errno;
6f8dfca7 241
9f27273b 242 /* If finished or unrecoverable error, return NULL. */
82448bce 243 if (sp->fts_cur == NULL || ISSET(FTS_STOP))
42ed89ca 244 return (NULL);
6f8dfca7 245
9f27273b 246 /* Set current node pointer. */
df589a0d 247 p = sp->fts_cur;
6f8dfca7 248
9f27273b 249 /* Save and zero out user instructions. */
df589a0d 250 instr = p->fts_instr;
10f860f8 251 p->fts_instr = FTS_NOINSTR;
6f8dfca7 252
10f860f8 253 /* Any type of file may be re-visited; re-stat and re-turn. */
6f8dfca7 254 if (instr == FTS_AGAIN) {
9f27273b 255 p->fts_info = fts_stat(sp, p, 0);
42ed89ca 256 return (p);
6f8dfca7
KB
257 }
258
10f860f8
KB
259 /*
260 * Following a symlink -- SLNONE test allows application to see
307d787e
KB
261 * SLNONE and recover. If indirecting through a symlink, have
262 * keep a pointer to current location. If unable to get that
263 * pointer, follow fails.
10f860f8
KB
264 */
265 if (instr == FTS_FOLLOW &&
266 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
9f27273b 267 p->fts_info = fts_stat(sp, p, 1);
307d787e
KB
268 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
269 if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
270 p->fts_errno = errno;
271 p->fts_info = FTS_ERR;
272 } else
273 p->fts_flags |= FTS_SYMFOLLOW;
42ed89ca 274 return (p);
c8f49d0a
KB
275 }
276
9f27273b 277 /* Directory in pre-order. */
c8f49d0a 278 if (p->fts_info == FTS_D) {
9f27273b 279 /* If skipped or crossed mount point, do post-order visit. */
10f860f8 280 if (instr == FTS_SKIP ||
a1297096 281 ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
307d787e
KB
282 if (p->fts_flags & FTS_SYMFOLLOW)
283 (void)close(p->fts_symfd);
df589a0d
KB
284 if (sp->fts_child) {
285 fts_lfree(sp->fts_child);
286 sp->fts_child = NULL;
6f8dfca7 287 }
e8fecd88 288 p->fts_info = FTS_DP;
42ed89ca 289 return (p);
c8f49d0a
KB
290 }
291
f270a61b 292 /* Rebuild if only read the names and now traversing. */
5dc08f4e
KB
293 if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) {
294 sp->fts_options &= ~FTS_NAMEONLY;
295 fts_lfree(sp->fts_child);
296 sp->fts_child = NULL;
297 }
298
10f860f8 299 /*
f270a61b
KB
300 * Cd to the subdirectory.
301 *
0548d806 302 * If have already read and now fail to chdir, whack the list
f270a61b 303 * to make the names come out right, and set the parent errno
0548d806 304 * so the application will eventually get an error condition.
f270a61b
KB
305 * Set the FTS_DONTCHDIR flag so that when we logically change
306 * directories back to the parent we don't do a chdir.
307 *
308 * If haven't read do so. If the read fails, fts_build sets
309 * FTS_STOP or the fts_info field of the node.
10f860f8
KB
310 */
311 if (sp->fts_child) {
c8f49d0a 312 if (CHDIR(sp, p->fts_accpath)) {
64a76529 313 p->fts_errno = errno;
307d787e 314 p->fts_flags |= FTS_DONTCHDIR;
10f860f8
KB
315 for (p = sp->fts_child; p; p = p->fts_link)
316 p->fts_accpath =
317 p->fts_parent->fts_accpath;
c8f49d0a 318 }
82448bce 319 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
307d787e 320 if (ISSET(FTS_STOP))
42ed89ca 321 return (NULL);
42ed89ca 322 return (p);
0548d806 323 }
10f860f8 324 p = sp->fts_child;
c8f49d0a 325 sp->fts_child = NULL;
10f860f8 326 goto name;
6f8dfca7
KB
327 }
328
64a76529 329 /* Move to the next node on this level. */
c8f49d0a
KB
330next: tmp = p;
331 if (p = p->fts_link) {
10f860f8
KB
332 free(tmp);
333
b508e1fc
KB
334 /*
335 * If reached the top, return to the original directory, and
336 * load the paths for the next root.
337 */
6cfdcbf9 338 if (p->fts_level == FTS_ROOTLEVEL) {
b508e1fc
KB
339 if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) {
340 SET(FTS_STOP);
341 return (NULL);
342 }
0a60cc84 343 fts_load(sp, p);
42ed89ca 344 return (sp->fts_cur = p);
10f860f8 345 }
c8f49d0a 346
64a76529 347 /*
307d787e
KB
348 * User may have called fts_set on the node. If skipped,
349 * ignore. If followed, get a file descriptor so we can
350 * get back if necessary.
64a76529 351 */
307d787e
KB
352 if (p->fts_instr == FTS_SKIP)
353 goto next;
10f860f8
KB
354 if (p->fts_instr == FTS_FOLLOW) {
355 p->fts_info = fts_stat(sp, p, 1);
307d787e
KB
356 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
357 if ((p->fts_symfd =
358 open(".", O_RDONLY, 0)) < 0) {
359 p->fts_errno = errno;
360 p->fts_info = FTS_ERR;
361 } else
362 p->fts_flags |= FTS_SYMFOLLOW;
10f860f8 363 p->fts_instr = FTS_NOINSTR;
6f8dfca7 364 }
10f860f8
KB
365
366name: t = sp->fts_path + NAPPEND(p->fts_parent);
367 *t++ = '/';
0e3758ac 368 memmove(t, p->fts_name, p->fts_namelen + 1);
42ed89ca 369 return (sp->fts_cur = p);
6f8dfca7
KB
370 }
371
0548d806 372 /* Move up to the parent node. */
c8f49d0a 373 p = tmp->fts_parent;
10f860f8 374 free(tmp);
c8f49d0a 375
6cfdcbf9 376 if (p->fts_level == FTS_ROOTPARENTLEVEL) {
6f8dfca7 377 /*
9f27273b 378 * Done; free everything up and set errno to 0 so the user
6f8dfca7
KB
379 * can distinguish between error and EOF.
380 */
10f860f8 381 free(p);
6f8dfca7 382 errno = 0;
42ed89ca 383 return (sp->fts_cur = NULL);
6f8dfca7
KB
384 }
385
f270a61b 386 /* Nul terminate the pathname. */
df589a0d 387 sp->fts_path[p->fts_pathlen] = '\0';
10f860f8
KB
388
389 /*
f270a61b
KB
390 * Return to the parent directory. If at a root node or came through
391 * a symlink, go back through the file descriptor. Otherwise, cd up
392 * one directory.
0548d806
KB
393 */
394 if (p->fts_level == FTS_ROOTLEVEL) {
f270a61b 395 if (!ISSET(FTS_NOCHDIR) && FCHDIR(sp, sp->fts_rfd)) {
307d787e
KB
396 SET(FTS_STOP);
397 return (NULL);
398 }
307d787e
KB
399 } else if (p->fts_flags & FTS_SYMFOLLOW) {
400 if (FCHDIR(sp, p->fts_symfd)) {
f5e5bbf1 401 saved_errno = errno;
307d787e 402 (void)close(p->fts_symfd);
f5e5bbf1 403 errno = saved_errno;
0548d806 404 SET(FTS_STOP);
42ed89ca 405 return (NULL);
0548d806 406 }
307d787e
KB
407 (void)close(p->fts_symfd);
408 } else if (!(p->fts_flags & FTS_DONTCHDIR)) {
64a76529
KB
409 if (CHDIR(sp, "..")) {
410 SET(FTS_STOP);
411 return (NULL);
412 }
64a76529 413 }
307d787e 414 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
42ed89ca 415 return (sp->fts_cur = p);
6f8dfca7
KB
416}
417
418/*
0548d806 419 * Fts_set takes the stream as an argument although it's not used in this
6f8dfca7 420 * implementation; it would be necessary if anyone wanted to add global
9f27273b
KB
421 * semantics to fts using fts_set. An error return is allowed for similar
422 * reasons.
6f8dfca7
KB
423 */
424/* ARGSUSED */
a1297096 425int
9f27273b 426fts_set(sp, p, instr)
6f8dfca7
KB
427 FTS *sp;
428 FTSENT *p;
429 int instr;
430{
5dc08f4e
KB
431 if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
432 instr != FTS_NOINSTR && instr != FTS_SKIP) {
433 errno = EINVAL;
434 return (1);
435 }
df589a0d 436 p->fts_instr = instr;
42ed89ca 437 return (0);
6f8dfca7
KB
438}
439
440FTSENT *
5dc08f4e 441fts_children(sp, instr)
6f8dfca7 442 register FTS *sp;
5dc08f4e 443 int instr;
6f8dfca7 444{
c8f49d0a
KB
445 register FTSENT *p;
446 int fd;
447
5dc08f4e
KB
448 if (instr && instr != FTS_NAMEONLY) {
449 errno = EINVAL;
450 return (NULL);
451 }
452
9f27273b
KB
453 /* Set current node pointer. */
454 p = sp->fts_cur;
455
6f8dfca7 456 /*
63fabffe
KB
457 * Errno set to 0 so user can distinguish empty directory from
458 * an error.
6f8dfca7
KB
459 */
460 errno = 0;
63fabffe
KB
461
462 /* Fatal errors stop here. */
463 if (ISSET(FTS_STOP))
464 return (NULL);
465
466 /* Return logical hierarchy of user's arguments. */
467 if (p->fts_info == FTS_INIT)
468 return (p->fts_link);
469
307d787e
KB
470 /*
471 * If not a directory being visited in pre-order, stop here. Could
472 * allow FTS_DNR, assuming the user has fixed the problem, but the
473 * same effect is available with FTS_AGAIN.
474 */
475 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
42ed89ca 476 return (NULL);
9f27273b
KB
477
478 /* Free up any previous child list. */
df589a0d
KB
479 if (sp->fts_child)
480 fts_lfree(sp->fts_child);
c8f49d0a 481
5dc08f4e
KB
482 if (instr == FTS_NAMEONLY) {
483 sp->fts_options |= FTS_NAMEONLY;
484 instr = BNAMES;
485 } else
486 instr = BCHILD;
487
c8f49d0a 488 /*
9f27273b 489 * If using chdir on a relative path and called BEFORE fts_read does
10f860f8
KB
490 * its chdir to the root of a traversal, we can lose -- we need to
491 * chdir into the subdirectory, and we don't know where the current
492 * directory is, so we can't get back so that the upcoming chdir by
493 * fts_read will work.
c8f49d0a 494 */
6cfdcbf9 495 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
9f27273b 496 ISSET(FTS_NOCHDIR))
5dc08f4e 497 return (sp->fts_child = fts_build(sp, instr));
c8f49d0a
KB
498
499 if ((fd = open(".", O_RDONLY, 0)) < 0)
42ed89ca 500 return (NULL);
5dc08f4e 501 sp->fts_child = fts_build(sp, instr);
c8f49d0a 502 if (fchdir(fd))
42ed89ca 503 return (NULL);
c8f49d0a 504 (void)close(fd);
42ed89ca 505 return (sp->fts_child);
6f8dfca7
KB
506}
507
10f860f8
KB
508/*
509 * This is the tricky part -- do not casually change *anything* in here. The
510 * idea is to build the linked list of entries that are used by fts_children
511 * and fts_read. There are lots of special cases.
512 *
513 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
514 * set and it's a physical walk (so that symbolic links can't be directories),
12fb4769
KB
515 * we can do things quickly. First, if it's a 4.4BSD file system, the type
516 * of the file is in the directory entry. Otherwise, we assume that the number
517 * of subdirectories in a node is equal to the number of links to the parent.
518 * The former skips all stat calls. The latter skips stat calls in any leaf
519 * directories and for any files after the subdirectories in the directory have
520 * been found, cutting the stat calls by about 2/3.
10f860f8 521 */
2c9e6184 522static FTSENT *
c8f49d0a 523fts_build(sp, type)
6f8dfca7 524 register FTS *sp;
c8f49d0a 525 int type;
6f8dfca7
KB
526{
527 register struct dirent *dp;
528 register FTSENT *p, *head;
529 register int nitems;
307d787e 530 FTSENT *cur, *tail;
6f8dfca7 531 DIR *dirp;
a1297096 532 void *adjaddr;
88edbcd4 533 int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno;
6f8dfca7
KB
534 char *cp;
535
9f27273b 536 /* Set current node pointer. */
10f860f8 537 cur = sp->fts_cur;
9f27273b 538
10f860f8
KB
539 /*
540 * Open the directory for reading. If this fails, we're done.
541 * If being called from fts_read, set the fts_info field.
542 */
88edbcd4
JSP
543#ifdef FTS_WHITEOUT
544 if (ISSET(FTS_WHITEOUT))
545 oflag = DTF_NODUP|DTF_REWIND;
546 else
547 oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
548#else
549#define __opendir2(path, flag) opendir(path)
550#endif
551 if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
2fadc34d 552 if (type == BREAD) {
10f860f8 553 cur->fts_info = FTS_DNR;
2fadc34d
KB
554 cur->fts_errno = errno;
555 }
42ed89ca 556 return (NULL);
6f8dfca7 557 }
6f8dfca7
KB
558
559 /*
10f860f8
KB
560 * Nlinks is the number of possible entries of type directory in the
561 * directory if we're cheating on stat calls, 0 if we're not doing
562 * any stat calls at all, -1 if we're doing stats on everything.
6f8dfca7 563 */
5dc08f4e
KB
564 if (type == BNAMES)
565 nlinks = 0;
566 else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL))
567 nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
568 else
569 nlinks = -1;
9f27273b 570
307d787e
KB
571#ifdef notdef
572 (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
573 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
574 ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
575#endif
10f860f8
KB
576 /*
577 * If we're going to need to stat anything or we want to descend
acfbcf26
KB
578 * and stay in the directory, chdir. If this fails we keep going,
579 * but set a flag so we don't chdir after the post-order visit.
10f860f8
KB
580 * We won't be able to stat anything, but we can still return the
581 * names themselves. Note, that since fts_read won't be able to
582 * chdir into the directory, it will have to return different path
583 * names than before, i.e. "a/b" instead of "b". Since the node
584 * has already been visited in pre-order, have to wait until the
307d787e
KB
585 * post-order visit to return the error. There is a special case
586 * here, if there was nothing to stat then it's not an error to
587 * not be able to stat. This is all fairly nasty. If a program
588 * needed sorted entries or stat information, they had better be
589 * checking FTS_NS on the returned nodes.
10f860f8 590 */
b508e1fc 591 cderrno = 0;
9f27273b 592 if (nlinks || type == BREAD)
10f860f8 593 if (FCHDIR(sp, dirfd(dirp))) {
897b637d 594 if (nlinks && type == BREAD)
63fabffe 595 cur->fts_errno = errno;
897b637d 596 cur->fts_flags |= FTS_DONTCHDIR;
307d787e 597 descend = 0;
64a76529 598 cderrno = errno;
b508e1fc 599 } else
9f27273b 600 descend = 1;
10f860f8
KB
601 else
602 descend = 0;
1730d75f 603
10f860f8
KB
604 /*
605 * Figure out the max file name length that can be stored in the
606 * current path -- the inner loop allocates more path as necessary.
607 * We really wouldn't have to do the maxlen calculations here, we
608 * could do them in fts_read before returning the path, but it's a
609 * lot easier here since the length is part of the dirent structure.
610 *
a1297096
KB
611 * If not changing directories set a pointer so that can just append
612 * each new name into the path.
10f860f8
KB
613 */
614 maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
615 len = NAPPEND(cur);
616 if (ISSET(FTS_NOCHDIR)) {
617 cp = sp->fts_path + len;
618 *cp++ = '/';
619 }
9b63b883 620
10f860f8 621 level = cur->fts_level + 1;
9b63b883 622
10f860f8 623 /* Read the directory, attaching each entry to the `link' pointer. */
a1297096 624 adjaddr = NULL;
307d787e 625 for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
10f860f8 626 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
6f8dfca7
KB
627 continue;
628
82448bce 629 if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
6f8dfca7 630 goto mem1;
6f8dfca7 631 if (dp->d_namlen > maxlen) {
0f7a6938 632 if (fts_palloc(sp, (size_t)dp->d_namlen)) {
10f860f8
KB
633 /*
634 * No more memory for path or structures. Save
635 * errno, free up the current structure and the
636 * structures already allocated.
637 */
638mem1: saved_errno = errno;
639 if (p)
640 free(p);
641 fts_lfree(head);
c8f49d0a 642 (void)closedir(dirp);
10f860f8
KB
643 errno = saved_errno;
644 cur->fts_info = FTS_ERR;
645 SET(FTS_STOP);
42ed89ca 646 return (NULL);
6f8dfca7 647 }
a1297096 648 adjaddr = sp->fts_path;
df589a0d 649 maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
6f8dfca7
KB
650 }
651
df589a0d 652 p->fts_pathlen = len + dp->d_namlen + 1;
df589a0d
KB
653 p->fts_parent = sp->fts_cur;
654 p->fts_level = level;
6f8dfca7 655
88edbcd4
JSP
656#ifdef FTS_WHITEOUT
657 switch (dp->d_type) {
658 case DT_WHT:
659 p->fts_flags |= FTS_ISW;
660 break;
661 case DT_WHTD:
662 p->fts_flags |= FTS_ISWD;
663 break;
664 }
665#endif
666
307d787e
KB
667 if (cderrno) {
668 if (nlinks) {
669 p->fts_info = FTS_NS;
670 p->fts_errno = cderrno;
671 } else
672 p->fts_info = FTS_NSOK;
673 p->fts_accpath = cur->fts_accpath;
12fb4769
KB
674 } else if (nlinks == 0
675#ifdef DT_DIR
676 || nlinks > 0 &&
677 dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN
678#endif
679 ) {
680 p->fts_accpath =
681 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
682 p->fts_info = FTS_NSOK;
683 } else {
10f860f8
KB
684 /* Build a file name for fts_stat to stat. */
685 if (ISSET(FTS_NOCHDIR)) {
686 p->fts_accpath = p->fts_path;
0e3758ac 687 memmove(cp, p->fts_name, p->fts_namelen + 1);
10f860f8
KB
688 } else
689 p->fts_accpath = p->fts_name;
12fb4769 690 /* Stat it. */
9f27273b 691 p->fts_info = fts_stat(sp, p, 0);
12fb4769
KB
692
693 /* Decrement link count if applicable. */
a1297096
KB
694 if (nlinks > 0 && (p->fts_info == FTS_D ||
695 p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
6f8dfca7 696 --nlinks;
10f860f8 697 }
6f8dfca7 698
307d787e
KB
699 /* We walk in directory order so "ls -f" doesn't get upset. */
700 p->fts_link = NULL;
701 if (head == NULL)
702 head = tail = p;
703 else {
704 tail->fts_link = p;
705 tail = p;
706 }
6f8dfca7
KB
707 ++nitems;
708 }
709 (void)closedir(dirp);
710
a1297096
KB
711 /*
712 * If had to realloc the path, adjust the addresses for the rest
713 * of the tree.
714 */
715 if (adjaddr)
716 fts_padjust(sp, adjaddr);
717
10f860f8
KB
718 /*
719 * If not changing directories, reset the path back to original
720 * state.
721 */
722 if (ISSET(FTS_NOCHDIR)) {
723 if (cp - 1 > sp->fts_path)
724 --cp;
725 *cp = '\0';
726 }
c8f49d0a
KB
727
728 /*
3679b670
KB
729 * If descended after called from fts_children or after called from
730 * fts_read and nothing found, get back. At the root level we use
731 * the saved fd; if one of fts_open()'s arguments is a relative path
732 * to an empty directory, we wind up here with no other way back. If
733 * can't get back, we're done.
c8f49d0a 734 */
3679b670
KB
735 if (descend && (type == BCHILD || !nitems) &&
736 (cur->fts_level == FTS_ROOTLEVEL ?
737 FCHDIR(sp, sp->fts_rfd) : CHDIR(sp, ".."))) {
10f860f8
KB
738 cur->fts_info = FTS_ERR;
739 SET(FTS_STOP);
42ed89ca 740 return (NULL);
1730d75f
KB
741 }
742
f5e5bbf1 743 /* If didn't find anything, return NULL. */
f270a61b
KB
744 if (!nitems) {
745 if (type == BREAD)
746 cur->fts_info = FTS_DP;
42ed89ca 747 return (NULL);
f270a61b 748 }
6f8dfca7 749
10f860f8 750 /* Sort the entries. */
df589a0d 751 if (sp->fts_compar && nitems > 1)
9f27273b 752 head = fts_sort(sp, head, nitems);
42ed89ca 753 return (head);
6f8dfca7
KB
754}
755
9f27273b
KB
756static u_short
757fts_stat(sp, p, follow)
758 FTS *sp;
6f8dfca7 759 register FTSENT *p;
9f27273b 760 int follow;
6f8dfca7 761{
63fabffe
KB
762 register FTSENT *t;
763 register dev_t dev;
764 register ino_t ino;
a1297096 765 struct stat *sbp, sb;
10f860f8
KB
766 int saved_errno;
767
a1297096
KB
768 /* If user needs stat info, stat buffer already allocated. */
769 sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
88edbcd4
JSP
770
771#ifdef FTS_WHITEOUT
772 /* check for whiteout */
773 if (p->fts_flags & (FTS_ISW|FTS_ISWD)) {
774 if (sbp != &sb) {
775 memset(sbp, '\0', sizeof (*sbp));
776 sbp->st_mode = S_IFWHT;
777 }
778 return (FTS_W);
779 }
780#endif
a1297096 781
6f8dfca7 782 /*
9f27273b 783 * If doing a logical walk, or application requested FTS_FOLLOW, do
10f860f8 784 * a stat(2). If that fails, check for a non-existent symlink. If
63fabffe 785 * fail, set the errno from the stat call.
6f8dfca7 786 */
9f27273b 787 if (ISSET(FTS_LOGICAL) || follow) {
a1297096 788 if (stat(p->fts_accpath, sbp)) {
10f860f8 789 saved_errno = errno;
a1297096 790 if (!lstat(p->fts_accpath, sbp)) {
9f27273b 791 errno = 0;
42ed89ca 792 return (FTS_SLNONE);
10f860f8 793 }
63fabffe 794 p->fts_errno = saved_errno;
a1297096 795 goto err;
9f27273b 796 }
a1297096 797 } else if (lstat(p->fts_accpath, sbp)) {
63fabffe 798 p->fts_errno = errno;
0e3758ac 799err: memset(sbp, 0, sizeof(struct stat));
42ed89ca 800 return (FTS_NS);
9f27273b
KB
801 }
802
a1297096 803 if (S_ISDIR(sbp->st_mode)) {
a1297096
KB
804 /*
805 * Set the device/inode. Used to find cycles and check for
806 * crossing mount points. Also remember the link count, used
807 * in fts_build to limit the number of stat calls. It is
808 * understood that these fields are only referenced if fts_info
809 * is set to FTS_D.
810 */
811 dev = p->fts_dev = sbp->st_dev;
812 ino = p->fts_ino = sbp->st_ino;
813 p->fts_nlink = sbp->st_nlink;
814
307d787e
KB
815 if (ISDOT(p->fts_name))
816 return (FTS_DOT);
817
a1297096
KB
818 /*
819 * Cycle detection is done by brute force when the directory
820 * is first encountered. If the tree gets deep enough or the
821 * number of symbolic links to directories is high enough,
822 * something faster might be worthwhile.
823 */
824 for (t = p->fts_parent;
825 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
826 if (ino == t->fts_ino && dev == t->fts_dev) {
7724243a 827 p->fts_cycle = t;
42ed89ca 828 return (FTS_DC);
10f860f8 829 }
42ed89ca 830 return (FTS_D);
10f860f8 831 }
a1297096 832 if (S_ISLNK(sbp->st_mode))
42ed89ca 833 return (FTS_SL);
a1297096 834 if (S_ISREG(sbp->st_mode))
42ed89ca
KB
835 return (FTS_F);
836 return (FTS_DEFAULT);
6f8dfca7
KB
837}
838
6f8dfca7 839static FTSENT *
9f27273b
KB
840fts_sort(sp, head, nitems)
841 FTS *sp;
6f8dfca7
KB
842 FTSENT *head;
843 register int nitems;
844{
845 register FTSENT **ap, *p;
846
847 /*
9f27273b 848 * Construct an array of pointers to the structures and call qsort(3).
6f8dfca7
KB
849 * Reassemble the array in the order returned by qsort. If unable to
850 * sort for memory reasons, return the directory entries in their
851 * current order. Allocate enough space for the current needs plus
a1297096 852 * 40 so don't realloc one entry at a time.
6f8dfca7 853 */
9f27273b
KB
854 if (nitems > sp->fts_nitems) {
855 sp->fts_nitems = nitems + 40;
a1297096
KB
856 if ((sp->fts_array = realloc(sp->fts_array,
857 (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
9f27273b 858 sp->fts_nitems = 0;
42ed89ca 859 return (head);
6f8dfca7
KB
860 }
861 }
9f27273b 862 for (ap = sp->fts_array, p = head; p; p = p->fts_link)
6f8dfca7 863 *ap++ = p;
9f27273b
KB
864 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
865 for (head = *(ap = sp->fts_array); --nitems; ++ap)
df589a0d
KB
866 ap[0]->fts_link = ap[1];
867 ap[0]->fts_link = NULL;
42ed89ca 868 return (head);
6f8dfca7
KB
869}
870
871static FTSENT *
0f7a6938 872fts_alloc(sp, name, namelen)
9f27273b 873 FTS *sp;
6f8dfca7 874 char *name;
0f7a6938 875 register int namelen;
6f8dfca7
KB
876{
877 register FTSENT *p;
0f7a6938 878 size_t len;
6f8dfca7
KB
879
880 /*
0f7a6938
KB
881 * The file name is a variable length array and no stat structure is
882 * necessary if the user has set the nostat bit. Allocate the FTSENT
883 * structure, the file name and the stat structure in one chunk, but
884 * be careful that the stat structure is reasonably aligned. Since the
885 * fts_name field is declared to be of size 1, the fts_name pointer is
886 * namelen + 2 before the first possible address of the stat structure.
6f8dfca7 887 */
0f7a6938
KB
888 len = sizeof(FTSENT) + namelen;
889 if (!ISSET(FTS_NOSTAT))
890 len += sizeof(struct stat) + ALIGNBYTES;
891 if ((p = malloc(len)) == NULL)
42ed89ca 892 return (NULL);
0f7a6938
KB
893
894 /* Copy the name plus the trailing NULL. */
0e3758ac 895 memmove(p->fts_name, name, namelen + 1);
0f7a6938
KB
896
897 if (!ISSET(FTS_NOSTAT))
898 p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
899 p->fts_namelen = namelen;
9f27273b 900 p->fts_path = sp->fts_path;
63fabffe 901 p->fts_errno = 0;
307d787e
KB
902 p->fts_flags = 0;
903 p->fts_instr = FTS_NOINSTR;
9f27273b
KB
904 p->fts_number = 0;
905 p->fts_pointer = NULL;
42ed89ca 906 return (p);
6f8dfca7
KB
907}
908
9f27273b 909static void
6f8dfca7
KB
910fts_lfree(head)
911 register FTSENT *head;
912{
913 register FTSENT *p;
914
9f27273b 915 /* Free a linked list of structures. */
6f8dfca7 916 while (p = head) {
df589a0d 917 head = head->fts_link;
10f860f8 918 free(p);
6f8dfca7
KB
919 }
920}
921
922/*
a1297096
KB
923 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
924 * Most systems will allow creation of paths much longer than MAXPATHLEN, even
925 * though the kernel won't resolve them. Add the size (not just what's needed)
926 * plus 256 bytes so don't realloc the path 2 bytes at a time.
927 */
928static int
929fts_palloc(sp, more)
930 FTS *sp;
0f7a6938 931 size_t more;
a1297096 932{
a1297096
KB
933 sp->fts_pathlen += more + 256;
934 sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen);
935 return (sp->fts_path == NULL);
936}
937
938/*
939 * When the path is realloc'd, have to fix all of the pointers in structures
940 * already returned.
6f8dfca7 941 */
a1297096
KB
942static void
943fts_padjust(sp, addr)
9f27273b 944 FTS *sp;
a1297096 945 void *addr;
6f8dfca7 946{
a1297096
KB
947 FTSENT *p;
948
dd92c531 949#define ADJUST(p) { \
1815ec15
KB
950 (p)->fts_accpath = \
951 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
dd92c531 952 (p)->fts_path = addr; \
a1297096
KB
953}
954 /* Adjust the current set of children. */
955 for (p = sp->fts_child; p; p = p->fts_link)
956 ADJUST(p);
957
958 /* Adjust the rest of the tree. */
959 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
960 ADJUST(p);
961 p = p->fts_link ? p->fts_link : p->fts_parent;
962 }
2c9e6184 963}
dd92c531
KB
964
965static size_t
966fts_maxarglen(argv)
967 char * const *argv;
968{
969 size_t len, max;
970
971 for (max = 0; *argv; ++argv)
972 if ((len = strlen(*argv)) > max)
973 max = len;
974 return (max);
975}