scandir64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright (C) 2000-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #define scandir __no_scandir_decl
  15. #include <dirent.h>
  16. #undef scandir
  17. int
  18. __scandir64 (const char *dir, struct dirent64 ***namelist,
  19. int (*select) (const struct dirent64 *),
  20. int (*cmp) (const struct dirent64 **, const struct dirent64 **))
  21. {
  22. return __scandir64_tail (__opendir (dir), namelist, select, cmp);
  23. }
  24. #if _DIRENT_MATCHES_DIRENT64
  25. weak_alias (__scandir64, scandir64)
  26. weak_alias (__scandir64, scandir)
  27. #else
  28. # include <shlib-compat.h>
  29. versioned_symbol (libc, __scandir64, scandir64, GLIBC_2_2);
  30. # if SHLIB_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
  31. # include <string.h>
  32. # include <errno.h>
  33. # include "olddirent.h"
  34. int
  35. __old_scandir64 (const char *dir, struct __old_dirent64 ***namelist,
  36. int (*select) (const struct __old_dirent64 *),
  37. int (*cmp) (const struct __old_dirent64 **,
  38. const struct __old_dirent64 **))
  39. {
  40. DIR *dp = __opendir (dir);
  41. struct __old_dirent64 **v = NULL;
  42. size_t vsize = 0;
  43. struct scandir_cancel_struct c;
  44. struct __old_dirent64 *d;
  45. int save;
  46. if (dp == NULL)
  47. return -1;
  48. save = errno;
  49. __set_errno (0);
  50. c.dp = dp;
  51. c.v = NULL;
  52. c.cnt = 0;
  53. __libc_cleanup_push (__scandir_cancel_handler, &c);
  54. while ((d = __old_readdir64 (dp)) != NULL)
  55. {
  56. int use_it = select == NULL;
  57. if (! use_it)
  58. {
  59. use_it = select (d);
  60. /* The select function might have changed errno. It was
  61. zero before and it need to be again to make the latter
  62. tests work. */
  63. __set_errno (0);
  64. }
  65. if (use_it)
  66. {
  67. struct __old_dirent64 *vnew;
  68. size_t dsize;
  69. /* Ignore errors from select or readdir */
  70. __set_errno (0);
  71. if (__glibc_unlikely (c.cnt == vsize))
  72. {
  73. struct __old_dirent64 **new;
  74. if (vsize == 0)
  75. vsize = 10;
  76. else
  77. vsize *= 2;
  78. new = (struct __old_dirent64 **) realloc (v,
  79. vsize * sizeof (*v));
  80. if (new == NULL)
  81. break;
  82. v = new;
  83. c.v = (void *) v;
  84. }
  85. dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
  86. vnew = (struct __old_dirent64 *) malloc (dsize);
  87. if (vnew == NULL)
  88. break;
  89. v[c.cnt++] = (struct __old_dirent64 *) memcpy (vnew, d, dsize);
  90. }
  91. }
  92. if (__builtin_expect (errno, 0) != 0)
  93. {
  94. save = errno;
  95. while (c.cnt > 0)
  96. free (v[--c.cnt]);
  97. free (v);
  98. c.cnt = -1;
  99. }
  100. else
  101. {
  102. /* Sort the list if we have a comparison function to sort with. */
  103. if (cmp != NULL)
  104. qsort (v, c.cnt, sizeof (*v),
  105. (int (*) (const void *, const void *)) cmp);
  106. *namelist = v;
  107. }
  108. __libc_cleanup_pop (0);
  109. (void) __closedir (dp);
  110. __set_errno (save);
  111. return c.cnt;
  112. }
  113. compat_symbol (libc, __old_scandir64, scandir64, GLIBC_2_1);
  114. # endif /* SHLIB_COMPAT (libc, GLIBC_2_1, GLIBC_2_2) */
  115. #endif /* _DIRENT_MATCHES_DIRENT64 */