savedir.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <djm@gnu.ai.mit.edu>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <sys/types.h>
  19. #if HAVE_UNISTD_H
  20. # include <unistd.h>
  21. #endif
  22. #if HAVE_DIRENT_H
  23. # include <dirent.h>
  24. # define NAMLEN(dirent) strlen((dirent)->d_name)
  25. #else
  26. # define dirent direct
  27. # define NAMLEN(dirent) (dirent)->d_namlen
  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. #else
  48. char *malloc ();
  49. char *realloc ();
  50. #endif
  51. #ifndef NULL
  52. # define NULL 0
  53. #endif
  54. #ifndef stpcpy
  55. char *stpcpy ();
  56. #endif
  57. #include "savedir.h"
  58. /* Return a freshly allocated string containing the filenames
  59. in directory DIR, separated by '\0' characters;
  60. the end is marked by two '\0' characters in a row.
  61. NAME_SIZE is the number of bytes to initially allocate
  62. for the string; it will be enlarged as needed.
  63. Return NULL if DIR cannot be opened or if out of memory. */
  64. char *
  65. savedir (const char *dir, off_t name_size)
  66. {
  67. DIR *dirp;
  68. struct dirent *dp;
  69. char *name_space;
  70. char *namep;
  71. dirp = opendir (dir);
  72. if (dirp == NULL)
  73. return NULL;
  74. /* Be sure name_size is at least `1' so there's room for
  75. the final NUL byte. */
  76. if (name_size <= 0)
  77. name_size = 1;
  78. name_space = (char *) malloc (name_size);
  79. if (name_space == NULL)
  80. {
  81. closedir (dirp);
  82. return NULL;
  83. }
  84. namep = name_space;
  85. while ((dp = readdir (dirp)) != NULL)
  86. {
  87. /* Skip "." and ".." (some NFS filesystems' directories lack them). */
  88. if (dp->d_name[0] != '.'
  89. || (dp->d_name[1] != '\0'
  90. && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
  91. {
  92. off_t size_needed = (namep - name_space) + NAMLEN (dp) + 2;
  93. if (size_needed > name_size)
  94. {
  95. char *new_name_space;
  96. while (size_needed > name_size)
  97. name_size += 1024;
  98. new_name_space = realloc (name_space, name_size);
  99. if (new_name_space == NULL)
  100. {
  101. closedir (dirp);
  102. return NULL;
  103. }
  104. namep += new_name_space - name_space;
  105. name_space = new_name_space;
  106. }
  107. namep = stpcpy (namep, dp->d_name) + 1;
  108. }
  109. }
  110. *namep = '\0';
  111. if (CLOSEDIR (dirp))
  112. {
  113. free (name_space);
  114. return NULL;
  115. }
  116. return name_space;
  117. }