1 /* Directory hashing for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95 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. */
21 #if defined (POSIX) || defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
23 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
25 #endif /* GNU C library. */
26 #else /* Not POSIX or HAVE_DIRENT_H. */
29 #ifdef HAVE_SYS_NDIR_H
31 #endif /* HAVE_SYS_NDIR_H */
34 #endif /* HAVE_SYS_DIR_H */
37 #endif /* HAVE_NDIR_H */
38 #endif /* POSIX or HAVE_DIRENT_H or __GNU_LIBRARY__. */
40 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
41 /* Posix does not require that the d_ino field be present, and some
42 systems do not provide it. */
43 #define REAL_DIR_ENTRY(dp) 1
44 #define FAKE_DIR_ENTRY(dp)
46 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
47 #define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
57 static char dos_filename
[14];
64 if (strpbrk (filename
, "\"*+,;<=>?[\\]|") != 0)
69 /* First, transform the name part. */
70 for (i
= 0; *filename
!= '\0' && i
< 8 && *filename
!= '.'; ++i
)
71 *df
++ = tolower (*filename
++);
73 /* Now skip to the next dot. */
74 while (*filename
!= '\0' && *filename
!= '.')
76 if (*filename
!= '\0')
79 for (i
= 0; *filename
!= '\0' && i
< 3 && *filename
!= '.'; ++i
)
80 *df
++ = tolower (*filename
++);
83 /* Look for more dots. */
84 while (*filename
!= '\0' && *filename
!= '.')
93 /* Hash table of directories. */
95 #ifndef DIRECTORY_BUCKETS
96 #define DIRECTORY_BUCKETS 199
99 struct directory_contents
101 struct directory_contents
*next
;
103 int dev
, ino
; /* Device and inode numbers of this dir. */
105 struct dirfile
**files
; /* Files in this directory. */
106 DIR *dirstream
; /* Stream reading this directory. */
109 /* Table of directory contents hashed by device and inode number. */
110 static struct directory_contents
*directories_contents
[DIRECTORY_BUCKETS
];
114 struct directory
*next
;
116 char *name
; /* Name of the directory. */
118 /* The directory's contents. This data may be shared by several
119 entries in the hash table, which refer to the same directory
120 (identified uniquely by `dev' and `ino') under different names. */
121 struct directory_contents
*contents
;
124 /* Table of directories hashed by name. */
125 static struct directory
*directories
[DIRECTORY_BUCKETS
];
128 /* Never have more than this many directories open at once. */
130 #define MAX_OPEN_DIRECTORIES 10
132 static unsigned int open_directories
= 0;
135 /* Hash table of files in each directory. */
139 struct dirfile
*next
;
140 char *name
; /* Name of the file. */
141 char impossible
; /* This file is impossible. */
144 #ifndef DIRFILE_BUCKETS
145 #define DIRFILE_BUCKETS 107
148 static int dir_contents_file_exists_p ();
150 /* Find the directory named NAME and return its `struct directory'. */
152 static struct directory
*
153 find_directory (name
)
156 register unsigned int hash
= 0;
158 register struct directory
*dir
;
160 for (p
= name
; *p
!= '\0'; ++p
)
162 hash
%= DIRECTORY_BUCKETS
;
164 for (dir
= directories
[hash
]; dir
!= 0; dir
= dir
->next
)
165 if (streq (dir
->name
, name
))
172 /* The directory was not found. Create a new entry for it. */
174 dir
= (struct directory
*) xmalloc (sizeof (struct directory
));
175 dir
->next
= directories
[hash
];
176 directories
[hash
] = dir
;
177 dir
->name
= savestring (name
, p
- name
);
179 /* The directory is not in the name hash table.
180 Find its device and inode numbers, and look it up by them. */
182 if (stat (name
, &st
) < 0)
183 /* Couldn't stat the directory. Mark this by
184 setting the `contents' member to a nil pointer. */
188 /* Search the contents hash table; device and inode are the key. */
190 struct directory_contents
*dc
;
192 hash
= ((unsigned int) st
.st_dev
<< 16) | (unsigned int) st
.st_ino
;
193 hash
%= DIRECTORY_BUCKETS
;
195 for (dc
= directories_contents
[hash
]; dc
!= 0; dc
= dc
->next
)
196 if (dc
->dev
== st
.st_dev
&& dc
->ino
== st
.st_ino
)
201 /* Nope; this really is a directory we haven't seen before. */
203 dc
= (struct directory_contents
*)
204 xmalloc (sizeof (struct directory_contents
));
206 /* Enter it in the contents hash table. */
209 dc
->next
= directories_contents
[hash
];
210 directories_contents
[hash
] = dc
;
212 dc
->dirstream
= opendir (name
);
213 if (dc
->dirstream
== 0)
214 /* Couldn't open the directory. Mark this by
215 setting the `files' member to a nil pointer. */
219 /* Allocate an array of buckets for files and zero it. */
220 dc
->files
= (struct dirfile
**)
221 xmalloc (sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
222 bzero ((char *) dc
->files
,
223 sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
225 /* Keep track of how many directories are open. */
227 if (open_directories
== MAX_OPEN_DIRECTORIES
)
228 /* We have too many directories open already.
229 Read the entire directory and then close it. */
230 (void) dir_contents_file_exists_p (dc
, (char *) 0);
234 /* Point the name-hashed entry for DIR at its contents data. */
242 /* Return 1 if the name FILENAME is entered in DIR's hash table.
243 FILENAME must contain no slashes. */
246 dir_contents_file_exists_p (dir
, filename
)
247 register struct directory_contents
*dir
;
248 register char *filename
;
250 register unsigned int hash
;
252 register struct dirfile
*df
;
253 register struct dirent
*d
;
255 if (dir
== 0 || dir
->files
== 0)
256 /* The directory could not be stat'd or opened. */
260 filename
= dosify (filename
);
266 if (*filename
== '\0')
267 /* Checking if the directory exists. */
270 for (p
= filename
; *p
!= '\0'; ++p
)
272 hash
%= DIRFILE_BUCKETS
;
274 /* Search the list of hashed files. */
276 for (df
= dir
->files
[hash
]; df
!= 0; df
= df
->next
)
277 if (streq (df
->name
, filename
))
278 return !df
->impossible
;
281 /* The file was not found in the hashed list.
282 Try to read the directory further. */
284 if (dir
->dirstream
== 0)
285 /* The directory has been all read in. */
288 while ((d
= readdir (dir
->dirstream
)) != 0)
290 /* Enter the file in the hash table. */
291 register unsigned int newhash
= 0;
293 register unsigned int i
;
295 if (!REAL_DIR_ENTRY (d
))
300 while (d
->d_name
[len
- 1] == '\0')
303 len
= strlen (d
->d_name
);
306 for (i
= 0; i
< len
; ++i
)
307 HASH (newhash
, d
->d_name
[i
]);
308 newhash
%= DIRFILE_BUCKETS
;
310 df
= (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
311 df
->next
= dir
->files
[newhash
];
312 dir
->files
[newhash
] = df
;
313 df
->name
= savestring (d
->d_name
, len
);
316 /* Check if the name matches the one we're searching for. */
318 && newhash
== hash
&& streq (d
->d_name
, filename
))
322 /* If the directory has been completely read in,
323 close the stream and reset the pointer to nil. */
327 closedir (dir
->dirstream
);
334 /* Return 1 if the name FILENAME in directory DIRNAME
335 is entered in the dir hash table.
336 FILENAME must contain no slashes. */
339 dir_file_exists_p (dirname
, filename
)
340 register char *dirname
;
341 register char *filename
;
343 return dir_contents_file_exists_p (find_directory (dirname
)->contents
,
347 /* Return 1 if the file named NAME exists. */
358 return ar_member_date (name
) != (time_t) -1;
361 dirend
= rindex (name
, '/');
363 return dir_file_exists_p (".", name
);
365 dirname
= (char *) alloca (dirend
- name
+ 1);
366 bcopy (name
, dirname
, dirend
- name
);
367 dirname
[dirend
- name
] = '\0';
368 return dir_file_exists_p (dirname
, dirend
+ 1);
371 /* Mark FILENAME as `impossible' for `file_impossible_p'.
372 This means an attempt has been made to search for FILENAME
373 as an intermediate file, and it has failed. */
376 file_impossible (filename
)
377 register char *filename
;
380 register char *p
= filename
;
381 register unsigned int hash
;
382 register struct directory
*dir
;
383 register struct dirfile
*new;
385 dirend
= rindex (p
, '/');
387 dir
= find_directory (".");
390 char *dirname
= (char *) alloca (dirend
- p
+ 1);
391 bcopy (p
, dirname
, dirend
- p
);
392 dirname
[dirend
- p
] = '\0';
393 dir
= find_directory (dirname
);
394 filename
= p
= dirend
+ 1;
397 for (hash
= 0; *p
!= '\0'; ++p
)
399 hash
%= DIRFILE_BUCKETS
;
401 if (dir
->contents
== 0)
403 /* The directory could not be stat'd. We allocate a contents
404 structure for it, but leave it out of the contents hash table. */
405 dir
->contents
= (struct directory_contents
*)
406 xmalloc (sizeof (struct directory_contents
));
407 dir
->contents
->dev
= dir
->contents
->ino
= 0;
408 dir
->contents
->files
= 0;
409 dir
->contents
->dirstream
= 0;
412 if (dir
->contents
->files
== 0)
414 /* The directory was not opened; we must allocate the hash buckets. */
415 dir
->contents
->files
= (struct dirfile
**)
416 xmalloc (sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
417 bzero ((char *) dir
->contents
->files
,
418 sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
421 /* Make a new entry and put it in the table. */
423 new = (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
424 new->next
= dir
->contents
->files
[hash
];
425 dir
->contents
->files
[hash
] = new;
426 new->name
= savestring (filename
, strlen (filename
));
430 /* Return nonzero if FILENAME has been marked impossible. */
433 file_impossible_p (filename
)
437 register char *p
= filename
;
438 register unsigned int hash
;
439 register struct directory_contents
*dir
;
440 register struct dirfile
*next
;
442 dirend
= rindex (filename
, '/');
444 dir
= find_directory (".")->contents
;
447 char *dirname
= (char *) alloca (dirend
- filename
+ 1);
448 bcopy (p
, dirname
, dirend
- p
);
449 dirname
[dirend
- p
] = '\0';
450 dir
= find_directory (dirname
)->contents
;
451 p
= filename
= dirend
+ 1;
454 if (dir
== 0 || dir
->files
== 0)
455 /* There are no files entered for this directory. */
459 p
= filename
= dosify (p
);
462 for (hash
= 0; *p
!= '\0'; ++p
)
464 hash
%= DIRFILE_BUCKETS
;
466 for (next
= dir
->files
[hash
]; next
!= 0; next
= next
->next
)
467 if (streq (filename
, next
->name
))
468 return next
->impossible
;
473 /* Return the already allocated name in the
474 directory hash table that matches DIR. */
480 return find_directory (dir
)->name
;
483 /* Print the data base of directories. */
486 print_dir_data_base ()
488 register unsigned int i
, dirs
, files
, impossible
;
489 register struct directory
*dir
;
491 puts ("\n# Directories\n");
493 dirs
= files
= impossible
= 0;
494 for (i
= 0; i
< DIRECTORY_BUCKETS
; ++i
)
495 for (dir
= directories
[i
]; dir
!= 0; dir
= dir
->next
)
498 if (dir
->contents
== 0)
499 printf ("# %s: could not be stat'd.\n", dir
->name
);
500 else if (dir
->contents
->files
== 0)
501 printf ("# %s (device %d, inode %d): could not be opened.\n",
502 dir
->name
, dir
->contents
->dev
, dir
->contents
->ino
);
505 register unsigned int f
= 0, im
= 0;
506 register unsigned int j
;
507 register struct dirfile
*df
;
508 for (j
= 0; j
< DIRFILE_BUCKETS
; ++j
)
509 for (df
= dir
->contents
->files
[j
]; df
!= 0; df
= df
->next
)
514 printf ("# %s (device %d, inode %d): ",
515 dir
->name
, dir
->contents
->dev
, dir
->contents
->ino
);
517 fputs ("No", stdout
);
520 fputs (" files, ", stdout
);
522 fputs ("no", stdout
);
525 fputs (" impossibilities", stdout
);
526 if (dir
->contents
->dirstream
== 0)
535 fputs ("\n# ", stdout
);
537 fputs ("No", stdout
);
539 printf ("%u", files
);
540 fputs (" files, ", stdout
);
542 fputs ("no", stdout
);
544 printf ("%u", impossible
);
545 printf (" impossibilities in %u directories.\n", dirs
);
548 /* Hooks for globbing. */
552 /* Structure describing state of iterating through a directory hash table. */
556 struct directory_contents
*contents
; /* The directory being read. */
558 unsigned int bucket
; /* Current hash bucket. */
559 struct dirfile
*elt
; /* Current elt in bucket. */
562 /* Forward declarations. */
563 static __ptr_t open_dirstream
__P ((const char *));
564 static struct dirent
*read_dirstream
__P ((__ptr_t
));
567 open_dirstream (directory
)
568 const char *directory
;
570 struct dirstream
*new;
571 struct directory
*dir
= find_directory (directory
);
573 if (dir
->contents
== 0 || dir
->contents
->files
== 0)
574 /* DIR->contents is nil if the directory could not be stat'd.
575 DIR->contents->files is nil if it could not be opened. */
578 /* Read all the contents of the directory now. There is no benefit
579 in being lazy, since glob will want to see every file anyway. */
581 (void) dir_contents_file_exists_p (dir
->contents
, (char *) 0);
583 new = (struct dirstream
*) xmalloc (sizeof (struct dirstream
));
584 new->contents
= dir
->contents
;
586 new->elt
= new->contents
->files
[0];
588 return (__ptr_t
) new;
591 static struct dirent
*
592 read_dirstream (stream
)
595 struct dirstream
*const ds
= (struct dirstream
*) stream
;
596 register struct dirfile
*df
;
598 static unsigned int bufsz
;
600 while (ds
->bucket
< DIRFILE_BUCKETS
)
602 while ((df
= ds
->elt
) != 0)
607 /* The glob interface wants a `struct dirent',
610 unsigned int len
= strlen (df
->name
) + 1;
611 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
616 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
617 bufsz
= sizeof *d
- sizeof d
->d_name
+ len
;
618 buf
= xmalloc (bufsz
);
620 d
= (struct dirent
*) buf
;
623 d
->d_namlen
= len
- 1;
625 memcpy (d
->d_name
, df
->name
, len
);
629 if (++ds
->bucket
== DIRFILE_BUCKETS
)
631 ds
->elt
= ds
->contents
->files
[ds
->bucket
];
641 extern int lstat (), stat ();
646 gl
->gl_opendir
= &open_dirstream
;
647 gl
->gl_readdir
= &read_dirstream
;
648 gl
->gl_closedir
= &free
;
649 gl
->gl_lstat
= &lstat
;