1 /* savedir.c -- save the list of files in a directory in a string
3 Copyright (C) 1990, 1997-2001, 2003-2006, 2009-2020 Free Software
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 3 of the License, or
9 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
25 #include <sys/types.h>
30 #ifndef _D_EXACT_NAMLEN
31 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
48 /* Compare the names of two directory entries */
51 direntry_cmp_name (void const *a
, void const *b
)
53 direntry_t
const *dea
= a
;
54 direntry_t
const *deb
= b
;
56 return strcmp (dea
->name
, deb
->name
);
60 /* Compare the inode numbers of two directory entries */
63 direntry_cmp_inode (void const *a
, void const *b
)
65 direntry_t
const *dea
= a
;
66 direntry_t
const *deb
= b
;
68 return _GL_CMP (dea
->ino
, deb
->ino
);
72 typedef int (*comparison_function
) (void const *, void const *);
74 static comparison_function
const comparison_function_table
[] =
83 /* Return a freshly allocated string containing the file names
84 in directory DIRP, separated by '\0' characters;
85 the end is marked by two '\0' characters in a row.
86 Returned values are sorted according to OPTION.
87 Return NULL (setting errno) if DIRP cannot be read.
88 If DIRP is NULL, return NULL without affecting errno. */
91 streamsavedir (DIR *dirp
, enum savedir_option option
)
93 char *name_space
= NULL
;
95 direntry_t
*entries
= NULL
;
96 size_t entries_allocated
= 0;
97 size_t entries_used
= 0;
100 comparison_function cmp
= comparison_function_table
[option
];
107 struct dirent
const *dp
;
115 /* Skip "", ".", and "..". "" is returned by at least one buggy
116 implementation: Solaris 2.4 readdir on NFS file systems. */
118 if (entry
[entry
[0] != '.' ? 0 : entry
[1] != '.' ? 1 : 2] != '\0')
120 size_t entry_size
= _D_EXACT_NAMLEN (dp
) + 1;
123 if (entries_allocated
== entries_used
)
125 size_t n
= entries_allocated
;
126 entries
= x2nrealloc (entries
, &n
, sizeof *entries
);
127 entries_allocated
= n
;
129 entries
[entries_used
].name
= xstrdup (entry
);
131 entries
[entries_used
].ino
= dp
->d_ino
;
137 if (allocated
- used
<= entry_size
)
139 size_t n
= used
+ entry_size
;
142 name_space
= x2nrealloc (name_space
, &n
, 1);
145 memcpy (name_space
+ used
, entry
, entry_size
);
151 readdir_errno
= errno
;
152 if (readdir_errno
!= 0)
156 errno
= readdir_errno
;
165 qsort (entries
, entries_used
, sizeof *entries
, cmp
);
166 name_space
= xmalloc (used
+ 1);
168 for (i
= 0; i
< entries_used
; i
++)
170 char *dest
= name_space
+ used
;
171 used
+= stpcpy (dest
, entries
[i
].name
) - dest
+ 1;
172 free (entries
[i
].name
);
176 else if (used
== allocated
)
177 name_space
= xrealloc (name_space
, used
+ 1);
179 name_space
[used
] = '\0';
183 /* Return a freshly allocated string containing the file names
184 in directory DIR, separated by '\0' characters;
185 the end is marked by two '\0' characters in a row.
186 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
189 savedir (char const *dir
, enum savedir_option option
)
191 DIR *dirp
= opendir (dir
);
196 char *name_space
= streamsavedir (dirp
, option
);
197 if (closedir (dirp
) != 0)
199 int closedir_errno
= errno
;
201 errno
= closedir_errno
;