Formerly file.c.~24~
[make.git] / dir.c
blob8c27c51f7c75aef330b1a181e42844004c76b5fb
1 /* Directory hashing for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993 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)
8 any later version.
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. */
19 #include "make.h"
21 #if defined (POSIX) || defined (DIRENT) || defined (__GNU_LIBRARY__)
22 #include <dirent.h>
23 #ifndef __GNU_LIBRARY__
24 #define D_NAMLEN(d) strlen((d)->d_name)
25 #else /* GNU C library. */
26 #define D_NAMLEN(d) ((d)->d_namlen)
27 #endif /* Not GNU C library. */
28 #else /* Not POSIX or DIRENT. */
29 #define direct dirent
30 #define D_NAMLEN(d) ((d)->d_namlen)
31 #ifdef SYSNDIR
32 #include <sys/ndir.h>
33 #endif /* SYSNDIR */
34 #ifdef SYSDIR
35 #include <sys/dir.h>
36 #endif /* SYSDIR */
37 #ifdef NDIR
38 #include <ndir.h>
39 #endif /* NDIR */
40 #endif /* POSIX or DIRENT or __GNU_LIBRARY__. */
42 #if defined (POSIX) && !defined (__GNU_LIBRARY__)
43 /* Posix does not require that the d_ino field be present, and some
44 systems do not provide it. */
45 #define REAL_DIR_ENTRY(dp) 1
46 #else
47 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
48 #endif /* POSIX */
50 /* Hash table of directories. */
52 #ifndef DIRECTORY_BUCKETS
53 #define DIRECTORY_BUCKETS 199
54 #endif
56 struct directory_contents
58 struct directory_contents *next;
60 int dev, ino; /* Device and inode numbers of this dir. */
62 struct dirfile **files; /* Files in this directory. */
63 DIR *dirstream; /* Stream reading this directory. */
66 /* Table of directory contents hashed by device and inode number. */
67 static struct directory_contents *directories_contents[DIRECTORY_BUCKETS];
69 struct directory
71 struct directory *next;
73 char *name; /* Name of the directory. */
75 /* The directory's contents. This data may be shared by several
76 entries in the hash table, which refer to the same directory
77 (identified uniquely by `dev' and `ino') under different names. */
78 struct directory_contents *contents;
81 /* Table of directories hashed by name. */
82 static struct directory *directories[DIRECTORY_BUCKETS];
85 /* Never have more than this many directories open at once. */
87 #define MAX_OPEN_DIRECTORIES 10
89 static unsigned int open_directories = 0;
92 /* Hash table of files in each directory. */
94 struct dirfile
96 struct dirfile *next;
97 char *name; /* Name of the file. */
98 char impossible; /* This file is impossible. */
101 #ifndef DIRFILE_BUCKETS
102 #define DIRFILE_BUCKETS 107
103 #endif
105 static int dir_contents_file_exists_p ();
107 /* Find the directory named NAME and return its `struct directory'. */
109 static struct directory *
110 find_directory (name)
111 register char *name;
113 register unsigned int hash = 0;
114 register char *p;
115 register struct directory *dir;
117 for (p = name; *p != '\0'; ++p)
118 HASH (hash, *p);
119 hash %= DIRECTORY_BUCKETS;
121 for (dir = directories[hash]; dir != 0; dir = dir->next)
122 if (streq (dir->name, name))
123 break;
125 if (dir == 0)
127 struct stat st;
129 /* The directory was not found. Create a new entry for it. */
131 dir = (struct directory *) xmalloc (sizeof (struct directory));
132 dir->next = directories[hash];
133 directories[hash] = dir;
134 dir->name = savestring (name, p - name);
136 /* The directory is not in the name hash table.
137 Find its device and inode numbers, and look it up by them. */
139 if (stat (name, &st) < 0)
140 /* Couldn't stat the directory. Mark this by
141 setting the `contents' member to a nil pointer. */
142 dir->contents = 0;
143 else
145 /* Search the contents hash table; device and inode are the key. */
147 struct directory_contents *dc;
149 hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ino;
150 hash %= DIRECTORY_BUCKETS;
152 for (dc = directories_contents[hash]; dc != 0; dc = dc->next)
153 if (dc->dev == st.st_dev && dc->ino == st.st_ino)
154 break;
156 if (dc == 0)
158 /* Nope; this really is a directory we haven't seen before. */
160 dc = (struct directory_contents *)
161 xmalloc (sizeof (struct directory_contents));
163 /* Enter it in the contents hash table. */
164 dc->dev = st.st_dev;
165 dc->ino = st.st_ino;
166 dc->next = directories_contents[hash];
167 directories_contents[hash] = dc;
169 dc->dirstream = opendir (name);
170 if (dc->dirstream == 0)
171 /* Couldn't open the directory. Mark this by
172 setting the `files' member to a nil pointer. */
173 dc->files = 0;
174 else
176 /* Allocate an array of buckets for files and zero it. */
177 dc->files = (struct dirfile **)
178 xmalloc (sizeof (struct dirfile *) * DIRFILE_BUCKETS);
179 bzero ((char *) dc->files,
180 sizeof (struct dirfile *) * DIRFILE_BUCKETS);
182 /* Keep track of how many directories are open. */
183 ++open_directories;
184 if (open_directories == MAX_OPEN_DIRECTORIES)
185 /* We have too many directories open already.
186 Read the entire directory and then close it. */
187 (void) dir_contents_file_exists_p (dc, (char *) 0);
191 /* Point the name-hashed entry for DIR at its contents data. */
192 dir->contents = dc;
196 return dir;
199 /* Return 1 if the name FILENAME is entered in DIR's hash table.
200 FILENAME must contain no slashes. */
202 static int
203 dir_contents_file_exists_p (dir, filename)
204 register struct directory_contents *dir;
205 register char *filename;
207 register unsigned int hash;
208 register char *p;
209 register struct dirfile *df;
210 register struct dirent *d;
212 if (dir == 0 || dir->files == 0)
213 /* The directory could not be stat'd or opened. */
214 return 0;
216 hash = 0;
217 if (filename != 0)
219 if (*filename == '\0')
220 /* Checking if the directory exists. */
221 return 1;
223 for (p = filename; *p != '\0'; ++p)
224 HASH (hash, *p);
225 hash %= DIRFILE_BUCKETS;
227 /* Search the list of hashed files. */
229 for (df = dir->files[hash]; df != 0; df = df->next)
230 if (streq (df->name, filename))
231 return !df->impossible;
234 /* The file was not found in the hashed list.
235 Try to read the directory further. */
237 if (dir->dirstream == 0)
238 /* The directory has been all read in. */
239 return 0;
241 while ((d = readdir (dir->dirstream)) != 0)
243 /* Enter the file in the hash table. */
244 register unsigned int newhash = 0;
245 register unsigned int i;
247 if (!REAL_DIR_ENTRY (d))
248 continue;
250 for (i = 0; i < D_NAMLEN(d); ++i)
251 HASH (newhash, d->d_name[i]);
252 newhash %= DIRFILE_BUCKETS;
254 df = (struct dirfile *) xmalloc (sizeof (struct dirfile));
255 df->next = dir->files[newhash];
256 dir->files[newhash] = df;
257 df->name = savestring (d->d_name, D_NAMLEN(d));
258 df->impossible = 0;
260 /* Check if the name matches the one we're searching for. */
261 if (filename != 0
262 && newhash == hash && streq (d->d_name, filename))
263 return 1;
266 /* If the directory has been completely read in,
267 close the stream and reset the pointer to nil. */
268 if (d == 0)
270 --open_directories;
271 closedir (dir->dirstream);
272 dir->dirstream = 0;
275 return 0;
278 /* Return 1 if the name FILENAME in directory DIRNAME
279 is entered in the dir hash table.
280 FILENAME must contain no slashes. */
283 dir_file_exists_p (dirname, filename)
284 register char *dirname;
285 register char *filename;
287 return dir_contents_file_exists_p (find_directory (dirname)->contents,
288 filename);
291 /* Return 1 if the file named NAME exists. */
294 file_exists_p (name)
295 register char *name;
297 char *dirend;
298 char *dirname;
300 #ifndef NO_ARCHIVES
301 if (ar_name (name))
302 return ar_member_date (name) != (time_t) -1;
303 #endif
305 dirend = rindex (name, '/');
306 if (dirend == 0)
307 return dir_file_exists_p (".", name);
309 dirname = (char *) alloca (dirend - name + 1);
310 bcopy (name, dirname, dirend - name);
311 dirname[dirend - name] = '\0';
312 return dir_file_exists_p (dirname, dirend + 1);
315 /* Mark FILENAME as `impossible' for `file_impossible_p'.
316 This means an attempt has been made to search for FILENAME
317 as an intermediate file, and it has failed. */
319 void
320 file_impossible (filename)
321 register char *filename;
323 char *dirend;
324 register char *p = filename;
325 register unsigned int hash;
326 register struct directory *dir;
327 register struct dirfile *new;
329 dirend = rindex (p, '/');
330 if (dirend == 0)
331 dir = find_directory (".");
332 else
334 char *dirname = (char *) alloca (dirend - p + 1);
335 bcopy (p, dirname, dirend - p);
336 dirname[dirend - p] = '\0';
337 dir = find_directory (dirname);
338 filename = p = dirend + 1;
341 for (hash = 0; *p != '\0'; ++p)
342 HASH (hash, *p);
343 hash %= DIRFILE_BUCKETS;
345 if (dir->contents == 0)
347 /* The directory could not be stat'd. We allocate a contents
348 structure for it, but leave it out of the contents hash table. */
349 dir->contents = (struct directory_contents *)
350 xmalloc (sizeof (struct directory_contents));
351 dir->contents->dev = dir->contents->ino = 0;
352 dir->contents->files = 0;
353 dir->contents->dirstream = 0;
356 if (dir->contents->files == 0)
358 /* The directory was not opened; we must allocate the hash buckets. */
359 dir->contents->files = (struct dirfile **)
360 xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
361 bzero ((char *) dir->contents->files,
362 sizeof (struct dirfile) * DIRFILE_BUCKETS);
365 /* Make a new entry and put it in the table. */
367 new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
368 new->next = dir->contents->files[hash];
369 dir->contents->files[hash] = new;
370 new->name = savestring (filename, strlen (filename));
371 new->impossible = 1;
374 /* Return nonzero if FILENAME has been marked impossible. */
377 file_impossible_p (filename)
378 char *filename;
380 char *dirend;
381 register char *p = filename;
382 register unsigned int hash;
383 register struct directory_contents *dir;
384 register struct dirfile *next;
386 dirend = rindex (filename, '/');
387 if (dirend == 0)
388 dir = find_directory (".")->contents;
389 else
391 char *dirname = (char *) alloca (dirend - filename + 1);
392 bcopy (p, dirname, dirend - p);
393 dirname[dirend - p] = '\0';
394 dir = find_directory (dirname)->contents;
395 p = dirend + 1;
398 if (dir == 0 || dir->files == 0)
399 /* There are no files entered for this directory. */
400 return 0;
402 for (hash = 0; *p != '\0'; ++p)
403 HASH (hash, *p);
404 hash %= DIRFILE_BUCKETS;
406 for (next = dir->files[hash]; next != 0; next = next->next)
407 if (streq (filename, next->name))
408 return next->impossible;
410 return 0;
413 /* Return the already allocated name in the
414 directory hash table that matches DIR. */
416 char *
417 dir_name (dir)
418 char *dir;
420 return find_directory (dir)->name;
423 /* Print the data base of directories. */
425 void
426 print_dir_data_base ()
428 register unsigned int i, dirs, files, impossible;
429 register struct directory *dir;
431 puts ("\n# Directories\n");
433 dirs = files = impossible = 0;
434 for (i = 0; i < DIRECTORY_BUCKETS; ++i)
435 for (dir = directories[i]; dir != 0; dir = dir->next)
437 ++dirs;
438 if (dir->contents == 0)
439 printf ("# %s: could not be stat'd.\n", dir->name);
440 else if (dir->contents->files == 0)
441 printf ("# %s (device %d, inode %d): could not be opened.\n",
442 dir->name, dir->contents->dev, dir->contents->ino);
443 else
445 register unsigned int f = 0, im = 0;
446 register unsigned int j;
447 register struct dirfile *df;
448 for (j = 0; j < DIRFILE_BUCKETS; ++j)
449 for (df = dir->contents->files[j]; df != 0; df = df->next)
450 if (df->impossible)
451 ++im;
452 else
453 ++f;
454 printf ("# %s (device %d, inode %d): ",
455 dir->name, dir->contents->dev, dir->contents->ino);
456 if (f == 0)
457 fputs ("No", stdout);
458 else
459 printf ("%u", f);
460 fputs (" files, ", stdout);
461 if (im == 0)
462 fputs ("no", stdout);
463 else
464 printf ("%u", im);
465 fputs (" impossibilities", stdout);
466 if (dir->contents->dirstream == 0)
467 puts (".");
468 else
469 puts (" so far.");
470 files += f;
471 impossible += im;
475 fputs ("\n# ", stdout);
476 if (files == 0)
477 fputs ("No", stdout);
478 else
479 printf ("%u", files);
480 fputs (" files, ", stdout);
481 if (impossible == 0)
482 fputs ("no", stdout);
483 else
484 printf ("%u", impossible);
485 printf (" impossibilities in %u directories.\n", dirs);
488 /* Hooks for globbing. */
490 #include <glob.h>
492 /* Structure describing state of iterating through a directory hash table. */
494 struct dirstream
496 struct directory_contents *contents; /* The directory being read. */
498 unsigned int bucket; /* Current hash bucket. */
499 struct dirfile *elt; /* Current elt in bucket. */
502 /* Forward declarations. */
503 static __ptr_t open_dirstream __P ((const char *));
504 static const char *read_dirstream __P ((__ptr_t));
506 static __ptr_t
507 open_dirstream (directory)
508 const char *directory;
510 struct dirstream *new;
511 struct directory *dir = find_directory (directory);
513 if (dir->contents == 0 || dir->contents->files == 0)
514 /* DIR->contents is nil if the directory could not be stat'd.
515 DIR->contents->files is nil if it could not be opened. */
516 return 0;
518 /* Read all the contents of the directory now. There is no benefit
519 in being lazy, since glob will want to see every file anyway. */
521 (void) dir_contents_file_exists_p (dir->contents, (char *) 0);
523 new = (struct dirstream *) xmalloc (sizeof (struct dirstream));
524 new->contents = dir->contents;
525 new->bucket = 0;
526 new->elt = new->contents->files[0];
528 return (__ptr_t) new;
531 static const char *
532 read_dirstream (stream)
533 __ptr_t stream;
535 struct dirstream *const ds = (struct dirstream *) stream;
536 register struct dirfile *df;
538 while (ds->bucket < DIRFILE_BUCKETS)
540 while ((df = ds->elt) != 0)
542 ds->elt = df->next;
543 if (!df->impossible)
544 return df->name;
546 if (++ds->bucket == DIRFILE_BUCKETS)
547 break;
548 ds->elt = ds->contents->files[ds->bucket];
551 return 0;
554 void
555 init_dir ()
557 __glob_opendir_hook = open_dirstream;
558 __glob_readdir_hook = read_dirstream;
559 __glob_closedir_hook = (void (*) __P ((__ptr_t stream))) free;