OSDN Git Service

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