ftw.c 19 KB

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