1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* savedir.c -- save the list of files in a directory in a string
5 Copyright (C) 1990, 1997-2001, 2003-2006, 2009-2010 Free Software
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
27 #include <sys/types.h>
32 #ifndef _D_EXACT_NAMLEN
33 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
42 #ifndef NAME_SIZE_DEFAULT
43 # define NAME_SIZE_DEFAULT 512
46 /* Return a freshly allocated string containing the file names
47 in directory DIRP, separated by '\0' characters;
48 the end is marked by two '\0' characters in a row.
49 Return NULL (setting errno) if DIRP cannot be read or closed.
50 If DIRP is NULL, return NULL without affecting errno. */
53 savedirstream (DIR *dirp)
56 size_t allocated = NAME_SIZE_DEFAULT;
63 name_space = xmalloc (allocated);
67 struct dirent const *dp;
75 /* Skip "", ".", and "..". "" is returned by at least one buggy
76 implementation: Solaris 2.4 readdir on NFS file systems. */
78 if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
80 size_t entry_size = _D_EXACT_NAMLEN (dp) + 1;
81 if (used + entry_size < used)
83 if (allocated <= used + entry_size)
87 if (2 * allocated < allocated)
91 while (allocated <= used + entry_size);
93 name_space = xrealloc (name_space, allocated);
95 memcpy (name_space + used, entry, entry_size);
99 name_space[used] = '\0';
101 if (closedir (dirp) != 0)
112 /* Return a freshly allocated string containing the file names
113 in directory DIR, separated by '\0' characters;
114 the end is marked by two '\0' characters in a row.
115 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
118 savedir (char const *dir)
120 return savedirstream (opendir (dir));
123 /* Return a freshly allocated string containing the file names
124 in directory FD, separated by '\0' characters;
125 the end is marked by two '\0' characters in a row.
126 Return NULL (setting errno) if FD cannot be read or closed. */
131 return savedirstream (fdopendir (fd));