1 /* savedir.c -- save the list of files in a directory in a string
3 Copyright 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
28 #include <sys/types.h>
35 # define dirent direct
37 # include <sys/ndir.h>
48 /* Fake a return value. */
49 # define CLOSEDIR(d) (closedir (d), 0)
51 # define CLOSEDIR(d) closedir (d)
60 /* Return a freshly allocated string containing the filenames
61 in directory DIR, separated by '\0' characters;
62 the end is marked by two '\0' characters in a row.
63 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
65 #ifndef NAME_SIZE_DEFAULT
66 # define NAME_SIZE_DEFAULT 512
70 savedir (const char *dir)
75 size_t allocated = NAME_SIZE_DEFAULT;
83 name_space = xmalloc (allocated);
86 while ((dp = readdir (dirp)) != NULL)
88 /* Skip "", ".", and "..". "" is returned by at least one buggy
89 implementation: Solaris 2.4 readdir on NFS file systems. */
90 char const *entry = dp->d_name;
91 if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
93 size_t entry_size = strlen (entry) + 1;
94 if (used + entry_size < used)
96 if (allocated <= used + entry_size)
100 if (2 * allocated < allocated)
104 while (allocated <= used + entry_size);
106 name_space = xrealloc (name_space, allocated);
108 memcpy (name_space + used, entry, entry_size);
112 name_space[used] = '\0';
114 if (CLOSEDIR (dirp) != 0)