scandir-tail-common.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Common implementation for scandir{at}.
  2. Copyright (C) 2018-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <string.h>
  16. #include <errno.h>
  17. int
  18. SCANDIR_TAIL (DIR *dp,
  19. DIRENT_TYPE ***namelist,
  20. int (*select) (const DIRENT_TYPE *),
  21. int (*cmp) (const DIRENT_TYPE **, const DIRENT_TYPE **))
  22. {
  23. if (dp == NULL)
  24. return -1;
  25. int save = errno;
  26. __set_errno (0);
  27. int result;
  28. struct scandir_cancel_struct c = { .dp = dp };
  29. __libc_cleanup_push (&__scandir_cancel_handler, &c);
  30. DIRENT_TYPE **v = NULL;
  31. size_t vsize = 0;
  32. DIRENT_TYPE *d;
  33. while ((d = READDIR (dp)) != NULL)
  34. {
  35. if (select != NULL)
  36. {
  37. int selected = (*select) (d);
  38. /* The SELECT function might have set errno to non-zero on
  39. success. It was zero before and it needs to be again to
  40. make the later tests work. */
  41. __set_errno (0);
  42. if (!selected)
  43. continue;
  44. }
  45. if (__glibc_unlikely (c.cnt == vsize))
  46. {
  47. if (vsize == 0)
  48. vsize = 10;
  49. else
  50. vsize *= 2;
  51. DIRENT_TYPE **new = realloc (v, vsize * sizeof *v);
  52. if (new == NULL)
  53. break;
  54. c.v = v = new;
  55. }
  56. size_t dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
  57. DIRENT_TYPE *vnew = malloc (dsize);
  58. if (vnew == NULL)
  59. break;
  60. v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
  61. /* Ignore errors from readdir, malloc or realloc. These functions
  62. might have set errno to non-zero on success. It was zero before
  63. and it needs to be again to make the latter tests work. */
  64. __set_errno (0);
  65. }
  66. if (__glibc_likely (errno == 0))
  67. {
  68. __closedir (dp);
  69. /* Sort the list if we have a comparison function to sort with. */
  70. if (cmp != NULL)
  71. qsort (v, c.cnt, sizeof *v, (__compar_fn_t) cmp);
  72. *namelist = v;
  73. result = c.cnt;
  74. }
  75. else
  76. {
  77. /* This frees everything and calls closedir. */
  78. __scandir_cancel_handler (&c);
  79. result = -1;
  80. }
  81. __libc_cleanup_pop (0);
  82. if (result >= 0)
  83. __set_errno (save);
  84. return result;
  85. }