* lib/savedir.c (CLOSEDIR): Remove. All uses changed to closedir.
[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, 2006 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 #include <stddef.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #include "openat.h"
52 #include "xalloc.h"
53
54 #ifndef NAME_SIZE_DEFAULT
55 # define NAME_SIZE_DEFAULT 512
56 #endif
57
58 /* Return a freshly allocated string containing the file names
59    in directory DIRP, separated by '\0' characters;
60    the end is marked by two '\0' characters in a row.
61    Return NULL (setting errno) if DIRP cannot be read or closed.
62    If DIRP is NULL, return NULL without affecting errno.  */
63
64 static char *
65 savedirstream (DIR *dirp)
66 {
67   char *name_space;
68   size_t allocated = NAME_SIZE_DEFAULT;
69   size_t used = 0;
70   int save_errno;
71
72   if (dirp == NULL)
73     return NULL;
74
75   name_space = xmalloc (allocated);
76
77   for (;;)
78     {
79       struct dirent const *dp;
80       char const *entry;
81
82       errno = 0;
83       dp = readdir (dirp);
84       if (! dp)
85         break;
86
87       /* Skip "", ".", and "..".  "" is returned by at least one buggy
88          implementation: Solaris 2.4 readdir on NFS file systems.  */
89       entry = dp->d_name;
90       if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
91         {
92           size_t entry_size = strlen (entry) + 1;
93           if (used + entry_size < used)
94             xalloc_die ();
95           if (allocated <= used + entry_size)
96             {
97               do
98                 {
99                   if (2 * allocated < allocated)
100                     xalloc_die ();
101                   allocated *= 2;
102                 }
103               while (allocated <= used + entry_size);
104
105               name_space = xrealloc (name_space, allocated);
106             }
107           memcpy (name_space + used, entry, entry_size);
108           used += entry_size;
109         }
110     }
111   name_space[used] = '\0';
112   save_errno = errno;
113   if (closedir (dirp) != 0)
114     save_errno = errno;
115   if (save_errno != 0)
116     {
117       free (name_space);
118       errno = save_errno;
119       return NULL;
120     }
121   return name_space;
122 }
123
124 /* Return a freshly allocated string containing the file names
125    in directory DIR, separated by '\0' characters;
126    the end is marked by two '\0' characters in a row.
127    Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
128
129 char *
130 savedir (char const *dir)
131 {
132   return savedirstream (opendir (dir));
133 }
134
135 /* Return a freshly allocated string containing the file names
136    in directory FD, separated by '\0' characters;
137    the end is marked by two '\0' characters in a row.
138    Return NULL (setting errno) if FD cannot be read or closed.  */
139
140 char *
141 fdsavedir (int fd)
142 {
143   return savedirstream (fdopendir (fd));
144 }