scandir64.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_HAVE_LFS__
  24. #define _FILE_OFFSET_BITS 64
  25. #define __USE_LARGEFILE64
  26. #define __USE_FILE_OFFSET64
  27. #include <dirent.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <sys/types.h>
  32. #include "dirstream.h"
  33. int scandir64(const char *dir, struct dirent64 ***namelist,
  34. int (*selector) (const struct dirent64 *),
  35. int (*compar) (const __ptr_t, const __ptr_t))
  36. {
  37. DIR *d = opendir(dir);
  38. struct dirent64 *current;
  39. struct dirent64 **names;
  40. int count = 0;
  41. int pos = 0;
  42. int result = -1;
  43. if (NULL == d)
  44. return -1;
  45. while (NULL != readdir64(d))
  46. count++;
  47. if (!(names = malloc(sizeof (struct dirent64 *) * count))) {
  48. closedir(d);
  49. return -1;
  50. }
  51. rewinddir(d);
  52. while (NULL != (current = readdir64(d))) {
  53. if (NULL == selector || selector(current)) {
  54. struct dirent64 *copyentry = malloc(current->d_reclen);
  55. memcpy(copyentry, current, current->d_reclen);
  56. names[pos] = copyentry;
  57. pos++;
  58. }
  59. }
  60. result = closedir(d);
  61. if (pos != count) {
  62. struct dirent64 **tmp;
  63. if (!(tmp = realloc(names, sizeof (struct dirent64 *) * pos))) {
  64. free(names);
  65. return -1;
  66. }
  67. names = tmp;
  68. }
  69. if (compar != NULL) {
  70. qsort(names, pos, sizeof (struct dirent64 *), compar);
  71. }
  72. *namelist = names;
  73. return pos;
  74. }
  75. #endif /* __UCLIBC_HAVE_LFS__ */