savedir.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* savedir.c -- save the list of files in a directory in a string
  2. Copyright (C) 1990, 1997, 1998, 1999, 2000 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. NAME_SIZE is the number of bytes to initially allocate
  56. for the string; it will be enlarged as needed.
  57. Use NAME_SIZE == -1 if you do not know the size.
  58. Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
  59. #ifndef NAME_SIZE_DEFAULT
  60. # define NAME_SIZE_DEFAULT 512
  61. #endif
  62. char *
  63. savedir (const char *dir, off_t name_size)
  64. {
  65. DIR *dirp;
  66. struct dirent *dp;
  67. char *name_space;
  68. size_t allocated = name_size; /* Overflow is checked indirectly below. */
  69. size_t used = 0;
  70. int save_errno;
  71. dirp = opendir (dir);
  72. if (dirp == NULL)
  73. return NULL;
  74. /* Use the default if the size is not known. Be sure "allocated"
  75. is at least `1' so there's room for the final NUL byte.
  76. Do not simply test name_size <= 0, because the initialization
  77. of "allocated" might have overflowed. */
  78. if (name_size < 0 || allocated == 0)
  79. allocated = NAME_SIZE_DEFAULT;
  80. name_space = xmalloc (allocated);
  81. errno = 0;
  82. while ((dp = readdir (dirp)) != NULL)
  83. {
  84. /* Skip "", ".", and "..". "" is returned by at least one buggy
  85. implementation: Solaris 2.4 readdir on NFS filesystems. */
  86. char const *entry = dp->d_name;
  87. if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
  88. {
  89. size_t entry_size = strlen (entry) + 1;
  90. if (used + entry_size < used)
  91. xalloc_die ();
  92. if (allocated <= used + entry_size)
  93. {
  94. do
  95. {
  96. if (2 * allocated < allocated)
  97. xalloc_die ();
  98. allocated *= 2;
  99. }
  100. while (allocated <= used + entry_size);
  101. name_space = xrealloc (name_space, allocated);
  102. }
  103. memcpy (name_space + used, entry, entry_size);
  104. used += entry_size;
  105. }
  106. }
  107. name_space[used] = '\0';
  108. save_errno = errno;
  109. if (CLOSEDIR (dirp) != 0)
  110. save_errno = errno;
  111. if (save_errno != 0)
  112. {
  113. free (name_space);
  114. errno = save_errno;
  115. return NULL;
  116. }
  117. return name_space;
  118. }