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