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.
5 
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.
10 
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.
15 
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/>.  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 
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
34 
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
49 
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
71 
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>
82 
83 #if !defined _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
84 char *stpcpy ();
85 #endif
86 
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
91 
92 /* #define NDEBUG 1 */
93 #include <assert.h>
94 
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 # ifndef __UCLIBC_HAS_LFS__
115 # define __readdir64 readdir
116 # else
117 # define __readdir64 readdir64
118 # endif
119 # undef __stpcpy
120 # define __stpcpy stpcpy
121 # undef __tdestroy
122 # define __tdestroy tdestroy
123 # undef __tfind
124 # define __tfind tfind
125 # undef __tsearch
126 # define __tsearch tsearch
127 # undef internal_function
128 # define internal_function /* empty */
129 # ifndef __UCLIBC_HAS_LFS__
130 # undef dirent64
131 # define dirent64 dirent
132 # endif
133 # undef MAX
134 # define MAX(a, b) ((a) > (b) ? (a) : (b))
135 #endif
136 
137 /* Arrange to make lstat calls go through the wrapper function
138    on systems with an lstat function that does not dereference symlinks
139    that are specified with a trailing slash.  */
140 #if !defined _LIBC && !defined LSTAT_FOLLOWS_SLASHED_SYMLINK && !defined __UCLIBC__
141 int rpl_lstat (const char *, struct stat *);
142 # undef lstat
143 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
144 #endif
145 
146 #ifndef __set_errno
147 # define __set_errno(Val) errno = (Val)
148 #endif
149 
150 /* Support for the LFS API version.  */
151 #ifndef FTW_NAME
152 # define FTW_NAME ftw
153 # define NFTW_NAME nftw
154 # define NFTW_OLD_NAME __old_nftw
155 # define NFTW_NEW_NAME __new_nftw
156 # define INO_T ino_t
157 # define STAT stat
158 # ifdef _LIBC
159 #  define LXSTAT __lxstat
160 #  define XSTAT __xstat
161 # else
162 #  define LXSTAT(V,f,sb) lstat (f,sb)
163 #  define XSTAT(V,f,sb) stat (f,sb)
164 # endif
165 # define FTW_FUNC_T __ftw_func_t
166 # define NFTW_FUNC_T __nftw_func_t
167 #endif
168 
169 /* We define PATH_MAX if the system does not provide a definition.
170    This does not artificially limit any operation.  PATH_MAX is simply
171    used as a guesstimate for the expected maximal path length.
172    Buffers will be enlarged if necessary.  */
173 #ifndef PATH_MAX
174 # define PATH_MAX 1024
175 #endif
176 
177 struct dir_data
178 {
179   DIR *stream;
180   char *content;
181 };
182 
183 struct known_object
184 {
185   dev_t dev;
186   INO_T ino;
187 };
188 
189 struct ftw_data
190 {
191   /* Array with pointers to open directory streams.  */
192   struct dir_data **dirstreams;
193   size_t actdir;
194   size_t maxdir;
195 
196   /* Buffer containing name of currently processed object.  */
197   char *dirbuf;
198   size_t dirbufsize;
199 
200   /* Passed as fourth argument to `nftw' callback.  The `base' member
201      tracks the content of the `dirbuf'.  */
202   struct FTW ftw;
203 
204   /* Flags passed to `nftw' function.  0 for `ftw'.  */
205   int flags;
206 
207   /* Conversion array for flag values.  It is the identity mapping for
208      `nftw' calls, otherwise it maps the values to those known by
209      `ftw'.  */
210   const int *cvt_arr;
211 
212   /* Callback function.  We always use the `nftw' form.  */
213   NFTW_FUNC_T func;
214 
215   /* Device of starting point.  Needed for FTW_MOUNT.  */
216   dev_t dev;
217 
218   /* Data structure for keeping fingerprints of already processed
219      object.  This is needed when not using FTW_PHYS.  */
220   void *known_objects;
221 };
222 
223 
224 /* Internally we use the FTW_* constants used for `nftw'.  When invoked
225    as `ftw', map each flag to the subset of values used by `ftw'.  */
226 static const int nftw_arr[] =
227 {
228   FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
229 };
230 
231 static const int ftw_arr[] =
232 {
233   FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
234 };
235 
236 
237 /* Forward declarations of local functions.  */
238 static int ftw_dir (struct ftw_data *data, struct STAT *st,
239 		    struct dir_data *old_dir) internal_function;
240 
241 
242 static int
object_compare(const void * p1,const void * p2)243 object_compare (const void *p1, const void *p2)
244 {
245   /* We don't need a sophisticated and useful comparison.  We are only
246      interested in equality.  However, we must be careful not to
247      accidentally compare `holes' in the structure.  */
248   const struct known_object *kp1 = p1, *kp2 = p2;
249   int cmp1;
250   cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
251   if (cmp1 != 0)
252     return cmp1;
253   return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
254 }
255 
256 
257 static __inline__ int
add_object(struct ftw_data * data,struct STAT * st)258 add_object (struct ftw_data *data, struct STAT *st)
259 {
260   struct known_object *newp = malloc (sizeof (struct known_object));
261   if (newp == NULL)
262     return -1;
263   newp->dev = st->st_dev;
264   newp->ino = st->st_ino;
265   return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
266 }
267 
268 
269 static __inline__ int
find_object(struct ftw_data * data,struct STAT * st)270 find_object (struct ftw_data *data, struct STAT *st)
271 {
272   struct known_object obj;
273   obj.dev = st->st_dev;
274   obj.ino = st->st_ino;
275   return __tfind (&obj, &data->known_objects, object_compare) != NULL;
276 }
277 
278 
279 static __inline__ int
280 __attribute ((always_inline))
open_dir_stream(struct ftw_data * data,struct dir_data * dirp)281 open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
282 {
283   int result = 0;
284 
285   if (data->dirstreams[data->actdir] != NULL)
286     {
287       /* Oh, oh.  We must close this stream.  Get all remaining
288 	 entries and store them as a list in the `content' member of
289 	 the `struct dir_data' variable.  */
290       size_t bufsize = 1024;
291       char *buf = malloc (bufsize);
292 
293       if (buf == NULL)
294 	result = -1;
295       else
296 	{
297 	  DIR *st = data->dirstreams[data->actdir]->stream;
298 	  struct dirent64 *d;
299 	  size_t actsize = 0;
300 
301 	  while ((d = __readdir64 (st)) != NULL)
302 	    {
303 	      size_t this_len = NAMLEN (d);
304 	      if (actsize + this_len + 2 >= bufsize)
305 		{
306 		  char *newp;
307 		  bufsize += MAX (1024, 2 * this_len);
308 		  newp = (char *) realloc (buf, bufsize);
309 		  if (newp == NULL)
310 		    {
311 		      /* No more memory.  */
312 		      int save_err = errno;
313 		      free (buf);
314 		      __set_errno (save_err);
315 		      result = -1;
316 		      break;
317 		    }
318 		  buf = newp;
319 		}
320 
321 	      *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
322 		= '\0';
323 	      actsize += this_len + 1;
324 	    }
325 
326 	  /* Terminate the list with an additional NUL byte.  */
327 	  buf[actsize++] = '\0';
328 
329 	  /* Shrink the buffer to what we actually need.  */
330 	  data->dirstreams[data->actdir]->content = realloc (buf, actsize);
331 	  if (data->dirstreams[data->actdir]->content == NULL)
332 	    {
333 	      int save_err = errno;
334 	      free (buf);
335 	      __set_errno (save_err);
336 	      result = -1;
337 	    }
338 	  else
339 	    {
340 	      __closedir (st);
341 	      data->dirstreams[data->actdir]->stream = NULL;
342 	      data->dirstreams[data->actdir] = NULL;
343 	    }
344 	}
345     }
346 
347   /* Open the new stream.  */
348   if (result == 0)
349     {
350       const char *name = ((data->flags & FTW_CHDIR)
351 			  ? data->dirbuf + data->ftw.base: data->dirbuf);
352       assert (data->dirstreams[data->actdir] == NULL);
353 
354       dirp->stream = __opendir (name);
355       if (dirp->stream == NULL)
356 	result = -1;
357       else
358 	{
359 	  dirp->content = NULL;
360 	  data->dirstreams[data->actdir] = dirp;
361 
362 	  if (++data->actdir == data->maxdir)
363 	    data->actdir = 0;
364 	}
365     }
366 
367   return result;
368 }
369 
370 
371 static int
372 internal_function
process_entry(struct ftw_data * data,struct dir_data * dir,const char * name,size_t namlen)373 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
374 	       size_t namlen)
375 {
376   struct STAT st;
377   int result = 0;
378   int flag = 0;
379   size_t new_buflen;
380 
381   if (name[0] == '.' && (name[1] == '\0'
382 			 || (name[1] == '.' && name[2] == '\0')))
383     /* Don't process the "." and ".." entries.  */
384     return 0;
385 
386   new_buflen = data->ftw.base + namlen + 2;
387   if (data->dirbufsize < new_buflen)
388     {
389       /* Enlarge the buffer.  */
390       char *newp;
391 
392       data->dirbufsize = 2 * new_buflen;
393       newp = (char *) realloc (data->dirbuf, data->dirbufsize);
394       if (newp == NULL)
395 	return -1;
396       data->dirbuf = newp;
397     }
398 
399   *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
400 
401   if ((data->flags & FTW_CHDIR) == 0)
402     name = data->dirbuf;
403 
404   if (((data->flags & FTW_PHYS)
405        ? LXSTAT (_STAT_VER, name, &st)
406        : XSTAT (_STAT_VER, name, &st)) < 0)
407     {
408       if (errno != EACCES && errno != ENOENT)
409 	result = -1;
410       else if (!(data->flags & FTW_PHYS)
411 	       && LXSTAT (_STAT_VER, name, &st) == 0
412 	       && S_ISLNK (st.st_mode))
413 	flag = FTW_SLN;
414       else
415 	flag = FTW_NS;
416     }
417   else
418     {
419       if (S_ISDIR (st.st_mode))
420 	flag = FTW_D;
421       else if (S_ISLNK (st.st_mode))
422 	flag = FTW_SL;
423       else
424 	flag = FTW_F;
425     }
426 
427   if (result == 0
428       && (flag == FTW_NS
429 	  || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
430     {
431       if (flag == FTW_D)
432 	{
433 	  if ((data->flags & FTW_PHYS)
434 	      || (!find_object (data, &st)
435 		  /* Remember the object.  */
436 		  && (result = add_object (data, &st)) == 0))
437 	    result = ftw_dir (data, &st, dir);
438 	}
439       else
440 	result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
441 				&data->ftw);
442     }
443 
444   if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
445     result = 0;
446 
447   return result;
448 }
449 
450 
451 static int
452 __attribute ((noinline))
453 internal_function
ftw_dir(struct ftw_data * data,struct STAT * st,struct dir_data * old_dir)454 ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
455 {
456   struct dir_data dir;
457   struct dirent64 *d;
458   int previous_base = data->ftw.base;
459   int result;
460   char *startp;
461 
462   /* Open the stream for this directory.  This might require that
463      another stream has to be closed.  */
464   result = open_dir_stream (data, &dir);
465   if (result != 0)
466     {
467       if (errno == EACCES)
468 	/* We cannot read the directory.  Signal this with a special flag.  */
469 	result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
470 
471       return result;
472     }
473 
474   /* First, report the directory (if not depth-first).  */
475   if (!(data->flags & FTW_DEPTH))
476     {
477       result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
478       if (result != 0)
479 	{
480 	  int save_err;
481 fail:
482 	  save_err = errno;
483 	  __closedir (dir.stream);
484 	  __set_errno (save_err);
485 
486 	  if (data->actdir-- == 0)
487 	    data->actdir = data->maxdir - 1;
488 	  data->dirstreams[data->actdir] = NULL;
489 	  return result;
490 	}
491     }
492 
493   /* If necessary, change to this directory.  */
494   if (data->flags & FTW_CHDIR)
495     {
496       if (__fchdir (dirfd (dir.stream)) < 0)
497 	{
498 	  result = -1;
499 	  goto fail;
500 	}
501     }
502 
503   /* Next, update the `struct FTW' information.  */
504   ++data->ftw.level;
505   startp = strchr (data->dirbuf, '\0');
506   /* There always must be a directory name.  */
507   assert (startp != data->dirbuf);
508   if (startp[-1] != '/')
509     *startp++ = '/';
510   data->ftw.base = startp - data->dirbuf;
511 
512   while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
513     {
514       result = process_entry (data, &dir, d->d_name, NAMLEN (d));
515       if (result != 0)
516 	break;
517     }
518 
519   if (dir.stream != NULL)
520     {
521       /* The stream is still open.  I.e., we did not need more
522 	 descriptors.  Simply close the stream now.  */
523       int save_err = errno;
524 
525       assert (dir.content == NULL);
526 
527       __closedir (dir.stream);
528       __set_errno (save_err);
529 
530       if (data->actdir-- == 0)
531 	data->actdir = data->maxdir - 1;
532       data->dirstreams[data->actdir] = NULL;
533     }
534   else
535     {
536       int save_err;
537       char *runp = dir.content;
538 
539       while (result == 0 && *runp != '\0')
540 	{
541 	  char *endp = strchr (runp, '\0');
542 
543 	  result = process_entry (data, &dir, runp, endp - runp);
544 
545 	  runp = endp + 1;
546 	}
547 
548       save_err = errno;
549       free (dir.content);
550       __set_errno (save_err);
551     }
552 
553   if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
554     result = 0;
555 
556   /* Prepare the return, revert the `struct FTW' information.  */
557   data->dirbuf[data->ftw.base - 1] = '\0';
558   --data->ftw.level;
559   data->ftw.base = previous_base;
560 
561   /* Finally, if we process depth-first report the directory.  */
562   if (result == 0 && (data->flags & FTW_DEPTH))
563     result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
564 
565   if (old_dir
566       && (data->flags & FTW_CHDIR)
567       && (result == 0
568 	  || ((data->flags & FTW_ACTIONRETVAL)
569 	      && (result != -1 && result != FTW_STOP))))
570     {
571       /* Change back to the parent directory.  */
572       int done = 0;
573       if (old_dir->stream != NULL)
574 	if (__fchdir (dirfd (old_dir->stream)) == 0)
575 	  done = 1;
576 
577       if (!done)
578 	{
579 	  if (data->ftw.base == 1)
580 	    {
581 	      if (__chdir ("/") < 0)
582 		result = -1;
583 	    }
584 	  else
585 	    if (__chdir ("..") < 0)
586 	      result = -1;
587 	}
588     }
589 
590   return result;
591 }
592 
593 
594 static int
595 __attribute ((noinline))
596 internal_function
ftw_startup(const char * dir,int is_nftw,void * func,int descriptors,int flags)597 ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
598 	     int flags)
599 {
600   struct ftw_data data;
601   struct STAT st;
602   int result = 0;
603   int save_err;
604   char *cwd = NULL;
605   char *cp;
606 
607   /* First make sure the parameters are reasonable.  */
608   if (dir[0] == '\0')
609     {
610       __set_errno (ENOENT);
611       return -1;
612     }
613 
614   data.maxdir = descriptors < 1 ? 1 : descriptors;
615   data.actdir = 0;
616   data.dirstreams = (struct dir_data **) alloca (data.maxdir
617 						 * sizeof (struct dir_data *));
618   memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
619 
620   /* PATH_MAX is always defined when we get here.  */
621   data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
622   data.dirbuf = (char *) malloc (data.dirbufsize);
623   if (data.dirbuf == NULL)
624     return -1;
625   cp = __stpcpy (data.dirbuf, dir);
626   /* Strip trailing slashes.  */
627   while (cp > data.dirbuf + 1 && cp[-1] == '/')
628     --cp;
629   *cp = '\0';
630 
631   data.ftw.level = 0;
632 
633   /* Find basename.  */
634   while (cp > data.dirbuf && cp[-1] != '/')
635     --cp;
636   data.ftw.base = cp - data.dirbuf;
637 
638   data.flags = flags;
639 
640   /* This assignment might seem to be strange but it is what we want.
641      The trick is that the first three arguments to the `ftw' and
642      `nftw' callback functions are equal.  Therefore we can call in
643      every case the callback using the format of the `nftw' version
644      and get the correct result since the stack layout for a function
645      call in C allows this.  */
646   data.func = (NFTW_FUNC_T) func;
647 
648   /* Since we internally use the complete set of FTW_* values we need
649      to reduce the value range before calling a `ftw' callback.  */
650   data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
651 
652   /* No object known so far.  */
653   data.known_objects = NULL;
654 
655   /* Now go to the directory containing the initial file/directory.  */
656   if (flags & FTW_CHDIR)
657     {
658       /* GNU extension ahead.  */
659       cwd =  __getcwd (NULL, 0);
660       if (cwd == NULL)
661 	result = -1;
662       else if (data.ftw.base > 0)
663 	{
664 	  /* Change to the directory the file is in.  In data.dirbuf
665 	     we have a writable copy of the file name.  Just NUL
666 	     terminate it for now and change the directory.  */
667 	  if (data.ftw.base == 1)
668 	    /* I.e., the file is in the root directory.  */
669 	    result = __chdir ("/");
670 	  else
671 	    {
672 	      char ch = data.dirbuf[data.ftw.base - 1];
673 	      data.dirbuf[data.ftw.base - 1] = '\0';
674 	      result = __chdir (data.dirbuf);
675 	      data.dirbuf[data.ftw.base - 1] = ch;
676 	    }
677 	}
678     }
679 
680   /* Get stat info for start directory.  */
681   if (result == 0)
682     {
683       const char *name = ((data.flags & FTW_CHDIR)
684 			  ? data.dirbuf + data.ftw.base
685 			  : data.dirbuf);
686 
687       if (((flags & FTW_PHYS)
688 	   ? LXSTAT (_STAT_VER, name, &st)
689 	   : XSTAT (_STAT_VER, name, &st)) < 0)
690 	{
691 	  if (!(flags & FTW_PHYS)
692 	      && errno == ENOENT
693 	      && LXSTAT (_STAT_VER, name, &st) == 0
694 	      && S_ISLNK (st.st_mode))
695 	    result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
696 				   &data.ftw);
697 	  else
698 	    /* No need to call the callback since we cannot say anything
699 	       about the object.  */
700 	    result = -1;
701 	}
702       else
703 	{
704 	  if (S_ISDIR (st.st_mode))
705 	    {
706 	      /* Remember the device of the initial directory in case
707 		 FTW_MOUNT is given.  */
708 	      data.dev = st.st_dev;
709 
710 	      /* We know this directory now.  */
711 	      if (!(flags & FTW_PHYS))
712 		result = add_object (&data, &st);
713 
714 	      if (result == 0)
715 		result = ftw_dir (&data, &st, NULL);
716 	    }
717 	  else
718 	    {
719 	      int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
720 
721 	      result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
722 				     &data.ftw);
723 	    }
724 	}
725 
726       if ((flags & FTW_ACTIONRETVAL)
727 	  && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
728 	result = 0;
729     }
730 
731   /* Return to the start directory (if necessary).  */
732   if (cwd != NULL)
733     {
734       save_err = errno;
735       __chdir (cwd);
736       free (cwd);
737       __set_errno (save_err);
738     }
739 
740   /* Free all memory.  */
741   save_err = errno;
742   __tdestroy (data.known_objects, free);
743   free (data.dirbuf);
744   __set_errno (save_err);
745 
746   return result;
747 }
748 
749 
750 
751 /* Entry points.  */
752 #ifdef __UCLIBC_HAS_FTW__
753 int
FTW_NAME(const char * path,FTW_FUNC_T func,int descriptors)754 FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
755 {
756   return ftw_startup (path, 0, func, descriptors, 0);
757 }
758 #endif
759 
760 #ifdef __UCLIBC_HAS_NFTW__
761 #ifndef _LIBC
762 int
NFTW_NAME(const char * path,NFTW_FUNC_T func,int descriptors,int flags)763 NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
764 {
765   return ftw_startup (path, 1, func, descriptors, flags);
766 }
767 #else
768 
769 #include <shlib-compat.h>
770 
771 int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
772 
773 int
NFTW_NEW_NAME(const char * path,NFTW_FUNC_T func,int descriptors,int flags)774 NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
775 {
776   if (flags
777       & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
778     {
779       __set_errno (EINVAL);
780       return -1;
781     }
782   return ftw_startup (path, 1, func, descriptors, flags);
783 }
784 
785 versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
786 
787 #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
788 
789 /* Older nftw* version just ignored all unknown flags.  */
790 
791 int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
792 
793 int
794 attribute_compat_text_section
NFTW_OLD_NAME(const char * path,NFTW_FUNC_T func,int descriptors,int flags)795 NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
796 {
797   flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
798   return ftw_startup (path, 1, func, descriptors, flags);
799 }
800 
801 compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);
802 #endif
803 #endif
804 #endif
805