fts.c revision 1.1.1.1 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1990 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
35 1.1 cgd static char sccsid[] = "@(#)fts.c 5.19 (Berkeley) 5/9/91";
36 1.1 cgd #endif /* LIBC_SCCS and not lint */
37 1.1 cgd
38 1.1 cgd #include <sys/cdefs.h>
39 1.1 cgd #include <sys/param.h>
40 1.1 cgd #include <sys/stat.h>
41 1.1 cgd #include <fcntl.h>
42 1.1 cgd #include <dirent.h>
43 1.1 cgd #include <errno.h>
44 1.1 cgd #include "fts.h"
45 1.1 cgd #include <stdlib.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd #include <unistd.h>
48 1.1 cgd
49 1.1 cgd static FTSENT *fts_alloc(), *fts_build(), *fts_sort();
50 1.1 cgd static void fts_load(), fts_lfree();
51 1.1 cgd static u_short fts_stat();
52 1.1 cgd static char *fts_path();
53 1.1 cgd
54 1.1 cgd #define ISSET(opt) (sp->fts_options & opt)
55 1.1 cgd #define SET(opt) (sp->fts_options |= opt)
56 1.1 cgd
57 1.1 cgd #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
58 1.1 cgd #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
59 1.1 cgd
60 1.1 cgd /* fts_build flags */
61 1.1 cgd #define BCHILD 1 /* from fts_children */
62 1.1 cgd #define BREAD 2 /* from fts_read */
63 1.1 cgd
64 1.1 cgd FTS *
65 1.1 cgd fts_open(argv, options, compar)
66 1.1 cgd char * const *argv;
67 1.1 cgd register int options;
68 1.1 cgd int (*compar)();
69 1.1 cgd {
70 1.1 cgd register FTS *sp;
71 1.1 cgd register FTSENT *p, *root;
72 1.1 cgd register int nitems, maxlen;
73 1.1 cgd FTSENT *parent, *tmp;
74 1.1 cgd int len;
75 1.1 cgd
76 1.1 cgd /* Allocate/initialize the stream */
77 1.1 cgd if (!(sp = (FTS *)malloc((u_int)sizeof(FTS))))
78 1.1 cgd return(NULL);
79 1.1 cgd bzero(sp, sizeof(FTS));
80 1.1 cgd sp->fts_compar = compar;
81 1.1 cgd sp->fts_options = options;
82 1.1 cgd
83 1.1 cgd /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
84 1.1 cgd if (ISSET(FTS_LOGICAL))
85 1.1 cgd SET(FTS_NOCHDIR);
86 1.1 cgd
87 1.1 cgd /* Allocate/initialize root's parent. */
88 1.1 cgd if (!(parent = fts_alloc(sp, "", 0)))
89 1.1 cgd goto mem1;
90 1.1 cgd parent->fts_level = FTS_ROOTPARENTLEVEL;
91 1.1 cgd
92 1.1 cgd /* Allocate/initialize root(s). */
93 1.1 cgd maxlen = -1;
94 1.1 cgd for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
95 1.1 cgd if (!(len = strlen(*argv))) {
96 1.1 cgd errno = ENOENT;
97 1.1 cgd goto mem2;
98 1.1 cgd }
99 1.1 cgd if (maxlen < len)
100 1.1 cgd maxlen = len;
101 1.1 cgd p = fts_alloc(sp, *argv, len);
102 1.1 cgd p->fts_level = FTS_ROOTLEVEL;
103 1.1 cgd p->fts_parent = parent;
104 1.1 cgd /*
105 1.1 cgd * If comparison routine supplied, traverse in sorted
106 1.1 cgd * order; otherwise traverse in the order specified.
107 1.1 cgd */
108 1.1 cgd if (compar) {
109 1.1 cgd p->fts_link = root;
110 1.1 cgd root = p;
111 1.1 cgd p->fts_accpath = p->fts_name;
112 1.1 cgd if (!(options & FTS_NOSTAT))
113 1.1 cgd p->fts_info = fts_stat(sp, p, 0);
114 1.1 cgd } else {
115 1.1 cgd p->fts_link = NULL;
116 1.1 cgd if (!root)
117 1.1 cgd tmp = root = p;
118 1.1 cgd else {
119 1.1 cgd tmp->fts_link = p;
120 1.1 cgd tmp = p;
121 1.1 cgd }
122 1.1 cgd }
123 1.1 cgd }
124 1.1 cgd if (compar && nitems > 1)
125 1.1 cgd root = fts_sort(sp, root, nitems);
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * Allocate a dummy pointer and make fts_read think that we've just
129 1.1 cgd * finished the node before the root(s); set p->fts_info to FTS_NS
130 1.1 cgd * so that everything about the "current" node is ignored.
131 1.1 cgd */
132 1.1 cgd if (!(sp->fts_cur = fts_alloc(sp, "", 0)))
133 1.1 cgd goto mem2;
134 1.1 cgd sp->fts_cur->fts_link = root;
135 1.1 cgd sp->fts_cur->fts_info = FTS_NS;
136 1.1 cgd
137 1.1 cgd /* Start out with at least 1K+ of path space. */
138 1.1 cgd if (!fts_path(sp, MAX(maxlen, MAXPATHLEN)))
139 1.1 cgd goto mem3;
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * If using chdir(2), grab a file descriptor pointing to dot to insure
143 1.1 cgd * that we can get back here; this could be avoided for some paths,
144 1.1 cgd * but almost certainly not worth the effort. Slashes, symbolic links,
145 1.1 cgd * and ".." are all fairly nasty problems. Note, if we can't get the
146 1.1 cgd * descriptor we run anyway, just more slowly.
147 1.1 cgd */
148 1.1 cgd if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
149 1.1 cgd SET(FTS_NOCHDIR);
150 1.1 cgd
151 1.1 cgd return(sp);
152 1.1 cgd
153 1.1 cgd mem3: free(sp->fts_cur);
154 1.1 cgd mem2: fts_lfree(root);
155 1.1 cgd free(parent);
156 1.1 cgd mem1: free(sp);
157 1.1 cgd return(NULL);
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd static void
161 1.1 cgd fts_load(sp, p)
162 1.1 cgd FTS *sp;
163 1.1 cgd register FTSENT *p;
164 1.1 cgd {
165 1.1 cgd register int len;
166 1.1 cgd register char *cp;
167 1.1 cgd
168 1.1 cgd /*
169 1.1 cgd * Load the stream structure for the next traversal. Since we don't
170 1.1 cgd * actually enter the directory until after the preorder visit, set
171 1.1 cgd * the fts_accpath field specially so the chdir gets done to the right
172 1.1 cgd * place and the user can access the first node.
173 1.1 cgd */
174 1.1 cgd len = p->fts_pathlen = p->fts_namelen;
175 1.1 cgd bcopy(p->fts_name, sp->fts_path, len + 1);
176 1.1 cgd if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
177 1.1 cgd len = strlen(++cp);
178 1.1 cgd bcopy(cp, p->fts_name, len + 1);
179 1.1 cgd p->fts_namelen = len;
180 1.1 cgd }
181 1.1 cgd p->fts_accpath = p->fts_path = sp->fts_path;
182 1.1 cgd
183 1.1 cgd p->fts_info = fts_stat(sp, p, 0);
184 1.1 cgd sp->rdev = p->fts_statb.st_dev;
185 1.1 cgd }
186 1.1 cgd
187 1.1 cgd fts_close(sp)
188 1.1 cgd FTS *sp;
189 1.1 cgd {
190 1.1 cgd register FTSENT *freep, *p;
191 1.1 cgd int saved_errno;
192 1.1 cgd
193 1.1 cgd if (sp->fts_cur) {
194 1.1 cgd /*
195 1.1 cgd * This still works if we haven't read anything -- the dummy
196 1.1 cgd * structure points to the root list, so we step through to
197 1.1 cgd * the end of the root list which has a valid parent pointer.
198 1.1 cgd */
199 1.1 cgd for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) {
200 1.1 cgd freep = p;
201 1.1 cgd p = p->fts_link ? p->fts_link : p->fts_parent;
202 1.1 cgd free(freep);
203 1.1 cgd }
204 1.1 cgd free(p);
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd /* Free up child linked list, sort array, path buffer. */
208 1.1 cgd if (sp->fts_child)
209 1.1 cgd fts_lfree(sp->fts_child);
210 1.1 cgd if (sp->fts_array)
211 1.1 cgd free(sp->fts_array);
212 1.1 cgd free(sp->fts_path);
213 1.1 cgd
214 1.1 cgd /* Return to original directory, save errno if necessary. */
215 1.1 cgd if (!ISSET(FTS_NOCHDIR)) {
216 1.1 cgd saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
217 1.1 cgd (void)close(sp->fts_rfd);
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd /* Free up the stream pointer. */
221 1.1 cgd free(sp);
222 1.1 cgd
223 1.1 cgd /* Set errno and return. */
224 1.1 cgd if (!ISSET(FTS_NOCHDIR) && saved_errno) {
225 1.1 cgd errno = saved_errno;
226 1.1 cgd return(-1);
227 1.1 cgd }
228 1.1 cgd return(0);
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd /*
232 1.1 cgd * Special case a root of "/" so that slashes aren't appended causing
233 1.1 cgd * paths to be written as "//foo".
234 1.1 cgd */
235 1.1 cgd #define NAPPEND(p) \
236 1.1 cgd (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
237 1.1 cgd p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
238 1.1 cgd
239 1.1 cgd FTSENT *
240 1.1 cgd fts_read(sp)
241 1.1 cgd register FTS *sp;
242 1.1 cgd {
243 1.1 cgd register FTSENT *p, *tmp;
244 1.1 cgd register int instr;
245 1.1 cgd register char *t;
246 1.1 cgd
247 1.1 cgd /* If finished or unrecoverable error, return NULL. */
248 1.1 cgd if (!sp->fts_cur || ISSET(FTS_STOP))
249 1.1 cgd return(NULL);
250 1.1 cgd
251 1.1 cgd /* Set current node pointer. */
252 1.1 cgd p = sp->fts_cur;
253 1.1 cgd
254 1.1 cgd /* Save and zero out user instructions. */
255 1.1 cgd instr = p->fts_instr;
256 1.1 cgd p->fts_instr = FTS_NOINSTR;
257 1.1 cgd
258 1.1 cgd /* If used fts_link pointer for cycle detection, restore it. */
259 1.1 cgd if (sp->fts_savelink) {
260 1.1 cgd p->fts_link = sp->fts_savelink;
261 1.1 cgd sp->fts_savelink = NULL;
262 1.1 cgd }
263 1.1 cgd
264 1.1 cgd /* Any type of file may be re-visited; re-stat and re-turn. */
265 1.1 cgd if (instr == FTS_AGAIN) {
266 1.1 cgd p->fts_info = fts_stat(sp, p, 0);
267 1.1 cgd return(p);
268 1.1 cgd }
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * Following a symlink -- SLNONE test allows application to see
272 1.1 cgd * SLNONE and recover.
273 1.1 cgd */
274 1.1 cgd if (instr == FTS_FOLLOW &&
275 1.1 cgd (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
276 1.1 cgd p->fts_info = fts_stat(sp, p, 1);
277 1.1 cgd return(p);
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd /* Directory in pre-order. */
281 1.1 cgd if (p->fts_info == FTS_D) {
282 1.1 cgd /* If skipped or crossed mount point, do post-order visit. */
283 1.1 cgd if (instr == FTS_SKIP ||
284 1.1 cgd ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) {
285 1.1 cgd if (sp->fts_child) {
286 1.1 cgd fts_lfree(sp->fts_child);
287 1.1 cgd sp->fts_child = NULL;
288 1.1 cgd }
289 1.1 cgd p->fts_info = FTS_DP;
290 1.1 cgd return(p);
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd /*
294 1.1 cgd * Cd to the subdirectory, reading it if haven't already. If
295 1.1 cgd * the read fails for any reason, or the directory is empty,
296 1.1 cgd * the fts_info field of the current node is set by fts_build.
297 1.1 cgd * If have already read and now fail to chdir, whack the list
298 1.1 cgd * to make the names come out right, and set the parent state
299 1.1 cgd * so the application will eventually get an error condition.
300 1.1 cgd * If haven't read and fail to chdir, check to see if we're
301 1.1 cgd * at the root node -- if so, we have to get back or the root
302 1.1 cgd * node may be inaccessible.
303 1.1 cgd */
304 1.1 cgd if (sp->fts_child) {
305 1.1 cgd if (CHDIR(sp, p->fts_accpath)) {
306 1.1 cgd p->fts_parent->fts_cderr = errno;
307 1.1 cgd for (p = sp->fts_child; p; p = p->fts_link)
308 1.1 cgd p->fts_accpath =
309 1.1 cgd p->fts_parent->fts_accpath;
310 1.1 cgd }
311 1.1 cgd } else if (!(sp->fts_child = fts_build(sp, BREAD))) {
312 1.1 cgd if ISSET(FTS_STOP)
313 1.1 cgd return(NULL);
314 1.1 cgd if (p->fts_level == FTS_ROOTLEVEL &&
315 1.1 cgd FCHDIR(sp, sp->fts_rfd)) {
316 1.1 cgd SET(FTS_STOP);
317 1.1 cgd return(NULL);
318 1.1 cgd }
319 1.1 cgd return(p);
320 1.1 cgd }
321 1.1 cgd p = sp->fts_child;
322 1.1 cgd sp->fts_child = NULL;
323 1.1 cgd goto name;
324 1.1 cgd }
325 1.1 cgd
326 1.1 cgd /* Move to next node on this level. */
327 1.1 cgd next: tmp = p;
328 1.1 cgd if (p = p->fts_link) {
329 1.1 cgd free(tmp);
330 1.1 cgd
331 1.1 cgd /* If reached the top, load the paths for the next root. */
332 1.1 cgd if (p->fts_level == FTS_ROOTLEVEL) {
333 1.1 cgd fts_load(sp, p);
334 1.1 cgd return(sp->fts_cur = p);
335 1.1 cgd }
336 1.1 cgd
337 1.1 cgd /* User may have called fts_set on the node. */
338 1.1 cgd if (p->fts_instr == FTS_SKIP)
339 1.1 cgd goto next;
340 1.1 cgd if (p->fts_instr == FTS_FOLLOW) {
341 1.1 cgd p->fts_info = fts_stat(sp, p, 1);
342 1.1 cgd p->fts_instr = FTS_NOINSTR;
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd name: t = sp->fts_path + NAPPEND(p->fts_parent);
346 1.1 cgd *t++ = '/';
347 1.1 cgd bcopy(p->fts_name, t, p->fts_namelen + 1);
348 1.1 cgd return(sp->fts_cur = p);
349 1.1 cgd }
350 1.1 cgd
351 1.1 cgd /* Move up to the parent node. */
352 1.1 cgd p = tmp->fts_parent;
353 1.1 cgd free(tmp);
354 1.1 cgd
355 1.1 cgd if (p->fts_level == FTS_ROOTPARENTLEVEL) {
356 1.1 cgd /*
357 1.1 cgd * Done; free everything up and set errno to 0 so the user
358 1.1 cgd * can distinguish between error and EOF.
359 1.1 cgd */
360 1.1 cgd free(p);
361 1.1 cgd errno = 0;
362 1.1 cgd return(sp->fts_cur = NULL);
363 1.1 cgd }
364 1.1 cgd
365 1.1 cgd sp->fts_path[p->fts_pathlen] = '\0';
366 1.1 cgd
367 1.1 cgd /*
368 1.1 cgd * Cd back up to the parent directory. If at a root node, have to cd
369 1.1 cgd * back to the original place, otherwise may not be able to access the
370 1.1 cgd * original node on post-order.
371 1.1 cgd */
372 1.1 cgd if (p->fts_level == FTS_ROOTLEVEL) {
373 1.1 cgd if (FCHDIR(sp, sp->fts_rfd)) {
374 1.1 cgd SET(FTS_STOP);
375 1.1 cgd return(NULL);
376 1.1 cgd }
377 1.1 cgd }
378 1.1 cgd else if (CHDIR(sp, "..")) {
379 1.1 cgd SET(FTS_STOP);
380 1.1 cgd return(NULL);
381 1.1 cgd }
382 1.1 cgd
383 1.1 cgd /*
384 1.1 cgd * If had a chdir error when trying to get into the directory, set the
385 1.1 cgd * info field to reflect this, and restore errno. The error indicator
386 1.1 cgd * has to be reset to 0 so that if the user does an FTS_AGAIN, it all
387 1.1 cgd * works.
388 1.1 cgd */
389 1.1 cgd if (p->fts_cderr) {
390 1.1 cgd errno = p->fts_cderr;
391 1.1 cgd p->fts_cderr = 0;
392 1.1 cgd p->fts_info = FTS_ERR;
393 1.1 cgd } else
394 1.1 cgd p->fts_info = FTS_DP;
395 1.1 cgd return(sp->fts_cur = p);
396 1.1 cgd }
397 1.1 cgd
398 1.1 cgd /*
399 1.1 cgd * Fts_set takes the stream as an argument although it's not used in this
400 1.1 cgd * implementation; it would be necessary if anyone wanted to add global
401 1.1 cgd * semantics to fts using fts_set. An error return is allowed for similar
402 1.1 cgd * reasons.
403 1.1 cgd */
404 1.1 cgd /* ARGSUSED */
405 1.1 cgd fts_set(sp, p, instr)
406 1.1 cgd FTS *sp;
407 1.1 cgd FTSENT *p;
408 1.1 cgd int instr;
409 1.1 cgd {
410 1.1 cgd p->fts_instr = instr;
411 1.1 cgd return(0);
412 1.1 cgd }
413 1.1 cgd
414 1.1 cgd FTSENT *
415 1.1 cgd fts_children(sp)
416 1.1 cgd register FTS *sp;
417 1.1 cgd {
418 1.1 cgd register FTSENT *p;
419 1.1 cgd int fd;
420 1.1 cgd
421 1.1 cgd /* Set current node pointer. */
422 1.1 cgd p = sp->fts_cur;
423 1.1 cgd
424 1.1 cgd /*
425 1.1 cgd * Set errno to 0 so that user can tell the difference between an
426 1.1 cgd * error and a directory without entries. If not a directory being
427 1.1 cgd * visited in *pre-order*, or we've already had fatal errors, return
428 1.1 cgd * immediately.
429 1.1 cgd */
430 1.1 cgd errno = 0;
431 1.1 cgd if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR)
432 1.1 cgd return(NULL);
433 1.1 cgd
434 1.1 cgd /* Free up any previous child list. */
435 1.1 cgd if (sp->fts_child)
436 1.1 cgd fts_lfree(sp->fts_child);
437 1.1 cgd
438 1.1 cgd /*
439 1.1 cgd * If using chdir on a relative path and called BEFORE fts_read does
440 1.1 cgd * its chdir to the root of a traversal, we can lose -- we need to
441 1.1 cgd * chdir into the subdirectory, and we don't know where the current
442 1.1 cgd * directory is, so we can't get back so that the upcoming chdir by
443 1.1 cgd * fts_read will work.
444 1.1 cgd */
445 1.1 cgd if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
446 1.1 cgd ISSET(FTS_NOCHDIR))
447 1.1 cgd return(sp->fts_child = fts_build(sp, BCHILD));
448 1.1 cgd
449 1.1 cgd if ((fd = open(".", O_RDONLY, 0)) < 0)
450 1.1 cgd return(NULL);
451 1.1 cgd sp->fts_child = fts_build(sp, BCHILD);
452 1.1 cgd if (fchdir(fd))
453 1.1 cgd return(NULL);
454 1.1 cgd (void)close(fd);
455 1.1 cgd return(sp->fts_child);
456 1.1 cgd }
457 1.1 cgd
458 1.1 cgd /*
459 1.1 cgd * This is the tricky part -- do not casually change *anything* in here. The
460 1.1 cgd * idea is to build the linked list of entries that are used by fts_children
461 1.1 cgd * and fts_read. There are lots of special cases.
462 1.1 cgd *
463 1.1 cgd * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
464 1.1 cgd * set and it's a physical walk (so that symbolic links can't be directories),
465 1.1 cgd * we assume that the number of subdirectories in a node is equal to the number
466 1.1 cgd * of links to the parent. This allows stat calls to be skipped in any leaf
467 1.1 cgd * directories and for any nodes after the directories in the parent node have
468 1.1 cgd * been found. This empirically cuts the stat calls by about 2/3.
469 1.1 cgd */
470 1.1 cgd #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
471 1.1 cgd
472 1.1 cgd static FTSENT *
473 1.1 cgd fts_build(sp, type)
474 1.1 cgd register FTS *sp;
475 1.1 cgd int type;
476 1.1 cgd {
477 1.1 cgd register struct dirent *dp;
478 1.1 cgd register FTSENT *p, *head;
479 1.1 cgd register int nitems;
480 1.1 cgd FTSENT *cur;
481 1.1 cgd DIR *dirp;
482 1.1 cgd int cderr, descend, len, level, maxlen, nlinks, saved_errno;
483 1.1 cgd char *cp;
484 1.1 cgd
485 1.1 cgd /* Set current node pointer. */
486 1.1 cgd cur = sp->fts_cur;
487 1.1 cgd
488 1.1 cgd /*
489 1.1 cgd * Open the directory for reading. If this fails, we're done.
490 1.1 cgd * If being called from fts_read, set the fts_info field.
491 1.1 cgd */
492 1.1 cgd if (!(dirp = opendir(cur->fts_accpath))) {
493 1.1 cgd if (type == BREAD)
494 1.1 cgd cur->fts_info = FTS_DNR;
495 1.1 cgd return(NULL);
496 1.1 cgd }
497 1.1 cgd
498 1.1 cgd /*
499 1.1 cgd * Nlinks is the number of possible entries of type directory in the
500 1.1 cgd * directory if we're cheating on stat calls, 0 if we're not doing
501 1.1 cgd * any stat calls at all, -1 if we're doing stats on everything.
502 1.1 cgd */
503 1.1 cgd nlinks =
504 1.1 cgd ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ?
505 1.1 cgd cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1;
506 1.1 cgd
507 1.1 cgd /*
508 1.1 cgd * If we're going to need to stat anything or we want to descend
509 1.1 cgd * and stay in the directory, chdir. If this fails we keep going.
510 1.1 cgd * We won't be able to stat anything, but we can still return the
511 1.1 cgd * names themselves. Note, that since fts_read won't be able to
512 1.1 cgd * chdir into the directory, it will have to return different path
513 1.1 cgd * names than before, i.e. "a/b" instead of "b". Since the node
514 1.1 cgd * has already been visited in pre-order, have to wait until the
515 1.1 cgd * post-order visit to return the error. This is all fairly nasty.
516 1.1 cgd * If a program needed sorted entries or stat information, they had
517 1.1 cgd * better be checking FTS_NS on the returned nodes.
518 1.1 cgd */
519 1.1 cgd if (nlinks || type == BREAD)
520 1.1 cgd if (FCHDIR(sp, dirfd(dirp))) {
521 1.1 cgd if (type == BREAD)
522 1.1 cgd cur->fts_cderr = errno;
523 1.1 cgd descend = nlinks = 0;
524 1.1 cgd cderr = 1;
525 1.1 cgd } else {
526 1.1 cgd descend = 1;
527 1.1 cgd cderr = 0;
528 1.1 cgd }
529 1.1 cgd else
530 1.1 cgd descend = 0;
531 1.1 cgd
532 1.1 cgd /*
533 1.1 cgd * Figure out the max file name length that can be stored in the
534 1.1 cgd * current path -- the inner loop allocates more path as necessary.
535 1.1 cgd * We really wouldn't have to do the maxlen calculations here, we
536 1.1 cgd * could do them in fts_read before returning the path, but it's a
537 1.1 cgd * lot easier here since the length is part of the dirent structure.
538 1.1 cgd *
539 1.1 cgd * If not changing directories set a pointer so that we can just
540 1.1 cgd * append each new name into the path.
541 1.1 cgd */
542 1.1 cgd maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
543 1.1 cgd len = NAPPEND(cur);
544 1.1 cgd if (ISSET(FTS_NOCHDIR)) {
545 1.1 cgd cp = sp->fts_path + len;
546 1.1 cgd *cp++ = '/';
547 1.1 cgd }
548 1.1 cgd
549 1.1 cgd level = cur->fts_level + 1;
550 1.1 cgd
551 1.1 cgd /* Read the directory, attaching each entry to the `link' pointer. */
552 1.1 cgd for (head = NULL, nitems = 0; dp = readdir(dirp);) {
553 1.1 cgd if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
554 1.1 cgd continue;
555 1.1 cgd
556 1.1 cgd if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)))
557 1.1 cgd goto mem1;
558 1.1 cgd if (dp->d_namlen > maxlen) {
559 1.1 cgd if (!fts_path(sp, (int)dp->d_namlen)) {
560 1.1 cgd /*
561 1.1 cgd * No more memory for path or structures. Save
562 1.1 cgd * errno, free up the current structure and the
563 1.1 cgd * structures already allocated.
564 1.1 cgd */
565 1.1 cgd mem1: saved_errno = errno;
566 1.1 cgd if (p)
567 1.1 cgd free(p);
568 1.1 cgd fts_lfree(head);
569 1.1 cgd (void)closedir(dirp);
570 1.1 cgd errno = saved_errno;
571 1.1 cgd cur->fts_info = FTS_ERR;
572 1.1 cgd SET(FTS_STOP);
573 1.1 cgd return(NULL);
574 1.1 cgd }
575 1.1 cgd maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
576 1.1 cgd }
577 1.1 cgd
578 1.1 cgd p->fts_pathlen = len + dp->d_namlen + 1;
579 1.1 cgd p->fts_parent = sp->fts_cur;
580 1.1 cgd p->fts_level = level;
581 1.1 cgd
582 1.1 cgd if (nlinks) {
583 1.1 cgd /* Build a file name for fts_stat to stat. */
584 1.1 cgd if (ISSET(FTS_NOCHDIR)) {
585 1.1 cgd p->fts_accpath = p->fts_path;
586 1.1 cgd bcopy(p->fts_name, cp, p->fts_namelen + 1);
587 1.1 cgd } else
588 1.1 cgd p->fts_accpath = p->fts_name;
589 1.1 cgd p->fts_info = fts_stat(sp, p, 0);
590 1.1 cgd if (nlinks > 0 && p->fts_info == FTS_D)
591 1.1 cgd --nlinks;
592 1.1 cgd } else if (cderr) {
593 1.1 cgd p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS;
594 1.1 cgd p->fts_accpath = cur->fts_accpath;
595 1.1 cgd } else {
596 1.1 cgd p->fts_accpath =
597 1.1 cgd ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
598 1.1 cgd p->fts_info = FTS_NSOK;
599 1.1 cgd }
600 1.1 cgd
601 1.1 cgd p->fts_link = head;
602 1.1 cgd head = p;
603 1.1 cgd ++nitems;
604 1.1 cgd }
605 1.1 cgd (void)closedir(dirp);
606 1.1 cgd
607 1.1 cgd /*
608 1.1 cgd * If not changing directories, reset the path back to original
609 1.1 cgd * state.
610 1.1 cgd */
611 1.1 cgd if (ISSET(FTS_NOCHDIR)) {
612 1.1 cgd if (cp - 1 > sp->fts_path)
613 1.1 cgd --cp;
614 1.1 cgd *cp = '\0';
615 1.1 cgd }
616 1.1 cgd
617 1.1 cgd /*
618 1.1 cgd * If descended after called from fts_children or called from
619 1.1 cgd * fts_read and didn't find anything, get back. If can't get
620 1.1 cgd * back, we're done.
621 1.1 cgd */
622 1.1 cgd if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) {
623 1.1 cgd cur->fts_info = FTS_ERR;
624 1.1 cgd SET(FTS_STOP);
625 1.1 cgd return(NULL);
626 1.1 cgd }
627 1.1 cgd
628 1.1 cgd /* If we didn't find anything, just do the post-order visit */
629 1.1 cgd if (!nitems) {
630 1.1 cgd if (type == BREAD)
631 1.1 cgd cur->fts_info = FTS_DP;
632 1.1 cgd return(NULL);
633 1.1 cgd }
634 1.1 cgd
635 1.1 cgd /* Sort the entries. */
636 1.1 cgd if (sp->fts_compar && nitems > 1)
637 1.1 cgd head = fts_sort(sp, head, nitems);
638 1.1 cgd return(head);
639 1.1 cgd }
640 1.1 cgd
641 1.1 cgd static u_short
642 1.1 cgd fts_stat(sp, p, follow)
643 1.1 cgd FTS *sp;
644 1.1 cgd register FTSENT *p;
645 1.1 cgd int follow;
646 1.1 cgd {
647 1.1 cgd int saved_errno;
648 1.1 cgd
649 1.1 cgd /*
650 1.1 cgd * If doing a logical walk, or application requested FTS_FOLLOW, do
651 1.1 cgd * a stat(2). If that fails, check for a non-existent symlink. If
652 1.1 cgd * fail, return the errno from the stat call.
653 1.1 cgd */
654 1.1 cgd if (ISSET(FTS_LOGICAL) || follow) {
655 1.1 cgd if (stat(p->fts_accpath, &p->fts_statb)) {
656 1.1 cgd saved_errno = errno;
657 1.1 cgd if (!lstat(p->fts_accpath, &p->fts_statb)) {
658 1.1 cgd errno = 0;
659 1.1 cgd return(FTS_SLNONE);
660 1.1 cgd }
661 1.1 cgd errno = saved_errno;
662 1.1 cgd bzero(&p->fts_statb, sizeof(struct stat));
663 1.1 cgd return(FTS_NS);
664 1.1 cgd }
665 1.1 cgd } else if (lstat(p->fts_accpath, &p->fts_statb)) {
666 1.1 cgd bzero(&p->fts_statb, sizeof(struct stat));
667 1.1 cgd return(FTS_NS);
668 1.1 cgd }
669 1.1 cgd
670 1.1 cgd /*
671 1.1 cgd * Cycle detection is done as soon as we find a directory. Detection
672 1.1 cgd * is by brute force; if the tree gets deep enough or the number of
673 1.1 cgd * symbolic links to directories high enough something faster might
674 1.1 cgd * be worthwhile.
675 1.1 cgd */
676 1.1 cgd if (S_ISDIR(p->fts_statb.st_mode)) {
677 1.1 cgd register FTSENT *t;
678 1.1 cgd register dev_t dev;
679 1.1 cgd register ino_t ino;
680 1.1 cgd
681 1.1 cgd dev = p->fts_statb.st_dev;
682 1.1 cgd ino = p->fts_statb.st_ino;
683 1.1 cgd for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL;
684 1.1 cgd t = t->fts_parent)
685 1.1 cgd if (ino == t->fts_statb.st_ino &&
686 1.1 cgd dev == t->fts_statb.st_dev) {
687 1.1 cgd sp->fts_savelink = p->fts_link;
688 1.1 cgd p->fts_link = t;
689 1.1 cgd return(FTS_DC);
690 1.1 cgd }
691 1.1 cgd return(FTS_D);
692 1.1 cgd }
693 1.1 cgd if (S_ISLNK(p->fts_statb.st_mode))
694 1.1 cgd return(FTS_SL);
695 1.1 cgd if (S_ISREG(p->fts_statb.st_mode))
696 1.1 cgd return(FTS_F);
697 1.1 cgd return(FTS_DEFAULT);
698 1.1 cgd }
699 1.1 cgd
700 1.1 cgd #define R(type, nelem, ptr) \
701 1.1 cgd (type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type)))
702 1.1 cgd
703 1.1 cgd static FTSENT *
704 1.1 cgd fts_sort(sp, head, nitems)
705 1.1 cgd FTS *sp;
706 1.1 cgd FTSENT *head;
707 1.1 cgd register int nitems;
708 1.1 cgd {
709 1.1 cgd register FTSENT **ap, *p;
710 1.1 cgd
711 1.1 cgd /*
712 1.1 cgd * Construct an array of pointers to the structures and call qsort(3).
713 1.1 cgd * Reassemble the array in the order returned by qsort. If unable to
714 1.1 cgd * sort for memory reasons, return the directory entries in their
715 1.1 cgd * current order. Allocate enough space for the current needs plus
716 1.1 cgd * 40 so we don't realloc one entry at a time.
717 1.1 cgd */
718 1.1 cgd if (nitems > sp->fts_nitems) {
719 1.1 cgd sp->fts_nitems = nitems + 40;
720 1.1 cgd if (!(sp->fts_array =
721 1.1 cgd R(FTSENT *, sp->fts_nitems, sp->fts_array))) {
722 1.1 cgd sp->fts_nitems = 0;
723 1.1 cgd return(head);
724 1.1 cgd }
725 1.1 cgd }
726 1.1 cgd for (ap = sp->fts_array, p = head; p; p = p->fts_link)
727 1.1 cgd *ap++ = p;
728 1.1 cgd qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
729 1.1 cgd for (head = *(ap = sp->fts_array); --nitems; ++ap)
730 1.1 cgd ap[0]->fts_link = ap[1];
731 1.1 cgd ap[0]->fts_link = NULL;
732 1.1 cgd return(head);
733 1.1 cgd }
734 1.1 cgd
735 1.1 cgd static FTSENT *
736 1.1 cgd fts_alloc(sp, name, len)
737 1.1 cgd FTS *sp;
738 1.1 cgd char *name;
739 1.1 cgd register int len;
740 1.1 cgd {
741 1.1 cgd register FTSENT *p;
742 1.1 cgd
743 1.1 cgd /*
744 1.1 cgd * Variable sized structures; the name is the last element so
745 1.1 cgd * we allocate enough extra space after the structure to store
746 1.1 cgd * it.
747 1.1 cgd */
748 1.1 cgd if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len))))
749 1.1 cgd return(NULL);
750 1.1 cgd bcopy(name, p->fts_name, len + 1);
751 1.1 cgd p->fts_namelen = len;
752 1.1 cgd p->fts_path = sp->fts_path;
753 1.1 cgd p->fts_instr = FTS_NOINSTR;
754 1.1 cgd p->fts_cderr = 0;
755 1.1 cgd p->fts_number = 0;
756 1.1 cgd p->fts_pointer = NULL;
757 1.1 cgd return(p);
758 1.1 cgd }
759 1.1 cgd
760 1.1 cgd static void
761 1.1 cgd fts_lfree(head)
762 1.1 cgd register FTSENT *head;
763 1.1 cgd {
764 1.1 cgd register FTSENT *p;
765 1.1 cgd
766 1.1 cgd /* Free a linked list of structures. */
767 1.1 cgd while (p = head) {
768 1.1 cgd head = head->fts_link;
769 1.1 cgd free(p);
770 1.1 cgd }
771 1.1 cgd }
772 1.1 cgd
773 1.1 cgd /*
774 1.1 cgd * Allow essentially unlimited paths; certain programs (find, rm, ls) need to
775 1.1 cgd * work on any tree. Most systems will allow creation of paths much longer
776 1.1 cgd * than MAXPATHLEN, even though the kernel won't resolve them. Add an extra
777 1.1 cgd * 128 bytes to the requested size so that we don't realloc the path 2 bytes
778 1.1 cgd * at a time.
779 1.1 cgd */
780 1.1 cgd static char *
781 1.1 cgd fts_path(sp, size)
782 1.1 cgd FTS *sp;
783 1.1 cgd int size;
784 1.1 cgd {
785 1.1 cgd sp->fts_pathlen += size + 128;
786 1.1 cgd return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path));
787 1.1 cgd }
788