savedir now sets errno on failure and invokes xmalloc to get memory.
[gnulib.git] / lib / savedir.c
1 /* savedir.c -- save the list of files in a directory in a string
2    Copyright (C) 1990, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <sys/types.h>
25
26 #include <errno.h>
27 #ifndef errno
28 extern int errno;
29 #endif
30
31 #if HAVE_DIRENT_H
32 # include <dirent.h>
33 #else
34 # define dirent direct
35 # if HAVE_SYS_NDIR_H
36 #  include <sys/ndir.h>
37 # endif
38 # if HAVE_SYS_DIR_H
39 #  include <sys/dir.h>
40 # endif
41 # if HAVE_NDIR_H
42 #  include <ndir.h>
43 # endif
44 #endif
45
46 #ifdef CLOSEDIR_VOID
47 /* Fake a return value. */
48 # define CLOSEDIR(d) (closedir (d), 0)
49 #else
50 # define CLOSEDIR(d) closedir (d)
51 #endif
52
53 #ifdef STDC_HEADERS
54 # include <stdlib.h>
55 # include <string.h>
56 #endif
57 #ifndef NULL
58 # define NULL 0
59 #endif
60
61 #include "savedir.h"
62 #include "xalloc.h"
63
64 /* Return a freshly allocated string containing the filenames
65    in directory DIR, separated by '\0' characters;
66    the end is marked by two '\0' characters in a row.
67    NAME_SIZE is the number of bytes to initially allocate
68    for the string; it will be enlarged as needed.
69    Use NAME_SIZE == -1 if you do not know the size.
70    Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
71
72 #ifndef NAME_SIZE_DEFAULT
73 # define NAME_SIZE_DEFAULT 512
74 #endif
75
76 char *
77 savedir (const char *dir, off_t name_size)
78 {
79   DIR *dirp;
80   struct dirent *dp;
81   char *name_space;
82   size_t allocated = name_size; /* Overflow is checked indirectly below.  */
83   size_t used = 0;
84   int save_errno;
85
86   dirp = opendir (dir);
87   if (dirp == NULL)
88     return NULL;
89
90   /* Use the default if the size is not known.  Be sure "allocated"
91      is at least `1' so there's room for the final NUL byte.
92      Do not simply test name_size <= 0, because the initialization
93      of "allocated" might have overflowed.  */
94   if (name_size < 0 || allocated == 0)
95     allocated = NAME_SIZE_DEFAULT;
96
97   name_space = xmalloc (allocated);
98
99   errno = 0;
100   while ((dp = readdir (dirp)) != NULL)
101     {
102       /* Skip "", ".", and "..".  "" is returned by at least one buggy
103          implementation: Solaris 2.4 readdir on NFS filesystems.  */
104       char const *entry = dp->d_name;
105       if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
106         {
107           size_t entry_size = strlen (entry) + 1;
108           if (used + entry_size < used)
109             xalloc_die ();
110           if (allocated <= used + entry_size)
111             {
112               do
113                 {
114                   if (2 * allocated < allocated)
115                     xalloc_die ();
116                   allocated *= 2;
117                 }
118               while (allocated <= used + entry_size);
119
120               name_space = xrealloc (name_space, allocated);
121             }
122           memcpy (name_space + used, entry, entry_size);
123           used += entry_size;
124         }
125     }
126   name_space[used] = '\0';
127   save_errno = errno;
128   if (CLOSEDIR (dirp) != 0)
129     save_errno = errno;
130   if (save_errno != 0)
131     {
132       free (name_space);
133       errno = save_errno;
134       return NULL;
135     }
136   return name_space;
137 }