savedir.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* savedir.c -- save the list of files in a directory in a string
  2. Copyright 1990,97,98,99,2000, 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by David MacKenzie <[email protected]>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <sys/types.h>
  19. #include <errno.h>
  20. #ifndef errno
  21. extern int errno;
  22. #endif
  23. #if HAVE_DIRENT_H
  24. # include <dirent.h>
  25. #else
  26. # define dirent direct
  27. # if HAVE_SYS_NDIR_H
  28. # include <sys/ndir.h>
  29. # endif
  30. # if HAVE_SYS_DIR_H
  31. # include <sys/dir.h>
  32. # endif
  33. # if HAVE_NDIR_H
  34. # include <ndir.h>
  35. # endif
  36. #endif
  37. #ifdef CLOSEDIR_VOID
  38. /* Fake a return value. */
  39. # define CLOSEDIR(d) (closedir (d), 0)
  40. #else
  41. # define CLOSEDIR(d) closedir (d)
  42. #endif
  43. #ifdef STDC_HEADERS
  44. # include <stdlib.h>
  45. # include <string.h>
  46. #endif
  47. #ifndef NULL
  48. # define NULL 0
  49. #endif
  50. #include "savedir.h"
  51. #include "xalloc.h"
  52. /* Return a freshly allocated string containing the filenames
  53. in directory DIR, separated by '\0' characters;
  54. the end is marked by two '\0' characters in a row.
  55. Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
  56. #ifndef NAME_SIZE_DEFAULT
  57. # define NAME_SIZE_DEFAULT 512
  58. #endif
  59. char *
  60. savedir (const char *dir)
  61. {
  62. DIR *dirp;
  63. struct dirent *dp;
  64. char *name_space;
  65. size_t allocated = NAME_SIZE_DEFAULT;
  66. size_t used = 0;
  67. int save_errno;
  68. dirp = opendir (dir);
  69. if (dirp == NULL)
  70. return NULL;
  71. name_space = xmalloc (allocated);
  72. errno = 0;
  73. while ((dp = readdir (dirp)) != NULL)
  74. {
  75. /* Skip "", ".", and "..". "" is returned by at least one buggy
  76. implementation: Solaris 2.4 readdir on NFS filesystems. */
  77. char const *entry = dp->d_name;
  78. if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
  79. {
  80. size_t entry_size = strlen (entry) + 1;
  81. if (used + entry_size < used)
  82. xalloc_die ();
  83. if (allocated <= used + entry_size)
  84. {
  85. do
  86. {
  87. if (2 * allocated < allocated)
  88. xalloc_die ();
  89. allocated *= 2;
  90. }
  91. while (allocated <= used + entry_size);
  92. name_space = xrealloc (name_space, allocated);
  93. }
  94. memcpy (name_space + used, entry, entry_size);
  95. used += entry_size;
  96. }
  97. }
  98. name_space[used] = '\0';
  99. save_errno = errno;
  100. if (CLOSEDIR (dirp) != 0)
  101. save_errno = errno;
  102. if (save_errno != 0)
  103. {
  104. free (name_space);
  105. errno = save_errno;
  106. return NULL;
  107. }
  108. return name_space;
  109. }