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