Remove K&R cruft.
[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 Free Software
4    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22 #if 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 #ifndef errno
32 extern int errno;
33 #endif
34
35 #if HAVE_DIRENT_H
36 # include <dirent.h>
37 #else
38 # define dirent direct
39 # if HAVE_SYS_NDIR_H
40 #  include <sys/ndir.h>
41 # endif
42 # if HAVE_SYS_DIR_H
43 #  include <sys/dir.h>
44 # endif
45 # if HAVE_NDIR_H
46 #  include <ndir.h>
47 # endif
48 #endif
49
50 #ifdef CLOSEDIR_VOID
51 /* Fake a return value. */
52 # define CLOSEDIR(d) (closedir (d), 0)
53 #else
54 # define CLOSEDIR(d) closedir (d)
55 #endif
56
57 #include <stddef.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61 #include "xalloc.h"
62
63 /* Return a freshly allocated string containing the filenames
64    in directory DIR, separated by '\0' characters;
65    the end is marked by two '\0' characters in a row.
66    Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
67
68 #ifndef NAME_SIZE_DEFAULT
69 # define NAME_SIZE_DEFAULT 512
70 #endif
71
72 char *
73 savedir (const char *dir)
74 {
75   DIR *dirp;
76   struct dirent *dp;
77   char *name_space;
78   size_t allocated = NAME_SIZE_DEFAULT;
79   size_t used = 0;
80   int save_errno;
81
82   dirp = opendir (dir);
83   if (dirp == NULL)
84     return NULL;
85
86   name_space = xmalloc (allocated);
87
88   errno = 0;
89   while ((dp = readdir (dirp)) != NULL)
90     {
91       /* Skip "", ".", and "..".  "" is returned by at least one buggy
92          implementation: Solaris 2.4 readdir on NFS filesystems.  */
93       char const *entry = dp->d_name;
94       if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
95         {
96           size_t entry_size = strlen (entry) + 1;
97           if (used + entry_size < used)
98             xalloc_die ();
99           if (allocated <= used + entry_size)
100             {
101               do
102                 {
103                   if (2 * allocated < allocated)
104                     xalloc_die ();
105                   allocated *= 2;
106                 }
107               while (allocated <= used + entry_size);
108
109               name_space = xrealloc (name_space, allocated);
110             }
111           memcpy (name_space + used, entry, entry_size);
112           used += entry_size;
113         }
114     }
115   name_space[used] = '\0';
116   save_errno = errno;
117   if (CLOSEDIR (dirp) != 0)
118     save_errno = errno;
119   if (save_errno != 0)
120     {
121       free (name_space);
122       errno = save_errno;
123       return NULL;
124     }
125   return name_space;
126 }