(exclude_fnmatch): New function.
[gnulib.git] / lib / savedir.c
1 /* savedir.c -- save the list of files in a directory in a string
2
3    Copyright 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
4    Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "savedir.h"
27
28 #include <sys/types.h>
29
30 #include <errno.h>
31
32 #if HAVE_DIRENT_H
33 # include <dirent.h>
34 #else
35 # define dirent direct
36 # if HAVE_SYS_NDIR_H
37 #  include <sys/ndir.h>
38 # endif
39 # if HAVE_SYS_DIR_H
40 #  include <sys/dir.h>
41 # endif
42 # if HAVE_NDIR_H
43 #  include <ndir.h>
44 # endif
45 #endif
46
47 #ifdef CLOSEDIR_VOID
48 /* Fake a return value. */
49 # define CLOSEDIR(d) (closedir (d), 0)
50 #else
51 # define CLOSEDIR(d) closedir (d)
52 #endif
53
54 #include <stddef.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include "openat.h"
59 #include "xalloc.h"
60
61 #ifndef NAME_SIZE_DEFAULT
62 # define NAME_SIZE_DEFAULT 512
63 #endif
64
65 /* Return a freshly allocated string containing the file names
66    in directory DIRP, separated by '\0' characters;
67    the end is marked by two '\0' characters in a row.
68    Return NULL (setting errno) if DIRP cannot be read or closed.
69    If DIRP is NULL, return NULL without affecting errno.  */
70
71 static char *
72 savedirstream (DIR *dirp)
73 {
74   char *name_space;
75   size_t allocated = NAME_SIZE_DEFAULT;
76   size_t used = 0;
77   int save_errno;
78
79   if (dirp == NULL)
80     return NULL;
81
82   name_space = xmalloc (allocated);
83
84   for (;;)
85     {
86       struct dirent const *dp;
87       char const *entry;
88
89       errno = 0;
90       dp = readdir (dirp);
91       if (! dp)
92         break;
93
94       /* Skip "", ".", and "..".  "" is returned by at least one buggy
95          implementation: Solaris 2.4 readdir on NFS file systems.  */
96       entry = dp->d_name;
97       if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
98         {
99           size_t entry_size = strlen (entry) + 1;
100           if (used + entry_size < used)
101             xalloc_die ();
102           if (allocated <= used + entry_size)
103             {
104               do
105                 {
106                   if (2 * allocated < allocated)
107                     xalloc_die ();
108                   allocated *= 2;
109                 }
110               while (allocated <= used + entry_size);
111
112               name_space = xrealloc (name_space, allocated);
113             }
114           memcpy (name_space + used, entry, entry_size);
115           used += entry_size;
116         }
117     }
118   name_space[used] = '\0';
119   save_errno = errno;
120   if (CLOSEDIR (dirp) != 0)
121     save_errno = errno;
122   if (save_errno != 0)
123     {
124       free (name_space);
125       errno = save_errno;
126       return NULL;
127     }
128   return name_space;
129 }
130
131 /* Return a freshly allocated string containing the file names
132    in directory DIR, separated by '\0' characters;
133    the end is marked by two '\0' characters in a row.
134    Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
135
136 char *
137 savedir (char const *dir)
138 {
139   return savedirstream (opendir (dir));
140 }
141
142 /* Return a freshly allocated string containing the file names
143    in directory FD, separated by '\0' characters;
144    the end is marked by two '\0' characters in a row.
145    Return NULL (setting errno) if FD cannot be read or closed.  */
146
147 char *
148 fdsavedir (int fd)
149 {
150   return savedirstream (fdopendir (fd));
151 }