1 /* savedir.c -- save the list of files in a directory in a string
2 Copyright (C) 1990, 1997-2001, 2009-2011 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 3, 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
22 #include <sys/types.h>
27 /* Fake a return value. */
28 # define CLOSEDIR(d) (closedir (d), 0)
30 # define CLOSEDIR(d) closedir (d)
40 static size_t pathlen;
42 extern int isdir (const char *name);
45 isdir1 (const char *dir, const char *file)
47 size_t dirlen = strlen (dir);
48 size_t filelen = strlen (file);
50 while (dirlen && dir[dirlen - 1] == '/')
53 if ((dirlen + filelen + 2) > pathlen)
56 if ((dirlen + filelen + 2) > pathlen)
57 pathlen = dirlen + filelen + 2;
59 path = xrealloc (path, pathlen);
62 memcpy (path, dir, dirlen);
64 strcpy (path + dirlen + 1, file);
68 /* Return a freshly allocated string containing the filenames
69 in directory DIR, separated by '\0' characters;
70 the end is marked by two '\0' characters in a row.
71 NAME_SIZE is the number of bytes to initially allocate
72 for the string; it will be enlarged as needed.
73 Return NULL if DIR cannot be opened or if out of memory. */
75 savedir (const char *dir, off_t name_size, struct exclude *included_patterns,
76 struct exclude *excluded_patterns, struct exclude *excluded_directory_patterns )
87 /* Be sure name_size is at least `1' so there's room for
88 the final NUL byte. */
92 name_space = (char *) malloc (name_size);
93 if (name_space == NULL)
100 while ((dp = readdir (dirp)) != NULL)
102 /* Skip "." and ".." (some NFS file systems' directories lack them). */
103 if (dp->d_name[0] != '.'
104 || (dp->d_name[1] != '\0'
105 && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
107 size_t namlen = strlen (dp->d_name);
108 size_t size_needed = (namep - name_space) + namlen + 2;
110 if ((included_patterns || excluded_patterns)
111 && !isdir1 (dir, dp->d_name))
113 if (included_patterns
114 && excluded_file_name (included_patterns, dp->d_name))
116 if (excluded_patterns
117 && excluded_file_name (excluded_patterns, dp->d_name))
121 if ( excluded_directory_patterns
122 && isdir1 (dir, dp->d_name) )
124 if (excluded_directory_patterns
125 && excluded_file_name (excluded_directory_patterns, dp->d_name))
129 if (size_needed > name_size)
131 char *new_name_space;
133 while (size_needed > name_size)
136 new_name_space = realloc (name_space, name_size);
137 if (new_name_space == NULL)
142 namep = new_name_space + (namep - name_space);
143 name_space = new_name_space;
145 strcpy (namep, dp->d_name);