1*fb69a85aSchristos /* $NetBSD: savedir.c,v 1.2 2016/01/10 22:16:40 christos Exp $ */
2a8fa202aSchristos
3a8fa202aSchristos /* savedir.c -- save the list of files in a directory in a string
4a8fa202aSchristos Copyright (C) 1990, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5a8fa202aSchristos
6a8fa202aSchristos This program is free software; you can redistribute it and/or modify
7a8fa202aSchristos it under the terms of the GNU General Public License as published by
8a8fa202aSchristos the Free Software Foundation; either version 2, or (at your option)
9a8fa202aSchristos any later version.
10a8fa202aSchristos
11a8fa202aSchristos This program is distributed in the hope that it will be useful,
12a8fa202aSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13a8fa202aSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a8fa202aSchristos GNU General Public License for more details.
15a8fa202aSchristos
16a8fa202aSchristos You should have received a copy of the GNU General Public License
17a8fa202aSchristos along with this program; if not, write to the Free Software Foundation,
18a8fa202aSchristos Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19a8fa202aSchristos
20a8fa202aSchristos /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21a8fa202aSchristos
22a8fa202aSchristos #if HAVE_CONFIG_H
23a8fa202aSchristos # include <config.h>
24a8fa202aSchristos #endif
25a8fa202aSchristos
26a8fa202aSchristos #include <sys/types.h>
27a8fa202aSchristos
28a8fa202aSchristos #if HAVE_UNISTD_H
29a8fa202aSchristos # include <unistd.h>
30a8fa202aSchristos #endif
31a8fa202aSchristos
32a8fa202aSchristos #if HAVE_DIRENT_H
33a8fa202aSchristos # include <dirent.h>
34a8fa202aSchristos # define NAMLEN(dirent) strlen((dirent)->d_name)
35a8fa202aSchristos #else
36a8fa202aSchristos # define dirent direct
37a8fa202aSchristos # define NAMLEN(dirent) (dirent)->d_namlen
38a8fa202aSchristos # if HAVE_SYS_NDIR_H
39a8fa202aSchristos # include <sys/ndir.h>
40a8fa202aSchristos # endif
41a8fa202aSchristos # if HAVE_SYS_DIR_H
42a8fa202aSchristos # include <sys/dir.h>
43a8fa202aSchristos # endif
44a8fa202aSchristos # if HAVE_NDIR_H
45a8fa202aSchristos # include <ndir.h>
46a8fa202aSchristos # endif
47a8fa202aSchristos #endif
48a8fa202aSchristos
49a8fa202aSchristos #ifdef CLOSEDIR_VOID
50a8fa202aSchristos /* Fake a return value. */
51a8fa202aSchristos # define CLOSEDIR(d) (closedir (d), 0)
52a8fa202aSchristos #else
53a8fa202aSchristos # define CLOSEDIR(d) closedir (d)
54a8fa202aSchristos #endif
55a8fa202aSchristos
56a8fa202aSchristos #ifdef STDC_HEADERS
57a8fa202aSchristos # include <stdlib.h>
58a8fa202aSchristos # include <string.h>
59a8fa202aSchristos #else
60a8fa202aSchristos char *malloc ();
61a8fa202aSchristos char *realloc ();
62a8fa202aSchristos #endif
63a8fa202aSchristos #ifndef NULL
64a8fa202aSchristos # define NULL 0
65a8fa202aSchristos #endif
66a8fa202aSchristos
67a8fa202aSchristos #ifndef stpcpy
68a8fa202aSchristos char *stpcpy ();
69a8fa202aSchristos #endif
70a8fa202aSchristos
71a8fa202aSchristos #include <fnmatch.h>
72a8fa202aSchristos #include "savedir.h"
73*fb69a85aSchristos #include "system.h"
74a8fa202aSchristos
75a8fa202aSchristos char *path;
76a8fa202aSchristos size_t pathlen;
77a8fa202aSchristos
78a8fa202aSchristos static int
isdir1(const char * dir,const char * file)79a8fa202aSchristos isdir1 (const char *dir, const char *file)
80a8fa202aSchristos {
81a8fa202aSchristos int status;
82a8fa202aSchristos int slash;
83a8fa202aSchristos size_t dirlen = strlen (dir);
84a8fa202aSchristos size_t filelen = strlen (file);
85a8fa202aSchristos if ((dirlen + filelen + 2) > pathlen)
86a8fa202aSchristos {
87a8fa202aSchristos path = calloc (dirlen + 1 + filelen + 1, sizeof (*path));
88a8fa202aSchristos pathlen = dirlen + filelen + 2;
89a8fa202aSchristos }
90a8fa202aSchristos strcpy (path, dir);
91a8fa202aSchristos slash = (path[dirlen] != '/');
92a8fa202aSchristos path[dirlen] = '/';
93a8fa202aSchristos strcpy (path + dirlen + slash , file);
94a8fa202aSchristos status = isdir (path);
95a8fa202aSchristos return status;
96a8fa202aSchristos }
97a8fa202aSchristos
98a8fa202aSchristos /* Return a freshly allocated string containing the filenames
99a8fa202aSchristos in directory DIR, separated by '\0' characters;
100a8fa202aSchristos the end is marked by two '\0' characters in a row.
101a8fa202aSchristos NAME_SIZE is the number of bytes to initially allocate
102a8fa202aSchristos for the string; it will be enlarged as needed.
103a8fa202aSchristos Return NULL if DIR cannot be opened or if out of memory. */
104a8fa202aSchristos char *
savedir(const char * dir,off_t name_size,struct exclude * included_patterns,struct exclude * excluded_patterns)105a8fa202aSchristos savedir (const char *dir, off_t name_size, struct exclude *included_patterns,
106a8fa202aSchristos struct exclude *excluded_patterns)
107a8fa202aSchristos {
108a8fa202aSchristos DIR *dirp;
109a8fa202aSchristos struct dirent *dp;
110a8fa202aSchristos char *name_space;
111a8fa202aSchristos char *namep;
112a8fa202aSchristos
113a8fa202aSchristos dirp = opendir (dir);
114a8fa202aSchristos if (dirp == NULL)
115a8fa202aSchristos return NULL;
116a8fa202aSchristos
117a8fa202aSchristos /* Be sure name_size is at least `1' so there's room for
118a8fa202aSchristos the final NUL byte. */
119a8fa202aSchristos if (name_size <= 0)
120a8fa202aSchristos name_size = 1;
121a8fa202aSchristos
122a8fa202aSchristos name_space = (char *) malloc (name_size);
123a8fa202aSchristos if (name_space == NULL)
124a8fa202aSchristos {
125a8fa202aSchristos closedir (dirp);
126a8fa202aSchristos return NULL;
127a8fa202aSchristos }
128a8fa202aSchristos namep = name_space;
129a8fa202aSchristos
130a8fa202aSchristos while ((dp = readdir (dirp)) != NULL)
131a8fa202aSchristos {
132a8fa202aSchristos /* Skip "." and ".." (some NFS filesystems' directories lack them). */
133a8fa202aSchristos if (dp->d_name[0] != '.'
134a8fa202aSchristos || (dp->d_name[1] != '\0'
135a8fa202aSchristos && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
136a8fa202aSchristos {
137a8fa202aSchristos off_t size_needed = (namep - name_space) + NAMLEN (dp) + 2;
138a8fa202aSchristos
139a8fa202aSchristos if ((included_patterns || excluded_patterns)
140a8fa202aSchristos && !isdir1 (dir, dp->d_name))
141a8fa202aSchristos {
142a8fa202aSchristos if (included_patterns
143a8fa202aSchristos && !excluded_filename (included_patterns, dp->d_name, 0))
144a8fa202aSchristos continue;
145a8fa202aSchristos if (excluded_patterns
146a8fa202aSchristos && excluded_filename (excluded_patterns, dp->d_name, 0))
147a8fa202aSchristos continue;
148a8fa202aSchristos }
149a8fa202aSchristos
150a8fa202aSchristos if (size_needed > name_size)
151a8fa202aSchristos {
152a8fa202aSchristos char *new_name_space;
153a8fa202aSchristos
154a8fa202aSchristos while (size_needed > name_size)
155a8fa202aSchristos name_size += 1024;
156a8fa202aSchristos
157a8fa202aSchristos new_name_space = realloc (name_space, name_size);
158a8fa202aSchristos if (new_name_space == NULL)
159a8fa202aSchristos {
160a8fa202aSchristos closedir (dirp);
161a8fa202aSchristos return NULL;
162a8fa202aSchristos }
163a8fa202aSchristos namep += new_name_space - name_space;
164a8fa202aSchristos name_space = new_name_space;
165a8fa202aSchristos }
166a8fa202aSchristos namep = stpcpy (namep, dp->d_name) + 1;
167a8fa202aSchristos }
168a8fa202aSchristos }
169a8fa202aSchristos *namep = '\0';
170a8fa202aSchristos if (CLOSEDIR (dirp))
171a8fa202aSchristos {
172a8fa202aSchristos free (name_space);
173a8fa202aSchristos return NULL;
174a8fa202aSchristos }
175a8fa202aSchristos if (path)
176a8fa202aSchristos {
177a8fa202aSchristos free (path);
178a8fa202aSchristos path = NULL;
179a8fa202aSchristos pathlen = 0;
180a8fa202aSchristos }
181a8fa202aSchristos return name_space;
182a8fa202aSchristos }
183