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