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