GNU file utilities
[gnulib.git] / lib / savedir.c
1 /* savedir.c -- save the list of files in a directory in a string
2    Copyright (C) 1990 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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
19
20 #include <sys/types.h>
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #if defined(DIRENT) || defined(_POSIX_VERSION)
27 #include <dirent.h>
28 #define NLENGTH(direct) (strlen((direct)->d_name))
29 #else /* not (DIRENT or _POSIX_VERSION) */
30 #define dirent direct
31 #define NLENGTH(direct) ((direct)->d_namlen)
32 #ifdef SYSNDIR
33 #include <sys/ndir.h>
34 #endif /* SYSNDIR */
35 #ifdef SYSDIR
36 #include <sys/dir.h>
37 #endif /* SYSDIR */
38 #ifdef NDIR
39 #include <ndir.h>
40 #endif /* NDIR */
41 #endif /* DIRENT or _POSIX_VERSION */
42
43 #ifdef VOID_CLOSEDIR
44 /* Fake a return value. */
45 #define CLOSEDIR(d) (closedir (d), 0)
46 #else
47 #define CLOSEDIR(d) closedir (d)
48 #endif
49
50 #ifdef STDC_HEADERS
51 #include <stdlib.h>
52 #include <string.h>
53 #else
54 char *malloc ();
55 char *realloc ();
56 #ifndef NULL
57 #define NULL 0
58 #endif
59 #endif
60
61 char *stpcpy ();
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    NAME_SIZE is the number of bytes to initially allocate
67    for the string; it will be enlarged as needed.
68    Return NULL if DIR cannot be opened or if out of memory. */
69
70 char *
71 savedir (dir, name_size)
72      char *dir;
73      unsigned name_size;
74 {
75   DIR *dirp;
76   struct dirent *dp;
77   char *name_space;
78   char *namep;
79
80   dirp = opendir (dir);
81   if (dirp == NULL)
82     return NULL;
83
84   name_space = (char *) malloc (name_size);
85   if (name_space == NULL)
86     {
87       closedir (dirp);
88       return NULL;
89     }
90   namep = name_space;
91
92   while ((dp = readdir (dirp)) != NULL)
93     {
94       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
95       if (dp->d_name[0] != '.'
96           || (dp->d_name[1] != '\0'
97               && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
98         {
99           unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
100
101           if (size_needed > name_size)
102             {
103               char *new_name_space;
104
105               while (size_needed > name_size)
106                 name_size += 1024;
107
108               new_name_space = realloc (name_space, name_size);
109               if (new_name_space == NULL)
110                 {
111                   closedir (dirp);
112                   return NULL;
113                 }
114               namep += new_name_space - name_space;
115               name_space = new_name_space;
116             }
117           namep = stpcpy (namep, dp->d_name) + 1;
118         }
119     }
120   *namep = '\0';
121   if (CLOSEDIR (dirp))
122     {
123       free (name_space);
124       return NULL;
125     }
126   return name_space;
127 }