xref: /netbsd-src/usr.bin/make/dir.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: dir.c,v 1.26 2000/04/16 23:24:04 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * Copyright (c) 1988, 1989 by Adam de Boor
6  * Copyright (c) 1989 by Berkeley Softworks
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Adam de Boor.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #ifdef MAKE_BOOTSTRAP
42 static char rcsid[] = "$NetBSD: dir.c,v 1.26 2000/04/16 23:24:04 christos Exp $";
43 #else
44 #include <sys/cdefs.h>
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)dir.c	8.2 (Berkeley) 1/2/94";
48 #else
49 __RCSID("$NetBSD: dir.c,v 1.26 2000/04/16 23:24:04 christos Exp $");
50 #endif
51 #endif /* not lint */
52 #endif
53 
54 /*-
55  * dir.c --
56  *	Directory searching using wildcards and/or normal names...
57  *	Used both for source wildcarding in the Makefile and for finding
58  *	implicit sources.
59  *
60  * The interface for this module is:
61  *	Dir_Init  	    Initialize the module.
62  *
63  *	Dir_End  	    Cleanup the module.
64  *
65  *	Dir_HasWildcards    Returns TRUE if the name given it needs to
66  *	    	  	    be wildcard-expanded.
67  *
68  *	Dir_Expand	    Given a pattern and a path, return a Lst of names
69  *	    	  	    which match the pattern on the search path.
70  *
71  *	Dir_FindFile	    Searches for a file on a given search path.
72  *	    	  	    If it exists, the entire path is returned.
73  *	    	  	    Otherwise NULL is returned.
74  *
75  *	Dir_MTime 	    Return the modification time of a node. The file
76  *	    	  	    is searched for along the default search path.
77  *	    	  	    The path and mtime fields of the node are filled
78  *	    	  	    in.
79  *
80  *	Dir_AddDir	    Add a directory to a search path.
81  *
82  *	Dir_MakeFlags	    Given a search path and a command flag, create
83  *	    	  	    a string with each of the directories in the path
84  *	    	  	    preceded by the command flag and all of them
85  *	    	  	    separated by a space.
86  *
87  *	Dir_Destroy	    Destroy an element of a search path. Frees up all
88  *	    	  	    things that can be freed for the element as long
89  *	    	  	    as the element is no longer referenced by any other
90  *	    	  	    search path.
91  *	Dir_ClearPath	    Resets a search path to the empty list.
92  *
93  * For debugging:
94  *	Dir_PrintDirectories	Print stats about the directory cache.
95  */
96 
97 #include <stdio.h>
98 #include <errno.h>
99 #include <sys/types.h>
100 #include <dirent.h>
101 #include <sys/stat.h>
102 #include "make.h"
103 #include "hash.h"
104 #include "dir.h"
105 
106 /*
107  *	A search path consists of a Lst of Path structures. A Path structure
108  *	has in it the name of the directory and a hash table of all the files
109  *	in the directory. This is used to cut down on the number of system
110  *	calls necessary to find implicit dependents and their like. Since
111  *	these searches are made before any actions are taken, we need not
112  *	worry about the directory changing due to creation commands. If this
113  *	hampers the style of some makefiles, they must be changed.
114  *
115  *	A list of all previously-read directories is kept in the
116  *	openDirectories Lst. This list is checked first before a directory
117  *	is opened.
118  *
119  *	The need for the caching of whole directories is brought about by
120  *	the multi-level transformation code in suff.c, which tends to search
121  *	for far more files than regular make does. In the initial
122  *	implementation, the amount of time spent performing "stat" calls was
123  *	truly astronomical. The problem with hashing at the start is,
124  *	of course, that pmake doesn't then detect changes to these directories
125  *	during the course of the make. Three possibilities suggest themselves:
126  *
127  *	    1) just use stat to test for a file's existence. As mentioned
128  *	       above, this is very inefficient due to the number of checks
129  *	       engendered by the multi-level transformation code.
130  *	    2) use readdir() and company to search the directories, keeping
131  *	       them open between checks. I have tried this and while it
132  *	       didn't slow down the process too much, it could severely
133  *	       affect the amount of parallelism available as each directory
134  *	       open would take another file descriptor out of play for
135  *	       handling I/O for another job. Given that it is only recently
136  *	       that UNIX OS's have taken to allowing more than 20 or 32
137  *	       file descriptors for a process, this doesn't seem acceptable
138  *	       to me.
139  *	    3) record the mtime of the directory in the Path structure and
140  *	       verify the directory hasn't changed since the contents were
141  *	       hashed. This will catch the creation or deletion of files,
142  *	       but not the updating of files. However, since it is the
143  *	       creation and deletion that is the problem, this could be
144  *	       a good thing to do. Unfortunately, if the directory (say ".")
145  *	       were fairly large and changed fairly frequently, the constant
146  *	       rehashing could seriously degrade performance. It might be
147  *	       good in such cases to keep track of the number of rehashes
148  *	       and if the number goes over a (small) limit, resort to using
149  *	       stat in its place.
150  *
151  *	An additional thing to consider is that pmake is used primarily
152  *	to create C programs and until recently pcc-based compilers refused
153  *	to allow you to specify where the resulting object file should be
154  *	placed. This forced all objects to be created in the current
155  *	directory. This isn't meant as a full excuse, just an explanation of
156  *	some of the reasons for the caching used here.
157  *
158  *	One more note: the location of a target's file is only performed
159  *	on the downward traversal of the graph and then only for terminal
160  *	nodes in the graph. This could be construed as wrong in some cases,
161  *	but prevents inadvertent modification of files when the "installed"
162  *	directory for a file is provided in the search path.
163  *
164  *	Another data structure maintained by this module is an mtime
165  *	cache used when the searching of cached directories fails to find
166  *	a file. In the past, Dir_FindFile would simply perform an access()
167  *	call in such a case to determine if the file could be found using
168  *	just the name given. When this hit, however, all that was gained
169  *	was the knowledge that the file existed. Given that an access() is
170  *	essentially a stat() without the copyout() call, and that the same
171  *	filesystem overhead would have to be incurred in Dir_MTime, it made
172  *	sense to replace the access() with a stat() and record the mtime
173  *	in a cache for when Dir_MTime was actually called.
174  */
175 
176 Lst          dirSearchPath;	/* main search path */
177 
178 static Lst   openDirectories;	/* the list of all open directories */
179 
180 /*
181  * Variables for gathering statistics on the efficiency of the hashing
182  * mechanism.
183  */
184 static int    hits,	      /* Found in directory cache */
185 	      misses,	      /* Sad, but not evil misses */
186 	      nearmisses,     /* Found under search path */
187 	      bigmisses;      /* Sought by itself */
188 
189 static Path    	  *dot;	    /* contents of current directory */
190 static Path    	  *cur;	    /* contents of current directory, if not dot */
191 static Path	  *dotLast; /* a fake path entry indicating we need to
192 			     * look for . last */
193 static Hash_Table mtimes;   /* Results of doing a last-resort stat in
194 			     * Dir_FindFile -- if we have to go to the
195 			     * system to find the file, we might as well
196 			     * have its mtime on record. XXX: If this is done
197 			     * way early, there's a chance other rules will
198 			     * have already updated the file, in which case
199 			     * we'll update it again. Generally, there won't
200 			     * be two rules to update a single file, so this
201 			     * should be ok, but... */
202 
203 
204 static int DirFindName __P((ClientData, ClientData));
205 static int DirMatchFiles __P((char *, Path *, Lst));
206 static void DirExpandCurly __P((char *, char *, Lst, Lst));
207 static void DirExpandInt __P((char *, Lst, Lst));
208 static int DirPrintWord __P((ClientData, ClientData));
209 static int DirPrintDir __P((ClientData, ClientData));
210 static char *DirLookup __P((Path *, char *, char *, Boolean));
211 static char *DirLookupSubdir __P((Path *, char *));
212 static char *DirFindDot __P((Boolean, char *, char *));
213 
214 /*-
215  *-----------------------------------------------------------------------
216  * Dir_Init --
217  *	initialize things for this module
218  *
219  * Results:
220  *	none
221  *
222  * Side Effects:
223  *	some directories may be opened.
224  *-----------------------------------------------------------------------
225  */
226 void
227 Dir_Init (cdname)
228     const char *cdname;
229 {
230     dirSearchPath = Lst_Init (FALSE);
231     openDirectories = Lst_Init (FALSE);
232     Hash_InitTable(&mtimes, 0);
233 
234     /*
235      * Since the Path structure is placed on both openDirectories and
236      * the path we give Dir_AddDir (which in this case is openDirectories),
237      * we need to remove "." from openDirectories and what better time to
238      * do it than when we have to fetch the thing anyway?
239      */
240     dot = Dir_AddDir (NULL, ".");
241     if (dot == NULL) {
242 	Error("Cannot open `.' (%s)", strerror(errno));
243 	exit(1);
244     }
245 
246     /*
247      * We always need to have dot around, so we increment its reference count
248      * to make sure it's not destroyed.
249      */
250     dot->refCount += 1;
251 
252     if (cdname != NULL) {
253 	/*
254 	 * Our build directory is not the same as our source directory.
255 	 * Keep this one around too.
256 	 */
257 	cur = Dir_AddDir (NULL, cdname);
258 	cur->refCount += 1;
259     }
260 
261     dotLast = (Path *) emalloc (sizeof (Path));
262     dotLast->refCount = 1;
263     dotLast->hits = 0;
264     dotLast->name = estrdup(".DOTLAST");
265     Hash_InitTable (&dotLast->files, -1);
266 }
267 
268 /*-
269  *-----------------------------------------------------------------------
270  * Dir_End --
271  *	cleanup things for this module
272  *
273  * Results:
274  *	none
275  *
276  * Side Effects:
277  *	none
278  *-----------------------------------------------------------------------
279  */
280 void
281 Dir_End()
282 {
283 #ifdef CLEANUP
284     if (cur) {
285 	cur->refCount -= 1;
286 	Dir_Destroy((ClientData) cur);
287     }
288     dot->refCount -= 1;
289     dotLast->refCount -= 1;
290     Dir_Destroy((ClientData) dotLast);
291     Dir_Destroy((ClientData) dot);
292     Dir_ClearPath(dirSearchPath);
293     Lst_Destroy(dirSearchPath, NOFREE);
294     Dir_ClearPath(openDirectories);
295     Lst_Destroy(openDirectories, NOFREE);
296     Hash_DeleteTable(&mtimes);
297 #endif
298 }
299 
300 /*-
301  *-----------------------------------------------------------------------
302  * DirFindName --
303  *	See if the Path structure describes the same directory as the
304  *	given one by comparing their names. Called from Dir_AddDir via
305  *	Lst_Find when searching the list of open directories.
306  *
307  * Results:
308  *	0 if it is the same. Non-zero otherwise
309  *
310  * Side Effects:
311  *	None
312  *-----------------------------------------------------------------------
313  */
314 static int
315 DirFindName (p, dname)
316     ClientData    p;	      /* Current name */
317     ClientData	  dname;      /* Desired name */
318 {
319     return (strcmp (((Path *)p)->name, (char *) dname));
320 }
321 
322 /*-
323  *-----------------------------------------------------------------------
324  * Dir_HasWildcards  --
325  *	see if the given name has any wildcard characters in it
326  *	be careful not to expand unmatching brackets or braces.
327  *	XXX: This code is not 100% correct. ([^]] fails etc.)
328  *	I really don't think that make(1) should be expanding
329  *	patterns, because then you have to set a mechanism for
330  *	escaping the expansion!
331  *
332  * Results:
333  *	returns TRUE if the word should be expanded, FALSE otherwise
334  *
335  * Side Effects:
336  *	none
337  *-----------------------------------------------------------------------
338  */
339 Boolean
340 Dir_HasWildcards (name)
341     char          *name;	/* name to check */
342 {
343     register char *cp;
344     int wild = 0, brace = 0, bracket = 0;
345 
346     for (cp = name; *cp; cp++) {
347 	switch(*cp) {
348 	case '{':
349 		brace++;
350 		wild = 1;
351 		break;
352 	case '}':
353 		brace--;
354 		break;
355 	case '[':
356 		bracket++;
357 		wild = 1;
358 		break;
359 	case ']':
360 		bracket--;
361 		break;
362 	case '?':
363 	case '*':
364 		wild = 1;
365 		break;
366 	default:
367 		break;
368 	}
369     }
370     return wild && bracket == 0 && brace == 0;
371 }
372 
373 /*-
374  *-----------------------------------------------------------------------
375  * DirMatchFiles --
376  * 	Given a pattern and a Path structure, see if any files
377  *	match the pattern and add their names to the 'expansions' list if
378  *	any do. This is incomplete -- it doesn't take care of patterns like
379  *	src / *src / *.c properly (just *.c on any of the directories), but it
380  *	will do for now.
381  *
382  * Results:
383  *	Always returns 0
384  *
385  * Side Effects:
386  *	File names are added to the expansions lst. The directory will be
387  *	fully hashed when this is done.
388  *-----------------------------------------------------------------------
389  */
390 static int
391 DirMatchFiles (pattern, p, expansions)
392     char	  *pattern;   	/* Pattern to look for */
393     Path	  *p;         	/* Directory to search */
394     Lst	    	  expansions;	/* Place to store the results */
395 {
396     Hash_Search	  search;   	/* Index into the directory's table */
397     Hash_Entry	  *entry;   	/* Current entry in the table */
398     Boolean 	  isDot;    	/* TRUE if the directory being searched is . */
399 
400     isDot = (*p->name == '.' && p->name[1] == '\0');
401 
402     for (entry = Hash_EnumFirst(&p->files, &search);
403 	 entry != (Hash_Entry *)NULL;
404 	 entry = Hash_EnumNext(&search))
405     {
406 	/*
407 	 * See if the file matches the given pattern. Note we follow the UNIX
408 	 * convention that dot files will only be found if the pattern
409 	 * begins with a dot (note also that as a side effect of the hashing
410 	 * scheme, .* won't match . or .. since they aren't hashed).
411 	 */
412 	if (Str_Match(entry->name, pattern) &&
413 	    ((entry->name[0] != '.') ||
414 	     (pattern[0] == '.')))
415 	{
416 	    (void)Lst_AtEnd(expansions,
417 			    (isDot ? estrdup(entry->name) :
418 			     str_concat(p->name, entry->name,
419 					STR_ADDSLASH)));
420 	}
421     }
422     return (0);
423 }
424 
425 /*-
426  *-----------------------------------------------------------------------
427  * DirExpandCurly --
428  *	Expand curly braces like the C shell. Does this recursively.
429  *	Note the special case: if after the piece of the curly brace is
430  *	done there are no wildcard characters in the result, the result is
431  *	placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.
432  *
433  * Results:
434  *	None.
435  *
436  * Side Effects:
437  *	The given list is filled with the expansions...
438  *
439  *-----------------------------------------------------------------------
440  */
441 static void
442 DirExpandCurly(word, brace, path, expansions)
443     char    	  *word;    	/* Entire word to expand */
444     char    	  *brace;   	/* First curly brace in it */
445     Lst	    	  path;	    	/* Search path to use */
446     Lst	    	  expansions;	/* Place to store the expansions */
447 {
448     char    	  *end;	    	/* Character after the closing brace */
449     char    	  *cp;	    	/* Current position in brace clause */
450     char    	  *start;   	/* Start of current piece of brace clause */
451     int	    	  bracelevel;	/* Number of braces we've seen. If we see a
452 				 * right brace when this is 0, we've hit the
453 				 * end of the clause. */
454     char    	  *file;    	/* Current expansion */
455     int	    	  otherLen; 	/* The length of the other pieces of the
456 				 * expansion (chars before and after the
457 				 * clause in 'word') */
458     char    	  *cp2;	    	/* Pointer for checking for wildcards in
459 				 * expansion before calling Dir_Expand */
460 
461     start = brace+1;
462 
463     /*
464      * Find the end of the brace clause first, being wary of nested brace
465      * clauses.
466      */
467     for (end = start, bracelevel = 0; *end != '\0'; end++) {
468 	if (*end == '{') {
469 	    bracelevel++;
470 	} else if ((*end == '}') && (bracelevel-- == 0)) {
471 	    break;
472 	}
473     }
474     if (*end == '\0') {
475 	Error("Unterminated {} clause \"%s\"", start);
476 	return;
477     } else {
478 	end++;
479     }
480     otherLen = brace - word + strlen(end);
481 
482     for (cp = start; cp < end; cp++) {
483 	/*
484 	 * Find the end of this piece of the clause.
485 	 */
486 	bracelevel = 0;
487 	while (*cp != ',') {
488 	    if (*cp == '{') {
489 		bracelevel++;
490 	    } else if ((*cp == '}') && (bracelevel-- <= 0)) {
491 		break;
492 	    }
493 	    cp++;
494 	}
495 	/*
496 	 * Allocate room for the combination and install the three pieces.
497 	 */
498 	file = emalloc(otherLen + cp - start + 1);
499 	if (brace != word) {
500 	    strncpy(file, word, brace-word);
501 	}
502 	if (cp != start) {
503 	    strncpy(&file[brace-word], start, cp-start);
504 	}
505 	strcpy(&file[(brace-word)+(cp-start)], end);
506 
507 	/*
508 	 * See if the result has any wildcards in it. If we find one, call
509 	 * Dir_Expand right away, telling it to place the result on our list
510 	 * of expansions.
511 	 */
512 	for (cp2 = file; *cp2 != '\0'; cp2++) {
513 	    switch(*cp2) {
514 	    case '*':
515 	    case '?':
516 	    case '{':
517 	    case '[':
518 		Dir_Expand(file, path, expansions);
519 		goto next;
520 	    }
521 	}
522 	if (*cp2 == '\0') {
523 	    /*
524 	     * Hit the end w/o finding any wildcards, so stick the expansion
525 	     * on the end of the list.
526 	     */
527 	    (void)Lst_AtEnd(expansions, file);
528 	} else {
529 	next:
530 	    free(file);
531 	}
532 	start = cp+1;
533     }
534 }
535 
536 
537 /*-
538  *-----------------------------------------------------------------------
539  * DirExpandInt --
540  *	Internal expand routine. Passes through the directories in the
541  *	path one by one, calling DirMatchFiles for each. NOTE: This still
542  *	doesn't handle patterns in directories...
543  *
544  * Results:
545  *	None.
546  *
547  * Side Effects:
548  *	Things are added to the expansions list.
549  *
550  *-----------------------------------------------------------------------
551  */
552 static void
553 DirExpandInt(word, path, expansions)
554     char    	  *word;    	/* Word to expand */
555     Lst	    	  path;	    	/* Path on which to look */
556     Lst	    	  expansions;	/* Place to store the result */
557 {
558     LstNode 	  ln;	    	/* Current node */
559     Path	  *p;	    	/* Directory in the node */
560 
561     if (Lst_Open(path) == SUCCESS) {
562 	while ((ln = Lst_Next(path)) != NILLNODE) {
563 	    p = (Path *)Lst_Datum(ln);
564 	    DirMatchFiles(word, p, expansions);
565 	}
566 	Lst_Close(path);
567     }
568 }
569 
570 /*-
571  *-----------------------------------------------------------------------
572  * DirPrintWord --
573  *	Print a word in the list of expansions. Callback for Dir_Expand
574  *	when DEBUG(DIR), via Lst_ForEach.
575  *
576  * Results:
577  *	=== 0
578  *
579  * Side Effects:
580  *	The passed word is printed, followed by a space.
581  *
582  *-----------------------------------------------------------------------
583  */
584 static int
585 DirPrintWord(word, dummy)
586     ClientData  word;
587     ClientData  dummy;
588 {
589     printf("%s ", (char *) word);
590 
591     return(dummy ? 0 : 0);
592 }
593 
594 /*-
595  *-----------------------------------------------------------------------
596  * Dir_Expand  --
597  *	Expand the given word into a list of words by globbing it looking
598  *	in the directories on the given search path.
599  *
600  * Results:
601  *	A list of words consisting of the files which exist along the search
602  *	path matching the given pattern.
603  *
604  * Side Effects:
605  *	Directories may be opened. Who knows?
606  *-----------------------------------------------------------------------
607  */
608 void
609 Dir_Expand (word, path, expansions)
610     char    *word;      /* the word to expand */
611     Lst     path;   	/* the list of directories in which to find
612 			 * the resulting files */
613     Lst	    expansions;	/* the list on which to place the results */
614 {
615     char    	  *cp;
616 
617     if (DEBUG(DIR)) {
618 	printf("expanding \"%s\"...", word);
619     }
620 
621     cp = strchr(word, '{');
622     if (cp) {
623 	DirExpandCurly(word, cp, path, expansions);
624     } else {
625 	cp = strchr(word, '/');
626 	if (cp) {
627 	    /*
628 	     * The thing has a directory component -- find the first wildcard
629 	     * in the string.
630 	     */
631 	    for (cp = word; *cp; cp++) {
632 		if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') {
633 		    break;
634 		}
635 	    }
636 	    if (*cp == '{') {
637 		/*
638 		 * This one will be fun.
639 		 */
640 		DirExpandCurly(word, cp, path, expansions);
641 		return;
642 	    } else if (*cp != '\0') {
643 		/*
644 		 * Back up to the start of the component
645 		 */
646 		char  *dirpath;
647 
648 		while (cp > word && *cp != '/') {
649 		    cp--;
650 		}
651 		if (cp != word) {
652 		    char sc;
653 		    /*
654 		     * If the glob isn't in the first component, try and find
655 		     * all the components up to the one with a wildcard.
656 		     */
657 		    sc = cp[1];
658 		    cp[1] = '\0';
659 		    dirpath = Dir_FindFile(word, path);
660 		    cp[1] = sc;
661 		    /*
662 		     * dirpath is null if can't find the leading component
663 		     * XXX: Dir_FindFile won't find internal components.
664 		     * i.e. if the path contains ../Etc/Object and we're
665 		     * looking for Etc, it won't be found. Ah well.
666 		     * Probably not important.
667 		     */
668 		    if (dirpath != (char *)NULL) {
669 			char *dp = &dirpath[strlen(dirpath) - 1];
670 			if (*dp == '/')
671 			    *dp = '\0';
672 			path = Lst_Init(FALSE);
673 			(void) Dir_AddDir(path, dirpath);
674 			DirExpandInt(cp+1, path, expansions);
675 			Lst_Destroy(path, NOFREE);
676 		    }
677 		} else {
678 		    /*
679 		     * Start the search from the local directory
680 		     */
681 		    DirExpandInt(word, path, expansions);
682 		}
683 	    } else {
684 		/*
685 		 * Return the file -- this should never happen.
686 		 */
687 		DirExpandInt(word, path, expansions);
688 	    }
689 	} else {
690 	    /*
691 	     * First the files in dot
692 	     */
693 	    DirMatchFiles(word, dot, expansions);
694 
695 	    /*
696 	     * Then the files in every other directory on the path.
697 	     */
698 	    DirExpandInt(word, path, expansions);
699 	}
700     }
701     if (DEBUG(DIR)) {
702 	Lst_ForEach(expansions, DirPrintWord, (ClientData) 0);
703 	fputc('\n', stdout);
704     }
705 }
706 
707 /*-
708  *-----------------------------------------------------------------------
709  * DirLookup  --
710  *	Find if the file with the given name exists in the given path.
711  *
712  * Results:
713  *	The path to the file, the empty string or NULL. If the file is
714  *	the empty string, the search should be terminated.
715  *	This path is guaranteed to be in a
716  *	different part of memory than name and so may be safely free'd.
717  *
718  * Side Effects:
719  *	None.
720  *-----------------------------------------------------------------------
721  */
722 static char *
723 DirLookup(p, name, cp, hasSlash)
724     Path *p;
725     char *name;
726     char *cp;
727     Boolean hasSlash;
728 {
729     char *p1;		/* pointer into p->name */
730     char *p2;		/* pointer into name */
731     char *file;		/* the current filename to check */
732 
733     if (DEBUG(DIR)) {
734 	printf("%s...", p->name);
735     }
736     if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
737 	if (DEBUG(DIR)) {
738 	    printf("here...");
739 	}
740 	if (hasSlash) {
741 	    /*
742 	     * If the name had a slash, its initial components and p's
743 	     * final components must match. This is false if a mismatch
744 	     * is encountered before all of the initial components
745 	     * have been checked (p2 > name at the end of the loop), or
746 	     * we matched only part of one of the components of p
747 	     * along with all the rest of them (*p1 != '/').
748 	     */
749 	    p1 = p->name + strlen (p->name) - 1;
750 	    p2 = cp - 2;
751 	    while (p2 >= name && p1 >= p->name && *p1 == *p2) {
752 		p1 -= 1; p2 -= 1;
753 	    }
754 	    if (p2 >= name || (p1 >= p->name && *p1 != '/')) {
755 		if (DEBUG(DIR)) {
756 		    printf("component mismatch -- continuing...");
757 		}
758 		return NULL;
759 	    }
760 	}
761 	file = str_concat (p->name, cp, STR_ADDSLASH);
762 	if (DEBUG(DIR)) {
763 	    printf("returning %s\n", file);
764 	}
765 	p->hits += 1;
766 	hits += 1;
767 	return file;
768     } else if (hasSlash) {
769 	/*
770 	 * If the file has a leading path component and that component
771 	 * exactly matches the entire name of the current search
772 	 * directory, we assume the file doesn't exist and return NULL.
773 	 */
774 	for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) {
775 	    continue;
776 	}
777 	if (*p1 == '\0' && p2 == cp - 1) {
778 	    if (DEBUG(DIR)) {
779 		printf("must be here but isn't -- returing\n");
780 	    }
781 	    return "";
782 	}
783     }
784     return NULL;
785 }
786 
787 
788 /*-
789  *-----------------------------------------------------------------------
790  * DirLookupSubdir  --
791  *	Find if the file with the given name exists in the given path.
792  *
793  * Results:
794  *	The path to the file or NULL. This path is guaranteed to be in a
795  *	different part of memory than name and so may be safely free'd.
796  *
797  * Side Effects:
798  *	If the file is found, it is added in the modification times hash
799  *	table.
800  *-----------------------------------------------------------------------
801  */
802 static char *
803 DirLookupSubdir(p, name)
804     Path *p;
805     char *name;
806 {
807     struct stat	  stb;		/* Buffer for stat, if necessary */
808     Hash_Entry	 *entry;	/* Entry for mtimes table */
809     char 	 *file;		/* the current filename to check */
810 
811     if (p != dot) {
812 	file = str_concat (p->name, name, STR_ADDSLASH);
813     } else {
814 	/*
815 	 * Checking in dot -- DON'T put a leading ./ on the thing.
816 	 */
817 	file = estrdup(name);
818     }
819 
820     if (DEBUG(DIR)) {
821 	printf("checking %s...", file);
822     }
823 
824     if (stat (file, &stb) == 0) {
825 	if (DEBUG(DIR)) {
826 	    printf("got it.\n");
827 	}
828 
829 	/*
830 	 * Save the modification time so if it's needed, we don't have
831 	 * to fetch it again.
832 	 */
833 	if (DEBUG(DIR)) {
834 	    printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
835 		    file);
836 	}
837 	entry = Hash_CreateEntry(&mtimes, (char *) file,
838 				 (Boolean *)NULL);
839 	Hash_SetValue(entry, (long)stb.st_mtime);
840 	nearmisses += 1;
841 	return (file);
842     }
843     free (file);
844     return NULL;
845 }
846 
847 /*-
848  *-----------------------------------------------------------------------
849  * DirFindDot  --
850  *	Find the file given on "." or curdir
851  *
852  * Results:
853  *	The path to the file or NULL. This path is guaranteed to be in a
854  *	different part of memory than name and so may be safely free'd.
855  *
856  * Side Effects:
857  *	Hit counts change
858  *-----------------------------------------------------------------------
859  */
860 static char *
861 DirFindDot(hasSlash, name, cp)
862     Boolean hasSlash;
863     char *name;
864     char *cp;
865 {
866     char *file;
867 
868     if ((!hasSlash || (cp - name == 2 && *name == '.'))) {
869 	if (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL) {
870 	    if (DEBUG(DIR)) {
871 		printf("in '.'\n");
872 	    }
873 	    hits += 1;
874 	    dot->hits += 1;
875 	    return (estrdup (name));
876 	}
877 	if (cur &&
878 	    Hash_FindEntry (&cur->files, cp) != (Hash_Entry *)NULL) {
879 	    if (DEBUG(DIR)) {
880 		printf("in ${.CURDIR} = %s\n", cur->name);
881 	    }
882 	    hits += 1;
883 	    cur->hits += 1;
884 	    return str_concat (cur->name, cp, STR_ADDSLASH);
885 	}
886     }
887 
888 
889     if (cur && (file = DirLookup(cur, name, cp, hasSlash)) != NULL) {
890 	if (*file)
891 	    return file;
892 	else
893 	    return NULL;
894     }
895     return NULL;
896 }
897 
898 /*-
899  *-----------------------------------------------------------------------
900  * Dir_FindFile  --
901  *	Find the file with the given name along the given search path.
902  *
903  * Results:
904  *	The path to the file or NULL. This path is guaranteed to be in a
905  *	different part of memory than name and so may be safely free'd.
906  *
907  * Side Effects:
908  *	If the file is found in a directory which is not on the path
909  *	already (either 'name' is absolute or it is a relative path
910  *	[ dir1/.../dirn/file ] which exists below one of the directories
911  *	already on the search path), its directory is added to the end
912  *	of the path on the assumption that there will be more files in
913  *	that directory later on. Sometimes this is true. Sometimes not.
914  *-----------------------------------------------------------------------
915  */
916 char *
917 Dir_FindFile (name, path)
918     char    	  *name;    /* the file to find */
919     Lst           path;	    /* the Lst of directories to search */
920 {
921     LstNode       ln;			/* a list element */
922     register char *file;		/* the current filename to check */
923     register Path *p;			/* current path member */
924     register char *cp;			/* index of first slash, if any */
925     Boolean	  lastDot = FALSE;	/* true we should search dot last */
926     Boolean	  hasSlash;		/* true if 'name' contains a / */
927     struct stat	  stb;			/* Buffer for stat, if necessary */
928     Hash_Entry	  *entry;		/* Entry for mtimes table */
929 
930     /*
931      * Find the final component of the name and note whether it has a
932      * slash in it (the name, I mean)
933      */
934     cp = strrchr (name, '/');
935     if (cp) {
936 	hasSlash = TRUE;
937 	cp += 1;
938     } else {
939 	hasSlash = FALSE;
940 	cp = name;
941     }
942 
943     if (DEBUG(DIR)) {
944 	printf("Searching for %s...", name);
945     }
946 
947     if (Lst_Open (path) == FAILURE) {
948 	if (DEBUG(DIR)) {
949 	    printf("couldn't open path, file not found\n");
950 	}
951 	misses += 1;
952 	return ((char *) NULL);
953     }
954 
955     if ((ln = Lst_First (path)) != NILLNODE) {
956 	p = (Path *) Lst_Datum (ln);
957 	if (p == dotLast)
958 	    lastDot = TRUE;
959 	if (DEBUG(DIR)) {
960 	    printf("[dot last]...");
961 	}
962     }
963 
964     /*
965      * No matter what, we always look for the file in the current directory
966      * before anywhere else and we *do not* add the ./ to it if it exists.
967      * This is so there are no conflicts between what the user specifies
968      * (fish.c) and what pmake finds (./fish.c).
969      * Unless we found the magic DOTLAST path...
970      */
971     if (!lastDot && (file = DirFindDot(hasSlash, name, cp)) != NULL)
972 	return file;
973 
974 
975     /*
976      * We look through all the directories on the path seeking one which
977      * contains the final component of the given name and whose final
978      * component(s) match the name's initial component(s). If such a beast
979      * is found, we concatenate the directory name and the final component
980      * and return the resulting string. If we don't find any such thing,
981      * we go on to phase two...
982      */
983     while ((ln = Lst_Next (path)) != NILLNODE) {
984 	p = (Path *) Lst_Datum (ln);
985 	if (p == dotLast)
986 	    continue;
987         if ((file = DirLookup(p, name, cp, hasSlash)) != NULL) {
988 	    Lst_Close (path);
989 	    if (*file)
990 		return file;
991 	    else
992 		return NULL;
993 	}
994     }
995 
996     if (lastDot && (file = DirFindDot(hasSlash, name, cp)) != NULL)
997 	return file;
998 
999     /*
1000      * We didn't find the file on any existing members of the directory.
1001      * If the name doesn't contain a slash, that means it doesn't exist.
1002      * If it *does* contain a slash, however, there is still hope: it
1003      * could be in a subdirectory of one of the members of the search
1004      * path. (eg. /usr/include and sys/types.h. The above search would
1005      * fail to turn up types.h in /usr/include, but it *is* in
1006      * /usr/include/sys/types.h) If we find such a beast, we assume there
1007      * will be more (what else can we assume?) and add all but the last
1008      * component of the resulting name onto the search path (at the
1009      * end). This phase is only performed if the file is *not* absolute.
1010      */
1011     if (!hasSlash) {
1012 	if (DEBUG(DIR)) {
1013 	    printf("failed.\n");
1014 	}
1015 	misses += 1;
1016 	return ((char *) NULL);
1017     }
1018 
1019     if (*name != '/') {
1020 	Boolean	checkedDot = FALSE;
1021 
1022 	if (DEBUG(DIR)) {
1023 	    printf("failed. Trying subdirectories...");
1024 	}
1025 
1026 	if (!lastDot && cur && (file = DirLookupSubdir(cur, name)) != NULL)
1027 	    return file;
1028 
1029 	(void) Lst_Open (path);
1030 	while ((ln = Lst_Next (path)) != NILLNODE) {
1031 	    p = (Path *) Lst_Datum (ln);
1032 	    if (p == dotLast)
1033 		continue;
1034 	    if (p == dot)
1035 		checkedDot = TRUE;
1036 	    if ((file = DirLookupSubdir(p, name)) != NULL) {
1037 		Lst_Close (path);
1038 		return file;
1039 	    }
1040 	}
1041 
1042 	if (lastDot && cur && (file = DirLookupSubdir(cur, name)) != NULL)
1043 	    return file;
1044 
1045 	if (DEBUG(DIR)) {
1046 	    printf("failed. ");
1047 	}
1048 	Lst_Close (path);
1049 
1050 	if (checkedDot) {
1051 	    /*
1052 	     * Already checked by the given name, since . was in the path,
1053 	     * so no point in proceeding...
1054 	     */
1055 	    if (DEBUG(DIR)) {
1056 		printf("Checked . already, returning NULL\n");
1057 	    }
1058 	    return(NULL);
1059 	}
1060     }
1061 
1062     /*
1063      * Didn't find it that way, either. Sigh. Phase 3. Add its directory
1064      * onto the search path in any case, just in case, then look for the
1065      * thing in the hash table. If we find it, grand. We return a new
1066      * copy of the name. Otherwise we sadly return a NULL pointer. Sigh.
1067      * Note that if the directory holding the file doesn't exist, this will
1068      * do an extra search of the final directory on the path. Unless something
1069      * weird happens, this search won't succeed and life will be groovy.
1070      *
1071      * Sigh. We cannot add the directory onto the search path because
1072      * of this amusing case:
1073      * $(INSTALLDIR)/$(FILE): $(FILE)
1074      *
1075      * $(FILE) exists in $(INSTALLDIR) but not in the current one.
1076      * When searching for $(FILE), we will find it in $(INSTALLDIR)
1077      * b/c we added it here. This is not good...
1078      */
1079 #ifdef notdef
1080     cp[-1] = '\0';
1081     (void) Dir_AddDir (path, name);
1082     cp[-1] = '/';
1083 
1084     bigmisses += 1;
1085     ln = Lst_Last (path);
1086     if (ln == NILLNODE) {
1087 	return ((char *) NULL);
1088     } else {
1089 	p = (Path *) Lst_Datum (ln);
1090     }
1091 
1092     if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
1093 	return (estrdup (name));
1094     } else {
1095 	return ((char *) NULL);
1096     }
1097 #else /* !notdef */
1098     if (DEBUG(DIR)) {
1099 	printf("Looking for \"%s\"...", name);
1100     }
1101 
1102     bigmisses += 1;
1103     entry = Hash_FindEntry(&mtimes, name);
1104     if (entry != (Hash_Entry *)NULL) {
1105 	if (DEBUG(DIR)) {
1106 	    printf("got it (in mtime cache)\n");
1107 	}
1108 	return(estrdup(name));
1109     } else if (stat (name, &stb) == 0) {
1110 	entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL);
1111 	if (DEBUG(DIR)) {
1112 	    printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
1113 		    name);
1114 	}
1115 	Hash_SetValue(entry, (long)stb.st_mtime);
1116 	return (estrdup (name));
1117     } else {
1118 	if (DEBUG(DIR)) {
1119 	    printf("failed. Returning NULL\n");
1120 	}
1121 	return ((char *)NULL);
1122     }
1123 #endif /* notdef */
1124 }
1125 
1126 /*-
1127  *-----------------------------------------------------------------------
1128  * Dir_MTime  --
1129  *	Find the modification time of the file described by gn along the
1130  *	search path dirSearchPath.
1131  *
1132  * Results:
1133  *	The modification time or 0 if it doesn't exist
1134  *
1135  * Side Effects:
1136  *	The modification time is placed in the node's mtime slot.
1137  *	If the node didn't have a path entry before, and Dir_FindFile
1138  *	found one for it, the full name is placed in the path slot.
1139  *-----------------------------------------------------------------------
1140  */
1141 int
1142 Dir_MTime (gn)
1143     GNode         *gn;	      /* the file whose modification time is
1144 			       * desired */
1145 {
1146     char          *fullName;  /* the full pathname of name */
1147     struct stat	  stb;	      /* buffer for finding the mod time */
1148     Hash_Entry	  *entry;
1149 
1150     if (gn->type & OP_ARCHV) {
1151 	return Arch_MTime (gn);
1152     } else if (gn->path == (char *)NULL) {
1153 	if (gn->type & (OP_PHONY|OP_NOPATH))
1154 	    fullName = NULL;
1155 	else
1156 	    fullName = Dir_FindFile (gn->name, dirSearchPath);
1157     } else {
1158 	fullName = gn->path;
1159     }
1160 
1161     if (fullName == (char *)NULL) {
1162 	fullName = estrdup(gn->name);
1163     }
1164 
1165     entry = Hash_FindEntry(&mtimes, fullName);
1166     if (entry != (Hash_Entry *)NULL) {
1167 	/*
1168 	 * Only do this once -- the second time folks are checking to
1169 	 * see if the file was actually updated, so we need to actually go
1170 	 * to the file system.
1171 	 */
1172 	if (DEBUG(DIR)) {
1173 	    printf("Using cached time %s for %s\n",
1174 		    Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName);
1175 	}
1176 	stb.st_mtime = (time_t)(long)Hash_GetValue(entry);
1177 	Hash_DeleteEntry(&mtimes, entry);
1178     } else if (stat (fullName, &stb) < 0) {
1179 	if (gn->type & OP_MEMBER) {
1180 	    if (fullName != gn->path)
1181 		free(fullName);
1182 	    return Arch_MemMTime (gn);
1183 	} else {
1184 	    stb.st_mtime = 0;
1185 	}
1186     }
1187     if (fullName && gn->path == (char *)NULL) {
1188 	gn->path = fullName;
1189     }
1190 
1191     gn->mtime = stb.st_mtime;
1192     return (gn->mtime);
1193 }
1194 
1195 /*-
1196  *-----------------------------------------------------------------------
1197  * Dir_AddDir --
1198  *	Add the given name to the end of the given path. The order of
1199  *	the arguments is backwards so ParseDoDependency can do a
1200  *	Lst_ForEach of its list of paths...
1201  *
1202  * Results:
1203  *	none
1204  *
1205  * Side Effects:
1206  *	A structure is added to the list and the directory is
1207  *	read and hashed.
1208  *-----------------------------------------------------------------------
1209  */
1210 Path *
1211 Dir_AddDir (path, name)
1212     Lst           path;	      /* the path to which the directory should be
1213 			       * added */
1214     const char   *name;	      /* the name of the directory to add */
1215 {
1216     LstNode       ln;	      /* node in case Path structure is found */
1217     register Path *p = NULL;  /* pointer to new Path structure */
1218     DIR     	  *d;	      /* for reading directory */
1219     register struct dirent *dp; /* entry in directory */
1220 
1221     if (strcmp(name, ".DOTLAST") == 0) {
1222 	ln = Lst_Find (path, (ClientData)name, DirFindName);
1223 	if (ln != NILLNODE)
1224 	    return (Path *) Lst_Datum(ln);
1225 	else {
1226 	    dotLast->refCount += 1;
1227 	    (void)Lst_AtFront(path, (ClientData)dotLast);
1228 	}
1229     }
1230 
1231     ln = Lst_Find (openDirectories, (ClientData)name, DirFindName);
1232     if (ln != NILLNODE) {
1233 	p = (Path *)Lst_Datum (ln);
1234 	if (Lst_Member(path, (ClientData)p) == NILLNODE) {
1235 	    p->refCount += 1;
1236 	    (void)Lst_AtEnd (path, (ClientData)p);
1237 	}
1238     } else {
1239 	if (DEBUG(DIR)) {
1240 	    printf("Caching %s...", name);
1241 	    fflush(stdout);
1242 	}
1243 
1244 	if ((d = opendir (name)) != (DIR *) NULL) {
1245 	    p = (Path *) emalloc (sizeof (Path));
1246 	    p->name = estrdup (name);
1247 	    p->hits = 0;
1248 	    p->refCount = 1;
1249 	    Hash_InitTable (&p->files, -1);
1250 
1251 	    /*
1252 	     * Skip the first two entries -- these will *always* be . and ..
1253 	     */
1254 	    (void)readdir(d);
1255 	    (void)readdir(d);
1256 
1257 	    while ((dp = readdir (d)) != (struct dirent *) NULL) {
1258 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
1259 		/*
1260 		 * The sun directory library doesn't check for a 0 inode
1261 		 * (0-inode slots just take up space), so we have to do
1262 		 * it ourselves.
1263 		 */
1264 		if (dp->d_fileno == 0) {
1265 		    continue;
1266 		}
1267 #endif /* sun && d_ino */
1268 		(void)Hash_CreateEntry(&p->files, dp->d_name, (Boolean *)NULL);
1269 	    }
1270 	    (void) closedir (d);
1271 	    (void)Lst_AtEnd (openDirectories, (ClientData)p);
1272 	    if (path != NULL)
1273 		(void)Lst_AtEnd (path, (ClientData)p);
1274 	}
1275 	if (DEBUG(DIR)) {
1276 	    printf("done\n");
1277 	}
1278     }
1279     return p;
1280 }
1281 
1282 /*-
1283  *-----------------------------------------------------------------------
1284  * Dir_CopyDir --
1285  *	Callback function for duplicating a search path via Lst_Duplicate.
1286  *	Ups the reference count for the directory.
1287  *
1288  * Results:
1289  *	Returns the Path it was given.
1290  *
1291  * Side Effects:
1292  *	The refCount of the path is incremented.
1293  *
1294  *-----------------------------------------------------------------------
1295  */
1296 ClientData
1297 Dir_CopyDir(p)
1298     ClientData p;
1299 {
1300     ((Path *) p)->refCount += 1;
1301 
1302     return ((ClientData)p);
1303 }
1304 
1305 /*-
1306  *-----------------------------------------------------------------------
1307  * Dir_MakeFlags --
1308  *	Make a string by taking all the directories in the given search
1309  *	path and preceding them by the given flag. Used by the suffix
1310  *	module to create variables for compilers based on suffix search
1311  *	paths.
1312  *
1313  * Results:
1314  *	The string mentioned above. Note that there is no space between
1315  *	the given flag and each directory. The empty string is returned if
1316  *	Things don't go well.
1317  *
1318  * Side Effects:
1319  *	None
1320  *-----------------------------------------------------------------------
1321  */
1322 char *
1323 Dir_MakeFlags (flag, path)
1324     char	  *flag;  /* flag which should precede each directory */
1325     Lst	    	  path;	  /* list of directories */
1326 {
1327     char	  *str;	  /* the string which will be returned */
1328     char	  *tstr;  /* the current directory preceded by 'flag' */
1329     LstNode	  ln;	  /* the node of the current directory */
1330     Path	  *p;	  /* the structure describing the current directory */
1331 
1332     str = estrdup ("");
1333 
1334     if (Lst_Open (path) == SUCCESS) {
1335 	while ((ln = Lst_Next (path)) != NILLNODE) {
1336 	    p = (Path *) Lst_Datum (ln);
1337 	    tstr = str_concat (flag, p->name, 0);
1338 	    str = str_concat (str, tstr, STR_ADDSPACE | STR_DOFREE);
1339 	}
1340 	Lst_Close (path);
1341     }
1342 
1343     return (str);
1344 }
1345 
1346 /*-
1347  *-----------------------------------------------------------------------
1348  * Dir_Destroy --
1349  *	Nuke a directory descriptor, if possible. Callback procedure
1350  *	for the suffixes module when destroying a search path.
1351  *
1352  * Results:
1353  *	None.
1354  *
1355  * Side Effects:
1356  *	If no other path references this directory (refCount == 0),
1357  *	the Path and all its data are freed.
1358  *
1359  *-----------------------------------------------------------------------
1360  */
1361 void
1362 Dir_Destroy (pp)
1363     ClientData 	  pp;	    /* The directory descriptor to nuke */
1364 {
1365     Path    	  *p = (Path *) pp;
1366     p->refCount -= 1;
1367 
1368     if (p->refCount == 0) {
1369 	LstNode	ln;
1370 
1371 	ln = Lst_Member (openDirectories, (ClientData)p);
1372 	(void) Lst_Remove (openDirectories, ln);
1373 
1374 	Hash_DeleteTable (&p->files);
1375 	free((Address)p->name);
1376 	free((Address)p);
1377     }
1378 }
1379 
1380 /*-
1381  *-----------------------------------------------------------------------
1382  * Dir_ClearPath --
1383  *	Clear out all elements of the given search path. This is different
1384  *	from destroying the list, notice.
1385  *
1386  * Results:
1387  *	None.
1388  *
1389  * Side Effects:
1390  *	The path is set to the empty list.
1391  *
1392  *-----------------------------------------------------------------------
1393  */
1394 void
1395 Dir_ClearPath(path)
1396     Lst	    path; 	/* Path to clear */
1397 {
1398     Path    *p;
1399     while (!Lst_IsEmpty(path)) {
1400 	p = (Path *)Lst_DeQueue(path);
1401 	Dir_Destroy((ClientData) p);
1402     }
1403 }
1404 
1405 
1406 /*-
1407  *-----------------------------------------------------------------------
1408  * Dir_Concat --
1409  *	Concatenate two paths, adding the second to the end of the first.
1410  *	Makes sure to avoid duplicates.
1411  *
1412  * Results:
1413  *	None
1414  *
1415  * Side Effects:
1416  *	Reference counts for added dirs are upped.
1417  *
1418  *-----------------------------------------------------------------------
1419  */
1420 void
1421 Dir_Concat(path1, path2)
1422     Lst	    path1;  	/* Dest */
1423     Lst	    path2;  	/* Source */
1424 {
1425     LstNode ln;
1426     Path    *p;
1427 
1428     for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) {
1429 	p = (Path *)Lst_Datum(ln);
1430 	if (Lst_Member(path1, (ClientData)p) == NILLNODE) {
1431 	    p->refCount += 1;
1432 	    (void)Lst_AtEnd(path1, (ClientData)p);
1433 	}
1434     }
1435 }
1436 
1437 /********** DEBUG INFO **********/
1438 void
1439 Dir_PrintDirectories()
1440 {
1441     LstNode	ln;
1442     Path	*p;
1443 
1444     printf ("#*** Directory Cache:\n");
1445     printf ("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
1446 	      hits, misses, nearmisses, bigmisses,
1447 	      (hits+bigmisses+nearmisses ?
1448 	       hits * 100 / (hits + bigmisses + nearmisses) : 0));
1449     printf ("# %-20s referenced\thits\n", "directory");
1450     if (Lst_Open (openDirectories) == SUCCESS) {
1451 	while ((ln = Lst_Next (openDirectories)) != NILLNODE) {
1452 	    p = (Path *) Lst_Datum (ln);
1453 	    printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
1454 	}
1455 	Lst_Close (openDirectories);
1456     }
1457 }
1458 
1459 static int DirPrintDir (p, dummy)
1460     ClientData	p;
1461     ClientData	dummy;
1462 {
1463     printf ("%s ", ((Path *) p)->name);
1464     return (dummy ? 0 : 0);
1465 }
1466 
1467 void
1468 Dir_PrintPath (path)
1469     Lst	path;
1470 {
1471     Lst_ForEach (path, DirPrintDir, (ClientData)0);
1472 }
1473