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 Free
4 Software Foundation, Inc.
5 This file is part of GNU Make.
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
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.
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/>. */
24 # define NAMLEN(dirent) strlen((dirent)->d_name)
26 /* its prototype is in vmsdir.h, which is not needed for HAVE_DIRENT_H */
27 const char *vmsify (const char *name
, int type
);
30 # define dirent direct
31 # define NAMLEN(dirent) (dirent)->d_namlen
32 # ifdef HAVE_SYS_NDIR_H
33 # include <sys/ndir.h>
35 # ifdef HAVE_SYS_DIR_H
43 # endif /* HAVE_VMSDIR_H */
46 /* In GNU systems, <dirent.h> defines this macro for us. */
49 # define NAMLEN(d) _D_NAMLEN(d)
52 #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
53 /* Posix does not require that the d_ino field be present, and some
54 systems do not provide it. */
55 # define REAL_DIR_ENTRY(dp) 1
56 # define FAKE_DIR_ENTRY(dp)
58 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
59 # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
66 /* If it's MSDOS that doesn't have _USE_LFN, disable LFN support. */
72 dosify (const char *filename
)
74 static char dos_filename
[14];
78 if (filename
== 0 || _USE_LFN
)
81 /* FIXME: what about filenames which violate
82 8+3 constraints, like "config.h.in", or ".emacs"? */
83 if (strpbrk (filename
, "\"*+,;<=>?[\\]|") != 0)
88 /* First, transform the name part. */
89 for (i
= 0; *filename
!= '\0' && i
< 8 && *filename
!= '.'; ++i
)
90 *df
++ = tolower ((unsigned char)*filename
++);
92 /* Now skip to the next dot. */
93 while (*filename
!= '\0' && *filename
!= '.')
95 if (*filename
!= '\0')
98 for (i
= 0; *filename
!= '\0' && i
< 3 && *filename
!= '.'; ++i
)
99 *df
++ = tolower ((unsigned char)*filename
++);
102 /* Look for more dots. */
103 while (*filename
!= '\0' && *filename
!= '.')
105 if (*filename
== '.')
110 #endif /* __MSDOS__ */
113 #include "pathstuff.h"
120 #ifdef HAVE_CASE_INSENSITIVE_FS
122 downcase (const char *filename
)
124 static PATH_VAR (new_filename
);
133 /* First, transform the name part. */
134 while (*filename
!= '\0')
136 *df
++ = tolower ((unsigned char)*filename
);
144 #endif /* HAVE_CASE_INSENSITIVE_FS */
149 vms_hash (const char *name
)
156 unsigned char uc
= *name
;
157 #ifdef HAVE_CASE_INSENSITIVE_FS
158 h
= (h
<< 4) + (isupper (uc
) ? tolower (uc
) : uc
);
173 /* fake stat entry for a directory */
175 vmsstat_dir (const char *name
, struct stat
*st
)
181 dir
= opendir (name
);
185 s
= strchr (name
, ':'); /* find device */
188 /* to keep the compiler happy we said "const char *name", now we cheat */
190 st
->st_dev
= (char *)vms_hash (name
);
200 st
->st_ino
[0] = h
& 0xff;
201 st
->st_ino
[1] = h
& 0xff00;
202 st
->st_ino
[2] = h
>> 16;
208 /* Hash table of directories. */
210 #ifndef DIRECTORY_BUCKETS
211 #define DIRECTORY_BUCKETS 199
214 struct directory_contents
216 dev_t dev
; /* Device and inode numbers of this dir. */
218 /* Inode means nothing on WINDOWS32. Even file key information is
219 * unreliable because it is random per file open and undefined for remote
220 * filesystems. The most unique attribute I can come up with is the fully
221 * qualified name of the directory. Beware though, this is also
222 * unreliable. I'm open to suggestion on a better way to emulate inode. */
225 int mtime
; /* controls check for stale directory cache */
226 int fs_flags
; /* FS_FAT, FS_NTFS, ... */
229 # define FS_UNKNOWN 0x4
236 #endif /* WINDOWS32 */
237 struct hash_table dirfiles
; /* Files in this directory. */
238 DIR *dirstream
; /* Stream reading this directory. */
242 directory_contents_hash_1 (const void *key_0
)
244 const struct directory_contents
*key
= key_0
;
249 ISTRING_HASH_1 (key
->path_key
, hash
);
250 hash
^= ((unsigned int) key
->dev
<< 4) ^ (unsigned int) key
->ctime
;
253 hash
= (((unsigned int) key
->dev
<< 4)
254 ^ ((unsigned int) key
->ino
[0]
255 + (unsigned int) key
->ino
[1]
256 + (unsigned int) key
->ino
[2]));
258 hash
= ((unsigned int) key
->dev
<< 4) ^ (unsigned int) key
->ino
;
260 #endif /* WINDOWS32 */
265 directory_contents_hash_2 (const void *key_0
)
267 const struct directory_contents
*key
= key_0
;
272 ISTRING_HASH_2 (key
->path_key
, hash
);
273 hash
^= ((unsigned int) key
->dev
<< 4) ^ (unsigned int) ~key
->ctime
;
276 hash
= (((unsigned int) key
->dev
<< 4)
277 ^ ~((unsigned int) key
->ino
[0]
278 + (unsigned int) key
->ino
[1]
279 + (unsigned int) key
->ino
[2]));
281 hash
= ((unsigned int) key
->dev
<< 4) ^ (unsigned int) ~key
->ino
;
283 #endif /* WINDOWS32 */
288 /* Sometimes it's OK to use subtraction to get this value:
290 But, if we're not sure of the type of X and Y they may be too large for an
291 int (on a 64-bit system for example). So, use ?: instead.
292 See Savannah bug #15534.
294 NOTE! This macro has side-effects!
297 #define MAKECMP(_x,_y) ((_x)<(_y)?-1:((_x)==(_y)?0:1))
300 directory_contents_hash_cmp (const void *xv
, const void *yv
)
302 const struct directory_contents
*x
= xv
;
303 const struct directory_contents
*y
= yv
;
307 ISTRING_COMPARE (x
->path_key
, y
->path_key
, result
);
310 result
= MAKECMP(x
->ctime
, y
->ctime
);
315 result
= MAKECMP(x
->ino
[0], y
->ino
[0]);
318 result
= MAKECMP(x
->ino
[1], y
->ino
[1]);
321 result
= MAKECMP(x
->ino
[2], y
->ino
[2]);
325 result
= MAKECMP(x
->ino
, y
->ino
);
329 #endif /* WINDOWS32 */
331 return MAKECMP(x
->dev
, y
->dev
);
334 /* Table of directory contents hashed by device and inode number. */
335 static struct hash_table directory_contents
;
339 const char *name
; /* Name of the directory. */
341 /* The directory's contents. This data may be shared by several
342 entries in the hash table, which refer to the same directory
343 (identified uniquely by `dev' and `ino') under different names. */
344 struct directory_contents
*contents
;
348 directory_hash_1 (const void *key
)
350 return_ISTRING_HASH_1 (((const struct directory
*) key
)->name
);
354 directory_hash_2 (const void *key
)
356 return_ISTRING_HASH_2 (((const struct directory
*) key
)->name
);
360 directory_hash_cmp (const void *x
, const void *y
)
362 return_ISTRING_COMPARE (((const struct directory
*) x
)->name
,
363 ((const struct directory
*) y
)->name
);
366 /* Table of directories hashed by name. */
367 static struct hash_table directories
;
369 /* Never have more than this many directories open at once. */
371 #define MAX_OPEN_DIRECTORIES 10
373 static unsigned int open_directories
= 0;
376 /* Hash table of files in each directory. */
380 const char *name
; /* Name of the file. */
382 short impossible
; /* This file is impossible. */
386 dirfile_hash_1 (const void *key
)
388 return_ISTRING_HASH_1 (((struct dirfile
const *) key
)->name
);
392 dirfile_hash_2 (const void *key
)
394 return_ISTRING_HASH_2 (((struct dirfile
const *) key
)->name
);
398 dirfile_hash_cmp (const void *xv
, const void *yv
)
400 const struct dirfile
*x
= xv
;
401 const struct dirfile
*y
= yv
;
402 int result
= x
->length
- y
->length
;
405 return_ISTRING_COMPARE (x
->name
, y
->name
);
408 #ifndef DIRFILE_BUCKETS
409 #define DIRFILE_BUCKETS 107
412 static int dir_contents_file_exists_p (struct directory_contents
*dir
,
413 const char *filename
);
414 static struct directory
*find_directory (const char *name
);
416 /* Find the directory named NAME and return its `struct directory'. */
418 static struct directory
*
419 find_directory (const char *name
)
422 struct directory
*dir
;
423 struct directory
**dir_slot
;
424 struct directory dir_key
;
428 char fs_label
[BUFSIZ
];
429 char fs_type
[BUFSIZ
];
430 unsigned long fs_serno
;
431 unsigned long fs_flags
;
432 unsigned long fs_len
;
435 if ((*name
== '.') && (*(name
+1) == 0))
438 name
= vmsify (name
,1);
442 dir_slot
= (struct directory
**) hash_find_slot (&directories
, &dir_key
);
445 if (HASH_VACANT (dir
))
449 /* The directory was not found. Create a new entry for it. */
451 p
= name
+ strlen (name
);
452 dir
= xmalloc (sizeof (struct directory
));
453 #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
454 dir
->name
= strcache_add_len (downcase(name
), p
- name
);
456 dir
->name
= strcache_add_len (name
, p
- name
);
458 hash_insert_at (&directories
, dir
, dir_slot
);
459 /* The directory is not in the name hash table.
460 Find its device and inode numbers, and look it up by them. */
463 r
= vmsstat_dir (name
, &st
);
464 #elif defined(WINDOWS32)
466 char tem
[MAXPATHLEN
], *tstart
, *tend
;
468 /* Remove any trailing slashes. Windows32 stat fails even on
469 valid directories if they end in a slash. */
470 memcpy (tem
, name
, p
- name
+ 1);
472 if (tstart
[1] == ':')
474 for (tend
= tem
+ (p
- name
- 1);
475 tend
> tstart
&& (*tend
== '/' || *tend
== '\\');
482 EINTRLOOP (r
, stat (name
, &st
));
487 /* Couldn't stat the directory. Mark this by
488 setting the `contents' member to a nil pointer. */
493 /* Search the contents hash table; device and inode are the key. */
495 struct directory_contents
*dc
;
496 struct directory_contents
**dc_slot
;
497 struct directory_contents dc_key
;
499 dc_key
.dev
= st
.st_dev
;
501 dc_key
.path_key
= w32_path
= w32ify (name
, 1);
502 dc_key
.ctime
= st
.st_ctime
;
505 dc_key
.ino
[0] = st
.st_ino
[0];
506 dc_key
.ino
[1] = st
.st_ino
[1];
507 dc_key
.ino
[2] = st
.st_ino
[2];
509 dc_key
.ino
= st
.st_ino
;
512 dc_slot
= (struct directory_contents
**) hash_find_slot (&directory_contents
, &dc_key
);
515 if (HASH_VACANT (dc
))
517 /* Nope; this really is a directory we haven't seen before. */
519 dc
= (struct directory_contents
*)
520 xmalloc (sizeof (struct directory_contents
));
522 /* Enter it in the contents hash table. */
525 dc
->path_key
= xstrdup (w32_path
);
526 dc
->ctime
= st
.st_ctime
;
527 dc
->mtime
= st
.st_mtime
;
530 * NTFS is the only WINDOWS32 filesystem that bumps mtime
531 * on a directory when files are added/deleted from
535 if (GetVolumeInformation(w32_path
,
536 fs_label
, sizeof (fs_label
),
538 &fs_flags
, fs_type
, sizeof (fs_type
)) == FALSE
)
539 dc
->fs_flags
= FS_UNKNOWN
;
540 else if (!strcmp(fs_type
, "FAT"))
541 dc
->fs_flags
= FS_FAT
;
542 else if (!strcmp(fs_type
, "NTFS"))
543 dc
->fs_flags
= FS_NTFS
;
545 dc
->fs_flags
= FS_UNKNOWN
;
548 dc
->ino
[0] = st
.st_ino
[0];
549 dc
->ino
[1] = st
.st_ino
[1];
550 dc
->ino
[2] = st
.st_ino
[2];
554 #endif /* WINDOWS32 */
555 hash_insert_at (&directory_contents
, dc
, dc_slot
);
556 ENULLLOOP (dc
->dirstream
, opendir (name
));
557 if (dc
->dirstream
== 0)
558 /* Couldn't open the directory. Mark this by setting the
559 `files' member to a nil pointer. */
560 dc
->dirfiles
.ht_vec
= 0;
563 hash_init (&dc
->dirfiles
, DIRFILE_BUCKETS
,
564 dirfile_hash_1
, dirfile_hash_2
, dirfile_hash_cmp
);
565 /* Keep track of how many directories are open. */
567 if (open_directories
== MAX_OPEN_DIRECTORIES
)
568 /* We have too many directories open already.
569 Read the entire directory and then close it. */
570 dir_contents_file_exists_p (dc
, 0);
574 /* Point the name-hashed entry for DIR at its contents data. */
582 /* Return 1 if the name FILENAME is entered in DIR's hash table.
583 FILENAME must contain no slashes. */
586 dir_contents_file_exists_p (struct directory_contents
*dir
,
587 const char *filename
)
597 if (dir
== 0 || dir
->dirfiles
.ht_vec
== 0)
598 /* The directory could not be stat'd or opened. */
602 filename
= dosify (filename
);
605 #ifdef HAVE_CASE_INSENSITIVE_FS
606 filename
= downcase (filename
);
611 _fnlwr (filename
); /* lower case for FAT drives */
615 filename
= vmsify (filename
,0);
621 struct dirfile dirfile_key
;
623 if (*filename
== '\0')
625 /* Checking if the directory exists. */
628 dirfile_key
.name
= filename
;
629 dirfile_key
.length
= strlen (filename
);
630 df
= hash_find_item (&dir
->dirfiles
, &dirfile_key
);
632 return !df
->impossible
;
635 /* The file was not found in the hashed list.
636 Try to read the directory further. */
638 if (dir
->dirstream
== 0)
642 * Check to see if directory has changed since last read. FAT
643 * filesystems force a rehash always as mtime does not change
644 * on directories (ugh!).
648 if ((dir
->fs_flags
& FS_FAT
) != 0)
650 dir
->mtime
= time ((time_t *) 0);
653 else if (stat (dir
->path_key
, &st
) == 0 && st
.st_mtime
> dir
->mtime
)
655 /* reset date stamp to show most recent re-process. */
656 dir
->mtime
= st
.st_mtime
;
660 /* If it has been already read in, all done. */
664 /* make sure directory can still be opened; if not return. */
665 dir
->dirstream
= opendir (dir
->path_key
);
671 /* The directory has been all read in. */
677 /* Enter the file in the hash table. */
679 struct dirfile dirfile_key
;
680 struct dirfile
**dirfile_slot
;
682 ENULLLOOP (d
, readdir (dir
->dirstream
));
686 fatal (NILF
, "INTERNAL: readdir: %s\n", strerror (errno
));
690 #if defined(VMS) && defined(HAVE_DIRENT_H)
691 /* In VMS we get file versions too, which have to be stripped off */
693 char *p
= strrchr (d
->d_name
, ';');
698 if (!REAL_DIR_ENTRY (d
))
702 dirfile_key
.name
= d
->d_name
;
703 dirfile_key
.length
= len
;
704 dirfile_slot
= (struct dirfile
**) hash_find_slot (&dir
->dirfiles
, &dirfile_key
);
707 * If re-reading a directory, don't cache files that have
708 * already been discovered.
710 if (! rehash
|| HASH_VACANT (*dirfile_slot
))
713 df
= xmalloc (sizeof (struct dirfile
));
714 #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
715 df
->name
= strcache_add_len (downcase(d
->d_name
), len
);
717 df
->name
= strcache_add_len (d
->d_name
, len
);
721 hash_insert_at (&dir
->dirfiles
, df
, dirfile_slot
);
723 /* Check if the name matches the one we're searching for. */
724 if (filename
!= 0 && patheq (d
->d_name
, filename
))
728 /* If the directory has been completely read in,
729 close the stream and reset the pointer to nil. */
733 closedir (dir
->dirstream
);
739 /* Return 1 if the name FILENAME in directory DIRNAME
740 is entered in the dir hash table.
741 FILENAME must contain no slashes. */
744 dir_file_exists_p (const char *dirname
, const char *filename
)
746 return dir_contents_file_exists_p (find_directory (dirname
)->contents
,
750 /* Return 1 if the file named NAME exists. */
753 file_exists_p (const char *name
)
761 return ar_member_date (name
) != (time_t) -1;
765 dirend
= strrchr (name
, ']');
767 dirend
= strrchr (name
, ':');
769 return dir_file_exists_p ("[]", name
);
771 dirend
= strrchr (name
, '/');
772 #ifdef HAVE_DOS_PATHS
773 /* Forward and backslashes might be mixed. We need the rightmost one. */
775 const char *bslash
= strrchr(name
, '\\');
776 if (!dirend
|| bslash
> dirend
)
778 /* The case of "d:file". */
779 if (!dirend
&& name
[0] && name
[1] == ':')
782 #endif /* HAVE_DOS_PATHS */
785 return dir_file_exists_p (".", name
);
786 #else /* !VMS && !AMIGA */
787 return dir_file_exists_p ("", name
);
797 #ifdef HAVE_DOS_PATHS
798 /* d:/ and d: are *very* different... */
799 if (dirend
< name
+ 3 && name
[1] == ':' &&
800 (*dirend
== '/' || *dirend
== '\\' || *dirend
== ':'))
803 p
= alloca (dirend
- name
+ 1);
804 memcpy (p
, name
, dirend
- name
);
805 p
[dirend
- name
] = '\0';
808 return dir_file_exists_p (dirname
, slash
+ 1);
811 /* Mark FILENAME as `impossible' for `file_impossible_p'.
812 This means an attempt has been made to search for FILENAME
813 as an intermediate file, and it has failed. */
816 file_impossible (const char *filename
)
819 const char *p
= filename
;
820 struct directory
*dir
;
824 dirend
= strrchr (p
, ']');
826 dirend
= strrchr (p
, ':');
828 if (dirend
== (char *)1)
829 dir
= find_directory ("[]");
831 dirend
= strrchr (p
, '/');
832 # ifdef HAVE_DOS_PATHS
833 /* Forward and backslashes might be mixed. We need the rightmost one. */
835 const char *bslash
= strrchr(p
, '\\');
836 if (!dirend
|| bslash
> dirend
)
838 /* The case of "d:file". */
839 if (!dirend
&& p
[0] && p
[1] == ':')
842 # endif /* HAVE_DOS_PATHS */
845 dir
= find_directory ("");
846 # else /* !VMS && !AMIGA */
847 dir
= find_directory (".");
853 const char *slash
= dirend
;
859 #ifdef HAVE_DOS_PATHS
860 /* d:/ and d: are *very* different... */
861 if (dirend
< p
+ 3 && p
[1] == ':' &&
862 (*dirend
== '/' || *dirend
== '\\' || *dirend
== ':'))
865 cp
= alloca (dirend
- p
+ 1);
866 memcpy (cp
, p
, dirend
- p
);
867 cp
[dirend
- p
] = '\0';
870 dir
= find_directory (dirname
);
871 filename
= p
= slash
+ 1;
874 if (dir
->contents
== 0)
875 /* The directory could not be stat'd. We allocate a contents
876 structure for it, but leave it out of the contents hash table. */
877 dir
->contents
= xcalloc (sizeof (struct directory_contents
));
879 if (dir
->contents
->dirfiles
.ht_vec
== 0)
881 hash_init (&dir
->contents
->dirfiles
, DIRFILE_BUCKETS
,
882 dirfile_hash_1
, dirfile_hash_2
, dirfile_hash_cmp
);
885 /* Make a new entry and put it in the table. */
887 new = xmalloc (sizeof (struct dirfile
));
888 new->length
= strlen (filename
);
889 #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
890 new->name
= strcache_add_len (downcase(filename
), new->length
);
892 new->name
= strcache_add_len (filename
, new->length
);
895 hash_insert (&dir
->contents
->dirfiles
, new);
898 /* Return nonzero if FILENAME has been marked impossible. */
901 file_impossible_p (const char *filename
)
904 const char *p
= filename
;
905 struct directory_contents
*dir
;
906 struct dirfile
*dirfile
;
907 struct dirfile dirfile_key
;
910 dirend
= strrchr (filename
, ']');
912 dir
= find_directory ("[]")->contents
;
914 dirend
= strrchr (filename
, '/');
915 #ifdef HAVE_DOS_PATHS
916 /* Forward and backslashes might be mixed. We need the rightmost one. */
918 const char *bslash
= strrchr(filename
, '\\');
919 if (!dirend
|| bslash
> dirend
)
921 /* The case of "d:file". */
922 if (!dirend
&& filename
[0] && filename
[1] == ':')
923 dirend
= filename
+ 1;
925 #endif /* HAVE_DOS_PATHS */
928 dir
= find_directory ("")->contents
;
929 #else /* !VMS && !AMIGA */
930 dir
= find_directory (".")->contents
;
936 const char *slash
= dirend
;
937 if (dirend
== filename
)
942 #ifdef HAVE_DOS_PATHS
943 /* d:/ and d: are *very* different... */
944 if (dirend
< filename
+ 3 && filename
[1] == ':' &&
945 (*dirend
== '/' || *dirend
== '\\' || *dirend
== ':'))
948 cp
= alloca (dirend
- filename
+ 1);
949 memcpy (cp
, p
, dirend
- p
);
950 cp
[dirend
- p
] = '\0';
953 dir
= find_directory (dirname
)->contents
;
954 p
= filename
= slash
+ 1;
957 if (dir
== 0 || dir
->dirfiles
.ht_vec
== 0)
958 /* There are no files entered for this directory. */
962 filename
= dosify (p
);
964 #ifdef HAVE_CASE_INSENSITIVE_FS
965 filename
= downcase (p
);
968 filename
= vmsify (p
, 1);
971 dirfile_key
.name
= filename
;
972 dirfile_key
.length
= strlen (filename
);
973 dirfile
= hash_find_item (&dir
->dirfiles
, &dirfile_key
);
975 return dirfile
->impossible
;
980 /* Return the already allocated name in the
981 directory hash table that matches DIR. */
984 dir_name (const char *dir
)
986 return find_directory (dir
)->name
;
989 /* Print the data base of directories. */
992 print_dir_data_base (void)
995 unsigned int impossible
;
996 struct directory
**dir_slot
;
997 struct directory
**dir_end
;
999 puts (_("\n# Directories\n"));
1001 files
= impossible
= 0;
1003 dir_slot
= (struct directory
**) directories
.ht_vec
;
1004 dir_end
= dir_slot
+ directories
.ht_size
;
1005 for ( ; dir_slot
< dir_end
; dir_slot
++)
1007 struct directory
*dir
= *dir_slot
;
1008 if (! HASH_VACANT (dir
))
1010 if (dir
->contents
== 0)
1011 printf (_("# %s: could not be stat'd.\n"), dir
->name
);
1012 else if (dir
->contents
->dirfiles
.ht_vec
== 0)
1015 printf (_("# %s (key %s, mtime %d): could not be opened.\n"),
1016 dir
->name
, dir
->contents
->path_key
,dir
->contents
->mtime
);
1017 #else /* WINDOWS32 */
1019 printf (_("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n"),
1020 dir
->name
, dir
->contents
->dev
,
1021 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
1022 dir
->contents
->ino
[2]);
1024 printf (_("# %s (device %ld, inode %ld): could not be opened.\n"),
1025 dir
->name
, (long int) dir
->contents
->dev
,
1026 (long int) dir
->contents
->ino
);
1028 #endif /* WINDOWS32 */
1033 unsigned int im
= 0;
1034 struct dirfile
**files_slot
;
1035 struct dirfile
**files_end
;
1037 files_slot
= (struct dirfile
**) dir
->contents
->dirfiles
.ht_vec
;
1038 files_end
= files_slot
+ dir
->contents
->dirfiles
.ht_size
;
1039 for ( ; files_slot
< files_end
; files_slot
++)
1041 struct dirfile
*df
= *files_slot
;
1042 if (! HASH_VACANT (df
))
1051 printf (_("# %s (key %s, mtime %d): "),
1052 dir
->name
, dir
->contents
->path_key
, dir
->contents
->mtime
);
1053 #else /* WINDOWS32 */
1055 printf (_("# %s (device %d, inode [%d,%d,%d]): "),
1056 dir
->name
, dir
->contents
->dev
,
1057 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
1058 dir
->contents
->ino
[2]);
1060 printf (_("# %s (device %ld, inode %ld): "),
1062 (long)dir
->contents
->dev
, (long)dir
->contents
->ino
);
1064 #endif /* WINDOWS32 */
1066 fputs (_("No"), stdout
);
1069 fputs (_(" files, "), stdout
);
1071 fputs (_("no"), stdout
);
1074 fputs (_(" impossibilities"), stdout
);
1075 if (dir
->contents
->dirstream
== 0)
1078 puts (_(" so far."));
1085 fputs ("\n# ", stdout
);
1087 fputs (_("No"), stdout
);
1089 printf ("%u", files
);
1090 fputs (_(" files, "), stdout
);
1091 if (impossible
== 0)
1092 fputs (_("no"), stdout
);
1094 printf ("%u", impossible
);
1095 printf (_(" impossibilities in %lu directories.\n"), directories
.ht_fill
);
1098 /* Hooks for globbing. */
1102 /* Structure describing state of iterating through a directory hash table. */
1106 struct directory_contents
*contents
; /* The directory being read. */
1107 struct dirfile
**dirfile_slot
; /* Current slot in table. */
1110 /* Forward declarations. */
1111 static __ptr_t
open_dirstream (const char *);
1112 static struct dirent
*read_dirstream (__ptr_t
);
1115 open_dirstream (const char *directory
)
1117 struct dirstream
*new;
1118 struct directory
*dir
= find_directory (directory
);
1120 if (dir
->contents
== 0 || dir
->contents
->dirfiles
.ht_vec
== 0)
1121 /* DIR->contents is nil if the directory could not be stat'd.
1122 DIR->contents->dirfiles is nil if it could not be opened. */
1125 /* Read all the contents of the directory now. There is no benefit
1126 in being lazy, since glob will want to see every file anyway. */
1128 dir_contents_file_exists_p (dir
->contents
, 0);
1130 new = xmalloc (sizeof (struct dirstream
));
1131 new->contents
= dir
->contents
;
1132 new->dirfile_slot
= (struct dirfile
**) new->contents
->dirfiles
.ht_vec
;
1134 return (__ptr_t
) new;
1137 static struct dirent
*
1138 read_dirstream (__ptr_t stream
)
1141 static unsigned int bufsz
;
1143 struct dirstream
*const ds
= (struct dirstream
*) stream
;
1144 struct directory_contents
*dc
= ds
->contents
;
1145 struct dirfile
**dirfile_end
= (struct dirfile
**) dc
->dirfiles
.ht_vec
+ dc
->dirfiles
.ht_size
;
1147 while (ds
->dirfile_slot
< dirfile_end
)
1149 struct dirfile
*df
= *ds
->dirfile_slot
++;
1150 if (! HASH_VACANT (df
) && !df
->impossible
)
1152 /* The glob interface wants a `struct dirent', so mock one up. */
1154 unsigned int len
= df
->length
+ 1;
1155 unsigned int sz
= sizeof (*d
) - sizeof (d
->d_name
) + len
;
1161 buf
= xrealloc (buf
, bufsz
);
1163 d
= (struct dirent
*) buf
;
1165 # if __MINGW32_MAJOR_VERSION < 3 || (__MINGW32_MAJOR_VERSION == 3 && \
1166 __MINGW32_MINOR_VERSION == 0)
1167 d
->d_name
= xmalloc(len
);
1171 #ifdef _DIRENT_HAVE_D_NAMLEN
1172 d
->d_namlen
= len
- 1;
1174 #ifdef _DIRENT_HAVE_D_TYPE
1175 d
->d_type
= DT_UNKNOWN
;
1177 memcpy (d
->d_name
, df
->name
, len
);
1192 /* On 64 bit ReliantUNIX (5.44 and above) in LFS mode, stat() is actually a
1193 * macro for stat64(). If stat is a macro, make a local wrapper function to
1198 int stat (const char *path
, struct stat
*sbuf
);
1200 # define local_stat stat
1203 local_stat (const char *path
, struct stat
*buf
)
1207 EINTRLOOP (e
, stat (path
, buf
));
1213 dir_setup_glob (glob_t
*gl
)
1215 gl
->gl_opendir
= open_dirstream
;
1216 gl
->gl_readdir
= read_dirstream
;
1217 gl
->gl_closedir
= ansi_free
;
1218 gl
->gl_stat
= local_stat
;
1219 /* We don't bother setting gl_lstat, since glob never calls it.
1220 The slot is only there for compatibility with 4.4 BSD. */
1224 hash_init_directories (void)
1226 hash_init (&directories
, DIRECTORY_BUCKETS
,
1227 directory_hash_1
, directory_hash_2
, directory_hash_cmp
);
1228 hash_init (&directory_contents
, DIRECTORY_BUCKETS
,
1229 directory_contents_hash_1
, directory_contents_hash_2
,
1230 directory_contents_hash_cmp
);