moved to filedef.h
[make.git] / dir.c
blob969f52a6f44a92bbc3d63aa282bd3fd83c75e07b
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)
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 #ifdef HAVE_DIRENT_H
22 # include <dirent.h>
23 # define NAMLEN(dirent) strlen((dirent)->d_name)
24 #else
25 # define dirent direct
26 # define NAMLEN(dirent) (dirent)->d_namlen
27 # ifdef HAVE_SYS_NDIR_H
28 # include <sys/ndir.h>
29 # endif
30 # ifdef HAVE_SYS_DIR_H
31 # include <sys/dir.h>
32 # endif
33 # ifdef HAVE_NDIR_H
34 # include <ndir.h>
35 # endif
36 # ifdef HAVE_VMSDIR_H
37 # include "vmsdir.h"
38 # endif /* HAVE_VMSDIR_H */
39 #endif
41 /* In GNU systems, <dirent.h> defines this macro for us. */
42 #ifdef _D_NAMLEN
43 #undef NAMLEN
44 #define NAMLEN(d) _D_NAMLEN(d)
45 #endif
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)
52 #else
53 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
54 #define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
55 #endif /* POSIX */
57 #ifdef __MSDOS__
58 #include <ctype.h>
60 static char *
61 dosify (filename)
62 char *filename;
64 static char dos_filename[14];
65 char *df;
66 int i;
68 if (filename == 0)
69 return 0;
71 if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
72 return filename;
74 df = dos_filename;
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 != '.')
82 ++filename;
83 if (*filename != '\0')
85 *df++ = *filename++;
86 for (i = 0; *filename != '\0' && i < 3 && *filename != '.'; ++i)
87 *df++ = tolower (*filename++);
90 /* Look for more dots. */
91 while (*filename != '\0' && *filename != '.')
92 ++filename;
93 if (*filename == '.')
94 return filename;
95 *df = 0;
96 return dos_filename;
98 #endif /* __MSDOS__ */
100 #ifdef VMS
102 static int
103 vms_hash (name)
104 char *name;
106 int h = 0;
107 int g;
109 while (*name)
111 h = (h << 4) + *name++;
112 g = h & 0xf0000000;
113 if (g)
115 h = h ^ (g >> 24);
116 h = h ^ g;
119 return h;
122 /* fake stat entry for a directory */
123 static int
124 vmsstat_dir (name, st)
125 char *name;
126 struct stat *st;
128 char *s;
129 int h;
130 DIR *dir;
132 dir = opendir (name);
133 if (dir == 0)
134 return -1;
135 closedir (dir);
136 s = strchr (name, ':'); /* find device */
137 if (s)
139 *s++ = 0;
140 st->st_dev = (char *)vms_hash (name);
142 else
144 st->st_dev = 0;
145 s = name;
147 h = vms_hash (s);
148 st->st_ino[0] = h & 0xff;
149 st->st_ino[1] = h & 0xff00;
150 st->st_ino[2] = h >> 16;
152 return 0;
154 #endif /* VMS */
156 /* Hash table of directories. */
158 #ifndef DIRECTORY_BUCKETS
159 #define DIRECTORY_BUCKETS 199
160 #endif
162 struct directory_contents
164 struct directory_contents *next;
166 dev_t dev; /* Device and inode numbers of this dir. */
167 #ifdef VMS
168 ino_t ino[3];
169 #else
170 ino_t ino;
171 #endif
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];
179 struct directory
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. */
204 struct dirfile
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
213 #endif
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)
222 register char *name;
224 register unsigned int hash = 0;
225 register char *p;
226 register struct directory *dir;
227 #ifdef VMS
228 if ((*name == '.') && (*(name+1) == 0))
229 name = "[]";
230 else
231 name = vmsify (name,1);
232 #endif
234 for (p = name; *p != '\0'; ++p)
235 HASH (hash, *p);
236 hash %= DIRECTORY_BUCKETS;
238 for (dir = directories[hash]; dir != 0; dir = dir->next)
239 if (streq (dir->name, name))
240 break;
242 if (dir == 0)
244 struct stat st;
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. */
256 #ifdef VMS
257 if (vmsstat_dir (name, &st) < 0)
258 #else
259 if (stat (name, &st) < 0)
260 #endif
262 /* Couldn't stat the directory. Mark this by
263 setting the `contents' member to a nil pointer. */
264 dir->contents = 0;
266 else
268 /* Search the contents hash table; device and inode are the key. */
270 struct directory_contents *dc;
272 #ifdef VMS
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]);
277 #else
278 hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ino;
279 #endif
280 hash %= DIRECTORY_BUCKETS;
282 for (dc = directories_contents[hash]; dc != 0; dc = dc->next)
283 if (dc->dev == st.st_dev
284 #ifdef VMS
285 && dc->ino[0] == st.st_ino[0]
286 && dc->ino[1] == st.st_ino[1]
287 && dc->ino[2] == st.st_ino[2])
288 #else
289 && dc->ino == st.st_ino)
290 #endif
291 break;
293 if (dc == 0)
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. */
301 dc->dev = st.st_dev;
302 #ifdef VMS
303 dc->ino[0] = st.st_ino[0];
304 dc->ino[1] = st.st_ino[1];
305 dc->ino[2] = st.st_ino[2];
306 #else
307 dc->ino = st.st_ino;
308 #endif
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. */
317 dc->files = 0;
319 else
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. */
328 ++open_directories;
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. */
337 dir->contents = dc;
341 return dir;
344 /* Return 1 if the name FILENAME is entered in DIR's hash table.
345 FILENAME must contain no slashes. */
347 static int
348 dir_contents_file_exists_p (dir, filename)
349 register struct directory_contents *dir;
350 register char *filename;
352 register unsigned int hash;
353 register char *p;
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. */
360 return 0;
362 #ifdef __MSDOS__
363 filename = dosify (filename);
364 #endif
366 #ifdef VMS
367 filename = vmsify (filename,0);
368 #endif
370 hash = 0;
371 if (filename != 0)
373 if (*filename == '\0')
375 /* Checking if the directory exists. */
376 return 1;
379 for (p = filename; *p != '\0'; ++p)
380 HASH (hash, *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. */
400 return 0;
403 while ((d = readdir (dir->dirstream)) != 0)
405 /* Enter the file in the hash table. */
406 register unsigned int newhash = 0;
407 unsigned int len;
408 register unsigned int i;
410 if (!REAL_DIR_ENTRY (d))
411 continue;
413 len = NAMLEN (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);
422 df->impossible = 0;
423 /* Check if the name matches the one we're searching for. */
424 if (filename != 0
425 && newhash == hash && streq (d->d_name, filename))
427 return 1;
431 /* If the directory has been completely read in,
432 close the stream and reset the pointer to nil. */
433 if (d == 0)
435 --open_directories;
436 closedir (dir->dirstream);
437 dir->dirstream = 0;
439 return 0;
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,
452 filename);
455 /* Return 1 if the file named NAME exists. */
458 file_exists_p (name)
459 register char *name;
461 char *dirend;
462 char *dirname;
464 #ifndef NO_ARCHIVES
465 if (ar_name (name))
466 return ar_member_date (name) != (time_t) -1;
467 #endif
469 #ifdef VMS
470 dirend = rindex (name, ']');
471 dirend++;
472 if (dirend == (char *)1)
473 return dir_file_exists_p ("[]", name);
474 #else
475 dirend = rindex (name, '/');
476 if (dirend == 0)
477 return dir_file_exists_p (".", name);
478 #endif
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. */
490 void
491 file_impossible (filename)
492 register char *filename;
494 char *dirend;
495 register char *p = filename;
496 register unsigned int hash;
497 register struct directory *dir;
498 register struct dirfile *new;
500 #ifdef VMS
501 dirend = rindex (p, ']');
502 dirend++;
503 if (dirend == (char *)1)
504 dir = find_directory ("[]");
505 #else
506 dirend = rindex (p, '/');
507 if (dirend == 0)
508 dir = find_directory (".");
509 #endif
510 else
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)
520 HASH (hash, *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));
529 #ifdef VMS
530 dir->contents->dev = 0;
531 dir->contents->ino[0] = dir->contents->ino[1] =
532 dir->contents->ino[2] = 0;
533 #else
534 dir->contents->dev = dir->contents->ino = 0;
535 #endif
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));
555 new->impossible = 1;
558 /* Return nonzero if FILENAME has been marked impossible. */
561 file_impossible_p (filename)
562 char *filename;
564 char *dirend;
565 register char *p = filename;
566 register unsigned int hash;
567 register struct directory_contents *dir;
568 register struct dirfile *next;
570 #ifdef VMS
571 dirend = rindex (filename, ']');
572 if (dirend == 0)
573 dir = find_directory ("[]")->contents;
574 #else
575 dirend = rindex (filename, '/');
576 if (dirend == 0)
577 dir = find_directory (".")->contents;
578 #endif
579 else
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. */
590 return 0;
592 #ifdef __MSDOS__
593 p = filename = dosify (p);
594 #endif
595 #ifdef VMS
596 p = filename = vmsify (p, 1);
597 #endif
599 for (hash = 0; *p != '\0'; ++p)
600 HASH (hash, *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;
607 return 0;
610 /* Return the already allocated name in the
611 directory hash table that matches DIR. */
613 char *
614 dir_name (dir)
615 char *dir;
617 return find_directory (dir)->name;
620 /* Print the data base of directories. */
622 void
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)
634 ++dirs;
635 if (dir->contents == 0)
636 printf ("# %s: could not be stat'd.\n", dir->name);
637 else if (dir->contents->files == 0)
638 #ifdef VMS
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]);
643 #else
644 printf ("# %s (device %d, inode %d): could not be opened.\n",
645 dir->name, dir->contents->dev, dir->contents->ino);
646 #endif
647 else
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)
654 if (df->impossible)
655 ++im;
656 else
657 ++f;
658 #ifdef VMS
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]);
663 #else
664 printf ("# %s (device %d, inode %d): ",
665 dir->name, dir->contents->dev, dir->contents->ino);
666 #endif
667 if (f == 0)
668 fputs ("No", stdout);
669 else
670 printf ("%u", f);
671 fputs (" files, ", stdout);
672 if (im == 0)
673 fputs ("no", stdout);
674 else
675 printf ("%u", im);
676 fputs (" impossibilities", stdout);
677 if (dir->contents->dirstream == 0)
678 puts (".");
679 else
680 puts (" so far.");
681 files += f;
682 impossible += im;
686 fputs ("\n# ", stdout);
687 if (files == 0)
688 fputs ("No", stdout);
689 else
690 printf ("%u", files);
691 fputs (" files, ", stdout);
692 if (impossible == 0)
693 fputs ("no", stdout);
694 else
695 printf ("%u", impossible);
696 printf (" impossibilities in %u directories.\n", dirs);
699 /* Hooks for globbing. */
701 #include <glob.h>
703 /* Structure describing state of iterating through a directory hash table. */
705 struct dirstream
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));
717 static __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. */
727 return 0;
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;
736 new->bucket = 0;
737 new->elt = new->contents->files[0];
739 return (__ptr_t) new;
742 static struct dirent *
743 read_dirstream (stream)
744 __ptr_t stream;
746 struct dirstream *const ds = (struct dirstream *) stream;
747 register struct dirfile *df;
748 static char *buf;
749 static unsigned int bufsz;
751 while (ds->bucket < DIRFILE_BUCKETS)
753 while ((df = ds->elt) != 0)
755 ds->elt = df->next;
756 if (!df->impossible)
758 /* The glob interface wants a `struct dirent',
759 so mock one up. */
760 struct dirent *d;
761 unsigned int len = strlen (df->name) + 1;
762 if (sizeof *d - sizeof d->d_name + len > bufsz)
764 if (buf != 0)
765 free (buf);
766 bufsz *= 2;
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;
772 FAKE_DIR_ENTRY (d);
773 #ifdef _DIRENT_HAVE_D_NAMLEN
774 d->d_namlen = len - 1;
775 #endif
776 memcpy (d->d_name, df->name, len);
777 return d;
780 if (++ds->bucket == DIRFILE_BUCKETS)
781 break;
782 ds->elt = ds->contents->files[ds->bucket];
785 return 0;
788 void
789 dir_setup_glob (gl)
790 glob_t *gl;
792 extern int stat ();
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;
798 gl->gl_stat = stat;
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. */