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