1 /* Directory hashing for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
5 This file is part of GNU Make.
6 
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
11 
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along with
17 this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 #include "make.h"
20 #include "hash.h"
21 
22 #ifdef	HAVE_DIRENT_H
23 # include <dirent.h>
24 # define NAMLEN(dirent) strlen((dirent)->d_name)
25 # ifdef VMS
26 /* its prototype is in vmsdir.h, which is not needed for HAVE_DIRENT_H */
27 const char *vmsify (const char *name, int type);
28 # endif
29 #else
30 # define dirent direct
31 # define NAMLEN(dirent) (dirent)->d_namlen
32 # ifdef HAVE_SYS_NDIR_H
33 #  include <sys/ndir.h>
34 # endif
35 # ifdef HAVE_SYS_DIR_H
36 #  include <sys/dir.h>
37 # endif
38 # ifdef HAVE_NDIR_H
39 #  include <ndir.h>
40 # endif
41 # ifdef HAVE_VMSDIR_H
42 #  include "vmsdir.h"
43 # endif /* HAVE_VMSDIR_H */
44 #endif
45 /* bird: FreeBSD + smbfs -> readdir() + EBADF */
46 #ifdef __FreeBSD__
47 # include <sys/mount.h>
48 #endif
49 /* bird: end */
50 
51 #ifdef CONFIG_WITH_STRCACHE2
52 # include <stddef.h>
53 #endif
54 
55 /* In GNU systems, <dirent.h> defines this macro for us.  */
56 #ifdef _D_NAMLEN
57 # undef NAMLEN
58 # define NAMLEN(d) _D_NAMLEN(d)
59 #endif
60 
61 #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
62 /* Posix does not require that the d_ino field be present, and some
63    systems do not provide it. */
64 # define REAL_DIR_ENTRY(dp) 1
65 # define FAKE_DIR_ENTRY(dp)
66 #else
67 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
68 # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
69 #endif /* POSIX */
70 
71 #ifdef __MSDOS__
72 #include <ctype.h>
73 #include <fcntl.h>
74 
75 /* If it's MSDOS that doesn't have _USE_LFN, disable LFN support.  */
76 #ifndef _USE_LFN
77 #define _USE_LFN 0
78 #endif
79 
80 static const char *
dosify(const char * filename)81 dosify (const char *filename)
82 {
83   static char dos_filename[14];
84   char *df;
85   int i;
86 
87   if (filename == 0 || _USE_LFN)
88     return filename;
89 
90   /* FIXME: what about filenames which violate
91      8+3 constraints, like "config.h.in", or ".emacs"?  */
92   if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
93     return filename;
94 
95   df = dos_filename;
96 
97   /* First, transform the name part.  */
98   for (i = 0; *filename != '\0' && i < 8 && *filename != '.'; ++i)
99     *df++ = tolower ((unsigned char)*filename++);
100 
101   /* Now skip to the next dot.  */
102   while (*filename != '\0' && *filename != '.')
103     ++filename;
104   if (*filename != '\0')
105     {
106       *df++ = *filename++;
107       for (i = 0; *filename != '\0' && i < 3 && *filename != '.'; ++i)
108 	*df++ = tolower ((unsigned char)*filename++);
109     }
110 
111   /* Look for more dots.  */
112   while (*filename != '\0' && *filename != '.')
113     ++filename;
114   if (*filename == '.')
115     return filename;
116   *df = 0;
117   return dos_filename;
118 }
119 #endif /* __MSDOS__ */
120 
121 #ifdef WINDOWS32
122 #include <Windows.h>
123 #include "pathstuff.h"
124 #endif
125 
126 #ifdef _AMIGA
127 #include <ctype.h>
128 #endif
129 
130 #ifdef HAVE_CASE_INSENSITIVE_FS
131 static const char *
downcase(const char * filename)132 downcase (const char *filename)
133 {
134   static PATH_VAR (new_filename);
135   char *df;
136 
137   if (filename == 0)
138     return 0;
139 
140   df = new_filename;
141   while (*filename != '\0')
142     {
143       *df++ = tolower ((unsigned char)*filename);
144       ++filename;
145     }
146 
147   *df = 0;
148 
149   return new_filename;
150 }
151 #endif /* HAVE_CASE_INSENSITIVE_FS */
152 
153 #ifdef VMS
154 
155 static int
vms_hash(const char * name)156 vms_hash (const char *name)
157 {
158   int h = 0;
159   int g;
160 
161   while (*name)
162     {
163       unsigned char uc = *name;
164 #ifdef HAVE_CASE_INSENSITIVE_FS
165       h = (h << 4) + (isupper (uc) ? tolower (uc) : uc);
166 #else
167       h = (h << 4) + uc;
168 #endif
169       name++;
170       g = h & 0xf0000000;
171       if (g)
172 	{
173 	  h = h ^ (g >> 24);
174 	  h = h ^ g;
175 	}
176     }
177   return h;
178 }
179 
180 /* fake stat entry for a directory */
181 static int
vmsstat_dir(const char * name,struct stat * st)182 vmsstat_dir (const char *name, struct stat *st)
183 {
184   char *s;
185   int h;
186   DIR *dir;
187 
188   dir = opendir (name);
189   if (dir == 0)
190     return -1;
191   closedir (dir);
192   s = strchr (name, ':');	/* find device */
193   if (s)
194     {
195       /* to keep the compiler happy we said "const char *name", now we cheat */
196       *s++ = 0;
197       st->st_dev = (char *)vms_hash (name);
198       h = vms_hash (s);
199       *(s-1) = ':';
200     }
201   else
202     {
203       st->st_dev = 0;
204       h = vms_hash (name);
205     }
206 
207   st->st_ino[0] = h & 0xff;
208   st->st_ino[1] = h & 0xff00;
209   st->st_ino[2] = h >> 16;
210 
211   return 0;
212 }
213 #endif /* VMS */
214 
215 /* Hash table of directories.  */
216 
217 #ifndef	DIRECTORY_BUCKETS
218 #ifdef KMK
219 #  define DIRECTORY_BUCKETS 4096
220 # else
221 #  define DIRECTORY_BUCKETS 199
222 # endif
223 #endif
224 
225 struct directory_contents
226   {
227     dev_t dev;			/* Device and inode numbers of this dir.  */
228 #ifdef WINDOWS32
229     /* Inode means nothing on WINDOWS32. Even file key information is
230      * unreliable because it is random per file open and undefined for remote
231      * filesystems. The most unique attribute I can come up with is the fully
232      * qualified name of the directory. Beware though, this is also
233      * unreliable. I'm open to suggestion on a better way to emulate inode.  */
234 # ifndef CONFIG_WITH_STRCACHE2
235     char *path_key;
236 # else
237     char const *path_key; /* strcache'ed */
238 # endif
239     int   ctime;
240     int   mtime;        /* controls check for stale directory cache */
241     int   fs_flags;     /* FS_FAT, FS_NTFS, ... */
242 # define FS_FAT      0x1
243 # define FS_NTFS     0x2
244 # define FS_UNKNOWN  0x4
245 # ifdef KMK
246     time_t last_updated; /**< The last time the directory was re-read. */
247 # endif
248 #else
249 # ifdef VMS
250     ino_t ino[3];
251 # else
252     ino_t ino;
253 # endif
254 #endif /* WINDOWS32 */
255     struct hash_table dirfiles;	/* Files in this directory.  */
256     DIR *dirstream;		/* Stream reading this directory.  */
257   };
258 
259 static unsigned long
directory_contents_hash_1(const void * key_0)260 directory_contents_hash_1 (const void *key_0)
261 {
262   const struct directory_contents *key = key_0;
263   unsigned long hash;
264 
265 #ifdef WINDOWS32
266 # ifndef CONFIG_WITH_STRCACHE2
267   hash = 0;
268   ISTRING_HASH_1 (key->path_key, hash);
269 # else  /* CONFIG_WITH_STRCACHE2 */
270   hash = strcache2_calc_ptr_hash (&file_strcache, key->path_key);
271 # endif /* CONFIG_WITH_STRCACHE2 */
272   hash ^= ((unsigned int) key->dev << 4) ^ (unsigned int) key->ctime;
273 #else
274 # ifdef VMS
275   hash = (((unsigned int) key->dev << 4)
276 	  ^ ((unsigned int) key->ino[0]
277 	     + (unsigned int) key->ino[1]
278 	     + (unsigned int) key->ino[2]));
279 # else
280   hash = ((unsigned int) key->dev << 4) ^ (unsigned int) key->ino;
281 # endif
282 #endif /* WINDOWS32 */
283   return hash;
284 }
285 
286 static unsigned long
directory_contents_hash_2(const void * key_0)287 directory_contents_hash_2 (const void *key_0)
288 {
289   const struct directory_contents *key = key_0;
290   unsigned long hash;
291 
292 #ifdef WINDOWS32
293 # ifndef CONFIG_WITH_STRCACHE2
294   hash = 0;
295   ISTRING_HASH_2 (key->path_key, hash);
296 # else  /* CONFIG_WITH_STRCACHE2 */
297   hash = strcache2_get_hash (&file_strcache, key->path_key);
298 # endif /* CONFIG_WITH_STRCACHE2 */
299   hash ^= ((unsigned int) key->dev << 4) ^ (unsigned int) ~key->ctime;
300 #else
301 # ifdef VMS
302   hash = (((unsigned int) key->dev << 4)
303 	  ^ ~((unsigned int) key->ino[0]
304 	      + (unsigned int) key->ino[1]
305 	      + (unsigned int) key->ino[2]));
306 # else
307   hash = ((unsigned int) key->dev << 4) ^ (unsigned int) ~key->ino;
308 # endif
309 #endif /* WINDOWS32 */
310 
311   return hash;
312 }
313 
314 /* Sometimes it's OK to use subtraction to get this value:
315      result = X - Y;
316    But, if we're not sure of the type of X and Y they may be too large for an
317    int (on a 64-bit system for example).  So, use ?: instead.
318    See Savannah bug #15534.
319 
320    NOTE!  This macro has side-effects!
321 */
322 
323 #define MAKECMP(_x,_y)  ((_x)<(_y)?-1:((_x)==(_y)?0:1))
324 
325 static int
directory_contents_hash_cmp(const void * xv,const void * yv)326 directory_contents_hash_cmp (const void *xv, const void *yv)
327 {
328   const struct directory_contents *x = xv;
329   const struct directory_contents *y = yv;
330   int result;
331 
332 #ifdef WINDOWS32
333 # ifndef CONFIG_WITH_STRCACHE2
334   ISTRING_COMPARE (x->path_key, y->path_key, result);
335   if (result)
336     return result;
337 # else  /* CONFIG_WITH_STRCACHE2 */
338   if (x->path_key != y->path_key)
339     return -1;
340 # endif /* CONFIG_WITH_STRCACHE2 */
341   result = MAKECMP(x->ctime, y->ctime);
342   if (result)
343     return result;
344 #else
345 # ifdef VMS
346   result = MAKECMP(x->ino[0], y->ino[0]);
347   if (result)
348     return result;
349   result = MAKECMP(x->ino[1], y->ino[1]);
350   if (result)
351     return result;
352   result = MAKECMP(x->ino[2], y->ino[2]);
353   if (result)
354     return result;
355 # else
356   result = MAKECMP(x->ino, y->ino);
357   if (result)
358     return result;
359 # endif
360 #endif /* WINDOWS32 */
361 
362   return MAKECMP(x->dev, y->dev);
363 }
364 
365 /* Table of directory contents hashed by device and inode number.  */
366 static struct hash_table directory_contents;
367 
368 #ifdef CONFIG_WITH_ALLOC_CACHES
369 /* Allocation cache for directory contents. */
370 struct alloccache directory_contents_cache;
371 #endif
372 
373 struct directory
374   {
375     const char *name;			/* Name of the directory.  */
376 
377     /* The directory's contents.  This data may be shared by several
378        entries in the hash table, which refer to the same directory
379        (identified uniquely by `dev' and `ino') under different names.  */
380     struct directory_contents *contents;
381   };
382 
383 #ifndef CONFIG_WITH_STRCACHE2
384 static unsigned long
directory_hash_1(const void * key)385 directory_hash_1 (const void *key)
386 {
387   return_ISTRING_HASH_1 (((const struct directory *) key)->name);
388 }
389 
390 static unsigned long
directory_hash_2(const void * key)391 directory_hash_2 (const void *key)
392 {
393   return_ISTRING_HASH_2 (((const struct directory *) key)->name);
394 }
395 
396 static int
directory_hash_cmp(const void * x,const void * y)397 directory_hash_cmp (const void *x, const void *y)
398 {
399   return_ISTRING_COMPARE (((const struct directory *) x)->name,
400 			  ((const struct directory *) y)->name);
401 }
402 #endif /* !CONFIG_WITH_STRCACHE2 */
403 
404 /* Table of directories hashed by name.  */
405 static struct hash_table directories;
406 
407 #ifdef CONFIG_WITH_ALLOC_CACHES
408 /* Allocation cache for directories. */
409 struct alloccache directories_cache;
410 #endif
411 
412 /* Never have more than this many directories open at once.  */
413 
414 #define MAX_OPEN_DIRECTORIES 10
415 
416 static unsigned int open_directories = 0;
417 
418 
419 /* Hash table of files in each directory.  */
420 
421 struct dirfile
422   {
423     const char *name;		/* Name of the file.  */
424     short length;
425     short impossible;		/* This file is impossible.  */
426   };
427 
428 #ifndef CONFIG_WITH_STRCACHE2
429 static unsigned long
dirfile_hash_1(const void * key)430 dirfile_hash_1 (const void *key)
431 {
432   return_ISTRING_HASH_1 (((struct dirfile const *) key)->name);
433 }
434 
435 static unsigned long
dirfile_hash_2(const void * key)436 dirfile_hash_2 (const void *key)
437 {
438   return_ISTRING_HASH_2 (((struct dirfile const *) key)->name);
439 }
440 
441 static int
dirfile_hash_cmp(const void * xv,const void * yv)442 dirfile_hash_cmp (const void *xv, const void *yv)
443 {
444   const struct dirfile *x = xv;
445   const struct dirfile *y = yv;
446   int result = x->length - y->length;
447   if (result)
448     return result;
449   return_ISTRING_COMPARE (x->name, y->name);
450 }
451 #endif /* !CONFIG_WITH_STRCACHE2 */
452 
453 #ifndef	DIRFILE_BUCKETS
454 #define DIRFILE_BUCKETS 107
455 #endif
456 
457 #ifdef CONFIG_WITH_ALLOC_CACHES
458 /* Allocation cache for dirfiles. */
459 struct alloccache dirfile_cache;
460 #endif
461 
462 
463 static int dir_contents_file_exists_p (struct directory_contents *dir,
464                                        const char *filename);
465 static struct directory *find_directory (const char *name);
466 
467 /* Find the directory named NAME and return its `struct directory'.  */
468 
469 static struct directory *
find_directory(const char * name)470 find_directory (const char *name)
471 {
472   const char *p;
473   struct directory *dir;
474   struct directory **dir_slot;
475   struct directory dir_key;
476   int r;
477 #ifdef WINDOWS32
478   char* w32_path;
479   char  fs_label[BUFSIZ];
480   char  fs_type[BUFSIZ];
481   unsigned long  fs_serno;
482   unsigned long  fs_flags;
483   unsigned long  fs_len;
484 #endif
485 #ifdef VMS
486   if ((*name == '.') && (*(name+1) == 0))
487     name = "[]";
488   else
489     name = vmsify (name,1);
490 #endif
491 
492 #ifndef CONFIG_WITH_STRCACHE2
493   dir_key.name = name;
494   dir_slot = (struct directory **) hash_find_slot (&directories, &dir_key);
495 #else
496   p = name + strlen (name);
497 # if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
498   dir_key.name = strcache_add_len (downcase(name), p - name);
499 # else
500   dir_key.name = strcache_add_len (name, p - name);
501 # endif
502   dir_slot = (struct directory **) hash_find_slot_strcached (&directories, &dir_key);
503 #endif
504   dir = *dir_slot;
505 
506   if (HASH_VACANT (dir))
507     {
508       struct stat st;
509 
510       /* The directory was not found.  Create a new entry for it.  */
511 
512 #ifndef CONFIG_WITH_STRCACHE2
513       p = name + strlen (name);
514 #endif
515 #ifndef CONFIG_WITH_ALLOC_CACHES
516       dir = xmalloc (sizeof (struct directory));
517 #else
518       dir = alloccache_alloc (&directories_cache);
519 #endif
520 #ifndef CONFIG_WITH_STRCACHE2
521 #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
522       dir->name = strcache_add_len (downcase(name), p - name);
523 #else
524       dir->name = strcache_add_len (name, p - name);
525 #endif
526 #else  /* CONFIG_WITH_STRCACHE2 */
527       dir->name = dir_key.name;
528 #endif /* CONFIG_WITH_STRCACHE2 */
529       hash_insert_at (&directories, dir, dir_slot);
530       /* The directory is not in the name hash table.
531 	 Find its device and inode numbers, and look it up by them.  */
532 
533 #ifdef VMS
534       r = vmsstat_dir (name, &st);
535 #elif defined(WINDOWS32)
536       {
537         char tem[MAXPATHLEN], *tstart, *tend;
538 
539         /* Remove any trailing slashes.  Windows32 stat fails even on
540            valid directories if they end in a slash. */
541         memcpy (tem, name, p - name + 1);
542         tstart = tem;
543         if (tstart[1] == ':')
544           tstart += 2;
545         for (tend = tem + (p - name - 1);
546              tend > tstart && (*tend == '/' || *tend == '\\');
547              tend--)
548           *tend = '\0';
549 
550         r = stat (tem, &st);
551       }
552 #else
553       EINTRLOOP (r, stat (name, &st));
554 #endif
555 
556       if (r < 0)
557         {
558 	/* Couldn't stat the directory.  Mark this by
559 	   setting the `contents' member to a nil pointer.  */
560 	  dir->contents = 0;
561 	}
562       else
563 	{
564 	  /* Search the contents hash table; device and inode are the key.  */
565 
566 	  struct directory_contents *dc;
567 	  struct directory_contents **dc_slot;
568 	  struct directory_contents dc_key;
569 
570 	  dc_key.dev = st.st_dev;
571 #ifdef WINDOWS32
572 # ifndef CONFIG_WITH_STRCACHE2
573 	  dc_key.path_key = w32_path = w32ify (name, 1);
574 # else  /* CONFIG_WITH_STRCACHE2 */
575 	  w32_path = w32ify (name, 1);
576 	  dc_key.path_key = strcache_add (w32_path);
577 # endif /* CONFIG_WITH_STRCACHE2 */
578 	  dc_key.ctime = st.st_ctime;
579 #else
580 # ifdef VMS
581 	  dc_key.ino[0] = st.st_ino[0];
582 	  dc_key.ino[1] = st.st_ino[1];
583 	  dc_key.ino[2] = st.st_ino[2];
584 # else
585 	  dc_key.ino = st.st_ino;
586 # endif
587 #endif
588 	  dc_slot = (struct directory_contents **) hash_find_slot (&directory_contents, &dc_key);
589 	  dc = *dc_slot;
590 
591 	  if (HASH_VACANT (dc))
592 	    {
593 #if defined(WINDOWS32) && defined(KMK)
594               static char s_last_volume[4];
595               static int  s_last_flags;
596 #endif
597 	      /* Nope; this really is a directory we haven't seen before.  */
598 
599 #ifndef CONFIG_WITH_ALLOC_CACHES
600 	      dc = (struct directory_contents *)
601 		xmalloc (sizeof (struct directory_contents));
602 #else
603               dc = (struct directory_contents *)
604                     alloccache_alloc (&directory_contents_cache);
605 #endif
606 
607 	      /* Enter it in the contents hash table.  */
608 	      dc->dev = st.st_dev;
609 #ifdef WINDOWS32
610 # ifndef CONFIG_WITH_STRCACHE2
611               dc->path_key = xstrdup (w32_path);
612 # else  /* CONFIG_WITH_STRCACHE2 */
613               dc->path_key = dc_key.path_key;
614 # endif /* CONFIG_WITH_STRCACHE2 */
615 
616 	      dc->ctime = st.st_ctime;
617               dc->mtime = st.st_mtime;
618 # ifdef KMK
619               dc->last_updated = time(NULL);
620 # endif
621 
622               /*
623                * NTFS is the only WINDOWS32 filesystem that bumps mtime
624                * on a directory when files are added/deleted from
625                * a directory.
626                */
627               w32_path[3] = '\0';
628 # ifdef KMK /* Need for speed: Cache the GetVolumeInformation result. */
629               if (   s_last_volume[0] == w32_path[0]
630                   && s_last_volume[1] == w32_path[1]
631                   && s_last_volume[2] == w32_path[2]
632                   && s_last_volume[3] == w32_path[3])
633                 dc->fs_flags = s_last_flags;
634               else
635                 {
636 # endif
637               if (GetVolumeInformation(w32_path,
638                      fs_label, sizeof (fs_label),
639                      &fs_serno, &fs_len,
640                      &fs_flags, fs_type, sizeof (fs_type)) == FALSE)
641                 dc->fs_flags = FS_UNKNOWN;
642               else if (!strcmp(fs_type, "FAT"))
643                 dc->fs_flags = FS_FAT;
644               else if (!strcmp(fs_type, "NTFS"))
645                 dc->fs_flags = FS_NTFS;
646               else
647                 dc->fs_flags = FS_UNKNOWN;
648 # ifdef KMK
649                   s_last_volume[0] = w32_path[0];
650                   s_last_volume[1] = w32_path[1];
651                   s_last_volume[2] = w32_path[2];
652                   s_last_volume[3] = w32_path[3];
653                   s_last_flags     = dc->fs_flags;
654                 }
655 # endif
656 #else
657 # ifdef VMS
658 	      dc->ino[0] = st.st_ino[0];
659 	      dc->ino[1] = st.st_ino[1];
660 	      dc->ino[2] = st.st_ino[2];
661 # else
662 	      dc->ino = st.st_ino;
663 # endif
664 #endif /* WINDOWS32 */
665 	      hash_insert_at (&directory_contents, dc, dc_slot);
666 	      ENULLLOOP (dc->dirstream, opendir (name));
667 	      if (dc->dirstream == 0)
668                 /* Couldn't open the directory.  Mark this by setting the
669                    `files' member to a nil pointer.  */
670                 dc->dirfiles.ht_vec = 0;
671 	      else
672 		{
673 #ifdef KMK
674 		  int buckets = st.st_nlink * 2;
675 		  if (buckets < DIRFILE_BUCKETS)
676 		    buckets = DIRFILE_BUCKETS;
677 		  hash_init_strcached (&dc->dirfiles, buckets, &file_strcache,
678                                        offsetof (struct dirfile, name));
679 #else
680 # ifndef CONFIG_WITH_STRCACHE2
681 		  hash_init (&dc->dirfiles, DIRFILE_BUCKETS,
682 			     dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
683 # else  /* CONFIG_WITH_STRCACHE2 */
684                   hash_init_strcached (&dc->dirfiles, DIRFILE_BUCKETS,
685                                        &file_strcache,
686                                        offsetof (struct dirfile, name));
687 # endif /* CONFIG_WITH_STRCACHE2 */
688 #endif
689 		  /* Keep track of how many directories are open.  */
690 		  ++open_directories;
691 		  if (open_directories == MAX_OPEN_DIRECTORIES)
692 		    /* We have too many directories open already.
693 		       Read the entire directory and then close it.  */
694 		    dir_contents_file_exists_p (dc, 0);
695 		}
696 	    }
697 
698 	  /* Point the name-hashed entry for DIR at its contents data.  */
699 	  dir->contents = dc;
700 	}
701     }
702 
703   return dir;
704 }
705 
706 /* Return 1 if the name FILENAME is entered in DIR's hash table.
707    FILENAME must contain no slashes.  */
708 
709 static int
dir_contents_file_exists_p(struct directory_contents * dir,const char * filename)710 dir_contents_file_exists_p (struct directory_contents *dir,
711                             const char *filename)
712 {
713   /*unsigned int hash;*/
714   struct dirfile *df;
715   struct dirent *d;
716 #ifdef WINDOWS32
717 # ifndef KMK
718   struct stat st;
719 # endif
720   int rehash = 0;
721 #endif
722 #ifdef KMK
723   int ret = 0;
724 #endif
725 
726   if (dir == 0 || dir->dirfiles.ht_vec == 0)
727     /* The directory could not be stat'd or opened.  */
728     return 0;
729 
730 #ifdef __MSDOS__
731   filename = dosify (filename);
732 #endif
733 
734 #ifdef HAVE_CASE_INSENSITIVE_FS
735   filename = downcase (filename);
736 #endif
737 
738 #ifdef __EMX__
739   if (filename != 0)
740     _fnlwr (filename); /* lower case for FAT drives */
741 #endif
742 
743 #ifdef VMS
744   filename = vmsify (filename,0);
745 #endif
746 
747   /*hash = 0;*/
748   if (filename != 0)
749     {
750       struct dirfile dirfile_key;
751 
752       if (*filename == '\0')
753 	{
754 	  /* Checking if the directory exists.  */
755 	  return 1;
756 	}
757 #ifndef CONFIG_WITH_STRCACHE2
758       dirfile_key.name = filename;
759       dirfile_key.length = strlen (filename);
760       df = hash_find_item (&dir->dirfiles, &dirfile_key);
761 #else  /* CONFIG_WITH_STRCACHE2 */
762       dirfile_key.length = strlen (filename);
763       dirfile_key.name = filename
764         = strcache_add_len (filename, dirfile_key.length);
765       df = hash_find_item_strcached (&dir->dirfiles, &dirfile_key);
766 #endif /* CONFIG_WITH_STRCACHE2 */
767       if (df)
768         return !df->impossible;
769     }
770 
771   /* The file was not found in the hashed list.
772      Try to read the directory further.  */
773 
774   if (dir->dirstream == 0)
775     {
776 #if defined(WINDOWS32) && !defined(KMK)
777       /*
778        * Check to see if directory has changed since last read. FAT
779        * filesystems force a rehash always as mtime does not change
780        * on directories (ugh!).
781        */
782 # ifdef KMK
783       if (dir->path_key && time(NULL) > dc->last_updated + 2) /* KMK: Only recheck every 2 seconds. */
784 # else
785       if (dir->path_key)
786 # endif
787 	{
788           if ((dir->fs_flags & FS_FAT) != 0)
789 	    {
790 	      dir->mtime = time ((time_t *) 0);
791 	      rehash = 1;
792 	    }
793 # ifdef KMK
794 	  else if (   birdStatModTimeOnly (dir->path_key, &st.st_mtim, 1) == 0
795                    && st.st_mtime > dir->mtime)
796 # else
797 	  else if (stat (dir->path_key, &st) == 0 && st.st_mtime > dir->mtime)
798 # endif
799 	    {
800 	      /* reset date stamp to show most recent re-process.  */
801 	      dir->mtime = st.st_mtime;
802 	      rehash = 1;
803 	    }
804 
805 
806           /* If it has been already read in, all done.  */
807 	  if (!rehash)
808 	    return 0;
809 
810           /* make sure directory can still be opened; if not return.  */
811           dir->dirstream = opendir (dir->path_key);
812           if (!dir->dirstream)
813             return 0;
814 # ifdef KMK
815           dc->last_updated = time(NULL);
816 # endif
817 	}
818       else
819 #endif
820 	/* The directory has been all read in.  */
821 	return 0;
822     }
823 
824   while (1)
825     {
826       /* Enter the file in the hash table.  */
827       unsigned int len;
828       struct dirfile dirfile_key;
829       struct dirfile **dirfile_slot;
830 
831       ENULLLOOP (d, readdir (dir->dirstream));
832       if (d == 0)
833         {
834 /* bird: Workaround for smbfs mounts returning EBADF at the end of the search.
835          To exactly determin the cause here, I should probably do some smbfs
836          tracing, but for now just ignoring the EBADF on seems to work.
837          (The smb server is 64-bit vista, btw.) */
838 #if defined (__FreeBSD__)
839           struct statfs stfs;
840           int saved_errno = errno;
841           errno = 0;
842           if (saved_errno == EBADF
843            && !fstatfs (dirfd (dir->dirstream), &stfs)
844            && !(stfs.f_flags & MNT_LOCAL)
845            && !strcmp(stfs.f_fstypename, "smbfs"))
846             {
847               /*fprintf (stderr, "EBADF on remote fs! dirfd=%d errno=%d\n",
848                        dirfd (dir->dirstream), errno);*/
849               saved_errno = 0;
850             }
851           errno = saved_errno;
852 #endif
853 /* bird: end */
854           if (errno)
855             fatal (NILF, "INTERNAL: readdir(%p): %s (filename=%s)\n", (void *)dir, strerror (errno), filename);
856           break;
857         }
858 
859 #if defined(VMS) && defined(HAVE_DIRENT_H)
860       /* In VMS we get file versions too, which have to be stripped off */
861       {
862         char *p = strrchr (d->d_name, ';');
863         if (p)
864           *p = '\0';
865       }
866 #endif
867       if (!REAL_DIR_ENTRY (d))
868 	continue;
869 
870       len = NAMLEN (d);
871 #ifndef CONFIG_WITH_STRCACHE2
872       dirfile_key.name = d->d_name;
873       dirfile_key.length = len;
874       dirfile_slot = (struct dirfile **) hash_find_slot (&dir->dirfiles, &dirfile_key);
875 #else
876 # if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
877       dirfile_key.name = strcache_add_len (downcase(d->d_name), len);
878 # else
879       dirfile_key.name = strcache_add_len (d->d_name, len);
880 # endif
881       dirfile_key.length = len;
882       dirfile_slot = (struct dirfile **) hash_find_slot_strcached (&dir->dirfiles, &dirfile_key);
883 #endif
884 #ifdef WINDOWS32
885       /*
886        * If re-reading a directory, don't cache files that have
887        * already been discovered.
888        */
889       if (! rehash || HASH_VACANT (*dirfile_slot))
890 #endif
891 	{
892 #ifndef CONFIG_WITH_ALLOC_CACHES
893 	  df = xmalloc (sizeof (struct dirfile));
894 #else
895 	  df = alloccache_alloc (&dirfile_cache);
896 #endif
897 #ifndef CONFIG_WITH_STRCACHE2
898 #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
899           df->name = strcache_add_len (downcase(d->d_name), len);
900 #else
901 	  df->name = strcache_add_len (d->d_name, len);
902 #endif
903 #else  /* CONFIG_WITH_STRCACHE2 */
904 	  df->name = dirfile_key.name;
905 #endif /* CONFIG_WITH_STRCACHE2 */
906 	  df->length = len;
907 	  df->impossible = 0;
908 	  hash_insert_at (&dir->dirfiles, df, dirfile_slot);
909 	}
910       /* Check if the name matches the one we're searching for.  */
911 #ifndef CONFIG_WITH_STRCACHE2
912       if (filename != 0 && patheq (d->d_name, filename))
913 #else
914       if (filename != 0 && dirfile_key.name == filename)
915 #endif
916 #ifdef KMK
917         ret = 1; /* Cache the whole dir. Prevents trouble on windows and os2 during 'rebuild'. */
918 #else
919         return 1;
920 #endif
921     }
922 
923   /* If the directory has been completely read in,
924      close the stream and reset the pointer to nil.  */
925   if (d == 0)
926     {
927       --open_directories;
928       closedir (dir->dirstream);
929       dir->dirstream = 0;
930     }
931 #ifdef KMK
932   return ret;
933 #else
934   return 0;
935 #endif
936 }
937 
938 /* Return 1 if the name FILENAME in directory DIRNAME
939    is entered in the dir hash table.
940    FILENAME must contain no slashes.  */
941 
942 int
dir_file_exists_p(const char * dirname,const char * filename)943 dir_file_exists_p (const char *dirname, const char *filename)
944 {
945   return dir_contents_file_exists_p (find_directory (dirname)->contents,
946 				     filename);
947 }
948 
949 /* Return 1 if the file named NAME exists.  */
950 
951 int
file_exists_p(const char * name)952 file_exists_p (const char *name)
953 {
954   const char *dirend;
955   const char *dirname;
956   const char *slash;
957 
958 #ifndef	NO_ARCHIVES
959   if (ar_name (name))
960     return ar_member_date (name) != (time_t) -1;
961 #endif
962 
963 #ifdef VMS
964   dirend = strrchr (name, ']');
965   if (dirend == 0)
966     dirend = strrchr (name, ':');
967   if (dirend == 0)
968     return dir_file_exists_p ("[]", name);
969 #else /* !VMS */
970   dirend = strrchr (name, '/');
971 #ifdef HAVE_DOS_PATHS
972   /* Forward and backslashes might be mixed.  We need the rightmost one.  */
973   {
974     const char *bslash = strrchr(name, '\\');
975     if (!dirend || bslash > dirend)
976       dirend = bslash;
977     /* The case of "d:file".  */
978     if (!dirend && name[0] && name[1] == ':')
979       dirend = name + 1;
980   }
981 #endif /* HAVE_DOS_PATHS */
982   if (dirend == 0)
983 #ifndef _AMIGA
984     return dir_file_exists_p (".", name);
985 #else /* !VMS && !AMIGA */
986     return dir_file_exists_p ("", name);
987 #endif /* AMIGA */
988 #endif /* VMS */
989 
990   slash = dirend;
991   if (dirend == name)
992     dirname = "/";
993   else
994     {
995       char *p;
996 #ifdef HAVE_DOS_PATHS
997   /* d:/ and d: are *very* different...  */
998       if (dirend < name + 3 && name[1] == ':' &&
999 	  (*dirend == '/' || *dirend == '\\' || *dirend == ':'))
1000 	dirend++;
1001 #endif
1002       p = alloca (dirend - name + 1);
1003       memcpy (p, name, dirend - name);
1004       p[dirend - name] = '\0';
1005       dirname = p;
1006     }
1007   return dir_file_exists_p (dirname, slash + 1);
1008 }
1009 
1010 /* Mark FILENAME as `impossible' for `file_impossible_p'.
1011    This means an attempt has been made to search for FILENAME
1012    as an intermediate file, and it has failed.  */
1013 
1014 void
file_impossible(const char * filename)1015 file_impossible (const char *filename)
1016 {
1017   const char *dirend;
1018   const char *p = filename;
1019   struct directory *dir;
1020   struct dirfile *new;
1021 
1022 #ifdef VMS
1023   dirend = strrchr (p, ']');
1024   if (dirend == 0)
1025     dirend = strrchr (p, ':');
1026   dirend++;
1027   if (dirend == (char *)1)
1028     dir = find_directory ("[]");
1029 #else
1030   dirend = strrchr (p, '/');
1031 # ifdef HAVE_DOS_PATHS
1032   /* Forward and backslashes might be mixed.  We need the rightmost one.  */
1033   {
1034     const char *bslash = strrchr(p, '\\');
1035     if (!dirend || bslash > dirend)
1036       dirend = bslash;
1037     /* The case of "d:file".  */
1038     if (!dirend && p[0] && p[1] == ':')
1039       dirend = p + 1;
1040   }
1041 # endif /* HAVE_DOS_PATHS */
1042   if (dirend == 0)
1043 # ifdef _AMIGA
1044     dir = find_directory ("");
1045 # else /* !VMS && !AMIGA */
1046     dir = find_directory (".");
1047 # endif /* AMIGA */
1048 #endif /* VMS */
1049   else
1050     {
1051       const char *dirname;
1052       const char *slash = dirend;
1053       if (dirend == p)
1054 	dirname = "/";
1055       else
1056 	{
1057           char *cp;
1058 #ifdef HAVE_DOS_PATHS
1059 	  /* d:/ and d: are *very* different...  */
1060 	  if (dirend < p + 3 && p[1] == ':' &&
1061 	      (*dirend == '/' || *dirend == '\\' || *dirend == ':'))
1062 	    dirend++;
1063 #endif
1064 	  cp = alloca (dirend - p + 1);
1065 	  memcpy (cp, p, dirend - p);
1066 	  cp[dirend - p] = '\0';
1067           dirname = cp;
1068 	}
1069       dir = find_directory (dirname);
1070       filename = p = slash + 1;
1071     }
1072 
1073   if (dir->contents == 0)
1074     /* The directory could not be stat'd.  We allocate a contents
1075        structure for it, but leave it out of the contents hash table.  */
1076 #ifndef CONFIG_WITH_ALLOC_CACHES
1077     dir->contents = xcalloc (sizeof (struct directory_contents));
1078 #else
1079     dir->contents = alloccache_calloc (&directory_contents_cache);
1080 #endif
1081 
1082   if (dir->contents->dirfiles.ht_vec == 0)
1083     {
1084 #ifndef CONFIG_WITH_STRCACHE2
1085       hash_init (&dir->contents->dirfiles, DIRFILE_BUCKETS,
1086 		 dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
1087 #else  /* CONFIG_WITH_STRCACHE2 */
1088       hash_init_strcached (&dir->contents->dirfiles, DIRFILE_BUCKETS,
1089                            &file_strcache, offsetof (struct dirfile, name));
1090 #endif /* CONFIG_WITH_STRCACHE2 */
1091     }
1092 
1093   /* Make a new entry and put it in the table.  */
1094 
1095 #ifndef CONFIG_WITH_ALLOC_CACHES
1096   new = xmalloc (sizeof (struct dirfile));
1097 #else
1098   new = alloccache_alloc (&dirfile_cache);
1099 #endif
1100   new->length = strlen (filename);
1101 #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
1102   new->name = strcache_add_len (downcase(filename), new->length);
1103 #else
1104   new->name = strcache_add_len (filename, new->length);
1105 #endif
1106   new->impossible = 1;
1107 #ifndef CONFIG_WITH_STRCACHE2
1108   hash_insert (&dir->contents->dirfiles, new);
1109 #else  /* CONFIG_WITH_STRCACHE2 */
1110   hash_insert_strcached (&dir->contents->dirfiles, new);
1111 #endif /* CONFIG_WITH_STRCACHE2 */
1112 }
1113 
1114 /* Return nonzero if FILENAME has been marked impossible.  */
1115 
1116 int
file_impossible_p(const char * filename)1117 file_impossible_p (const char *filename)
1118 {
1119   const char *dirend;
1120   const char *p = filename;
1121   struct directory_contents *dir;
1122   struct dirfile *dirfile;
1123   struct dirfile dirfile_key;
1124 
1125 #ifdef VMS
1126   dirend = strrchr (filename, ']');
1127   if (dirend == 0)
1128     dir = find_directory ("[]")->contents;
1129 #else
1130   dirend = strrchr (filename, '/');
1131 #ifdef HAVE_DOS_PATHS
1132   /* Forward and backslashes might be mixed.  We need the rightmost one.  */
1133   {
1134     const char *bslash = strrchr(filename, '\\');
1135     if (!dirend || bslash > dirend)
1136       dirend = bslash;
1137     /* The case of "d:file".  */
1138     if (!dirend && filename[0] && filename[1] == ':')
1139       dirend = filename + 1;
1140   }
1141 #endif /* HAVE_DOS_PATHS */
1142   if (dirend == 0)
1143 #ifdef _AMIGA
1144     dir = find_directory ("")->contents;
1145 #else /* !VMS && !AMIGA */
1146     dir = find_directory (".")->contents;
1147 #endif /* AMIGA */
1148 #endif /* VMS */
1149   else
1150     {
1151       const char *dirname;
1152       const char *slash = dirend;
1153       if (dirend == filename)
1154 	dirname = "/";
1155       else
1156 	{
1157           char *cp;
1158 #ifdef HAVE_DOS_PATHS
1159 	  /* d:/ and d: are *very* different...  */
1160 	  if (dirend < filename + 3 && filename[1] == ':' &&
1161 	      (*dirend == '/' || *dirend == '\\' || *dirend == ':'))
1162 	    dirend++;
1163 #endif
1164 	  cp = alloca (dirend - filename + 1);
1165 	  memcpy (cp, p, dirend - p);
1166 	  cp[dirend - p] = '\0';
1167           dirname = cp;
1168 	}
1169       dir = find_directory (dirname)->contents;
1170       p = filename = slash + 1;
1171     }
1172 
1173   if (dir == 0 || dir->dirfiles.ht_vec == 0)
1174     /* There are no files entered for this directory.  */
1175     return 0;
1176 
1177 #ifdef __MSDOS__
1178   filename = dosify (p);
1179 #endif
1180 #ifdef HAVE_CASE_INSENSITIVE_FS
1181   filename = downcase (p);
1182 #endif
1183 #ifdef VMS
1184   filename = vmsify (p, 1);
1185 #endif
1186 
1187 #ifndef CONFIG_WITH_STRCACHE2
1188   dirfile_key.name = filename;
1189   dirfile_key.length = strlen (filename);
1190   dirfile = hash_find_item (&dir->dirfiles, &dirfile_key);
1191 #else
1192   dirfile_key.length = strlen (filename);
1193   dirfile_key.name = strcache_add_len (filename, dirfile_key.length);
1194   dirfile = hash_find_item_strcached (&dir->dirfiles, &dirfile_key);
1195 #endif
1196   if (dirfile)
1197     return dirfile->impossible;
1198 
1199   return 0;
1200 }
1201 
1202 /* Return the already allocated name in the
1203    directory hash table that matches DIR.  */
1204 
1205 const char *
dir_name(const char * dir)1206 dir_name (const char *dir)
1207 {
1208   return find_directory (dir)->name;
1209 }
1210 
1211 /* Print the data base of directories.  */
1212 
1213 void
print_dir_data_base(void)1214 print_dir_data_base (void)
1215 {
1216   unsigned int files;
1217   unsigned int impossible;
1218   struct directory **dir_slot;
1219   struct directory **dir_end;
1220 
1221   puts (_("\n# Directories\n"));
1222 
1223   files = impossible = 0;
1224 
1225   dir_slot = (struct directory **) directories.ht_vec;
1226   dir_end = dir_slot + directories.ht_size;
1227   for ( ; dir_slot < dir_end; dir_slot++)
1228     {
1229       struct directory *dir = *dir_slot;
1230       if (! HASH_VACANT (dir))
1231 	{
1232 	  if (dir->contents == 0)
1233 	    printf (_("# %s: could not be stat'd.\n"), dir->name);
1234 	  else if (dir->contents->dirfiles.ht_vec == 0)
1235 	    {
1236 #ifdef WINDOWS32
1237 	      printf (_("# %s (key %s, mtime %d): could not be opened.\n"),
1238 		      dir->name, dir->contents->path_key,dir->contents->mtime);
1239 #else  /* WINDOWS32 */
1240 #ifdef VMS
1241 	      printf (_("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n"),
1242 		      dir->name, dir->contents->dev,
1243 		      dir->contents->ino[0], dir->contents->ino[1],
1244 		      dir->contents->ino[2]);
1245 #else
1246 	      printf (_("# %s (device %ld, inode %ld): could not be opened.\n"),
1247 		      dir->name, (long int) dir->contents->dev,
1248 		      (long int) dir->contents->ino);
1249 #endif
1250 #endif /* WINDOWS32 */
1251 	    }
1252 	  else
1253 	    {
1254 	      unsigned int f = 0;
1255 	      unsigned int im = 0;
1256 	      struct dirfile **files_slot;
1257 	      struct dirfile **files_end;
1258 
1259 	      files_slot = (struct dirfile **) dir->contents->dirfiles.ht_vec;
1260 	      files_end = files_slot + dir->contents->dirfiles.ht_size;
1261 	      for ( ; files_slot < files_end; files_slot++)
1262 		{
1263 		  struct dirfile *df = *files_slot;
1264 		  if (! HASH_VACANT (df))
1265 		    {
1266 		      if (df->impossible)
1267 			++im;
1268 		      else
1269 			++f;
1270 		    }
1271 		}
1272 #ifdef WINDOWS32
1273 	      printf (_("# %s (key %s, mtime %d): "),
1274 		      dir->name, dir->contents->path_key, dir->contents->mtime);
1275 #else  /* WINDOWS32 */
1276 #ifdef VMS
1277 	      printf (_("# %s (device %d, inode [%d,%d,%d]): "),
1278 		      dir->name, dir->contents->dev,
1279 		      dir->contents->ino[0], dir->contents->ino[1],
1280 		      dir->contents->ino[2]);
1281 #else
1282 	      printf (_("# %s (device %ld, inode %ld): "),
1283 		      dir->name,
1284 		      (long)dir->contents->dev, (long)dir->contents->ino);
1285 #endif
1286 #endif /* WINDOWS32 */
1287 	      if (f == 0)
1288 		fputs (_("No"), stdout);
1289 	      else
1290 		printf ("%u", f);
1291 	      fputs (_(" files, "), stdout);
1292 	      if (im == 0)
1293 		fputs (_("no"), stdout);
1294 	      else
1295 		printf ("%u", im);
1296 	      fputs (_(" impossibilities"), stdout);
1297 	      if (dir->contents->dirstream == 0)
1298 		puts (".");
1299 	      else
1300 		puts (_(" so far."));
1301 	      files += f;
1302 	      impossible += im;
1303 #ifdef KMK
1304               fputs ("# ", stdout);
1305               hash_print_stats (&dir->contents->dirfiles, stdout);
1306               fputs ("\n", stdout);
1307 #endif
1308 	    }
1309 	}
1310     }
1311 
1312   fputs ("\n# ", stdout);
1313   if (files == 0)
1314     fputs (_("No"), stdout);
1315   else
1316     printf ("%u", files);
1317   fputs (_(" files, "), stdout);
1318   if (impossible == 0)
1319     fputs (_("no"), stdout);
1320   else
1321     printf ("%u", impossible);
1322   printf (_(" impossibilities in %lu directories.\n"), directories.ht_fill);
1323 #ifdef KMK
1324   fputs ("# directories: ", stdout);
1325   hash_print_stats (&directories, stdout);
1326   fputs ("\n# directory_contents: ", stdout);
1327   hash_print_stats (&directory_contents, stdout);
1328   fputs ("\n", stdout);
1329 #endif
1330 }
1331 
1332 #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
1333 /* Print stats */
1334 
print_dir_stats(void)1335 void print_dir_stats (void)
1336 {
1337   /** @todo normal dir stats.  */
1338 }
1339 #endif
1340 
1341 /* Hooks for globbing.  */
1342 
1343 #include <glob.h>
1344 
1345 /* Structure describing state of iterating through a directory hash table.  */
1346 
1347 struct dirstream
1348   {
1349     struct directory_contents *contents; /* The directory being read.  */
1350     struct dirfile **dirfile_slot; /* Current slot in table.  */
1351   };
1352 
1353 /* Forward declarations.  */
1354 static __ptr_t open_dirstream (const char *);
1355 static struct dirent *read_dirstream (__ptr_t);
1356 
1357 static __ptr_t
open_dirstream(const char * directory)1358 open_dirstream (const char *directory)
1359 {
1360   struct dirstream *new;
1361   struct directory *dir = find_directory (directory);
1362 
1363   if (dir->contents == 0 || dir->contents->dirfiles.ht_vec == 0)
1364     /* DIR->contents is nil if the directory could not be stat'd.
1365        DIR->contents->dirfiles is nil if it could not be opened.  */
1366     return 0;
1367 
1368   /* Read all the contents of the directory now.  There is no benefit
1369      in being lazy, since glob will want to see every file anyway.  */
1370 
1371   dir_contents_file_exists_p (dir->contents, 0);
1372 
1373   new = xmalloc (sizeof (struct dirstream));
1374   new->contents = dir->contents;
1375   new->dirfile_slot = (struct dirfile **) new->contents->dirfiles.ht_vec;
1376 
1377   return (__ptr_t) new;
1378 }
1379 
1380 static struct dirent *
read_dirstream(__ptr_t stream)1381 read_dirstream (__ptr_t stream)
1382 {
1383   static char *buf;
1384   static unsigned int bufsz;
1385 
1386   struct dirstream *const ds = (struct dirstream *) stream;
1387   struct directory_contents *dc = ds->contents;
1388   struct dirfile **dirfile_end = (struct dirfile **) dc->dirfiles.ht_vec + dc->dirfiles.ht_size;
1389 
1390   while (ds->dirfile_slot < dirfile_end)
1391     {
1392       struct dirfile *df = *ds->dirfile_slot++;
1393       if (! HASH_VACANT (df) && !df->impossible)
1394 	{
1395 	  /* The glob interface wants a `struct dirent', so mock one up.  */
1396 	  struct dirent *d;
1397 	  unsigned int len = df->length + 1;
1398           unsigned int sz = sizeof (*d) - sizeof (d->d_name) + len;
1399 	  if (sz > bufsz)
1400 	    {
1401 	      bufsz *= 2;
1402 	      if (sz > bufsz)
1403 		bufsz = sz;
1404 	      buf = xrealloc (buf, bufsz);
1405 	    }
1406 	  d = (struct dirent *) buf;
1407 #ifdef __MINGW32__
1408 # if __MINGW32_MAJOR_VERSION < 3 || (__MINGW32_MAJOR_VERSION == 3 && \
1409 				     __MINGW32_MINOR_VERSION == 0)
1410 	  d->d_name = xmalloc(len);
1411 # endif
1412 #endif
1413 	  FAKE_DIR_ENTRY (d);
1414 #ifdef _DIRENT_HAVE_D_NAMLEN
1415 	  d->d_namlen = len - 1;
1416 #endif
1417 #ifdef _DIRENT_HAVE_D_TYPE
1418 	  d->d_type = DT_UNKNOWN;
1419 #endif
1420 	  memcpy (d->d_name, df->name, len);
1421 	  return d;
1422 	}
1423     }
1424 
1425   return 0;
1426 }
1427 
1428 static void
ansi_free(void * p)1429 ansi_free (void *p)
1430 {
1431   if (p)
1432     free(p);
1433 }
1434 
1435 /* On 64 bit ReliantUNIX (5.44 and above) in LFS mode, stat() is actually a
1436  * macro for stat64().  If stat is a macro, make a local wrapper function to
1437  * invoke it.
1438  */
1439 #ifndef stat
1440 # ifndef VMS
1441 int stat (const char *path, struct stat *sbuf);
1442 # endif
1443 # define local_stat stat
1444 #else
1445 static int
local_stat(const char * path,struct stat * buf)1446 local_stat (const char *path, struct stat *buf)
1447 {
1448   int e;
1449 
1450   EINTRLOOP (e, stat (path, buf));
1451   return e;
1452 }
1453 #endif
1454 
1455 #ifdef KMK
dir_exists_p(const char * dirname)1456 static int dir_exists_p (const char *dirname)
1457 {
1458   if (file_exists_p (dirname))
1459     {
1460       struct directory *dir = find_directory (dirname);
1461       if (dir != NULL && dir->contents && dir->contents->dirfiles.ht_vec != NULL)
1462         return 1;
1463     }
1464   return 0;
1465 }
1466 #endif
1467 
1468 void
dir_setup_glob(glob_t * gl)1469 dir_setup_glob (glob_t *gl)
1470 {
1471   gl->gl_opendir = open_dirstream;
1472   gl->gl_readdir = read_dirstream;
1473   gl->gl_closedir = ansi_free;
1474   gl->gl_stat = local_stat;
1475 #ifdef __EMX__ /* The FreeBSD implementation actually uses gl_lstat!! */
1476   gl->gl_lstat = local_stat;
1477 #endif
1478 #ifdef GLOB_WITH_EXTENDED_KMK_MEMBERS
1479   gl->gl_exists = file_exists_p;
1480   gl->gl_isdir = dir_exists_p;
1481 #endif
1482   /* We don't bother setting gl_lstat, since glob never calls it.
1483      The slot is only there for compatibility with 4.4 BSD.  */
1484 }
1485 
1486 void
hash_init_directories(void)1487 hash_init_directories (void)
1488 {
1489 #ifndef CONFIG_WITH_STRCACHE2
1490   hash_init (&directories, DIRECTORY_BUCKETS,
1491 	     directory_hash_1, directory_hash_2, directory_hash_cmp);
1492 #else  /* CONFIG_WITH_STRCACHE2 */
1493   hash_init_strcached (&directories, DIRECTORY_BUCKETS, &file_strcache,
1494                        offsetof (struct directory, name));
1495 #endif /* CONFIG_WITH_STRCACHE2 */
1496   hash_init (&directory_contents, DIRECTORY_BUCKETS,
1497 	     directory_contents_hash_1, directory_contents_hash_2,
1498              directory_contents_hash_cmp);
1499 #ifdef CONFIG_WITH_ALLOC_CACHES
1500   alloccache_init (&directories_cache, sizeof (struct directory),
1501                    "directories", NULL, NULL);
1502   alloccache_init (&directory_contents_cache, sizeof (struct directory_contents),
1503                    "directory_contents", NULL, NULL);
1504   alloccache_init (&dirfile_cache, sizeof (struct dirfile),
1505                    "dirfile", NULL, NULL);
1506 #endif /* CONFIG_WITH_ALLOC_CACHES */
1507 }
1508 
1509