Sun Jul 14 12:59:27 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[make.git] / dir.c
blob11343dd529614fd36c39d02902cd9ee46d1e472d
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>
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 WIN32
101 #include "pathstuff.h"
102 #endif
104 #ifdef _AMIGA
105 #include <ctype.h>
107 static char *
108 amigafy (filename)
109 char *filename;
111 static char amiga_filename[136];
112 char *df;
113 int i;
115 if (filename == 0)
116 return 0;
118 df = amiga_filename;
120 /* First, transform the name part. */
121 for (i = 0; *filename != '\0'; ++i)
123 *df++ = tolower (*filename);
124 ++filename;
127 *df = 0;
129 return amiga_filename;
131 #endif /* _AMIGA */
133 #ifdef VMS
135 static int
136 vms_hash (name)
137 char *name;
139 int h = 0;
140 int g;
142 while (*name)
144 h = (h << 4) + *name++;
145 g = h & 0xf0000000;
146 if (g)
148 h = h ^ (g >> 24);
149 h = h ^ g;
152 return h;
155 /* fake stat entry for a directory */
156 static int
157 vmsstat_dir (name, st)
158 char *name;
159 struct stat *st;
161 char *s;
162 int h;
163 DIR *dir;
165 dir = opendir (name);
166 if (dir == 0)
167 return -1;
168 closedir (dir);
169 s = strchr (name, ':'); /* find device */
170 if (s)
172 *s++ = 0;
173 st->st_dev = (char *)vms_hash (name);
175 else
177 st->st_dev = 0;
178 s = name;
180 h = vms_hash (s);
181 st->st_ino[0] = h & 0xff;
182 st->st_ino[1] = h & 0xff00;
183 st->st_ino[2] = h >> 16;
185 return 0;
187 #endif /* VMS */
189 /* Hash table of directories. */
191 #ifndef DIRECTORY_BUCKETS
192 #define DIRECTORY_BUCKETS 199
193 #endif
195 struct directory_contents
197 struct directory_contents *next;
199 dev_t dev; /* Device and inode numbers of this dir. */
200 #ifdef WIN32
202 * Inode means nothing on WIN32. Even file key information is
203 * unreliable because it is random per file open and undefined
204 * for remote filesystems. The most unique attribute I can
205 * come up with is the fully qualified name of the directory. Beware
206 * though, this is also unreliable. I'm open to suggestion on a better
207 * way to emulate inode.
209 char *path_key;
210 int mtime; /* controls check for stale directory cache */
211 #else
212 #ifdef VMS
213 ino_t ino[3];
214 #else
215 ino_t ino;
216 #endif
217 #endif /* WIN32 */
218 struct dirfile **files; /* Files in this directory. */
219 DIR *dirstream; /* Stream reading this directory. */
222 /* Table of directory contents hashed by device and inode number. */
223 static struct directory_contents *directories_contents[DIRECTORY_BUCKETS];
225 struct directory
227 struct directory *next;
229 char *name; /* Name of the directory. */
231 /* The directory's contents. This data may be shared by several
232 entries in the hash table, which refer to the same directory
233 (identified uniquely by `dev' and `ino') under different names. */
234 struct directory_contents *contents;
237 /* Table of directories hashed by name. */
238 static struct directory *directories[DIRECTORY_BUCKETS];
241 /* Never have more than this many directories open at once. */
243 #define MAX_OPEN_DIRECTORIES 10
245 static unsigned int open_directories = 0;
248 /* Hash table of files in each directory. */
250 struct dirfile
252 struct dirfile *next;
253 char *name; /* Name of the file. */
254 char impossible; /* This file is impossible. */
257 #ifndef DIRFILE_BUCKETS
258 #define DIRFILE_BUCKETS 107
259 #endif
261 static int dir_contents_file_exists_p PARAMS ((struct directory_contents *dir, char *filename));
262 static struct directory *find_directory PARAMS ((char *name));
264 /* Find the directory named NAME and return its `struct directory'. */
266 static struct directory *
267 find_directory (name)
268 register char *name;
270 register unsigned int hash = 0;
271 register char *p;
272 register struct directory *dir;
273 #ifdef WIN32
274 char* w32_path;
275 #endif
276 #ifdef VMS
277 if ((*name == '.') && (*(name+1) == 0))
278 name = "[]";
279 else
280 name = vmsify (name,1);
281 #endif
283 for (p = name; *p != '\0'; ++p)
284 HASHI (hash, *p);
285 hash %= DIRECTORY_BUCKETS;
287 for (dir = directories[hash]; dir != 0; dir = dir->next)
288 if (strieq (dir->name, name))
289 break;
291 if (dir == 0)
293 struct stat st;
295 /* The directory was not found. Create a new entry for it. */
297 dir = (struct directory *) xmalloc (sizeof (struct directory));
298 dir->next = directories[hash];
299 directories[hash] = dir;
300 dir->name = savestring (name, p - name);
302 /* The directory is not in the name hash table.
303 Find its device and inode numbers, and look it up by them. */
305 #ifdef VMS
306 if (vmsstat_dir (name, &st) < 0)
307 #else
308 if (stat (name, &st) < 0)
309 #endif
311 /* Couldn't stat the directory. Mark this by
312 setting the `contents' member to a nil pointer. */
313 dir->contents = 0;
315 else
317 /* Search the contents hash table; device and inode are the key. */
319 struct directory_contents *dc;
321 #ifdef WIN32
322 w32_path = w32ify(name, 1);
323 hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ctime;
324 #else
325 #ifdef VMS
326 hash = ((unsigned int) st.st_dev << 16)
327 | ((unsigned int) st.st_ino[0]
328 + (unsigned int) st.st_ino[1]
329 + (unsigned int) st.st_ino[2]);
330 #else
331 hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ino;
332 #endif
333 #endif
334 hash %= DIRECTORY_BUCKETS;
336 for (dc = directories_contents[hash]; dc != 0; dc = dc->next)
337 #ifdef WIN32
338 if (!strcmp(dc->path_key, w32_path))
339 #else
340 if (dc->dev == st.st_dev
341 #ifdef VMS
342 && dc->ino[0] == st.st_ino[0]
343 && dc->ino[1] == st.st_ino[1]
344 && dc->ino[2] == st.st_ino[2])
345 #else
346 && dc->ino == st.st_ino)
347 #endif
348 #endif /* WIN32 */
349 break;
351 if (dc == 0)
353 /* Nope; this really is a directory we haven't seen before. */
355 dc = (struct directory_contents *)
356 xmalloc (sizeof (struct directory_contents));
358 /* Enter it in the contents hash table. */
359 dc->dev = st.st_dev;
360 #ifdef WIN32
361 dc->path_key = strdup(w32_path);
362 dc->mtime = st.st_mtime;
363 #else
364 #ifdef VMS
365 dc->ino[0] = st.st_ino[0];
366 dc->ino[1] = st.st_ino[1];
367 dc->ino[2] = st.st_ino[2];
368 #else
369 dc->ino = st.st_ino;
370 #endif
371 #endif /* WIN32 */
372 dc->next = directories_contents[hash];
373 directories_contents[hash] = dc;
375 dc->dirstream = opendir (name);
376 if (dc->dirstream == 0)
378 /* Couldn't open the directory. Mark this by
379 setting the `files' member to a nil pointer. */
380 dc->files = 0;
382 else
384 /* Allocate an array of buckets for files and zero it. */
385 dc->files = (struct dirfile **)
386 xmalloc (sizeof (struct dirfile *) * DIRFILE_BUCKETS);
387 bzero ((char *) dc->files,
388 sizeof (struct dirfile *) * DIRFILE_BUCKETS);
390 /* Keep track of how many directories are open. */
391 ++open_directories;
392 if (open_directories == MAX_OPEN_DIRECTORIES)
393 /* We have too many directories open already.
394 Read the entire directory and then close it. */
395 (void) dir_contents_file_exists_p (dc, (char *) 0);
399 /* Point the name-hashed entry for DIR at its contents data. */
400 dir->contents = dc;
404 return dir;
407 /* Return 1 if the name FILENAME is entered in DIR's hash table.
408 FILENAME must contain no slashes. */
410 static int
411 dir_contents_file_exists_p (dir, filename)
412 register struct directory_contents *dir;
413 register char *filename;
415 register unsigned int hash;
416 register char *p;
417 register struct dirfile *df;
418 register struct dirent *d;
419 #ifdef WIN32
420 struct stat st;
421 int rehash = 0;
422 #endif
424 if (dir == 0 || dir->files == 0)
426 /* The directory could not be stat'd or opened. */
427 return 0;
429 #ifdef __MSDOS__
430 filename = dosify (filename);
431 #endif
433 #ifdef _AMIGA
434 filename = amigafy (filename);
435 #endif
437 #ifdef VMS
438 filename = vmsify (filename,0);
439 #endif
441 hash = 0;
442 if (filename != 0)
444 if (*filename == '\0')
446 /* Checking if the directory exists. */
447 return 1;
450 for (p = filename; *p != '\0'; ++p)
451 HASH (hash, *p);
452 hash %= DIRFILE_BUCKETS;
454 /* Search the list of hashed files. */
456 for (df = dir->files[hash]; df != 0; df = df->next)
458 if (strieq (df->name, filename))
460 return !df->impossible;
465 /* The file was not found in the hashed list.
466 Try to read the directory further. */
468 if (dir->dirstream == 0)
470 #ifdef WIN32
471 /* Check to see if directory has changed since last read */
472 if (dir->path_key &&
473 stat(dir->path_key, &st) == 0 &&
474 st.st_mtime > dir->mtime) {
476 /* reset date stamp to show most recent re-process */
477 dir->mtime = st.st_mtime;
479 /* make sure directory can still be opened */
480 dir->dirstream = opendir(dir->path_key);
482 if (dir->dirstream)
483 rehash = 1;
484 else
485 return 0; /* couldn't re-read - fail */
486 } else
487 #endif
488 /* The directory has been all read in. */
489 return 0;
492 while ((d = readdir (dir->dirstream)) != 0)
494 /* Enter the file in the hash table. */
495 register unsigned int newhash = 0;
496 unsigned int len;
497 register unsigned int i;
499 if (!REAL_DIR_ENTRY (d))
500 continue;
502 len = NAMLEN (d);
503 for (i = 0; i < len; ++i)
504 HASHI (newhash, d->d_name[i]);
505 newhash %= DIRFILE_BUCKETS;
506 #ifdef WIN32
508 * If re-reading a directory, check that this file isn't already
509 * in the cache.
511 if (rehash) {
512 for (df = dir->files[newhash]; df != 0; df = df->next)
513 if (streq(df->name, d->d_name))
514 break;
515 } else
516 df = 0;
519 * If re-reading a directory, don't cache files that have
520 * already been discovered.
522 if (!df) {
523 #endif
525 df = (struct dirfile *) xmalloc (sizeof (struct dirfile));
526 df->next = dir->files[newhash];
527 dir->files[newhash] = df;
528 df->name = savestring (d->d_name, len);
529 df->impossible = 0;
530 #ifdef WIN32
532 #endif
533 /* Check if the name matches the one we're searching for. */
534 if (filename != 0
535 && newhash == hash && strieq (d->d_name, filename))
537 return 1;
541 /* If the directory has been completely read in,
542 close the stream and reset the pointer to nil. */
543 if (d == 0)
545 --open_directories;
546 closedir (dir->dirstream);
547 dir->dirstream = 0;
549 return 0;
552 /* Return 1 if the name FILENAME in directory DIRNAME
553 is entered in the dir hash table.
554 FILENAME must contain no slashes. */
557 dir_file_exists_p (dirname, filename)
558 register char *dirname;
559 register char *filename;
561 return dir_contents_file_exists_p (find_directory (dirname)->contents,
562 filename);
565 /* Return 1 if the file named NAME exists. */
568 file_exists_p (name)
569 register char *name;
571 char *dirend;
572 char *dirname;
574 #ifndef NO_ARCHIVES
575 if (ar_name (name))
576 return ar_member_date (name) != (time_t) -1;
577 #endif
579 #ifdef VMS
580 dirend = rindex (name, ']');
581 dirend++;
582 if (dirend == (char *)1)
583 return dir_file_exists_p ("[]", name);
584 #else /* !VMS */
585 dirend = rindex (name, '/');
586 #ifdef WIN32
587 if (!dirend)
588 dirend = rindex(name, '\\');
589 #endif /* WIN32 */
590 if (dirend == 0)
591 return dir_file_exists_p (".", name);
592 if (dirend == 0)
593 #ifndef _AMIGA
594 return dir_file_exists_p (".", name);
595 #else /* !VMS && !AMIGA */
596 return dir_file_exists_p ("", name);
597 #endif /* AMIGA */
598 #endif /* VMS */
600 dirname = (char *) alloca (dirend - name + 1);
601 bcopy (name, dirname, dirend - name);
602 dirname[dirend - name] = '\0';
603 return dir_file_exists_p (dirname, dirend + 1);
606 /* Mark FILENAME as `impossible' for `file_impossible_p'.
607 This means an attempt has been made to search for FILENAME
608 as an intermediate file, and it has failed. */
610 void
611 file_impossible (filename)
612 register char *filename;
614 char *dirend;
615 register char *p = filename;
616 register unsigned int hash;
617 register struct directory *dir;
618 register struct dirfile *new;
620 #ifdef VMS
621 dirend = rindex (p, ']');
622 dirend++;
623 if (dirend == (char *)1)
624 dir = find_directory ("[]");
625 #else
626 dirend = rindex (p, '/');
627 if (dirend == 0)
628 #ifdef _AMIGA
629 dir = find_directory ("");
630 #else /* !VMS && !AMIGA */
631 dir = find_directory (".");
632 #endif /* AMIGA */
633 #endif /* VMS */
634 else
636 char *dirname = (char *) alloca (dirend - p + 1);
637 bcopy (p, dirname, dirend - p);
638 dirname[dirend - p] = '\0';
639 dir = find_directory (dirname);
640 filename = p = dirend + 1;
643 for (hash = 0; *p != '\0'; ++p)
644 HASHI (hash, *p);
645 hash %= DIRFILE_BUCKETS;
647 if (dir->contents == 0)
649 /* The directory could not be stat'd. We allocate a contents
650 structure for it, but leave it out of the contents hash table. */
651 dir->contents = (struct directory_contents *)
652 xmalloc (sizeof (struct directory_contents));
653 #ifdef WIN32
654 dir->contents->path_key = NULL;
655 dir->contents->mtime = 0;
656 #else /* WIN32 */
657 #ifdef VMS
658 dir->contents->dev = 0;
659 dir->contents->ino[0] = dir->contents->ino[1] =
660 dir->contents->ino[2] = 0;
661 #else
662 dir->contents->dev = dir->contents->ino = 0;
663 #endif
664 #endif /* WIN32 */
665 dir->contents->files = 0;
666 dir->contents->dirstream = 0;
669 if (dir->contents->files == 0)
671 /* The directory was not opened; we must allocate the hash buckets. */
672 dir->contents->files = (struct dirfile **)
673 xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
674 bzero ((char *) dir->contents->files,
675 sizeof (struct dirfile) * DIRFILE_BUCKETS);
678 /* Make a new entry and put it in the table. */
680 new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
681 new->next = dir->contents->files[hash];
682 dir->contents->files[hash] = new;
683 new->name = savestring (filename, strlen (filename));
684 new->impossible = 1;
687 /* Return nonzero if FILENAME has been marked impossible. */
690 file_impossible_p (filename)
691 char *filename;
693 char *dirend;
694 register char *p = filename;
695 register unsigned int hash;
696 register struct directory_contents *dir;
697 register struct dirfile *next;
699 #ifdef VMS
700 dirend = rindex (filename, ']');
701 if (dirend == 0)
702 dir = find_directory ("[]")->contents;
703 #else
704 dirend = rindex (filename, '/');
705 #ifdef WIN32
706 if (!dirend)
707 dirend = rindex (filename, '\\');
708 #endif /* WIN32 */
709 if (dirend == 0)
710 #ifdef _AMIGA
711 dir = find_directory ("")->contents;
712 #else /* !VMS && !AMIGA */
713 dir = find_directory (".")->contents;
714 #endif /* AMIGA */
715 #endif /* VMS */
716 else
718 char *dirname = (char *) alloca (dirend - filename + 1);
719 bcopy (p, dirname, dirend - p);
720 dirname[dirend - p] = '\0';
721 dir = find_directory (dirname)->contents;
722 p = filename = dirend + 1;
725 if (dir == 0 || dir->files == 0)
726 /* There are no files entered for this directory. */
727 return 0;
729 #ifdef __MSDOS__
730 p = filename = dosify (p);
731 #endif
732 #ifdef _AMIGA
733 p = filename = amigafy (p);
734 #endif
735 #ifdef VMS
736 p = filename = vmsify (p, 1);
737 #endif
739 for (hash = 0; *p != '\0'; ++p)
740 HASH (hash, *p);
741 hash %= DIRFILE_BUCKETS;
743 for (next = dir->files[hash]; next != 0; next = next->next)
744 if (strieq (filename, next->name))
745 return next->impossible;
747 return 0;
750 /* Return the already allocated name in the
751 directory hash table that matches DIR. */
753 char *
754 dir_name (dir)
755 char *dir;
757 return find_directory (dir)->name;
760 /* Print the data base of directories. */
762 void
763 print_dir_data_base ()
765 register unsigned int i, dirs, files, impossible;
766 register struct directory *dir;
768 puts ("\n# Directories\n");
770 dirs = files = impossible = 0;
771 for (i = 0; i < DIRECTORY_BUCKETS; ++i)
772 for (dir = directories[i]; dir != 0; dir = dir->next)
774 ++dirs;
775 if (dir->contents == 0)
776 printf ("# %s: could not be stat'd.\n", dir->name);
777 else if (dir->contents->files == 0)
778 #ifdef WIN32
779 printf ("# %s (key %s, mtime %d): could not be opened.\n",
780 dir->name, dir->contents->path_key,dir->contents->mtime);
781 #else /* WIN32 */
782 #ifdef VMS
783 printf ("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n",
784 dir->name, dir->contents->dev,
785 dir->contents->ino[0], dir->contents->ino[1],
786 dir->contents->ino[2]);
787 #else
788 printf ("# %s (device %ld, inode %ld): could not be opened.\n",
789 dir->name, (long int) dir->contents->dev,
790 (long int) dir->contents->ino);
791 #endif
792 #endif /* WIN32 */
793 else
795 register unsigned int f = 0, im = 0;
796 register unsigned int j;
797 register struct dirfile *df;
798 for (j = 0; j < DIRFILE_BUCKETS; ++j)
799 for (df = dir->contents->files[j]; df != 0; df = df->next)
800 if (df->impossible)
801 ++im;
802 else
803 ++f;
804 #ifdef WIN32
805 printf ("# %s (key %s, mtime %d): ",
806 dir->name, dir->contents->path_key, dir->contents->mtime);
807 #else /* WIN32 */
808 #ifdef VMS
809 printf ("# %s (device %d, inode [%d,%d,%d]): ",
810 dir->name, dir->contents->dev,
811 dir->contents->ino[0], dir->contents->ino[1],
812 dir->contents->ino[2]);
813 #else
814 printf ("# %s (device %d, inode %d): ",
815 dir->name, dir->contents->dev, dir->contents->ino);
816 #endif
817 #endif /* WIN32 */
818 if (f == 0)
819 fputs ("No", stdout);
820 else
821 printf ("%u", f);
822 fputs (" files, ", stdout);
823 if (im == 0)
824 fputs ("no", stdout);
825 else
826 printf ("%u", im);
827 fputs (" impossibilities", stdout);
828 if (dir->contents->dirstream == 0)
829 puts (".");
830 else
831 puts (" so far.");
832 files += f;
833 impossible += im;
837 fputs ("\n# ", stdout);
838 if (files == 0)
839 fputs ("No", stdout);
840 else
841 printf ("%u", files);
842 fputs (" files, ", stdout);
843 if (impossible == 0)
844 fputs ("no", stdout);
845 else
846 printf ("%u", impossible);
847 printf (" impossibilities in %u directories.\n", dirs);
850 /* Hooks for globbing. */
852 #include <glob.h>
854 /* Structure describing state of iterating through a directory hash table. */
856 struct dirstream
858 struct directory_contents *contents; /* The directory being read. */
860 unsigned int bucket; /* Current hash bucket. */
861 struct dirfile *elt; /* Current elt in bucket. */
864 /* Forward declarations. */
865 static __ptr_t open_dirstream PARAMS ((const char *));
866 static struct dirent *read_dirstream PARAMS ((__ptr_t));
868 static __ptr_t
869 open_dirstream (directory)
870 const char *directory;
872 struct dirstream *new;
873 struct directory *dir = find_directory ((char *)directory);
875 if (dir->contents == 0 || dir->contents->files == 0)
876 /* DIR->contents is nil if the directory could not be stat'd.
877 DIR->contents->files is nil if it could not be opened. */
878 return 0;
880 /* Read all the contents of the directory now. There is no benefit
881 in being lazy, since glob will want to see every file anyway. */
883 (void) dir_contents_file_exists_p (dir->contents, (char *) 0);
885 new = (struct dirstream *) xmalloc (sizeof (struct dirstream));
886 new->contents = dir->contents;
887 new->bucket = 0;
888 new->elt = new->contents->files[0];
890 return (__ptr_t) new;
893 static struct dirent *
894 read_dirstream (stream)
895 __ptr_t stream;
897 struct dirstream *const ds = (struct dirstream *) stream;
898 register struct dirfile *df;
899 static char *buf;
900 static unsigned int bufsz;
902 while (ds->bucket < DIRFILE_BUCKETS)
904 while ((df = ds->elt) != 0)
906 ds->elt = df->next;
907 if (!df->impossible)
909 /* The glob interface wants a `struct dirent',
910 so mock one up. */
911 struct dirent *d;
912 unsigned int len = strlen (df->name) + 1;
913 if (sizeof *d - sizeof d->d_name + len > bufsz)
915 if (buf != 0)
916 free (buf);
917 bufsz *= 2;
918 if (sizeof *d - sizeof d->d_name + len > bufsz)
919 bufsz = sizeof *d - sizeof d->d_name + len;
920 buf = xmalloc (bufsz);
922 d = (struct dirent *) buf;
923 FAKE_DIR_ENTRY (d);
924 #ifdef _DIRENT_HAVE_D_NAMLEN
925 d->d_namlen = len - 1;
926 #endif
927 memcpy (d->d_name, df->name, len);
928 return d;
931 if (++ds->bucket == DIRFILE_BUCKETS)
932 break;
933 ds->elt = ds->contents->files[ds->bucket];
936 return 0;
939 void
940 dir_setup_glob (gl)
941 glob_t *gl;
943 extern int stat ();
945 /* Bogus sunos4 compiler complains (!) about & before functions. */
946 gl->gl_opendir = open_dirstream;
947 gl->gl_readdir = read_dirstream;
948 gl->gl_closedir = free;
949 gl->gl_stat = stat;
950 /* We don't bother setting gl_lstat, since glob never calls it.
951 The slot is only there for compatibility with 4.4 BSD. */