php_scandir.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Shane Caraveo <shane@caraveo.com> |
  14. | Ilia Alshanetsky <ilia@prohost.org> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php.h"
  18. #include "php_scandir.h"
  19. #ifdef HAVE_SYS_TYPES_H
  20. #include <sys/types.h>
  21. #endif
  22. #ifdef HAVE_DIRENT_H
  23. #include <dirent.h>
  24. #endif
  25. #ifndef HAVE_SCANDIR
  26. #ifdef PHP_WIN32
  27. #include "win32/param.h"
  28. #include "win32/readdir.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <search.h>
  32. #endif /* HAVE_SCANDIR */
  33. #ifndef HAVE_ALPHASORT
  34. #include <string.h>
  35. PHPAPI int php_alphasort(const struct dirent **a, const struct dirent **b)
  36. {
  37. return strcoll((*a)->d_name,(*b)->d_name);
  38. }
  39. #endif /* HAVE_ALPHASORT */
  40. #ifndef HAVE_SCANDIR
  41. PHPAPI int php_scandir(const char *dirname, struct dirent **namelist[], int (*selector) (const struct dirent *entry), int (*compare) (const struct dirent **a, const struct dirent **b))
  42. {
  43. DIR *dirp = NULL;
  44. struct dirent **vector = NULL;
  45. int vector_size = 0;
  46. int nfiles = 0;
  47. struct dirent *dp;
  48. if (namelist == NULL) {
  49. return -1;
  50. }
  51. if (!(dirp = opendir(dirname))) {
  52. return -1;
  53. }
  54. while ((dp = readdir(dirp))) {
  55. size_t dsize = 0;
  56. struct dirent *newdp = NULL;
  57. if (selector && (*selector)(dp) == 0) {
  58. continue;
  59. }
  60. if (nfiles == vector_size) {
  61. struct dirent **newv;
  62. if (vector_size == 0) {
  63. vector_size = 10;
  64. } else {
  65. vector_size *= 2;
  66. }
  67. newv = (struct dirent **) realloc (vector, vector_size * sizeof (struct dirent *));
  68. if (!newv) {
  69. return -1;
  70. }
  71. vector = newv;
  72. }
  73. dsize = sizeof (struct dirent) + ((strlen(dp->d_name) + 1) * sizeof(char));
  74. newdp = (struct dirent *) malloc(dsize);
  75. if (newdp == NULL) {
  76. goto fail;
  77. }
  78. vector[nfiles++] = (struct dirent *) memcpy(newdp, dp, dsize);
  79. }
  80. closedir(dirp);
  81. *namelist = vector;
  82. if (compare) {
  83. qsort (*namelist, nfiles, sizeof(struct dirent *), (int (*) (const void *, const void *)) compare);
  84. }
  85. return nfiles;
  86. fail:
  87. while (nfiles-- > 0) {
  88. free(vector[nfiles]);
  89. }
  90. free(vector);
  91. return -1;
  92. }
  93. #endif