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