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-2014 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)
50 /* Compare the names of two directory entries */
53 direntry_cmp_name (void const *a, void const *b)
55 direntry_t const *dea = a;
56 direntry_t const *deb = b;
58 return strcmp (dea->name, deb->name);
62 /* Compare the inode numbers of two directory entries */
65 direntry_cmp_inode (void const *a, void const *b)
67 direntry_t const *dea = a;
68 direntry_t const *deb = b;
70 return dea->ino < deb->ino ? -1 : dea->ino > deb->ino;
74 typedef int (*comparison_function) (void const *, void const *);
76 static comparison_function const comparison_function_table[] =
85 /* Return a freshly allocated string containing the file names
86 in directory DIRP, separated by '\0' characters;
87 the end is marked by two '\0' characters in a row.
88 Returned values are sorted according to OPTION.
89 Return NULL (setting errno) if DIRP cannot be read.
90 If DIRP is NULL, return NULL without affecting errno. */
93 streamsavedir (DIR *dirp, enum savedir_option option)
95 char *name_space = NULL;
97 direntry_t *entries = NULL;
98 size_t entries_allocated = 0;
99 size_t entries_used = 0;
102 comparison_function cmp = comparison_function_table[option];
109 struct dirent const *dp;
117 /* Skip "", ".", and "..". "" is returned by at least one buggy
118 implementation: Solaris 2.4 readdir on NFS file systems. */
120 if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
122 size_t entry_size = _D_EXACT_NAMLEN (dp) + 1;
125 if (entries_allocated == entries_used)
127 size_t n = entries_allocated;
128 entries = x2nrealloc (entries, &n, sizeof *entries);
129 entries_allocated = n;
131 entries[entries_used].name = xstrdup (entry);
133 entries[entries_used].ino = dp->d_ino;
139 if (allocated - used <= entry_size)
141 size_t n = used + entry_size;
144 name_space = x2nrealloc (name_space, &n, 1);
147 memcpy (name_space + used, entry, entry_size);
153 readdir_errno = errno;
154 if (readdir_errno != 0)
158 errno = readdir_errno;
166 qsort (entries, entries_used, sizeof *entries, cmp);
167 name_space = xmalloc (used + 1);
169 for (i = 0; i < entries_used; i++)
171 char *dest = name_space + used;
172 used += stpcpy (dest, entries[i].name) - dest + 1;
173 free (entries[i].name);
177 else if (used == allocated)
178 name_space = xrealloc (name_space, used + 1);
180 name_space[used] = '\0';
184 /* Return a freshly allocated string containing the file names
185 in directory DIR, separated by '\0' characters;
186 the end is marked by two '\0' characters in a row.
187 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
190 savedir (char const *dir, enum savedir_option option)
192 DIR *dirp = opendir (dir);
197 char *name_space = streamsavedir (dirp, option);
198 if (closedir (dirp) != 0)
200 int closedir_errno = errno;
202 errno = closedir_errno;