ftw.c 19 KB

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