autoconf
[make.git] / dir.c
blobcc0c3e3a069b036da3094ec04dce117008c2834a
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 (WIN32)) && !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>
59 #if (DJGPP > 1)
60 #include <libc/dosio.h>
61 int __opendir_flags = 0;
62 #endif
64 static char *
65 dosify (filename)
66 char *filename;
68 static char dos_filename[14];
69 char *df;
70 int i;
72 #if (DJGPP > 1)
73 if (_USE_LFN)
74 /* Using long file names; do no transformation. */
75 return filename;
76 #endif
77 if (filename == 0)
78 return 0;
80 if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
81 return filename;
83 df = dos_filename;
85 /* First, transform the name part. */
86 for (i = 0; *filename != '\0' && i < 8 && *filename != '.'; ++i)
87 *df++ = tolower (*filename++);
89 /* Now skip to the next dot. */
90 while (*filename != '\0' && *filename != '.')
91 ++filename;
92 if (*filename != '\0')
94 *df++ = *filename++;
95 for (i = 0; *filename != '\0' && i < 3 && *filename != '.'; ++i)
96 *df++ = tolower (*filename++);
99 /* Look for more dots. */
100 while (*filename != '\0' && *filename != '.')
101 ++filename;
102 if (*filename == '.')
103 return filename;
104 *df = 0;
105 return dos_filename;
107 #endif /* __MSDOS__ */
109 #ifdef WIN32
110 #include "pathstuff.h"
111 #endif
113 #ifdef _AMIGA
114 #include <ctype.h>
116 static char *
117 amigafy (filename)
118 char *filename;
120 static char amiga_filename[136];
121 char *df;
122 int i;
124 if (filename == 0)
125 return 0;
127 df = amiga_filename;
129 /* First, transform the name part. */
130 for (i = 0; *filename != '\0'; ++i)
132 *df++ = tolower (*filename);
133 ++filename;
136 *df = 0;
138 return amiga_filename;
140 #endif /* _AMIGA */
142 #ifdef VMS
144 static int
145 vms_hash (name)
146 char *name;
148 int h = 0;
149 int g;
151 while (*name)
153 h = (h << 4) + *name++;
154 g = h & 0xf0000000;
155 if (g)
157 h = h ^ (g >> 24);
158 h = h ^ g;
161 return h;
164 /* fake stat entry for a directory */
165 static int
166 vmsstat_dir (name, st)
167 char *name;
168 struct stat *st;
170 char *s;
171 int h;
172 DIR *dir;
174 dir = opendir (name);
175 if (dir == 0)
176 return -1;
177 closedir (dir);
178 s = strchr (name, ':'); /* find device */
179 if (s)
181 *s++ = 0;
182 st->st_dev = (char *)vms_hash (name);
184 else
186 st->st_dev = 0;
187 s = name;
189 h = vms_hash (s);
190 st->st_ino[0] = h & 0xff;
191 st->st_ino[1] = h & 0xff00;
192 st->st_ino[2] = h >> 16;
194 return 0;
196 #endif /* VMS */
198 /* Hash table of directories. */
200 #ifndef DIRECTORY_BUCKETS
201 #define DIRECTORY_BUCKETS 199
202 #endif
204 struct directory_contents
206 struct directory_contents *next;
208 dev_t dev; /* Device and inode numbers of this dir. */
209 #ifdef WIN32
211 * Inode means nothing on WIN32. Even file key information is
212 * unreliable because it is random per file open and undefined
213 * for remote filesystems. The most unique attribute I can
214 * come up with is the fully qualified name of the directory. Beware
215 * though, this is also unreliable. I'm open to suggestion on a better
216 * way to emulate inode.
218 char *path_key;
219 int mtime; /* controls check for stale directory cache */
220 #else
221 #ifdef VMS
222 ino_t ino[3];
223 #else
224 ino_t ino;
225 #endif
226 #endif /* WIN32 */
227 struct dirfile **files; /* Files in this directory. */
228 DIR *dirstream; /* Stream reading this directory. */
231 /* Table of directory contents hashed by device and inode number. */
232 static struct directory_contents *directories_contents[DIRECTORY_BUCKETS];
234 struct directory
236 struct directory *next;
238 char *name; /* Name of the directory. */
240 /* The directory's contents. This data may be shared by several
241 entries in the hash table, which refer to the same directory
242 (identified uniquely by `dev' and `ino') under different names. */
243 struct directory_contents *contents;
246 /* Table of directories hashed by name. */
247 static struct directory *directories[DIRECTORY_BUCKETS];
250 /* Never have more than this many directories open at once. */
252 #define MAX_OPEN_DIRECTORIES 10
254 static unsigned int open_directories = 0;
257 /* Hash table of files in each directory. */
259 struct dirfile
261 struct dirfile *next;
262 char *name; /* Name of the file. */
263 char impossible; /* This file is impossible. */
266 #ifndef DIRFILE_BUCKETS
267 #define DIRFILE_BUCKETS 107
268 #endif
270 static int dir_contents_file_exists_p PARAMS ((struct directory_contents *dir, char *filename));
271 static struct directory *find_directory PARAMS ((char *name));
273 /* Find the directory named NAME and return its `struct directory'. */
275 static struct directory *
276 find_directory (name)
277 register char *name;
279 register unsigned int hash = 0;
280 register char *p;
281 register struct directory *dir;
282 #ifdef WIN32
283 char* w32_path;
284 #endif
285 #ifdef VMS
286 if ((*name == '.') && (*(name+1) == 0))
287 name = "[]";
288 else
289 name = vmsify (name,1);
290 #endif
292 for (p = name; *p != '\0'; ++p)
293 HASHI (hash, *p);
294 hash %= DIRECTORY_BUCKETS;
296 for (dir = directories[hash]; dir != 0; dir = dir->next)
297 if (strieq (dir->name, name))
298 break;
300 if (dir == 0)
302 struct stat st;
304 /* The directory was not found. Create a new entry for it. */
306 dir = (struct directory *) xmalloc (sizeof (struct directory));
307 dir->next = directories[hash];
308 directories[hash] = dir;
309 dir->name = savestring (name, p - name);
311 /* The directory is not in the name hash table.
312 Find its device and inode numbers, and look it up by them. */
314 #ifdef VMS
315 if (vmsstat_dir (name, &st) < 0)
316 #else
317 if (stat (name, &st) < 0)
318 #endif
320 /* Couldn't stat the directory. Mark this by
321 setting the `contents' member to a nil pointer. */
322 dir->contents = 0;
324 else
326 /* Search the contents hash table; device and inode are the key. */
328 struct directory_contents *dc;
330 #ifdef WIN32
331 w32_path = w32ify(name, 1);
332 hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ctime;
333 #else
334 #ifdef VMS
335 hash = ((unsigned int) st.st_dev << 16)
336 | ((unsigned int) st.st_ino[0]
337 + (unsigned int) st.st_ino[1]
338 + (unsigned int) st.st_ino[2]);
339 #else
340 hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ino;
341 #endif
342 #endif
343 hash %= DIRECTORY_BUCKETS;
345 for (dc = directories_contents[hash]; dc != 0; dc = dc->next)
346 #ifdef WIN32
347 if (!strcmp(dc->path_key, w32_path))
348 #else
349 if (dc->dev == st.st_dev
350 #ifdef VMS
351 && dc->ino[0] == st.st_ino[0]
352 && dc->ino[1] == st.st_ino[1]
353 && dc->ino[2] == st.st_ino[2])
354 #else
355 && dc->ino == st.st_ino)
356 #endif
357 #endif /* WIN32 */
358 break;
360 if (dc == 0)
362 /* Nope; this really is a directory we haven't seen before. */
364 dc = (struct directory_contents *)
365 xmalloc (sizeof (struct directory_contents));
367 /* Enter it in the contents hash table. */
368 dc->dev = st.st_dev;
369 #ifdef WIN32
370 dc->path_key = strdup(w32_path);
371 dc->mtime = st.st_mtime;
372 #else
373 #ifdef VMS
374 dc->ino[0] = st.st_ino[0];
375 dc->ino[1] = st.st_ino[1];
376 dc->ino[2] = st.st_ino[2];
377 #else
378 dc->ino = st.st_ino;
379 #endif
380 #endif /* WIN32 */
381 dc->next = directories_contents[hash];
382 directories_contents[hash] = dc;
384 #if defined (__MSDOS__) && (DJGPP > 1)
385 if (_USE_LFN)
386 /* We are using long filenames, so tell opendir not
387 to mess with them. */
388 __opendir_flags = __OPENDIR_PRESERVE_CASE;
389 #endif
391 dc->dirstream = opendir (name);
392 if (dc->dirstream == 0)
394 /* Couldn't open the directory. Mark this by
395 setting the `files' member to a nil pointer. */
396 dc->files = 0;
398 else
400 /* Allocate an array of buckets for files and zero it. */
401 dc->files = (struct dirfile **)
402 xmalloc (sizeof (struct dirfile *) * DIRFILE_BUCKETS);
403 bzero ((char *) dc->files,
404 sizeof (struct dirfile *) * DIRFILE_BUCKETS);
406 /* Keep track of how many directories are open. */
407 ++open_directories;
408 if (open_directories == MAX_OPEN_DIRECTORIES)
409 /* We have too many directories open already.
410 Read the entire directory and then close it. */
411 (void) dir_contents_file_exists_p (dc, (char *) 0);
415 /* Point the name-hashed entry for DIR at its contents data. */
416 dir->contents = dc;
420 return dir;
423 /* Return 1 if the name FILENAME is entered in DIR's hash table.
424 FILENAME must contain no slashes. */
426 static int
427 dir_contents_file_exists_p (dir, filename)
428 register struct directory_contents *dir;
429 register char *filename;
431 register unsigned int hash;
432 register char *p;
433 register struct dirfile *df;
434 register struct dirent *d;
435 #ifdef WIN32
436 struct stat st;
437 int rehash = 0;
438 #endif
440 if (dir == 0 || dir->files == 0)
442 /* The directory could not be stat'd or opened. */
443 return 0;
445 #ifdef __MSDOS__
446 filename = dosify (filename);
447 #endif
449 #ifdef _AMIGA
450 filename = amigafy (filename);
451 #endif
453 #ifdef VMS
454 filename = vmsify (filename,0);
455 #endif
457 hash = 0;
458 if (filename != 0)
460 if (*filename == '\0')
462 /* Checking if the directory exists. */
463 return 1;
466 for (p = filename; *p != '\0'; ++p)
467 HASH (hash, *p);
468 hash %= DIRFILE_BUCKETS;
470 /* Search the list of hashed files. */
472 for (df = dir->files[hash]; df != 0; df = df->next)
474 if (strieq (df->name, filename))
476 return !df->impossible;
481 /* The file was not found in the hashed list.
482 Try to read the directory further. */
484 if (dir->dirstream == 0)
486 #ifdef WIN32
487 /* Check to see if directory has changed since last read */
488 if (dir->path_key &&
489 stat(dir->path_key, &st) == 0 &&
490 st.st_mtime > dir->mtime) {
492 /* reset date stamp to show most recent re-process */
493 dir->mtime = st.st_mtime;
495 /* make sure directory can still be opened */
496 dir->dirstream = opendir(dir->path_key);
498 if (dir->dirstream)
499 rehash = 1;
500 else
501 return 0; /* couldn't re-read - fail */
502 } else
503 #endif
504 /* The directory has been all read in. */
505 return 0;
508 while ((d = readdir (dir->dirstream)) != 0)
510 /* Enter the file in the hash table. */
511 register unsigned int newhash = 0;
512 unsigned int len;
513 register unsigned int i;
515 if (!REAL_DIR_ENTRY (d))
516 continue;
518 len = NAMLEN (d);
519 for (i = 0; i < len; ++i)
520 HASHI (newhash, d->d_name[i]);
521 newhash %= DIRFILE_BUCKETS;
522 #ifdef WIN32
524 * If re-reading a directory, check that this file isn't already
525 * in the cache.
527 if (rehash) {
528 for (df = dir->files[newhash]; df != 0; df = df->next)
529 if (streq(df->name, d->d_name))
530 break;
531 } else
532 df = 0;
535 * If re-reading a directory, don't cache files that have
536 * already been discovered.
538 if (!df) {
539 #endif
541 df = (struct dirfile *) xmalloc (sizeof (struct dirfile));
542 df->next = dir->files[newhash];
543 dir->files[newhash] = df;
544 df->name = savestring (d->d_name, len);
545 df->impossible = 0;
546 #ifdef WIN32
548 #endif
549 /* Check if the name matches the one we're searching for. */
550 if (filename != 0
551 && newhash == hash && strieq (d->d_name, filename))
553 return 1;
557 /* If the directory has been completely read in,
558 close the stream and reset the pointer to nil. */
559 if (d == 0)
561 --open_directories;
562 closedir (dir->dirstream);
563 dir->dirstream = 0;
565 return 0;
568 /* Return 1 if the name FILENAME in directory DIRNAME
569 is entered in the dir hash table.
570 FILENAME must contain no slashes. */
573 dir_file_exists_p (dirname, filename)
574 register char *dirname;
575 register char *filename;
577 return dir_contents_file_exists_p (find_directory (dirname)->contents,
578 filename);
581 /* Return 1 if the file named NAME exists. */
584 file_exists_p (name)
585 register char *name;
587 char *dirend;
588 char *dirname;
590 #ifndef NO_ARCHIVES
591 if (ar_name (name))
592 return ar_member_date (name) != (time_t) -1;
593 #endif
595 #ifdef VMS
596 dirend = rindex (name, ']');
597 dirend++;
598 if (dirend == (char *)1)
599 return dir_file_exists_p ("[]", name);
600 #else /* !VMS */
601 dirend = rindex (name, '/');
602 #ifdef WIN32
603 if (!dirend)
604 dirend = rindex(name, '\\');
605 #endif /* WIN32 */
606 if (dirend == 0)
607 return dir_file_exists_p (".", name);
608 if (dirend == 0)
609 #ifndef _AMIGA
610 return dir_file_exists_p (".", name);
611 #else /* !VMS && !AMIGA */
612 return dir_file_exists_p ("", name);
613 #endif /* AMIGA */
614 #endif /* VMS */
616 dirname = (char *) alloca (dirend - name + 1);
617 bcopy (name, dirname, dirend - name);
618 dirname[dirend - name] = '\0';
619 return dir_file_exists_p (dirname, dirend + 1);
622 /* Mark FILENAME as `impossible' for `file_impossible_p'.
623 This means an attempt has been made to search for FILENAME
624 as an intermediate file, and it has failed. */
626 void
627 file_impossible (filename)
628 register char *filename;
630 char *dirend;
631 register char *p = filename;
632 register unsigned int hash;
633 register struct directory *dir;
634 register struct dirfile *new;
636 #ifdef VMS
637 dirend = rindex (p, ']');
638 dirend++;
639 if (dirend == (char *)1)
640 dir = find_directory ("[]");
641 #else
642 dirend = rindex (p, '/');
643 if (dirend == 0)
644 #ifdef _AMIGA
645 dir = find_directory ("");
646 #else /* !VMS && !AMIGA */
647 dir = find_directory (".");
648 #endif /* AMIGA */
649 #endif /* VMS */
650 else
652 char *dirname = (char *) alloca (dirend - p + 1);
653 bcopy (p, dirname, dirend - p);
654 dirname[dirend - p] = '\0';
655 dir = find_directory (dirname);
656 filename = p = dirend + 1;
659 for (hash = 0; *p != '\0'; ++p)
660 HASHI (hash, *p);
661 hash %= DIRFILE_BUCKETS;
663 if (dir->contents == 0)
665 /* The directory could not be stat'd. We allocate a contents
666 structure for it, but leave it out of the contents hash table. */
667 dir->contents = (struct directory_contents *)
668 xmalloc (sizeof (struct directory_contents));
669 #ifdef WIN32
670 dir->contents->path_key = NULL;
671 dir->contents->mtime = 0;
672 #else /* WIN32 */
673 #ifdef VMS
674 dir->contents->dev = 0;
675 dir->contents->ino[0] = dir->contents->ino[1] =
676 dir->contents->ino[2] = 0;
677 #else
678 dir->contents->dev = dir->contents->ino = 0;
679 #endif
680 #endif /* WIN32 */
681 dir->contents->files = 0;
682 dir->contents->dirstream = 0;
685 if (dir->contents->files == 0)
687 /* The directory was not opened; we must allocate the hash buckets. */
688 dir->contents->files = (struct dirfile **)
689 xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
690 bzero ((char *) dir->contents->files,
691 sizeof (struct dirfile) * DIRFILE_BUCKETS);
694 /* Make a new entry and put it in the table. */
696 new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
697 new->next = dir->contents->files[hash];
698 dir->contents->files[hash] = new;
699 new->name = savestring (filename, strlen (filename));
700 new->impossible = 1;
703 /* Return nonzero if FILENAME has been marked impossible. */
706 file_impossible_p (filename)
707 char *filename;
709 char *dirend;
710 register char *p = filename;
711 register unsigned int hash;
712 register struct directory_contents *dir;
713 register struct dirfile *next;
715 #ifdef VMS
716 dirend = rindex (filename, ']');
717 if (dirend == 0)
718 dir = find_directory ("[]")->contents;
719 #else
720 dirend = rindex (filename, '/');
721 #ifdef WIN32
722 if (!dirend)
723 dirend = rindex (filename, '\\');
724 #endif /* WIN32 */
725 if (dirend == 0)
726 #ifdef _AMIGA
727 dir = find_directory ("")->contents;
728 #else /* !VMS && !AMIGA */
729 dir = find_directory (".")->contents;
730 #endif /* AMIGA */
731 #endif /* VMS */
732 else
734 char *dirname = (char *) alloca (dirend - filename + 1);
735 bcopy (p, dirname, dirend - p);
736 dirname[dirend - p] = '\0';
737 dir = find_directory (dirname)->contents;
738 p = filename = dirend + 1;
741 if (dir == 0 || dir->files == 0)
742 /* There are no files entered for this directory. */
743 return 0;
745 #ifdef __MSDOS__
746 p = filename = dosify (p);
747 #endif
748 #ifdef _AMIGA
749 p = filename = amigafy (p);
750 #endif
751 #ifdef VMS
752 p = filename = vmsify (p, 1);
753 #endif
755 for (hash = 0; *p != '\0'; ++p)
756 HASH (hash, *p);
757 hash %= DIRFILE_BUCKETS;
759 for (next = dir->files[hash]; next != 0; next = next->next)
760 if (strieq (filename, next->name))
761 return next->impossible;
763 return 0;
766 /* Return the already allocated name in the
767 directory hash table that matches DIR. */
769 char *
770 dir_name (dir)
771 char *dir;
773 return find_directory (dir)->name;
776 /* Print the data base of directories. */
778 void
779 print_dir_data_base ()
781 register unsigned int i, dirs, files, impossible;
782 register struct directory *dir;
784 puts ("\n# Directories\n");
786 dirs = files = impossible = 0;
787 for (i = 0; i < DIRECTORY_BUCKETS; ++i)
788 for (dir = directories[i]; dir != 0; dir = dir->next)
790 ++dirs;
791 if (dir->contents == 0)
792 printf ("# %s: could not be stat'd.\n", dir->name);
793 else if (dir->contents->files == 0)
794 #ifdef WIN32
795 printf ("# %s (key %s, mtime %d): could not be opened.\n",
796 dir->name, dir->contents->path_key,dir->contents->mtime);
797 #else /* WIN32 */
798 #ifdef VMS
799 printf ("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n",
800 dir->name, dir->contents->dev,
801 dir->contents->ino[0], dir->contents->ino[1],
802 dir->contents->ino[2]);
803 #else
804 printf ("# %s (device %ld, inode %ld): could not be opened.\n",
805 dir->name, (long int) dir->contents->dev,
806 (long int) dir->contents->ino);
807 #endif
808 #endif /* WIN32 */
809 else
811 register unsigned int f = 0, im = 0;
812 register unsigned int j;
813 register struct dirfile *df;
814 for (j = 0; j < DIRFILE_BUCKETS; ++j)
815 for (df = dir->contents->files[j]; df != 0; df = df->next)
816 if (df->impossible)
817 ++im;
818 else
819 ++f;
820 #ifdef WIN32
821 printf ("# %s (key %s, mtime %d): ",
822 dir->name, dir->contents->path_key, dir->contents->mtime);
823 #else /* WIN32 */
824 #ifdef VMS
825 printf ("# %s (device %d, inode [%d,%d,%d]): ",
826 dir->name, dir->contents->dev,
827 dir->contents->ino[0], dir->contents->ino[1],
828 dir->contents->ino[2]);
829 #else
830 printf ("# %s (device %d, inode %d): ",
831 dir->name, dir->contents->dev, dir->contents->ino);
832 #endif
833 #endif /* WIN32 */
834 if (f == 0)
835 fputs ("No", stdout);
836 else
837 printf ("%u", f);
838 fputs (" files, ", stdout);
839 if (im == 0)
840 fputs ("no", stdout);
841 else
842 printf ("%u", im);
843 fputs (" impossibilities", stdout);
844 if (dir->contents->dirstream == 0)
845 puts (".");
846 else
847 puts (" so far.");
848 files += f;
849 impossible += im;
853 fputs ("\n# ", stdout);
854 if (files == 0)
855 fputs ("No", stdout);
856 else
857 printf ("%u", files);
858 fputs (" files, ", stdout);
859 if (impossible == 0)
860 fputs ("no", stdout);
861 else
862 printf ("%u", impossible);
863 printf (" impossibilities in %u directories.\n", dirs);
866 /* Hooks for globbing. */
868 #include <glob.h>
870 /* Structure describing state of iterating through a directory hash table. */
872 struct dirstream
874 struct directory_contents *contents; /* The directory being read. */
876 unsigned int bucket; /* Current hash bucket. */
877 struct dirfile *elt; /* Current elt in bucket. */
880 /* Forward declarations. */
881 static __ptr_t open_dirstream PARAMS ((const char *));
882 static struct dirent *read_dirstream PARAMS ((__ptr_t));
884 static __ptr_t
885 open_dirstream (directory)
886 const char *directory;
888 struct dirstream *new;
889 struct directory *dir = find_directory ((char *)directory);
891 if (dir->contents == 0 || dir->contents->files == 0)
892 /* DIR->contents is nil if the directory could not be stat'd.
893 DIR->contents->files is nil if it could not be opened. */
894 return 0;
896 /* Read all the contents of the directory now. There is no benefit
897 in being lazy, since glob will want to see every file anyway. */
899 (void) dir_contents_file_exists_p (dir->contents, (char *) 0);
901 new = (struct dirstream *) xmalloc (sizeof (struct dirstream));
902 new->contents = dir->contents;
903 new->bucket = 0;
904 new->elt = new->contents->files[0];
906 return (__ptr_t) new;
909 static struct dirent *
910 read_dirstream (stream)
911 __ptr_t stream;
913 struct dirstream *const ds = (struct dirstream *) stream;
914 register struct dirfile *df;
915 static char *buf;
916 static unsigned int bufsz;
918 while (ds->bucket < DIRFILE_BUCKETS)
920 while ((df = ds->elt) != 0)
922 ds->elt = df->next;
923 if (!df->impossible)
925 /* The glob interface wants a `struct dirent',
926 so mock one up. */
927 struct dirent *d;
928 unsigned int len = strlen (df->name) + 1;
929 if (sizeof *d - sizeof d->d_name + len > bufsz)
931 if (buf != 0)
932 free (buf);
933 bufsz *= 2;
934 if (sizeof *d - sizeof d->d_name + len > bufsz)
935 bufsz = sizeof *d - sizeof d->d_name + len;
936 buf = xmalloc (bufsz);
938 d = (struct dirent *) buf;
939 FAKE_DIR_ENTRY (d);
940 #ifdef _DIRENT_HAVE_D_NAMLEN
941 d->d_namlen = len - 1;
942 #endif
943 memcpy (d->d_name, df->name, len);
944 return d;
947 if (++ds->bucket == DIRFILE_BUCKETS)
948 break;
949 ds->elt = ds->contents->files[ds->bucket];
952 return 0;
955 void
956 dir_setup_glob (gl)
957 glob_t *gl;
959 extern int stat ();
961 /* Bogus sunos4 compiler complains (!) about & before functions. */
962 gl->gl_opendir = open_dirstream;
963 gl->gl_readdir = read_dirstream;
964 gl->gl_closedir = free;
965 gl->gl_stat = stat;
966 /* We don't bother setting gl_lstat, since glob never calls it.
967 The slot is only there for compatibility with 4.4 BSD. */