1 /* Directory hashing for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 # define NAMLEN(dirent) strlen((dirent)->d_name)
25 # define dirent direct
26 # define NAMLEN(dirent) (dirent)->d_namlen
27 # ifdef HAVE_SYS_NDIR_H
28 # include <sys/ndir.h>
30 # ifdef HAVE_SYS_DIR_H
38 # endif /* HAVE_VMSDIR_H */
41 /* In GNU systems, <dirent.h> defines this macro for us. */
44 #define NAMLEN(d) _D_NAMLEN(d)
47 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
48 /* Posix does not require that the d_ino field be present, and some
49 systems do not provide it. */
50 #define REAL_DIR_ENTRY(dp) 1
51 #define FAKE_DIR_ENTRY(dp)
53 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
54 #define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
64 static char dos_filename
[14];
71 if (strpbrk (filename
, "\"*+,;<=>?[\\]|") != 0)
76 /* First, transform the name part. */
77 for (i
= 0; *filename
!= '\0' && i
< 8 && *filename
!= '.'; ++i
)
78 *df
++ = tolower (*filename
++);
80 /* Now skip to the next dot. */
81 while (*filename
!= '\0' && *filename
!= '.')
83 if (*filename
!= '\0')
86 for (i
= 0; *filename
!= '\0' && i
< 3 && *filename
!= '.'; ++i
)
87 *df
++ = tolower (*filename
++);
90 /* Look for more dots. */
91 while (*filename
!= '\0' && *filename
!= '.')
98 #endif /* __MSDOS__ */
111 h
= (h
<< 4) + *name
++;
122 /* fake stat entry for a directory */
124 vmsstat_dir (name
, st
)
132 dir
= opendir (name
);
136 s
= strchr (name
, ':'); /* find device */
140 st
->st_dev
= (char *)vms_hash (name
);
148 st
->st_ino
[0] = h
& 0xff;
149 st
->st_ino
[1] = h
& 0xff00;
150 st
->st_ino
[2] = h
>> 16;
156 /* Hash table of directories. */
158 #ifndef DIRECTORY_BUCKETS
159 #define DIRECTORY_BUCKETS 199
162 struct directory_contents
164 struct directory_contents
*next
;
166 dev_t dev
; /* Device and inode numbers of this dir. */
172 struct dirfile
**files
; /* Files in this directory. */
173 DIR *dirstream
; /* Stream reading this directory. */
176 /* Table of directory contents hashed by device and inode number. */
177 static struct directory_contents
*directories_contents
[DIRECTORY_BUCKETS
];
181 struct directory
*next
;
183 char *name
; /* Name of the directory. */
185 /* The directory's contents. This data may be shared by several
186 entries in the hash table, which refer to the same directory
187 (identified uniquely by `dev' and `ino') under different names. */
188 struct directory_contents
*contents
;
191 /* Table of directories hashed by name. */
192 static struct directory
*directories
[DIRECTORY_BUCKETS
];
195 /* Never have more than this many directories open at once. */
197 #define MAX_OPEN_DIRECTORIES 10
199 static unsigned int open_directories
= 0;
202 /* Hash table of files in each directory. */
206 struct dirfile
*next
;
207 char *name
; /* Name of the file. */
208 char impossible
; /* This file is impossible. */
211 #ifndef DIRFILE_BUCKETS
212 #define DIRFILE_BUCKETS 107
215 static int dir_contents_file_exists_p
PARAMS ((struct directory_contents
*dir
, char *filename
));
216 static struct directory
*find_directory
PARAMS ((char *name
));
218 /* Find the directory named NAME and return its `struct directory'. */
220 static struct directory
*
221 find_directory (name
)
224 register unsigned int hash
= 0;
226 register struct directory
*dir
;
228 if ((*name
== '.') && (*(name
+1) == 0))
231 name
= vmsify (name
,1);
234 for (p
= name
; *p
!= '\0'; ++p
)
236 hash
%= DIRECTORY_BUCKETS
;
238 for (dir
= directories
[hash
]; dir
!= 0; dir
= dir
->next
)
239 if (streq (dir
->name
, name
))
246 /* The directory was not found. Create a new entry for it. */
248 dir
= (struct directory
*) xmalloc (sizeof (struct directory
));
249 dir
->next
= directories
[hash
];
250 directories
[hash
] = dir
;
251 dir
->name
= savestring (name
, p
- name
);
253 /* The directory is not in the name hash table.
254 Find its device and inode numbers, and look it up by them. */
257 if (vmsstat_dir (name
, &st
) < 0)
259 if (stat (name
, &st
) < 0)
262 /* Couldn't stat the directory. Mark this by
263 setting the `contents' member to a nil pointer. */
268 /* Search the contents hash table; device and inode are the key. */
270 struct directory_contents
*dc
;
273 hash
= ((unsigned int) st
.st_dev
<< 16)
274 | ((unsigned int) st
.st_ino
[0]
275 + (unsigned int) st
.st_ino
[1]
276 + (unsigned int) st
.st_ino
[2]);
278 hash
= ((unsigned int) st
.st_dev
<< 16) | (unsigned int) st
.st_ino
;
280 hash
%= DIRECTORY_BUCKETS
;
282 for (dc
= directories_contents
[hash
]; dc
!= 0; dc
= dc
->next
)
283 if (dc
->dev
== st
.st_dev
285 && dc
->ino
[0] == st
.st_ino
[0]
286 && dc
->ino
[1] == st
.st_ino
[1]
287 && dc
->ino
[2] == st
.st_ino
[2])
289 && dc
->ino
== st
.st_ino
)
295 /* Nope; this really is a directory we haven't seen before. */
297 dc
= (struct directory_contents
*)
298 xmalloc (sizeof (struct directory_contents
));
300 /* Enter it in the contents hash table. */
303 dc
->ino
[0] = st
.st_ino
[0];
304 dc
->ino
[1] = st
.st_ino
[1];
305 dc
->ino
[2] = st
.st_ino
[2];
309 dc
->next
= directories_contents
[hash
];
310 directories_contents
[hash
] = dc
;
312 dc
->dirstream
= opendir (name
);
313 if (dc
->dirstream
== 0)
315 /* Couldn't open the directory. Mark this by
316 setting the `files' member to a nil pointer. */
321 /* Allocate an array of buckets for files and zero it. */
322 dc
->files
= (struct dirfile
**)
323 xmalloc (sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
324 bzero ((char *) dc
->files
,
325 sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
327 /* Keep track of how many directories are open. */
329 if (open_directories
== MAX_OPEN_DIRECTORIES
)
330 /* We have too many directories open already.
331 Read the entire directory and then close it. */
332 (void) dir_contents_file_exists_p (dc
, (char *) 0);
336 /* Point the name-hashed entry for DIR at its contents data. */
344 /* Return 1 if the name FILENAME is entered in DIR's hash table.
345 FILENAME must contain no slashes. */
348 dir_contents_file_exists_p (dir
, filename
)
349 register struct directory_contents
*dir
;
350 register char *filename
;
352 register unsigned int hash
;
354 register struct dirfile
*df
;
355 register struct dirent
*d
;
357 if (dir
== 0 || dir
->files
== 0)
359 /* The directory could not be stat'd or opened. */
363 filename
= dosify (filename
);
367 filename
= vmsify (filename
,0);
373 if (*filename
== '\0')
375 /* Checking if the directory exists. */
379 for (p
= filename
; *p
!= '\0'; ++p
)
381 hash
%= DIRFILE_BUCKETS
;
383 /* Search the list of hashed files. */
385 for (df
= dir
->files
[hash
]; df
!= 0; df
= df
->next
)
387 if (streq (df
->name
, filename
))
389 return !df
->impossible
;
394 /* The file was not found in the hashed list.
395 Try to read the directory further. */
397 if (dir
->dirstream
== 0)
399 /* The directory has been all read in. */
403 while ((d
= readdir (dir
->dirstream
)) != 0)
405 /* Enter the file in the hash table. */
406 register unsigned int newhash
= 0;
408 register unsigned int i
;
410 if (!REAL_DIR_ENTRY (d
))
414 for (i
= 0; i
< len
; ++i
)
415 HASH (newhash
, d
->d_name
[i
]);
416 newhash
%= DIRFILE_BUCKETS
;
418 df
= (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
419 df
->next
= dir
->files
[newhash
];
420 dir
->files
[newhash
] = df
;
421 df
->name
= savestring (d
->d_name
, len
);
423 /* Check if the name matches the one we're searching for. */
425 && newhash
== hash
&& streq (d
->d_name
, filename
))
431 /* If the directory has been completely read in,
432 close the stream and reset the pointer to nil. */
436 closedir (dir
->dirstream
);
442 /* Return 1 if the name FILENAME in directory DIRNAME
443 is entered in the dir hash table.
444 FILENAME must contain no slashes. */
447 dir_file_exists_p (dirname
, filename
)
448 register char *dirname
;
449 register char *filename
;
451 return dir_contents_file_exists_p (find_directory (dirname
)->contents
,
455 /* Return 1 if the file named NAME exists. */
466 return ar_member_date (name
) != (time_t) -1;
470 dirend
= rindex (name
, ']');
472 if (dirend
== (char *)1)
473 return dir_file_exists_p ("[]", name
);
475 dirend
= rindex (name
, '/');
477 return dir_file_exists_p (".", name
);
480 dirname
= (char *) alloca (dirend
- name
+ 1);
481 bcopy (name
, dirname
, dirend
- name
);
482 dirname
[dirend
- name
] = '\0';
483 return dir_file_exists_p (dirname
, dirend
+ 1);
486 /* Mark FILENAME as `impossible' for `file_impossible_p'.
487 This means an attempt has been made to search for FILENAME
488 as an intermediate file, and it has failed. */
491 file_impossible (filename
)
492 register char *filename
;
495 register char *p
= filename
;
496 register unsigned int hash
;
497 register struct directory
*dir
;
498 register struct dirfile
*new;
501 dirend
= rindex (p
, ']');
503 if (dirend
== (char *)1)
504 dir
= find_directory ("[]");
506 dirend
= rindex (p
, '/');
508 dir
= find_directory (".");
512 char *dirname
= (char *) alloca (dirend
- p
+ 1);
513 bcopy (p
, dirname
, dirend
- p
);
514 dirname
[dirend
- p
] = '\0';
515 dir
= find_directory (dirname
);
516 filename
= p
= dirend
+ 1;
519 for (hash
= 0; *p
!= '\0'; ++p
)
521 hash
%= DIRFILE_BUCKETS
;
523 if (dir
->contents
== 0)
525 /* The directory could not be stat'd. We allocate a contents
526 structure for it, but leave it out of the contents hash table. */
527 dir
->contents
= (struct directory_contents
*)
528 xmalloc (sizeof (struct directory_contents
));
530 dir
->contents
->dev
= 0;
531 dir
->contents
->ino
[0] = dir
->contents
->ino
[1] =
532 dir
->contents
->ino
[2] = 0;
534 dir
->contents
->dev
= dir
->contents
->ino
= 0;
536 dir
->contents
->files
= 0;
537 dir
->contents
->dirstream
= 0;
540 if (dir
->contents
->files
== 0)
542 /* The directory was not opened; we must allocate the hash buckets. */
543 dir
->contents
->files
= (struct dirfile
**)
544 xmalloc (sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
545 bzero ((char *) dir
->contents
->files
,
546 sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
549 /* Make a new entry and put it in the table. */
551 new = (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
552 new->next
= dir
->contents
->files
[hash
];
553 dir
->contents
->files
[hash
] = new;
554 new->name
= savestring (filename
, strlen (filename
));
558 /* Return nonzero if FILENAME has been marked impossible. */
561 file_impossible_p (filename
)
565 register char *p
= filename
;
566 register unsigned int hash
;
567 register struct directory_contents
*dir
;
568 register struct dirfile
*next
;
571 dirend
= rindex (filename
, ']');
573 dir
= find_directory ("[]")->contents
;
575 dirend
= rindex (filename
, '/');
577 dir
= find_directory (".")->contents
;
581 char *dirname
= (char *) alloca (dirend
- filename
+ 1);
582 bcopy (p
, dirname
, dirend
- p
);
583 dirname
[dirend
- p
] = '\0';
584 dir
= find_directory (dirname
)->contents
;
585 p
= filename
= dirend
+ 1;
588 if (dir
== 0 || dir
->files
== 0)
589 /* There are no files entered for this directory. */
593 p
= filename
= dosify (p
);
596 p
= filename
= vmsify (p
, 1);
599 for (hash
= 0; *p
!= '\0'; ++p
)
601 hash
%= DIRFILE_BUCKETS
;
603 for (next
= dir
->files
[hash
]; next
!= 0; next
= next
->next
)
604 if (streq (filename
, next
->name
))
605 return next
->impossible
;
610 /* Return the already allocated name in the
611 directory hash table that matches DIR. */
617 return find_directory (dir
)->name
;
620 /* Print the data base of directories. */
623 print_dir_data_base ()
625 register unsigned int i
, dirs
, files
, impossible
;
626 register struct directory
*dir
;
628 puts ("\n# Directories\n");
630 dirs
= files
= impossible
= 0;
631 for (i
= 0; i
< DIRECTORY_BUCKETS
; ++i
)
632 for (dir
= directories
[i
]; dir
!= 0; dir
= dir
->next
)
635 if (dir
->contents
== 0)
636 printf ("# %s: could not be stat'd.\n", dir
->name
);
637 else if (dir
->contents
->files
== 0)
639 printf ("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n",
640 dir
->name
, dir
->contents
->dev
,
641 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
642 dir
->contents
->ino
[2]);
644 printf ("# %s (device %d, inode %d): could not be opened.\n",
645 dir
->name
, dir
->contents
->dev
, dir
->contents
->ino
);
649 register unsigned int f
= 0, im
= 0;
650 register unsigned int j
;
651 register struct dirfile
*df
;
652 for (j
= 0; j
< DIRFILE_BUCKETS
; ++j
)
653 for (df
= dir
->contents
->files
[j
]; df
!= 0; df
= df
->next
)
659 printf ("# %s (device %d, inode [%d,%d,%d]): ",
660 dir
->name
, dir
->contents
->dev
,
661 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
662 dir
->contents
->ino
[2]);
664 printf ("# %s (device %d, inode %d): ",
665 dir
->name
, dir
->contents
->dev
, dir
->contents
->ino
);
668 fputs ("No", stdout
);
671 fputs (" files, ", stdout
);
673 fputs ("no", stdout
);
676 fputs (" impossibilities", stdout
);
677 if (dir
->contents
->dirstream
== 0)
686 fputs ("\n# ", stdout
);
688 fputs ("No", stdout
);
690 printf ("%u", files
);
691 fputs (" files, ", stdout
);
693 fputs ("no", stdout
);
695 printf ("%u", impossible
);
696 printf (" impossibilities in %u directories.\n", dirs
);
699 /* Hooks for globbing. */
703 /* Structure describing state of iterating through a directory hash table. */
707 struct directory_contents
*contents
; /* The directory being read. */
709 unsigned int bucket
; /* Current hash bucket. */
710 struct dirfile
*elt
; /* Current elt in bucket. */
713 /* Forward declarations. */
714 static __ptr_t open_dirstream
PARAMS ((const char *));
715 static struct dirent
*read_dirstream
PARAMS ((__ptr_t
));
718 open_dirstream (directory
)
719 const char *directory
;
721 struct dirstream
*new;
722 struct directory
*dir
= find_directory ((char *)directory
);
724 if (dir
->contents
== 0 || dir
->contents
->files
== 0)
725 /* DIR->contents is nil if the directory could not be stat'd.
726 DIR->contents->files is nil if it could not be opened. */
729 /* Read all the contents of the directory now. There is no benefit
730 in being lazy, since glob will want to see every file anyway. */
732 (void) dir_contents_file_exists_p (dir
->contents
, (char *) 0);
734 new = (struct dirstream
*) xmalloc (sizeof (struct dirstream
));
735 new->contents
= dir
->contents
;
737 new->elt
= new->contents
->files
[0];
739 return (__ptr_t
) new;
742 static struct dirent
*
743 read_dirstream (stream
)
746 struct dirstream
*const ds
= (struct dirstream
*) stream
;
747 register struct dirfile
*df
;
749 static unsigned int bufsz
;
751 while (ds
->bucket
< DIRFILE_BUCKETS
)
753 while ((df
= ds
->elt
) != 0)
758 /* The glob interface wants a `struct dirent',
761 unsigned int len
= strlen (df
->name
) + 1;
762 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
767 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
768 bufsz
= sizeof *d
- sizeof d
->d_name
+ len
;
769 buf
= xmalloc (bufsz
);
771 d
= (struct dirent
*) buf
;
773 #ifdef _DIRENT_HAVE_D_NAMLEN
774 d
->d_namlen
= len
- 1;
776 memcpy (d
->d_name
, df
->name
, len
);
780 if (++ds
->bucket
== DIRFILE_BUCKETS
)
782 ds
->elt
= ds
->contents
->files
[ds
->bucket
];
794 /* Bogus sunos4 compiler complains (!) about & before functions. */
795 gl
->gl_opendir
= open_dirstream
;
796 gl
->gl_readdir
= read_dirstream
;
797 gl
->gl_closedir
= free
;
799 /* We don't bother setting gl_lstat, since glob never calls it.
800 The slot is only there for compatibility with 4.4 BSD. */