1 /* savedir.c -- save the list of files in a directory in a string
2 Copyright (C) 1990, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
27 #include <sys/types.h>
35 # define NAMLEN(dirent) strlen((dirent)->d_name)
37 # define dirent direct
38 # define NAMLEN(dirent) (dirent)->d_namlen
40 # include <sys/ndir.h>
51 /* Fake a return value. */
52 # define CLOSEDIR(d) (closedir (d), 0)
54 # define CLOSEDIR(d) closedir (d)
79 isdir1 (const char *dir, const char *file)
83 size_t dirlen = strlen (dir);
84 size_t filelen = strlen (file);
85 if ((dirlen + filelen + 2) > pathlen)
87 path = calloc (dirlen + 1 + filelen + 1, sizeof (*path));
88 pathlen = dirlen + filelen + 2;
91 slash = (path[dirlen] != '/');
93 strcpy (path + dirlen + slash , file);
94 status = isdir (path);
98 /* Return a freshly allocated string containing the filenames
99 in directory DIR, separated by '\0' characters;
100 the end is marked by two '\0' characters in a row.
101 NAME_SIZE is the number of bytes to initially allocate
102 for the string; it will be enlarged as needed.
103 Return NULL if DIR cannot be opened or if out of memory. */
105 savedir (const char *dir, off_t name_size, struct exclude *included_patterns,
106 struct exclude *excluded_patterns)
113 dirp = opendir (dir);
117 /* Be sure name_size is at least `1' so there's room for
118 the final NUL byte. */
122 name_space = (char *) malloc (name_size);
123 if (name_space == NULL)
130 while ((dp = readdir (dirp)) != NULL)
132 /* Skip "." and ".." (some NFS filesystems' directories lack them). */
133 if (dp->d_name[0] != '.'
134 || (dp->d_name[1] != '\0'
135 && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
137 off_t size_needed = (namep - name_space) + NAMLEN (dp) + 2;
139 if ((included_patterns || excluded_patterns)
140 && !isdir1 (dir, dp->d_name))
142 if (included_patterns
143 && !excluded_filename (included_patterns, path, 0))
145 if (excluded_patterns
146 && excluded_filename (excluded_patterns, path, 0))
150 if (size_needed > name_size)
152 char *new_name_space;
154 while (size_needed > name_size)
157 new_name_space = realloc (name_space, name_size);
158 if (new_name_space == NULL)
163 namep += new_name_space - name_space;
164 name_space = new_name_space;
166 namep = stpcpy (namep, dp->d_name) + 1;