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