savedir.c 3.0 KB

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