scandir.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * libcompat - system compatibility library
  3. *
  4. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  5. * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <dirent.h>
  24. #include <stdlib.h>
  25. #include "compat.h"
  26. static int
  27. cleanup(DIR *dir, struct dirent **dirlist, int used)
  28. {
  29. if (dir)
  30. closedir(dir);
  31. if (dirlist) {
  32. int i;
  33. for (i = 0; i < used; i++)
  34. free(dirlist[i]);
  35. free(dirlist);
  36. }
  37. return -1;
  38. }
  39. int
  40. scandir(const char *dir, struct dirent ***namelist,
  41. int (*filter)(const struct dirent *),
  42. int (*cmp)(const void *, const void *))
  43. {
  44. DIR *d;
  45. struct dirent *e, *m, **list;
  46. int used, avail;
  47. d = opendir(dir);
  48. if (!d)
  49. return -1;
  50. list = NULL;
  51. used = avail = 0;
  52. while ((e = readdir(d)) != NULL) {
  53. if (filter != NULL && !filter(e))
  54. continue;
  55. if (used >= avail - 1) {
  56. struct dirent **newlist;
  57. if (avail)
  58. avail *= 2;
  59. else
  60. avail = 20;
  61. newlist = realloc(list, avail * sizeof(struct dirent *));
  62. if (!newlist)
  63. return cleanup(d, list, used);
  64. list = newlist;
  65. }
  66. m = malloc(sizeof(struct dirent) + strlen(e->d_name));
  67. if (!m)
  68. return cleanup(d, list, used);
  69. *m = *e;
  70. strcpy(m->d_name, e->d_name);
  71. list[used] = m;
  72. used++;
  73. }
  74. closedir(d);
  75. if (list != NULL && cmp != NULL)
  76. qsort(list, used, sizeof(struct dirent *), cmp);
  77. *namelist = list;
  78. return used;
  79. }