ftw.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <features.h>
  21. /* need errno.h before undefining _LIBC */
  22. #include <errno.h>
  23. #ifdef __UCLIBC__
  24. #undef _LIBC
  25. #define HAVE_DIRENT_H 1
  26. #define HAVE_SYS_PARAM_H 1
  27. #define HAVE_DECL_STPCPY 1
  28. #define HAVE_MEMPCPY 1
  29. #endif
  30. #if __GNUC__
  31. # undef alloca
  32. # define alloca __builtin_alloca
  33. #else
  34. # if HAVE_ALLOCA_H
  35. # include <alloca.h>
  36. # else
  37. # ifdef _AIX
  38. # pragma alloca
  39. # else
  40. char *alloca ();
  41. # endif
  42. # endif
  43. #endif
  44. #if defined _LIBC
  45. # include <dirent.h>
  46. # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent)
  47. #else
  48. # if HAVE_DIRENT_H
  49. # include <dirent.h>
  50. # define NAMLEN(dirent) strlen ((dirent)->d_name)
  51. # else
  52. # define dirent direct
  53. # define NAMLEN(dirent) (dirent)->d_namlen
  54. # if HAVE_SYS_NDIR_H
  55. # include <sys/ndir.h>
  56. # endif
  57. # if HAVE_SYS_DIR_H
  58. # include <sys/dir.h>
  59. # endif
  60. # if HAVE_NDIR_H
  61. # include <ndir.h>
  62. # endif
  63. # endif
  64. #endif
  65. #include <ftw.h>
  66. #include <limits.h>
  67. #include <search.h>
  68. #include <stdlib.h>
  69. #include <string.h>
  70. #include <unistd.h>
  71. #if HAVE_SYS_PARAM_H || defined _LIBC
  72. # include <sys/param.h>
  73. #endif
  74. #include <sys/stat.h>
  75. #if !defined _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
  76. char *stpcpy ();
  77. #endif
  78. #if !defined _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
  79. /* Be CAREFUL that there are no side effects in N. */
  80. # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
  81. #endif
  82. /* #define NDEBUG 1 */
  83. #include <assert.h>
  84. #if !defined _LIBC
  85. # undef __chdir
  86. # define __chdir chdir
  87. # undef __closedir
  88. # define __closedir closedir
  89. # undef __fchdir
  90. # define __fchdir fchdir
  91. # undef __getcwd
  92. # ifndef __UCLIBC__
  93. # define __getcwd(P, N) xgetcwd ()
  94. extern char *xgetcwd (void);
  95. # else
  96. # define __getcwd getcwd
  97. # endif
  98. # undef __mempcpy
  99. # define __mempcpy mempcpy
  100. # undef __opendir
  101. # define __opendir opendir
  102. # undef __readdir64
  103. # ifndef __UCLIBC_HAS_LFS__
  104. # define __readdir64 readdir
  105. # else
  106. # define __readdir64 readdir64
  107. # endif
  108. # undef __stpcpy
  109. # define __stpcpy stpcpy
  110. # undef __tdestroy
  111. # define __tdestroy tdestroy
  112. # undef __tfind
  113. # define __tfind tfind
  114. # undef __tsearch
  115. # define __tsearch tsearch
  116. # undef internal_function
  117. # define internal_function /* empty */
  118. # ifndef __UCLIBC_HAS_LFS__
  119. # undef dirent64
  120. # define dirent64 dirent
  121. # endif
  122. # undef MAX
  123. # define MAX(a, b) ((a) > (b) ? (a) : (b))
  124. #endif
  125. /* Arrange to make lstat calls go through the wrapper function
  126. on systems with an lstat function that does not dereference symlinks
  127. that are specified with a trailing slash. */
  128. #if !defined _LIBC && !defined LSTAT_FOLLOWS_SLASHED_SYMLINK && !defined __UCLIBC__
  129. int rpl_lstat (const char *, struct stat *);
  130. # undef lstat
  131. # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
  132. #endif
  133. #ifndef __set_errno
  134. # define __set_errno(Val) errno = (Val)
  135. #endif
  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. # else
  148. # define LXSTAT(V,f,sb) lstat (f,sb)
  149. # define XSTAT(V,f,sb) stat (f,sb)
  150. # endif
  151. # define FTW_FUNC_T __ftw_func_t
  152. # define NFTW_FUNC_T __nftw_func_t
  153. #endif
  154. /* We define PATH_MAX if the system does not provide a definition.
  155. This does not artificially limit any operation. PATH_MAX is simply
  156. used as a guesstimate for the expected maximal path length.
  157. Buffers will be enlarged if necessary. */
  158. #ifndef PATH_MAX
  159. # define PATH_MAX 1024
  160. #endif
  161. struct dir_data
  162. {
  163. DIR *stream;
  164. char *content;
  165. };
  166. struct known_object
  167. {
  168. dev_t dev;
  169. INO_T ino;
  170. };
  171. struct ftw_data
  172. {
  173. /* Array with pointers to open directory streams. */
  174. struct dir_data **dirstreams;
  175. size_t actdir;
  176. size_t maxdir;
  177. /* Buffer containing name of currently processed object. */
  178. char *dirbuf;
  179. size_t dirbufsize;
  180. /* Passed as fourth argument to `nftw' callback. The `base' member
  181. tracks the content of the `dirbuf'. */
  182. struct FTW ftw;
  183. /* Flags passed to `nftw' function. 0 for `ftw'. */
  184. int flags;
  185. /* Conversion array for flag values. It is the identity mapping for
  186. `nftw' calls, otherwise it maps the values to those known by
  187. `ftw'. */
  188. const int *cvt_arr;
  189. /* Callback function. We always use the `nftw' form. */
  190. NFTW_FUNC_T func;
  191. /* Device of starting point. Needed for FTW_MOUNT. */
  192. dev_t dev;
  193. /* Data structure for keeping fingerprints of already processed
  194. object. This is needed when not using FTW_PHYS. */
  195. void *known_objects;
  196. };
  197. /* Internally we use the FTW_* constants used for `nftw'. When invoked
  198. as `ftw', map each flag to the subset of values used by `ftw'. */
  199. static const int nftw_arr[] =
  200. {
  201. FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
  202. };
  203. static const int ftw_arr[] =
  204. {
  205. FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
  206. };
  207. /* Forward declarations of local functions. */
  208. static int ftw_dir (struct ftw_data *data, struct STAT *st,
  209. struct dir_data *old_dir) internal_function;
  210. static int
  211. object_compare (const void *p1, const void *p2)
  212. {
  213. /* We don't need a sophisticated and useful comparison. We are only
  214. interested in equality. However, we must be careful not to
  215. accidentally compare `holes' in the structure. */
  216. const struct known_object *kp1 = p1, *kp2 = p2;
  217. int cmp1;
  218. cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
  219. if (cmp1 != 0)
  220. return cmp1;
  221. return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
  222. }
  223. static __inline__ int
  224. add_object (struct ftw_data *data, struct STAT *st)
  225. {
  226. struct known_object *newp = malloc (sizeof (struct known_object));
  227. if (newp == NULL)
  228. return -1;
  229. newp->dev = st->st_dev;
  230. newp->ino = st->st_ino;
  231. return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
  232. }
  233. static __inline__ int
  234. find_object (struct ftw_data *data, struct STAT *st)
  235. {
  236. struct known_object obj;
  237. obj.dev = st->st_dev;
  238. obj.ino = st->st_ino;
  239. return __tfind (&obj, &data->known_objects, object_compare) != NULL;
  240. }
  241. static __inline__ int
  242. __attribute ((always_inline))
  243. open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
  244. {
  245. int result = 0;
  246. if (data->dirstreams[data->actdir] != NULL)
  247. {
  248. /* Oh, oh. We must close this stream. Get all remaining
  249. entries and store them as a list in the `content' member of
  250. the `struct dir_data' variable. */
  251. size_t bufsize = 1024;
  252. char *buf = malloc (bufsize);
  253. if (buf == NULL)
  254. result = -1;
  255. else
  256. {
  257. DIR *st = data->dirstreams[data->actdir]->stream;
  258. struct dirent64 *d;
  259. size_t actsize = 0;
  260. while ((d = __readdir64 (st)) != NULL)
  261. {
  262. size_t this_len = NAMLEN (d);
  263. if (actsize + this_len + 2 >= bufsize)
  264. {
  265. char *newp;
  266. bufsize += MAX (1024, 2 * this_len);
  267. newp = (char *) realloc (buf, bufsize);
  268. if (newp == NULL)
  269. {
  270. /* No more memory. */
  271. int save_err = errno;
  272. free (buf);
  273. __set_errno (save_err);
  274. result = -1;
  275. break;
  276. }
  277. buf = newp;
  278. }
  279. *((char *) __mempcpy (buf + actsize, d->d_name, this_len))
  280. = '\0';
  281. actsize += this_len + 1;
  282. }
  283. /* Terminate the list with an additional NUL byte. */
  284. buf[actsize++] = '\0';
  285. /* Shrink the buffer to what we actually need. */
  286. data->dirstreams[data->actdir]->content = realloc (buf, actsize);
  287. if (data->dirstreams[data->actdir]->content == NULL)
  288. {
  289. int save_err = errno;
  290. free (buf);
  291. __set_errno (save_err);
  292. result = -1;
  293. }
  294. else
  295. {
  296. __closedir (st);
  297. data->dirstreams[data->actdir]->stream = NULL;
  298. data->dirstreams[data->actdir] = NULL;
  299. }
  300. }
  301. }
  302. /* Open the new stream. */
  303. if (result == 0)
  304. {
  305. const char *name = ((data->flags & FTW_CHDIR)
  306. ? data->dirbuf + data->ftw.base: data->dirbuf);
  307. assert (data->dirstreams[data->actdir] == NULL);
  308. dirp->stream = __opendir (name);
  309. if (dirp->stream == NULL)
  310. result = -1;
  311. else
  312. {
  313. dirp->content = NULL;
  314. data->dirstreams[data->actdir] = dirp;
  315. if (++data->actdir == data->maxdir)
  316. data->actdir = 0;
  317. }
  318. }
  319. return result;
  320. }
  321. static int
  322. internal_function
  323. process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
  324. size_t namlen)
  325. {
  326. struct STAT st;
  327. int result = 0;
  328. int flag = 0;
  329. size_t new_buflen;
  330. if (name[0] == '.' && (name[1] == '\0'
  331. || (name[1] == '.' && name[2] == '\0')))
  332. /* Don't process the "." and ".." entries. */
  333. return 0;
  334. new_buflen = data->ftw.base + namlen + 2;
  335. if (data->dirbufsize < new_buflen)
  336. {
  337. /* Enlarge the buffer. */
  338. char *newp;
  339. data->dirbufsize = 2 * new_buflen;
  340. newp = (char *) realloc (data->dirbuf, data->dirbufsize);
  341. if (newp == NULL)
  342. return -1;
  343. data->dirbuf = newp;
  344. }
  345. *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
  346. if ((data->flags & FTW_CHDIR) == 0)
  347. name = data->dirbuf;
  348. if (((data->flags & FTW_PHYS)
  349. ? LXSTAT (_STAT_VER, name, &st)
  350. : XSTAT (_STAT_VER, name, &st)) < 0)
  351. {
  352. if (errno != EACCES && errno != ENOENT)
  353. result = -1;
  354. else if (!(data->flags & FTW_PHYS)
  355. && LXSTAT (_STAT_VER, name, &st) == 0
  356. && S_ISLNK (st.st_mode))
  357. flag = FTW_SLN;
  358. else
  359. flag = FTW_NS;
  360. }
  361. else
  362. {
  363. if (S_ISDIR (st.st_mode))
  364. flag = FTW_D;
  365. else if (S_ISLNK (st.st_mode))
  366. flag = FTW_SL;
  367. else
  368. flag = FTW_F;
  369. }
  370. if (result == 0
  371. && (flag == FTW_NS
  372. || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
  373. {
  374. if (flag == FTW_D)
  375. {
  376. if ((data->flags & FTW_PHYS)
  377. || (!find_object (data, &st)
  378. /* Remember the object. */
  379. && (result = add_object (data, &st)) == 0))
  380. result = ftw_dir (data, &st, dir);
  381. }
  382. else
  383. result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
  384. &data->ftw);
  385. }
  386. if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE)
  387. result = 0;
  388. return result;
  389. }
  390. static int
  391. __attribute ((noinline))
  392. internal_function
  393. ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir)
  394. {
  395. struct dir_data dir;
  396. struct dirent64 *d;
  397. int previous_base = data->ftw.base;
  398. int result;
  399. char *startp;
  400. /* Open the stream for this directory. This might require that
  401. another stream has to be closed. */
  402. result = open_dir_stream (data, &dir);
  403. if (result != 0)
  404. {
  405. if (errno == EACCES)
  406. /* We cannot read the directory. Signal this with a special flag. */
  407. result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
  408. return result;
  409. }
  410. /* First, report the directory (if not depth-first). */
  411. if (!(data->flags & FTW_DEPTH))
  412. {
  413. result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
  414. if (result != 0)
  415. {
  416. int save_err;
  417. fail:
  418. save_err = errno;
  419. __closedir (dir.stream);
  420. __set_errno (save_err);
  421. if (data->actdir-- == 0)
  422. data->actdir = data->maxdir - 1;
  423. data->dirstreams[data->actdir] = NULL;
  424. return result;
  425. }
  426. }
  427. /* If necessary, change to this directory. */
  428. if (data->flags & FTW_CHDIR)
  429. {
  430. if (__fchdir (dirfd (dir.stream)) < 0)
  431. {
  432. result = -1;
  433. goto fail;
  434. }
  435. }
  436. /* Next, update the `struct FTW' information. */
  437. ++data->ftw.level;
  438. startp = strchr (data->dirbuf, '\0');
  439. /* There always must be a directory name. */
  440. assert (startp != data->dirbuf);
  441. if (startp[-1] != '/')
  442. *startp++ = '/';
  443. data->ftw.base = startp - data->dirbuf;
  444. while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
  445. {
  446. result = process_entry (data, &dir, d->d_name, NAMLEN (d));
  447. if (result != 0)
  448. break;
  449. }
  450. if (dir.stream != NULL)
  451. {
  452. /* The stream is still open. I.e., we did not need more
  453. descriptors. Simply close the stream now. */
  454. int save_err = errno;
  455. assert (dir.content == NULL);
  456. __closedir (dir.stream);
  457. __set_errno (save_err);
  458. if (data->actdir-- == 0)
  459. data->actdir = data->maxdir - 1;
  460. data->dirstreams[data->actdir] = NULL;
  461. }
  462. else
  463. {
  464. int save_err;
  465. char *runp = dir.content;
  466. while (result == 0 && *runp != '\0')
  467. {
  468. char *endp = strchr (runp, '\0');
  469. result = process_entry (data, &dir, runp, endp - runp);
  470. runp = endp + 1;
  471. }
  472. save_err = errno;
  473. free (dir.content);
  474. __set_errno (save_err);
  475. }
  476. if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS)
  477. result = 0;
  478. /* Prepare the return, revert the `struct FTW' information. */
  479. data->dirbuf[data->ftw.base - 1] = '\0';
  480. --data->ftw.level;
  481. data->ftw.base = previous_base;
  482. /* Finally, if we process depth-first report the directory. */
  483. if (result == 0 && (data->flags & FTW_DEPTH))
  484. result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
  485. if (old_dir
  486. && (data->flags & FTW_CHDIR)
  487. && (result == 0
  488. || ((data->flags & FTW_ACTIONRETVAL)
  489. && (result != -1 && result != FTW_STOP))))
  490. {
  491. /* Change back to the parent directory. */
  492. int done = 0;
  493. if (old_dir->stream != NULL)
  494. if (__fchdir (dirfd (old_dir->stream)) == 0)
  495. done = 1;
  496. if (!done)
  497. {
  498. if (data->ftw.base == 1)
  499. {
  500. if (__chdir ("/") < 0)
  501. result = -1;
  502. }
  503. else
  504. if (__chdir ("..") < 0)
  505. result = -1;
  506. }
  507. }
  508. return result;
  509. }
  510. static int
  511. __attribute ((noinline))
  512. internal_function
  513. ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
  514. int flags)
  515. {
  516. struct ftw_data data;
  517. struct STAT st;
  518. int result = 0;
  519. int save_err;
  520. char *cwd = NULL;
  521. char *cp;
  522. /* First make sure the parameters are reasonable. */
  523. if (dir[0] == '\0')
  524. {
  525. __set_errno (ENOENT);
  526. return -1;
  527. }
  528. data.maxdir = descriptors < 1 ? 1 : descriptors;
  529. data.actdir = 0;
  530. data.dirstreams = (struct dir_data **) alloca (data.maxdir
  531. * sizeof (struct dir_data *));
  532. memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
  533. /* PATH_MAX is always defined when we get here. */
  534. data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
  535. data.dirbuf = (char *) malloc (data.dirbufsize);
  536. if (data.dirbuf == NULL)
  537. return -1;
  538. cp = __stpcpy (data.dirbuf, dir);
  539. /* Strip trailing slashes. */
  540. while (cp > data.dirbuf + 1 && cp[-1] == '/')
  541. --cp;
  542. *cp = '\0';
  543. data.ftw.level = 0;
  544. /* Find basename. */
  545. while (cp > data.dirbuf && cp[-1] != '/')
  546. --cp;
  547. data.ftw.base = cp - data.dirbuf;
  548. data.flags = flags;
  549. /* This assignment might seem to be strange but it is what we want.
  550. The trick is that the first three arguments to the `ftw' and
  551. `nftw' callback functions are equal. Therefore we can call in
  552. every case the callback using the format of the `nftw' version
  553. and get the correct result since the stack layout for a function
  554. call in C allows this. */
  555. data.func = (NFTW_FUNC_T) func;
  556. /* Since we internally use the complete set of FTW_* values we need
  557. to reduce the value range before calling a `ftw' callback. */
  558. data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
  559. /* No object known so far. */
  560. data.known_objects = NULL;
  561. /* Now go to the directory containing the initial file/directory. */
  562. if (flags & FTW_CHDIR)
  563. {
  564. /* GNU extension ahead. */
  565. cwd = __getcwd (NULL, 0);
  566. if (cwd == NULL)
  567. result = -1;
  568. else if (data.ftw.base > 0)
  569. {
  570. /* Change to the directory the file is in. In data.dirbuf
  571. we have a writable copy of the file name. Just NUL
  572. terminate it for now and change the directory. */
  573. if (data.ftw.base == 1)
  574. /* I.e., the file is in the root directory. */
  575. result = __chdir ("/");
  576. else
  577. {
  578. char ch = data.dirbuf[data.ftw.base - 1];
  579. data.dirbuf[data.ftw.base - 1] = '\0';
  580. result = __chdir (data.dirbuf);
  581. data.dirbuf[data.ftw.base - 1] = ch;
  582. }
  583. }
  584. }
  585. /* Get stat info for start directory. */
  586. if (result == 0)
  587. {
  588. const char *name = ((data.flags & FTW_CHDIR)
  589. ? data.dirbuf + data.ftw.base
  590. : data.dirbuf);
  591. if (((flags & FTW_PHYS)
  592. ? LXSTAT (_STAT_VER, name, &st)
  593. : XSTAT (_STAT_VER, name, &st)) < 0)
  594. {
  595. if (!(flags & FTW_PHYS)
  596. && errno == ENOENT
  597. && LXSTAT (_STAT_VER, name, &st) == 0
  598. && S_ISLNK (st.st_mode))
  599. result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
  600. &data.ftw);
  601. else
  602. /* No need to call the callback since we cannot say anything
  603. about the object. */
  604. result = -1;
  605. }
  606. else
  607. {
  608. if (S_ISDIR (st.st_mode))
  609. {
  610. /* Remember the device of the initial directory in case
  611. FTW_MOUNT is given. */
  612. data.dev = st.st_dev;
  613. /* We know this directory now. */
  614. if (!(flags & FTW_PHYS))
  615. result = add_object (&data, &st);
  616. if (result == 0)
  617. result = ftw_dir (&data, &st, NULL);
  618. }
  619. else
  620. {
  621. int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
  622. result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
  623. &data.ftw);
  624. }
  625. }
  626. if ((flags & FTW_ACTIONRETVAL)
  627. && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS))
  628. result = 0;
  629. }
  630. /* Return to the start directory (if necessary). */
  631. if (cwd != NULL)
  632. {
  633. save_err = errno;
  634. __chdir (cwd);
  635. free (cwd);
  636. __set_errno (save_err);
  637. }
  638. /* Free all memory. */
  639. save_err = errno;
  640. __tdestroy (data.known_objects, free);
  641. free (data.dirbuf);
  642. __set_errno (save_err);
  643. return result;
  644. }
  645. /* Entry points. */
  646. #ifdef __UCLIBC_HAS_FTW__
  647. int
  648. FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
  649. {
  650. return ftw_startup (path, 0, func, descriptors, 0);
  651. }
  652. #endif
  653. #ifdef __UCLIBC_HAS_NFTW__
  654. #ifndef _LIBC
  655. int
  656. NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
  657. {
  658. return ftw_startup (path, 1, func, descriptors, flags);
  659. }
  660. #else
  661. #include <shlib-compat.h>
  662. int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int);
  663. int
  664. NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
  665. {
  666. if (flags
  667. & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL))
  668. {
  669. __set_errno (EINVAL);
  670. return -1;
  671. }
  672. return ftw_startup (path, 1, func, descriptors, flags);
  673. }
  674. versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3);
  675. #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3)
  676. /* Older nftw* version just ignored all unknown flags. */
  677. int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int);
  678. int
  679. attribute_compat_text_section
  680. NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
  681. {
  682. flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH);
  683. return ftw_startup (path, 1, func, descriptors, flags);
  684. }
  685. compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1);
  686. #endif
  687. #endif
  688. #endif