1 /* savedir.c -- save the list of files in a directory in a string
2 Copyright (C) 1990, 1997-2001, 2009-2010 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>
28 /* Fake a return value. */
29 # define CLOSEDIR(d) (closedir (d), 0)
31 # define CLOSEDIR(d) closedir (d)
41 static size_t pathlen;
43 extern int isdir (const char *name);
46 isdir1 (const char *dir, const char *file)
48 size_t dirlen = strlen (dir);
49 size_t filelen = strlen (file);
51 while (dirlen && dir[dirlen - 1] == '/')
54 if ((dirlen + filelen + 2) > pathlen)
57 if ((dirlen + filelen + 2) > pathlen)
58 pathlen = dirlen + filelen + 2;
60 path = xrealloc (path, pathlen);
63 memcpy (path, dir, dirlen);
65 strcpy (path + dirlen + 1, file);
69 /* Return a freshly allocated string containing the filenames
70 in directory DIR, separated by '\0' characters;
71 the end is marked by two '\0' characters in a row.
72 NAME_SIZE is the number of bytes to initially allocate
73 for the string; it will be enlarged as needed.
74 Return NULL if DIR cannot be opened or if out of memory. */
76 savedir (const char *dir, off_t name_size, struct exclude *included_patterns,
77 struct exclude *excluded_patterns, struct exclude *excluded_directory_patterns )
88 /* Be sure name_size is at least `1' so there's room for
89 the final NUL byte. */
93 name_space = (char *) malloc (name_size);
94 if (name_space == NULL)
101 while ((dp = readdir (dirp)) != NULL)
103 /* Skip "." and ".." (some NFS file systems' directories lack them). */
104 if (dp->d_name[0] != '.'
105 || (dp->d_name[1] != '\0'
106 && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
108 size_t namlen = strlen (dp->d_name);
109 size_t size_needed = (namep - name_space) + namlen + 2;
111 if ((included_patterns || excluded_patterns)
112 && !isdir1 (dir, dp->d_name))
114 if (included_patterns
115 && excluded_file_name (included_patterns, dp->d_name))
117 if (excluded_patterns
118 && excluded_file_name (excluded_patterns, dp->d_name))
122 if ( excluded_directory_patterns
123 && isdir1 (dir, dp->d_name) )
125 if (excluded_directory_patterns
126 && excluded_file_name (excluded_directory_patterns, dp->d_name))
130 if (size_needed > name_size)
132 char *new_name_space;
134 while (size_needed > name_size)
137 new_name_space = realloc (name_space, name_size);
138 if (new_name_space == NULL)
143 namep = new_name_space + (namep - name_space);
144 name_space = new_name_space;
146 strcpy (namep, dp->d_name);