fdopendir: split into its own module
[gnulib.git] / lib / savedir.c
1 /* savedir.c -- save the list of files in a directory in a string
2
3    Copyright (C) 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
4    2006, 2009 Free 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 3 of the License, or
9    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20
21 #include <config.h>
22
23 #include "savedir.h"
24
25 #include <sys/types.h>
26
27 #include <errno.h>
28
29 #include <dirent.h>
30 #ifndef _D_EXACT_NAMLEN
31 # define _D_EXACT_NAMLEN(dp)    strlen ((dp)->d_name)
32 #endif
33
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "xalloc.h"
39
40 #ifndef NAME_SIZE_DEFAULT
41 # define NAME_SIZE_DEFAULT 512
42 #endif
43
44 /* The results of opendir() in this file are not used with dirfd and fchdir,
45    therefore save some unnecessary work in fchdir.c.  */
46 #undef opendir
47 #undef closedir
48
49 /* Return a freshly allocated string containing the file names
50    in directory DIRP, separated by '\0' characters;
51    the end is marked by two '\0' characters in a row.
52    Return NULL (setting errno) if DIRP cannot be read or closed.
53    If DIRP is NULL, return NULL without affecting errno.  */
54
55 static char *
56 savedirstream (DIR *dirp)
57 {
58   char *name_space;
59   size_t allocated = NAME_SIZE_DEFAULT;
60   size_t used = 0;
61   int save_errno;
62
63   if (dirp == NULL)
64     return NULL;
65
66   name_space = xmalloc (allocated);
67
68   for (;;)
69     {
70       struct dirent const *dp;
71       char const *entry;
72
73       errno = 0;
74       dp = readdir (dirp);
75       if (! dp)
76         break;
77
78       /* Skip "", ".", and "..".  "" is returned by at least one buggy
79          implementation: Solaris 2.4 readdir on NFS file systems.  */
80       entry = dp->d_name;
81       if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
82         {
83           size_t entry_size = _D_EXACT_NAMLEN (dp) + 1;
84           if (used + entry_size < used)
85             xalloc_die ();
86           if (allocated <= used + entry_size)
87             {
88               do
89                 {
90                   if (2 * allocated < allocated)
91                     xalloc_die ();
92                   allocated *= 2;
93                 }
94               while (allocated <= used + entry_size);
95
96               name_space = xrealloc (name_space, allocated);
97             }
98           memcpy (name_space + used, entry, entry_size);
99           used += entry_size;
100         }
101     }
102   name_space[used] = '\0';
103   save_errno = errno;
104   if (closedir (dirp) != 0)
105     save_errno = errno;
106   if (save_errno != 0)
107     {
108       free (name_space);
109       errno = save_errno;
110       return NULL;
111     }
112   return name_space;
113 }
114
115 /* Return a freshly allocated string containing the file names
116    in directory DIR, separated by '\0' characters;
117    the end is marked by two '\0' characters in a row.
118    Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
119
120 char *
121 savedir (char const *dir)
122 {
123   return savedirstream (opendir (dir));
124 }
125
126 /* Return a freshly allocated string containing the file names
127    in directory FD, separated by '\0' characters;
128    the end is marked by two '\0' characters in a row.
129    Return NULL (setting errno) if FD cannot be read or closed.  */
130
131 char *
132 fdsavedir (int fd)
133 {
134   return savedirstream (fdopendir (fd));
135 }