scandir64.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* -*- Mode: C; c-file-style: "gnu" -*- */
  2. /*
  3. Copyright (c) 2000 Petter Reinholdtsen
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use, copy,
  8. modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. #include <features.h>
  23. #ifdef __UCLIBC_HAS_LFS__
  24. #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64
  25. #undef _FILE_OFFSET_BITS
  26. #define _FILE_OFFSET_BITS 64
  27. #endif
  28. #ifndef __USE_LARGEFILE64
  29. # define __USE_LARGEFILE64 1
  30. #endif
  31. /* We absolutely do _NOT_ want interfaces silently
  32. * renamed under us or very bad things will happen... */
  33. #ifdef __USE_FILE_OFFSET64
  34. # undef __USE_FILE_OFFSET64
  35. #endif
  36. #include <dirent.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <sys/types.h>
  41. #include "dirstream.h"
  42. int scandir64(const char *dir, struct dirent64 ***namelist,
  43. int (*selector) (const struct dirent64 *),
  44. int (*compar) (const __ptr_t, const __ptr_t))
  45. {
  46. DIR *d = opendir(dir);
  47. struct dirent64 *current;
  48. struct dirent64 **names;
  49. int count = 0;
  50. int pos = 0;
  51. int result = -1;
  52. if (NULL == d)
  53. return -1;
  54. while (NULL != readdir64(d))
  55. count++;
  56. if (!(names = malloc(sizeof (struct dirent64 *) * count))) {
  57. closedir(d);
  58. return -1;
  59. }
  60. rewinddir(d);
  61. while (NULL != (current = readdir64(d))) {
  62. if (NULL == selector || selector(current)) {
  63. struct dirent64 *copyentry = malloc(current->d_reclen);
  64. memcpy(copyentry, current, current->d_reclen);
  65. names[pos] = copyentry;
  66. pos++;
  67. }
  68. }
  69. result = closedir(d);
  70. if (pos != count) {
  71. struct dirent64 **tmp;
  72. if (!(tmp = realloc(names, sizeof (struct dirent64 *) * pos))) {
  73. free(names);
  74. return -1;
  75. }
  76. names = tmp;
  77. }
  78. if (compar != NULL) {
  79. qsort(names, pos, sizeof (struct dirent64 *), compar);
  80. }
  81. *namelist = names;
  82. return pos;
  83. }
  84. #endif /* __UCLIBC_HAS_LFS__ */