fix gcc warning with -Wmisleading-indentation
[uclibc-ng.git] / libc / misc / ftw / ftw.c
blob0c6e617da8199958b3f0ad17c99522c614d4f4d4
1 /* File tree walker functions.
2 Copyright (C) 1996-2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <features.h>
25 /* need errno.h before undefining _LIBC */
26 #include <errno.h>
27 #ifdef __UCLIBC__
28 #undef _LIBC
29 #define HAVE_DIRENT_H 1
30 #define HAVE_SYS_PARAM_H 1
31 #define HAVE_DECL_STPCPY 1
32 #define HAVE_MEMPCPY 1
33 #endif
35 #if __GNUC__
36 # undef alloca
37 # define alloca __builtin_alloca
38 #else
39 # if HAVE_ALLOCA_H
40 # include <alloca.h>
41 # else
42 # ifdef _AIX
43 # pragma alloca
44 # else
45 char *alloca ();
46 # endif
47 # endif
48 #endif
50 #if defined _LIBC
51 # include <dirent.h>
52 # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
53 #else
54 # if HAVE_DIRENT_H
55 # include <dirent.h>
56 # define NAMLEN(dirent) strlen ((dirent)->d_name)
57 # else
58 # define dirent direct
59 # define NAMLEN(dirent) (dirent)->d_namlen
60 # if HAVE_SYS_NDIR_H
61 # include <sys/ndir.h>
62 # endif
63 # if HAVE_SYS_DIR_H
64 # include <sys/dir.h>
65 # endif
66 # if HAVE_NDIR_H
67 # include <ndir.h>
68 # endif
69 # endif
70 #endif
72 #include <ftw.h>
73 #include <limits.h>
74 #include <search.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
78 #if HAVE_SYS_PARAM_H || defined _LIBC
79 # include <sys/param.h>
80 #endif
81 #include <sys/stat.h>
83 #if !defined _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
84 char *stpcpy ();
85 #endif
87 #if !defined _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
88 /* Be CAREFUL that there are no side effects in N. */
89 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
90 #endif
92 /* #define NDEBUG 1 */
93 #include <assert.h>
95 #if !defined _LIBC
96 # undef __chdir
97 # define __chdir chdir
98 # undef __closedir
99 # define __closedir closedir
100 # undef __fchdir
101 # define __fchdir fchdir
102 # undef __getcwd
103 # ifndef __UCLIBC__
104 # define __getcwd(P, N) xgetcwd ()
105 extern char *xgetcwd (void);
106 # else
107 # define __getcwd getcwd
108 # endif
109 # undef __mempcpy
110 # define __mempcpy mempcpy
111 # undef __opendir
112 # define __opendir opendir
113 # undef __readdir64
114 # define __readdir64 readdir64
115 # undef __stpcpy
116 # define __stpcpy stpcpy
117 # undef __tdestroy
118 # define __tdestroy tdestroy
119 # undef __tfind
120 # define __tfind tfind
121 # undef __tsearch
122 # define __tsearch tsearch
123 # undef internal_function
124 # define internal_function /* empty */
125 # undef MAX
126 # define MAX(a, b) ((a) > (b) ? (a) : (b))
127 #endif
129 /* Arrange to make lstat calls go through the wrapper function
130 on systems with an lstat function that does not dereference symlinks
131 that are specified with a trailing slash. */
132 #if !defined _LIBC && !defined LSTAT_FOLLOWS_SLASHED_SYMLINK && !defined __UCLIBC__
133 int rpl_lstat (const char *, struct stat *);
134 # undef lstat
135 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
136 #endif
138 #ifndef __set_errno
139 # define __set_errno(Val) errno = (Val)
140 #endif
142 /* Support for the LFS API version. */
143 #ifndef FTW_NAME
144 # define FTW_NAME ftw
145 # define NFTW_NAME nftw
146 # define NFTW_OLD_NAME __old_nftw
147 # define NFTW_NEW_NAME __new_nftw
148 # define INO_T ino_t
149 # define STAT stat
150 # ifdef _LIBC
151 # define LXSTAT __lxstat
152 # define XSTAT __xstat
153 # else
154 # define LXSTAT(V,f,sb) lstat (f,sb)
155 # define XSTAT(V,f,sb) stat (f,sb)
156 # endif
157 # define FTW_FUNC_T __ftw_func_t
158 # define NFTW_FUNC_T __nftw_func_t
159 #endif
161 /* We define PATH_MAX if the system does not provide a definition.
162 This does not artificially limit any operation. PATH_MAX is simply
163 used as a guesstimate for the expected maximal path length.
164 Buffers will be enlarged if necessary. */
165 #ifndef PATH_MAX
166 # define PATH_MAX 1024
167 #endif
169 struct dir_data
171 DIR *stream;
172 char *content;
175 struct known_object
177 dev_t dev;
178 INO_T ino;
181 struct ftw_data
183 /* Array with pointers to open directory streams. */
184 struct dir_data **dirstreams;
185 size_t actdir;
186 size_t maxdir;
188 /* Buffer containing name of currently processed object. */
189 char *dirbuf;
190 size_t dirbufsize;
192 /* Passed as fourth argument to `nftw' callback. The `base' member
193 tracks the content of the `dirbuf'. */
194 struct FTW ftw;
196 /* Flags passed to `nftw' function. 0 for `ftw'. */
197 int flags;
199 /* Conversion array for flag values. It is the identity mapping for
200 `nftw' calls, otherwise it maps the values to those known by
201 `ftw'. */
202 const int *cvt_arr;
204 /* Callback function. We always use the `nftw' form. */
205 NFTW_FUNC_T func;
207 /* Device of starting point. Needed for FTW_MOUNT. */
208 dev_t dev;
210 /* Data structure for keeping fingerprints of already processed
211 object. This is needed when not using FTW_PHYS. */
212 void *known_objects;
216 /* Internally we use the FTW_* constants used for `nftw'. When invoked
217 as `ftw', map each flag to the subset of values used by `ftw'. */
218 static const int nftw_arr[] =
220 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
223 static const int ftw_arr[] =
225 FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
229 /* Forward declarations of local functions. */
230 static int ftw_dir (struct ftw_data *data, struct STAT *st,
231 struct dir_data *old_dir) internal_function;
234 static int
235 object_compare (const void *p1, const void *p2)
237 /* We don't need a sophisticated and useful comparison. We are only
238 interested in equality. However, we must be careful not to
239 accidentally compare `holes' in the structure. */
240 const struct known_object *kp1 = p1, *kp2 = p2;
241 int cmp1;
242 cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
243 if (cmp1 != 0)
244 return cmp1;
245 return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
249 static __inline__ int
250 add_object (struct ftw_data *data, struct STAT *st)
252 struct known_object *newp = malloc (sizeof (struct known_object));
253 if (newp == NULL)
254 return -1;
255 newp->dev = st->st_dev;
256 newp->ino = st->st_ino;
257 return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
261 static __inline__ int
262 find_object (struct ftw_data *data, struct STAT *st)
264 struct known_object obj;
265 obj.dev = st->st_dev;
266 obj.ino = st->st_ino;
267 return __tfind (&obj, &data->known_objects, object_compare) != NULL;
271 static __inline__ int
272 __attribute ((always_inline))
273 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
275 int result = 0;
277 if (data->dirstreams[data->actdir] != NULL)
279 /* Oh, oh. We must close this stream. Get all remaining
280 entries and store them as a list in the `content' member of
281 the `struct dir_data' variable. */
282 size_t bufsize = 1024;
283 char *buf = malloc (bufsize);
285 if (buf == NULL)
286 result = -1;
287 else
289 DIR *st = data->dirstreams[data->actdir]->stream;
290 struct dirent64 *d;
291 size_t actsize = 0;
293 while ((d = __readdir64 (st)) != NULL)
295 size_t this_len = NAMLEN (d);
296 if (actsize + this_len + 2 >= bufsize)
298 char *newp;
299 bufsize += MAX (1024, 2 * this_len);
300 newp = (char *) realloc (buf, bufsize);
301 if (newp == NULL)
303 /* No more memory. */
304 int save_err = errno;
305 free (buf);
306 __set_errno (save_err);
307 result = -1;
308 break;
310 buf = newp;
313 *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
314 = '\0';
315 actsize += this_len + 1;
318 /* Terminate the list with an additional NUL byte. */
319 buf[actsize++] = '\0';
321 /* Shrink the buffer to what we actually need. */
322 data->dirstreams[data->actdir]->content = realloc (buf, actsize);
323 if (data->dirstreams[data->actdir]->content == NULL)
325 int save_err = errno;
326 free (buf);
327 __set_errno (save_err);
328 result = -1;
330 else
332 __closedir (st);
333 data->dirstreams[data->actdir]->stream = NULL;
334 data->dirstreams[data->actdir] = NULL;
339 /* Open the new stream. */
340 if (result == 0)
342 const char *name = ((data->flags & FTW_CHDIR)
343 ? data->dirbuf + data->ftw.base: data->dirbuf);
344 assert (data->dirstreams[data->actdir] == NULL);
346 dirp->stream = __opendir (name);
347 if (dirp->stream == NULL)
348 result = -1;
349 else
351 dirp->content = NULL;
352 data->dirstreams[data->actdir] = dirp;
354 if (++data->actdir == data->maxdir)
355 data->actdir = 0;
359 return result;
363 static int
364 internal_function
365 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
366 size_t namlen)
368 struct STAT st;
369 int result = 0;
370 int flag = 0;
371 size_t new_buflen;
373 if (name[0] == '.' && (name[1] == '\0'
374 || (name[1] == '.' && name[2] == '\0')))
375 /* Don't process the "." and ".." entries. */
376 return 0;
378 new_buflen = data->ftw.base + namlen + 2;
379 if (data->dirbufsize < new_buflen)
381 /* Enlarge the buffer. */
382 char *newp;
384 data->dirbufsize = 2 * new_buflen;
385 newp = (char *) realloc (data->dirbuf, data->dirbufsize);
386 if (newp == NULL)
387 return -1;
388 data->dirbuf = newp;
391 *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
393 if ((data->flags & FTW_CHDIR) == 0)
394 name = data->dirbuf;
396 if (((data->flags & FTW_PHYS)
397 ? LXSTAT (_STAT_VER, name, &st)
398 : XSTAT (_STAT_VER, name, &st)) < 0)
400 if (errno != EACCES && errno != ENOENT)
401 result = -1;
402 else if (!(data->flags & FTW_PHYS)
403 && LXSTAT (_STAT_VER, name, &st) == 0
404 && S_ISLNK (st.st_mode))
405 flag = FTW_SLN;
406 else
407 flag = FTW_NS;
409 else
411 if (S_ISDIR (st.st_mode))
412 flag = FTW_D;
413 else if (S_ISLNK (st.st_mode))
414 flag = FTW_SL;
415 else
416 flag = FTW_F;
419 if (result == 0
420 && (flag == FTW_NS
421 || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
423 if (flag == FTW_D)
425 if ((data->flags & FTW_PHYS)
426 || (!find_object (data, &st)
427 /* Remember the object. */
428 && (result = add_object (data, &st)) == 0))
429 result = ftw_dir (data, &st, dir);
431 else
432 result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
433 &data->ftw);
436 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
437 result = 0;
439 return result;
443 static int
444 __attribute ((noinline))
445 internal_function
446 ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
448 struct dir_data dir;
449 struct dirent64 *d;
450 int previous_base = data->ftw.base;
451 int result;
452 char *startp;
454 /* Open the stream for this directory. This might require that
455 another stream has to be closed. */
456 result = open_dir_stream (data, &dir);
457 if (result != 0)
459 if (errno == EACCES)
460 /* We cannot read the directory. Signal this with a special flag. */
461 result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
463 return result;
466 /* First, report the directory (if not depth-first). */
467 if (!(data->flags & FTW_DEPTH))
469 result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
470 if (result != 0)
472 int save_err;
473 fail:
474 save_err = errno;
475 __closedir (dir.stream);
476 __set_errno (save_err);
478 if (data->actdir-- == 0)
479 data->actdir = data->maxdir - 1;
480 data->dirstreams[data->actdir] = NULL;
481 return result;
485 /* If necessary, change to this directory. */
486 if (data->flags & FTW_CHDIR)
488 if (__fchdir (dirfd (dir.stream)) < 0)
490 result = -1;
491 goto fail;
495 /* Next, update the `struct FTW' information. */
496 ++data->ftw.level;
497 startp = strchr (data->dirbuf, '\0');
498 /* There always must be a directory name. */
499 assert (startp != data->dirbuf);
500 if (startp[-1] != '/')
501 *startp++ = '/';
502 data->ftw.base = startp - data->dirbuf;
504 while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
506 result = process_entry (data, &dir, d->d_name, NAMLEN (d));
507 if (result != 0)
508 break;
511 if (dir.stream != NULL)
513 /* The stream is still open. I.e., we did not need more
514 descriptors. Simply close the stream now. */
515 int save_err = errno;
517 assert (dir.content == NULL);
519 __closedir (dir.stream);
520 __set_errno (save_err);
522 if (data->actdir-- == 0)
523 data->actdir = data->maxdir - 1;
524 data->dirstreams[data->actdir] = NULL;
526 else
528 int save_err;
529 char *runp = dir.content;
531 while (result == 0 && *runp != '\0')
533 char *endp = strchr (runp, '\0');
535 result = process_entry (data, &dir, runp, endp - runp);
537 runp = endp + 1;
540 save_err = errno;
541 free (dir.content);
542 __set_errno (save_err);
545 if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
546 result = 0;
548 /* Prepare the return, revert the `struct FTW' information. */
549 data->dirbuf[data->ftw.base - 1] = '\0';
550 --data->ftw.level;
551 data->ftw.base = previous_base;
553 if ((data->flags & FTW_CHDIR)
554 && (result == 0
555 || ((data->flags & FTW_ACTIONRETVAL)
556 && (result != -1 && result != FTW_STOP))))
558 /* Change back to the parent directory. */
559 int done = 0;
560 if (old_dir && old_dir->stream != NULL)
561 if (__fchdir (dirfd (old_dir->stream)) == 0)
562 done = 1;
564 if (!done)
566 if (data->ftw.base == 1)
568 if (__chdir ("/") < 0)
569 result = -1;
571 else
572 if (__chdir ("..") < 0)
573 result = -1;
577 /* Finally, if we process depth-first report the directory. */
578 if (result == 0 && (data->flags & FTW_DEPTH))
579 result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
581 return result;
585 static int
586 __attribute ((noinline))
587 internal_function
588 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
589 int flags)
591 struct ftw_data data;
592 struct STAT st;
593 int result = 0;
594 int save_err;
595 char *cwd = NULL;
596 char *cp;
598 /* First make sure the parameters are reasonable. */
599 if (dir[0] == '\0')
601 __set_errno (ENOENT);
602 return -1;
605 data.maxdir = descriptors < 1 ? 1 : descriptors;
606 data.actdir = 0;
607 data.dirstreams = (struct dir_data **) alloca (data.maxdir
608 * sizeof (struct dir_data *));
609 memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
611 /* PATH_MAX is always defined when we get here. */
612 data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
613 data.dirbuf = (char *) malloc (data.dirbufsize);
614 if (data.dirbuf == NULL)
615 return -1;
616 cp = __stpcpy (data.dirbuf, dir);
617 /* Strip trailing slashes. */
618 while (cp > data.dirbuf + 1 && cp[-1] == '/')
619 --cp;
620 *cp = '\0';
622 data.ftw.level = 0;
624 /* Find basename. */
625 while (cp > data.dirbuf && cp[-1] != '/')
626 --cp;
627 data.ftw.base = cp - data.dirbuf;
629 data.flags = flags;
631 /* This assignment might seem to be strange but it is what we want.
632 The trick is that the first three arguments to the `ftw' and
633 `nftw' callback functions are equal. Therefore we can call in
634 every case the callback using the format of the `nftw' version
635 and get the correct result since the stack layout for a function
636 call in C allows this. */
637 data.func = (NFTW_FUNC_T) func;
639 /* Since we internally use the complete set of FTW_* values we need
640 to reduce the value range before calling a `ftw' callback. */
641 data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
643 /* No object known so far. */
644 data.known_objects = NULL;
646 /* Now go to the directory containing the initial file/directory. */
647 if (flags & FTW_CHDIR)
649 /* GNU extension ahead. */
650 cwd = __getcwd (NULL, 0);
651 if (cwd == NULL)
652 result = -1;
653 else if (data.ftw.base > 0)
655 /* Change to the directory the file is in. In data.dirbuf
656 we have a writable copy of the file name. Just NUL
657 terminate it for now and change the directory. */
658 if (data.ftw.base == 1)
659 /* I.e., the file is in the root directory. */
660 result = __chdir ("/");
661 else
663 char ch = data.dirbuf[data.ftw.base - 1];
664 data.dirbuf[data.ftw.base - 1] = '\0';
665 result = __chdir (data.dirbuf);
666 data.dirbuf[data.ftw.base - 1] = ch;
671 /* Get stat info for start directory. */
672 if (result == 0)
674 const char *name = ((data.flags & FTW_CHDIR)
675 ? data.dirbuf + data.ftw.base
676 : data.dirbuf);
678 if (((flags & FTW_PHYS)
679 ? LXSTAT (_STAT_VER, name, &st)
680 : XSTAT (_STAT_VER, name, &st)) < 0)
682 if (!(flags & FTW_PHYS)
683 && errno == ENOENT
684 && LXSTAT (_STAT_VER, name, &st) == 0
685 && S_ISLNK (st.st_mode))
686 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
687 &data.ftw);
688 else
689 /* No need to call the callback since we cannot say anything
690 about the object. */
691 result = -1;
693 else
695 if (S_ISDIR (st.st_mode))
697 /* Remember the device of the initial directory in case
698 FTW_MOUNT is given. */
699 data.dev = st.st_dev;
701 /* We know this directory now. */
702 if (!(flags & FTW_PHYS))
703 result = add_object (&data, &st);
705 if (result == 0)
706 result = ftw_dir (&data, &st, NULL);
708 else
710 int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
712 result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
713 &data.ftw);
717 if ((flags & FTW_ACTIONRETVAL)
718 && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
719 result = 0;
722 /* Return to the start directory (if necessary). */
723 if (cwd != NULL)
725 save_err = errno;
726 __chdir (cwd);
727 free (cwd);
728 __set_errno (save_err);
731 /* Free all memory. */
732 save_err = errno;
733 __tdestroy (data.known_objects, free);
734 free (data.dirbuf);
735 __set_errno (save_err);
737 return result;
742 /* Entry points. */
743 #ifdef __UCLIBC_HAS_FTW__
745 FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
747 return ftw_startup (path, 0, func, descriptors, 0);
749 #endif
751 #ifdef __UCLIBC_HAS_NFTW__
752 #ifndef _LIBC
754 NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
756 return ftw_startup (path, 1, func, descriptors, flags);
758 #else
760 #include <shlib-compat.h>
762 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
765 NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
767 if (flags
768 & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
770 __set_errno (EINVAL);
771 return -1;
773 return ftw_startup (path, 1, func, descriptors, flags);
776 versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
778 #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
780 /* Older nftw* version just ignored all unknown flags. */
782 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
785 attribute_compat_text_section
786 NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
788 flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
789 return ftw_startup (path, 1, func, descriptors, flags);
792 compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);
793 #endif
794 #endif
795 #endif