1/*	$NetBSD: dir.c,v 1.290 2024/05/20 19:14:12 sjg Exp $	*/
2
3/*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
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. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 *    must display the following acknowledgement:
53 *	This product includes software developed by the University of
54 *	California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 *    may be used to endorse or promote products derived from this software
57 *    without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72/*
73 * Directory searching using wildcards and/or normal names.
74 * Used both for source wildcarding in the makefile and for finding
75 * implicit sources.
76 *
77 * The interface for this module is:
78 *	Dir_Init	Initialize the module.
79 *
80 *	Dir_InitCur	Set the cur CachedDir.
81 *
82 *	Dir_InitDot	Set the dot CachedDir.
83 *
84 *	Dir_End		Clean up the module.
85 *
86 *	Dir_SetPATH	Set ${.PATH} to reflect the state of dirSearchPath.
87 *
88 *	Dir_HasWildcards
89 *			Returns true if the name given it needs to
90 *			be wildcard-expanded.
91 *
92 *	SearchPath_Expand
93 *			Expand a filename pattern to find all matching files
94 *			from the search path.
95 *
96 *	Dir_FindFile	Searches for a file on a given search path.
97 *			If it exists, returns the entire path, otherwise NULL.
98 *
99 *	Dir_FindHereOrAbove
100 *			Search for a path in the current directory and then
101 *			all the directories above it in turn, until the path
102 *			is found or the root directory ("/") is reached.
103 *
104 *	Dir_UpdateMTime
105 *			Update the modification time and path of a node with
106 *			data from the file corresponding to the node.
107 *
108 *	SearchPath_Add	Add a directory to a search path.
109 *
110 *	SearchPath_ToFlags
111 *			Given a search path and a command flag, create
112 *			a string with each of the directories in the path
113 *			preceded by the command flag and all of them
114 *			separated by a space.
115 *
116 *	SearchPath_Clear
117 *			Resets a search path to the empty list.
118 *
119 * For debugging:
120 *	Dir_PrintDirectories
121 *			Print stats about the directory cache.
122 */
123
124#include <sys/types.h>
125#include <sys/stat.h>
126
127#include <dirent.h>
128#include <errno.h>
129
130#include "make.h"
131#include "dir.h"
132#include "job.h"
133
134/*	"@(#)dir.c	8.2 (Berkeley) 1/2/94"	*/
135MAKE_RCSID("$NetBSD: dir.c,v 1.290 2024/05/20 19:14:12 sjg Exp $");
136
137/*
138 * A search path is a list of CachedDir structures. A CachedDir has in it the
139 * name of the directory and the names of all the files in the directory.
140 * This is used to cut down on the number of system calls necessary to find
141 * implicit dependents and their like. Since these searches are made before
142 * any actions are taken, we need not worry about the directory changing due
143 * to creation commands. If this hampers the style of some makefiles, they
144 * must be changed.
145 *
146 * All previously-read directories are kept in openDirs, which is checked
147 * first before a directory is opened.
148 *
149 * This cache is used by the multi-level transformation code in suff.c, which
150 * tends to search for far more files than in regular explicit targets. After
151 * a directory has been cached, any later changes to that directory are not
152 * reflected in the cache. To keep the cache up to date, there are several
153 * ideas:
154 *
155 * 1)	just use stat to test for a file's existence. As mentioned above,
156 *	this is very inefficient due to the number of checks performed by
157 *	the multi-level transformation code.
158 *
159 * 2)	use readdir() to search the directories, keeping them open between
160 *	checks. Around 1993 or earlier, this didn't slow down the process too
161 *	much, but it consumed one file descriptor per open directory, which
162 *	was critical on the then-current operating systems, as many limited
163 *	the number of open file descriptors to 20 or 32.
164 *
165 * 3)	record the mtime of the directory in the CachedDir structure and
166 *	verify the directory hasn't changed since the contents were cached.
167 *	This will catch the creation or deletion of files, but not the
168 *	updating of files. However, since it is the creation and deletion
169 *	that is the problem, this could be a good thing to do. Unfortunately,
170 *	if the directory (say ".") were fairly large and changed fairly
171 *	frequently, the constant reloading could seriously degrade
172 *	performance. It might be good in such cases to keep track of the
173 *	number of reloadings and if the number goes over a (small) limit,
174 *	resort to using stat in its place.
175 *
176 * An additional thing to consider is that make is used primarily to create
177 * C programs and until recently (as of 1993 or earlier), pcc-based compilers
178 * didn't have an option to specify where the resulting object file should be
179 * placed. This forced all objects to be created in the current directory.
180 * This isn't meant as a full excuse, just an explanation of some of the
181 * reasons for the caching used here.
182 *
183 * One more note: the location of a target's file is only performed on the
184 * downward traversal of the graph and then only for terminal nodes in the
185 * graph. This could be construed as wrong in some cases, but prevents
186 * inadvertent modification of files when the "installed" directory for a
187 * file is provided in the search path.
188 *
189 * Another data structure maintained by this module is an mtime cache used
190 * when the searching of cached directories fails to find a file. In the past,
191 * Dir_FindFile would simply perform an access() call in such a case to
192 * determine if the file could be found using just the name given. When this
193 * hit, however, all that was gained was the knowledge that the file existed.
194 * Given that an access() is essentially a stat() without the copyout() call,
195 * and that the same filesystem overhead would have to be incurred in
196 * Dir_MTime, it made sense to replace the access() with a stat() and record
197 * the mtime in a cache for when Dir_UpdateMTime was actually called.
198 */
199
200
201/* A cache for the filenames in a directory. */
202struct CachedDir {
203	/*
204	 * Name of the directory, either absolute or relative to the current
205	 * directory. The name is not normalized in any way, that is, "."
206	 * and "./." are different.
207	 *
208	 * Not sure what happens when .CURDIR is assigned a new value; see
209	 * Parse_Var.
210	 */
211	char *name;
212
213	/*
214	 * The number of SearchPaths that refer to this directory.
215	 * Plus the number of global variables that refer to this directory.
216	 * References from openDirs do not count though.
217	 */
218	int refCount;
219
220	/* The number of times a file in this directory has been found. */
221	int hits;
222
223	/* The names of the directory entries. */
224	HashSet files;
225};
226
227typedef List CachedDirList;
228typedef ListNode CachedDirListNode;
229
230/* A list of cached directories, with fast lookup by directory name. */
231typedef struct OpenDirs {
232	CachedDirList list;
233	HashTable /* of CachedDirListNode */ table;
234} OpenDirs;
235
236
237SearchPath dirSearchPath = { LST_INIT }; /* main search path */
238
239static OpenDirs openDirs;	/* all cached directories */
240
241/*
242 * Variables for gathering statistics on the efficiency of the caching
243 * mechanism.
244 */
245static int hits;		/* Found in directory cache */
246static int misses;		/* Sad, but not evil misses */
247static int nearmisses;		/* Found under search path */
248static int bigmisses;		/* Sought by itself */
249
250/* The cached contents of ".", the relative current directory. */
251static CachedDir *dot = NULL;
252/* The cached contents of the absolute current directory. */
253static CachedDir *cur = NULL;
254/* A fake path entry indicating we need to look for '.' last. */
255static CachedDir *dotLast = NULL;
256
257/*
258 * Results of doing a last-resort stat in Dir_FindFile -- if we have to go to
259 * the system to find the file, we might as well have its mtime on record.
260 *
261 * XXX: If this is done way early, there's a chance other rules will have
262 * already updated the file, in which case we'll update it again. Generally,
263 * there won't be two rules to update a single file, so this should be ok.
264 */
265static HashTable mtimes;
266
267static HashTable lmtimes;	/* same as mtimes but for lstat */
268
269
270static void OpenDirs_Remove(OpenDirs *, const char *);
271
272
273static CachedDir *
274CachedDir_New(const char *name)
275{
276	CachedDir *dir = bmake_malloc(sizeof *dir);
277
278	dir->name = bmake_strdup(name);
279	dir->refCount = 0;
280	dir->hits = 0;
281	HashSet_Init(&dir->files);
282
283#ifdef DEBUG_REFCNT
284	DEBUG2(DIR, "CachedDir %p new  for \"%s\"\n", dir, dir->name);
285#endif
286
287	return dir;
288}
289
290static CachedDir *
291CachedDir_Ref(CachedDir *dir)
292{
293	dir->refCount++;
294
295#ifdef DEBUG_REFCNT
296	DEBUG3(DIR, "CachedDir %p ++ %d for \"%s\"\n",
297	    dir, dir->refCount, dir->name);
298#endif
299
300	return dir;
301}
302
303static void
304CachedDir_Unref(CachedDir *dir)
305{
306	dir->refCount--;
307
308#ifdef DEBUG_REFCNT
309	DEBUG3(DIR, "CachedDir %p -- %d for \"%s\"\n",
310	    dir, dir->refCount, dir->name);
311#endif
312
313	if (dir->refCount > 0)
314		return;
315
316#ifdef DEBUG_REFCNT
317	DEBUG2(DIR, "CachedDir %p free for \"%s\"\n", dir, dir->name);
318#endif
319
320	OpenDirs_Remove(&openDirs, dir->name);
321
322	free(dir->name);
323	HashSet_Done(&dir->files);
324	free(dir);
325}
326
327/* Update the value of 'var', updating the reference counts. */
328static void
329CachedDir_Assign(CachedDir **var, CachedDir *dir)
330{
331	CachedDir *prev;
332
333	prev = *var;
334	*var = dir;
335	if (dir != NULL)
336		CachedDir_Ref(dir);
337	if (prev != NULL)
338		CachedDir_Unref(prev);
339}
340
341static void
342OpenDirs_Init(OpenDirs *odirs)
343{
344	Lst_Init(&odirs->list);
345	HashTable_Init(&odirs->table);
346}
347
348#ifdef CLEANUP
349static void
350OpenDirs_Done(OpenDirs *odirs)
351{
352	CachedDirListNode *ln = odirs->list.first;
353	DEBUG1(DIR, "OpenDirs_Done: %u entries to remove\n",
354	    odirs->table.numEntries);
355	while (ln != NULL) {
356		CachedDirListNode *next = ln->next;
357		CachedDir *dir = ln->datum;
358		DEBUG2(DIR, "OpenDirs_Done: refCount %d for \"%s\"\n",
359		    dir->refCount, dir->name);
360		CachedDir_Unref(dir);	/* removes the dir from odirs->list */
361		ln = next;
362	}
363	Lst_Done(&odirs->list);
364	HashTable_Done(&odirs->table);
365}
366#endif
367
368static CachedDir *
369OpenDirs_Find(OpenDirs *odirs, const char *name)
370{
371	CachedDirListNode *ln = HashTable_FindValue(&odirs->table, name);
372	return ln != NULL ? ln->datum : NULL;
373}
374
375static void
376OpenDirs_Add(OpenDirs *odirs, CachedDir *cdir)
377{
378	if (HashTable_FindEntry(&odirs->table, cdir->name) != NULL)
379		return;
380	Lst_Append(&odirs->list, cdir);
381	HashTable_Set(&odirs->table, cdir->name, odirs->list.last);
382}
383
384static void
385OpenDirs_Remove(OpenDirs *odirs, const char *name)
386{
387	HashEntry *he = HashTable_FindEntry(&odirs->table, name);
388	CachedDirListNode *ln;
389	if (he == NULL)
390		return;
391	ln = HashEntry_Get(he);
392	HashTable_DeleteEntry(&odirs->table, he);
393	Lst_Remove(&odirs->list, ln);
394}
395
396/*
397 * Returns 0 and the result of stat(2) or lstat(2) in *out_cst,
398 * or -1 on error.
399 */
400static int
401cached_stats(const char *pathname, struct cached_stat *out_cst,
402	     bool useLstat, bool forceRefresh)
403{
404	HashTable *tbl = useLstat ? &lmtimes : &mtimes;
405	struct stat sys_st;
406	struct cached_stat *cst;
407	int rc;
408
409	if (pathname == NULL || pathname[0] == '\0')
410		return -1;	/* This can happen in meta mode. */
411
412	cst = HashTable_FindValue(tbl, pathname);
413	if (cst != NULL && !forceRefresh) {
414		*out_cst = *cst;
415		DEBUG2(DIR, "Using cached time %s for %s\n",
416		    Targ_FmtTime(cst->cst_mtime), pathname);
417		return 0;
418	}
419
420	rc = (useLstat ? lstat : stat)(pathname, &sys_st);
421	if (rc == -1)
422		return -1;	/* don't cache negative lookups */
423
424	if (sys_st.st_mtime == 0)
425		sys_st.st_mtime = 1; /* avoid confusion with missing file */
426
427	if (cst == NULL) {
428		cst = bmake_malloc(sizeof *cst);
429		HashTable_Set(tbl, pathname, cst);
430	}
431
432	cst->cst_mtime = sys_st.st_mtime;
433	cst->cst_mode = sys_st.st_mode;
434
435	*out_cst = *cst;
436	DEBUG2(DIR, "   Caching %s for %s\n",
437	    Targ_FmtTime(sys_st.st_mtime), pathname);
438
439	return 0;
440}
441
442int
443cached_stat(const char *pathname, struct cached_stat *cst)
444{
445	return cached_stats(pathname, cst, false, false);
446}
447
448int
449cached_lstat(const char *pathname, struct cached_stat *cst)
450{
451	return cached_stats(pathname, cst, true, false);
452}
453
454/* Initialize the directories module. */
455void
456Dir_Init(void)
457{
458	OpenDirs_Init(&openDirs);
459	HashTable_Init(&mtimes);
460	HashTable_Init(&lmtimes);
461	CachedDir_Assign(&dotLast, CachedDir_New(".DOTLAST"));
462}
463
464/* Called by Dir_InitDir and whenever .CURDIR is assigned to. */
465void
466Dir_InitCur(const char *newCurdir)
467{
468	CachedDir *dir;
469
470	if (newCurdir == NULL)
471		return;
472
473	/*
474	 * The build directory is not the same as the source directory.
475	 * Keep this one around too.
476	 */
477	dir = SearchPath_Add(NULL, newCurdir);
478	if (dir == NULL)
479		return;
480
481	CachedDir_Assign(&cur, dir);
482}
483
484/*
485 * (Re)initialize "dot" (the current/object directory).
486 * Some directories may be cached.
487 */
488void
489Dir_InitDot(void)
490{
491	CachedDir *dir;
492
493	dir = SearchPath_Add(NULL, ".");
494	if (dir == NULL) {
495		Error("Cannot open `.' (%s)", strerror(errno));
496		exit(2);	/* Not 1 so -q can distinguish error */
497	}
498
499	CachedDir_Assign(&dot, dir);
500
501	Dir_SetPATH();		/* initialize */
502}
503
504/* Clean up the directories module. */
505void
506Dir_End(void)
507{
508#ifdef CLEANUP
509	CachedDir_Assign(&cur, NULL);
510	CachedDir_Assign(&dot, NULL);
511	CachedDir_Assign(&dotLast, NULL);
512	SearchPath_Clear(&dirSearchPath);
513	OpenDirs_Done(&openDirs);
514	HashTable_Done(&mtimes);
515	HashTable_Done(&lmtimes);
516#endif
517}
518
519/*
520 * We want ${.PATH} to indicate the order in which we will actually
521 * search, so we rebuild it after any .PATH: target.
522 * This is the simplest way to deal with the effect of .DOTLAST.
523 */
524void
525Dir_SetPATH(void)
526{
527	CachedDirListNode *ln;
528	bool seenDotLast = false;	/* true if we should search '.' last */
529
530	Global_Delete(".PATH");
531
532	if ((ln = dirSearchPath.dirs.first) != NULL) {
533		CachedDir *dir = ln->datum;
534		if (dir == dotLast) {
535			seenDotLast = true;
536			Global_Append(".PATH", dotLast->name);
537		}
538	}
539
540	if (!seenDotLast) {
541		if (dot != NULL)
542			Global_Append(".PATH", dot->name);
543		if (cur != NULL)
544			Global_Append(".PATH", cur->name);
545	}
546
547	for (ln = dirSearchPath.dirs.first; ln != NULL; ln = ln->next) {
548		CachedDir *dir = ln->datum;
549		if (dir == dotLast)
550			continue;
551		if (dir == dot && seenDotLast)
552			continue;
553		Global_Append(".PATH", dir->name);
554	}
555
556	if (seenDotLast) {
557		if (dot != NULL)
558			Global_Append(".PATH", dot->name);
559		if (cur != NULL)
560			Global_Append(".PATH", cur->name);
561	}
562}
563
564
565void
566Dir_SetSYSPATH(void)
567{
568	CachedDirListNode *ln;
569	SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
570		? defSysIncPath : sysIncPath;
571
572	Var_ReadOnly(".SYSPATH", false);
573	Global_Delete(".SYSPATH");
574	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
575		CachedDir *dir = ln->datum;
576		Global_Append(".SYSPATH", dir->name);
577	}
578	Var_ReadOnly(".SYSPATH", true);
579}
580
581/*
582 * See if the given name has any wildcard characters in it and all braces and
583 * brackets are properly balanced.
584 *
585 * XXX: This code is not 100% correct ([^]] fails etc.). I really don't think
586 * that make(1) should be expanding patterns, because then you have to set a
587 * mechanism for escaping the expansion!
588 */
589bool
590Dir_HasWildcards(const char *name)
591{
592	const char *p;
593	bool wild = false;
594	int braces = 0, brackets = 0;
595
596	for (p = name; *p != '\0'; p++) {
597		switch (*p) {
598		case '{':
599			braces++;
600			wild = true;
601			break;
602		case '}':
603			braces--;
604			break;
605		case '[':
606			brackets++;
607			wild = true;
608			break;
609		case ']':
610			brackets--;
611			break;
612		case '?':
613		case '*':
614			wild = true;
615			break;
616		default:
617			break;
618		}
619	}
620	return wild && brackets == 0 && braces == 0;
621}
622
623/*
624 * See if any files as seen from 'dir' match 'pattern', and add their names
625 * to 'expansions' if they do.
626 *
627 * Wildcards are only expanded in the final path component, but not in
628 * directories like src/lib*c/file*.c. To expand these wildcards,
629 * delegate the work to the shell, using the '!=' variable assignment
630 * operator, the ':sh' variable modifier or the ':!...!' variable modifier,
631 * such as in ${:!echo src/lib*c/file*.c!}.
632 */
633static void
634DirMatchFiles(const char *pattern, CachedDir *dir, StringList *expansions)
635{
636	const char *dirName = dir->name;
637	bool isDot = dirName[0] == '.' && dirName[1] == '\0';
638	HashIter hi;
639
640	/*
641	 * XXX: Iterating over all hash entries is inefficient.  If the
642	 * pattern is a plain string without any wildcards, a direct lookup
643	 * is faster.
644	 */
645
646	HashIter_InitSet(&hi, &dir->files);
647	while (HashIter_Next(&hi) != NULL) {
648		const char *base = hi.entry->key;
649		StrMatchResult res = Str_Match(base, pattern);
650		/* TODO: handle errors from res.error */
651
652		if (!res.matched)
653			continue;
654
655		/*
656		 * Follow the UNIX convention that dot files are only found
657		 * if the pattern begins with a dot. The pattern '.*' does
658		 * not match '.' or '..' since these are not included in the
659		 * directory cache.
660		 *
661		 * This means that the pattern '[a-z.]*' does not find
662		 * '.file', which is consistent with NetBSD sh, NetBSD ksh,
663		 * bash, dash, csh and probably many other shells as well.
664		 */
665		if (base[0] == '.' && pattern[0] != '.')
666			continue;
667
668		{
669			char *fullName = isDot
670			    ? bmake_strdup(base)
671			    : str_concat3(dirName, "/", base);
672			Lst_Append(expansions, fullName);
673		}
674	}
675}
676
677/* Find the next closing brace in 'p', taking nested braces into account. */
678static const char *
679closing_brace(const char *p)
680{
681	int depth = 0;
682	while (*p != '\0') {
683		if (*p == '}' && depth == 0)
684			break;
685		if (*p == '{')
686			depth++;
687		if (*p == '}')
688			depth--;
689		p++;
690	}
691	return p;
692}
693
694/*
695 * Find the next closing brace or comma in the string, taking nested braces
696 * into account.
697 */
698static const char *
699separator_comma(const char *p)
700{
701	int depth = 0;
702	while (*p != '\0') {
703		if ((*p == '}' || *p == ',') && depth == 0)
704			break;
705		if (*p == '{')
706			depth++;
707		if (*p == '}')
708			depth--;
709		p++;
710	}
711	return p;
712}
713
714static bool
715contains_wildcard(const char *p)
716{
717	for (; *p != '\0'; p++) {
718		switch (*p) {
719		case '*':
720		case '?':
721		case '{':
722		case '[':
723			return true;
724		}
725	}
726	return false;
727}
728
729static char *
730concat3(const char *a, size_t a_len, const char *b, size_t b_len,
731	const char *c, size_t c_len)
732{
733	size_t s_len = a_len + b_len + c_len;
734	char *s = bmake_malloc(s_len + 1);
735	memcpy(s, a, a_len);
736	memcpy(s + a_len, b, b_len);
737	memcpy(s + a_len + b_len, c, c_len);
738	s[s_len] = '\0';
739	return s;
740}
741
742/*
743 * Expand curly braces like the C shell. Brace expansion by itself is purely
744 * textual, the expansions are not looked up in the file system. But if an
745 * expanded word contains wildcard characters, it is expanded further,
746 * matching only the actually existing files.
747 *
748 * Example: "{a{b,c}}" expands to "ab" and "ac".
749 * Example: "{a}" expands to "a".
750 * Example: "{a,*.c}" expands to "a" and all "*.c" files that exist.
751 *
752 * Input:
753 *	word		Entire word to expand
754 *	brace		First curly brace in it
755 *	path		Search path to use
756 *	expansions	Place to store the expansions
757 */
758static void
759DirExpandCurly(const char *word, const char *brace, SearchPath *path,
760	       StringList *expansions)
761{
762	const char *prefix, *middle, *piece, *middle_end, *suffix;
763	size_t prefix_len, suffix_len;
764
765	/* Split the word into prefix, '{', middle, '}' and suffix. */
766
767	middle = brace + 1;
768	middle_end = closing_brace(middle);
769	if (*middle_end == '\0') {
770		Error("Unterminated {} clause \"%s\"", middle);
771		return;
772	}
773
774	prefix = word;
775	prefix_len = (size_t)(brace - prefix);
776	suffix = middle_end + 1;
777	suffix_len = strlen(suffix);
778
779	/* Split the middle into pieces, separated by commas. */
780
781	piece = middle;
782	while (piece < middle_end + 1) {
783		const char *piece_end = separator_comma(piece);
784		size_t piece_len = (size_t)(piece_end - piece);
785
786		char *file = concat3(prefix, prefix_len, piece, piece_len,
787		    suffix, suffix_len);
788
789		if (contains_wildcard(file)) {
790			SearchPath_Expand(path, file, expansions);
791			free(file);
792		} else {
793			Lst_Append(expansions, file);
794		}
795
796		/* skip over the comma or closing brace */
797		piece = piece_end + 1;
798	}
799}
800
801
802/* Expand 'pattern' in each of the directories from 'path'. */
803static void
804DirExpandPath(const char *pattern, SearchPath *path, StringList *expansions)
805{
806	CachedDirListNode *ln;
807	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
808		CachedDir *dir = ln->datum;
809		DirMatchFiles(pattern, dir, expansions);
810	}
811}
812
813static void
814PrintExpansions(StringList *expansions)
815{
816	const char *sep = "";
817	StringListNode *ln;
818	for (ln = expansions->first; ln != NULL; ln = ln->next) {
819		const char *word = ln->datum;
820		debug_printf("%s%s", sep, word);
821		sep = " ";
822	}
823	debug_printf("\n");
824}
825
826/*
827 * The wildcard isn't in the first component.
828 * Find all the components up to the one with the wildcard.
829 */
830static void
831SearchPath_ExpandMiddle(SearchPath *path, const char *pattern,
832			const char *wildcardComponent, StringList *expansions)
833{
834	char *prefix, *dirpath, *end;
835	SearchPath *partPath;
836
837	prefix = bmake_strsedup(pattern, wildcardComponent + 1);
838	/*
839	 * XXX: Only the first match of the prefix in the path is
840	 * taken, any others are ignored.  The expectation may be
841	 * that the pattern is expanded in the whole path.
842	 */
843	dirpath = Dir_FindFile(prefix, path);
844	free(prefix);
845
846	/*
847	 * dirpath is null if can't find the leading component
848	 *
849	 * XXX: Dir_FindFile won't find internal components.  i.e. if the
850	 * path contains ../Etc/Object and we're looking for Etc, it won't
851	 * be found.  Ah well.  Probably not important.
852	 *
853	 * TODO: Check whether the above comment is still true.
854	 */
855	if (dirpath == NULL)
856		return;
857
858	end = &dirpath[strlen(dirpath) - 1];
859	/* XXX: What about multiple trailing slashes? */
860	if (*end == '/')
861		*end = '\0';
862
863	partPath = SearchPath_New();
864	(void)SearchPath_Add(partPath, dirpath);
865	DirExpandPath(wildcardComponent + 1, partPath, expansions);
866	SearchPath_Free(partPath);
867}
868
869/*
870 * Expand the given pattern into a list of existing filenames by globbing it,
871 * looking in each directory from the search path.
872 *
873 * Input:
874 *	path		the directories in which to find the files
875 *	pattern		the pattern to expand
876 *	expansions	the list on which to place the results
877 */
878void
879SearchPath_Expand(SearchPath *path, const char *pattern, StringList *expansions)
880{
881	const char *brace, *slash, *wildcard, *wildcardComponent;
882
883	assert(path != NULL);
884	assert(expansions != NULL);
885
886	DEBUG1(DIR, "Expanding \"%s\"... ", pattern);
887
888	brace = strchr(pattern, '{');
889	if (brace != NULL) {
890		DirExpandCurly(pattern, brace, path, expansions);
891		goto done;
892	}
893
894	slash = strchr(pattern, '/');
895	if (slash == NULL) {
896		DirMatchFiles(pattern, dot, expansions);
897		DirExpandPath(pattern, path, expansions);
898		goto done;
899	}
900
901	/* At this point, the pattern has a directory component. */
902
903	/* Find the first wildcard in the pattern. */
904	for (wildcard = pattern; *wildcard != '\0'; wildcard++)
905		if (*wildcard == '?' || *wildcard == '[' || *wildcard == '*')
906			break;
907
908	if (*wildcard == '\0') {
909		/*
910		 * No directory component and no wildcard at all -- this
911		 * should never happen as in such a simple case there is no
912		 * need to expand anything.
913		 */
914		DirExpandPath(pattern, path, expansions);
915		goto done;
916	}
917
918	/* Back up to the start of the component containing the wildcard. */
919	/* XXX: This handles '///' and '/' differently. */
920	wildcardComponent = wildcard;
921	while (wildcardComponent > pattern && *wildcardComponent != '/')
922		wildcardComponent--;
923
924	if (wildcardComponent == pattern) {
925		/* The first component contains the wildcard. */
926		/* Start the search from the local directory */
927		DirExpandPath(pattern, path, expansions);
928	} else {
929		SearchPath_ExpandMiddle(path, pattern, wildcardComponent,
930		    expansions);
931	}
932
933done:
934	if (DEBUG(DIR))
935		PrintExpansions(expansions);
936}
937
938/*
939 * Find if 'base' exists in 'dir'.
940 * Return the freshly allocated path to the file, or NULL.
941 */
942static char *
943DirLookup(CachedDir *dir, const char *base)
944{
945	char *file;
946
947	DEBUG1(DIR, "   %s ...\n", dir->name);
948
949	if (!HashSet_Contains(&dir->files, base))
950		return NULL;
951
952	file = str_concat3(dir->name, "/", base);
953	DEBUG1(DIR, "   returning %s\n", file);
954	dir->hits++;
955	hits++;
956	return file;
957}
958
959
960/*
961 * Find if 'name' exists in 'dir'.
962 * Return the freshly allocated path to the file, or NULL.
963 */
964static char *
965DirLookupSubdir(CachedDir *dir, const char *name)
966{
967	struct cached_stat cst;
968	char *file = dir == dot
969	    ? bmake_strdup(name)
970	    : str_concat3(dir->name, "/", name);
971
972	DEBUG1(DIR, "checking %s ...\n", file);
973
974	if (cached_stat(file, &cst) == 0) {
975		nearmisses++;
976		return file;
977	}
978	free(file);
979	return NULL;
980}
981
982/*
983 * Find if 'name' (which has basename 'base') exists in 'dir'.
984 * Return the freshly allocated path to the file, an empty string, or NULL.
985 * Returning an empty string means that the search should be terminated.
986 */
987static char *
988DirLookupAbs(CachedDir *dir, const char *name, const char *base)
989{
990	const char *dnp;	/* pointer into dir->name */
991	const char *np;		/* pointer into name */
992
993	DEBUG1(DIR, "   %s ...\n", dir->name);
994
995	/*
996	 * If the file has a leading path component and that component
997	 * exactly matches the entire name of the current search
998	 * directory, we can attempt another cache lookup. And if we don't
999	 * have a hit, we can safely assume the file does not exist at all.
1000	 */
1001	for (dnp = dir->name, np = name;
1002	     *dnp != '\0' && *dnp == *np; dnp++, np++)
1003		continue;
1004	if (*dnp != '\0' || np != base - 1)
1005		return NULL;
1006
1007	if (!HashSet_Contains(&dir->files, base)) {
1008		DEBUG0(DIR, "   must be here but isn't -- returning\n");
1009		return bmake_strdup("");	/* to terminate the search */
1010	}
1011
1012	dir->hits++;
1013	hits++;
1014	DEBUG1(DIR, "   returning %s\n", name);
1015	return bmake_strdup(name);
1016}
1017
1018/*
1019 * Find the given file in "." or curdir.
1020 * Return the freshly allocated path to the file, or NULL.
1021 */
1022static char *
1023DirFindDot(const char *name, const char *base)
1024{
1025
1026	if (HashSet_Contains(&dot->files, base)) {
1027		DEBUG0(DIR, "   in '.'\n");
1028		hits++;
1029		dot->hits++;
1030		return bmake_strdup(name);
1031	}
1032
1033	if (cur != NULL && HashSet_Contains(&cur->files, base)) {
1034		DEBUG1(DIR, "   in ${.CURDIR} = %s\n", cur->name);
1035		hits++;
1036		cur->hits++;
1037		return str_concat3(cur->name, "/", base);
1038	}
1039
1040	return NULL;
1041}
1042
1043static bool
1044FindFileRelative(SearchPath *path, bool seenDotLast,
1045		 const char *name, char **out_file)
1046{
1047	CachedDirListNode *ln;
1048	bool checkedDot = false;
1049	char *file;
1050
1051	DEBUG0(DIR, "   Trying subdirectories...\n");
1052
1053	if (!seenDotLast) {
1054		if (dot != NULL) {
1055			checkedDot = true;
1056			if ((file = DirLookupSubdir(dot, name)) != NULL)
1057				goto done;
1058		}
1059		if (cur != NULL &&
1060		    (file = DirLookupSubdir(cur, name)) != NULL)
1061			goto done;
1062	}
1063
1064	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1065		CachedDir *dir = ln->datum;
1066		if (dir == dotLast)
1067			continue;
1068		if (dir == dot) {
1069			if (checkedDot)
1070				continue;
1071			checkedDot = true;
1072		}
1073		if ((file = DirLookupSubdir(dir, name)) != NULL)
1074			goto done;
1075	}
1076
1077	if (seenDotLast) {
1078		if (dot != NULL && !checkedDot) {
1079			checkedDot = true;
1080			if ((file = DirLookupSubdir(dot, name)) != NULL)
1081				goto done;
1082		}
1083		if (cur != NULL &&
1084		    (file = DirLookupSubdir(cur, name)) != NULL)
1085			goto done;
1086	}
1087
1088	if (checkedDot) {
1089		/*
1090		 * Already checked by the given name, since . was in
1091		 * the path, so no point in proceeding.
1092		 */
1093		DEBUG0(DIR, "   Checked . already, returning NULL\n");
1094		file = NULL;
1095		goto done;
1096	}
1097
1098	return false;
1099
1100done:
1101	*out_file = file;
1102	return true;
1103}
1104
1105static bool
1106FindFileAbsolute(SearchPath *path, bool seenDotLast,
1107		 const char *name, const char *base, char **out_file)
1108{
1109	char *file;
1110	CachedDirListNode *ln;
1111
1112	DEBUG0(DIR, "   Trying exact path matches...\n");
1113
1114	if (!seenDotLast && cur != NULL &&
1115	    ((file = DirLookupAbs(cur, name, base)) != NULL))
1116		goto found;
1117
1118	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1119		CachedDir *dir = ln->datum;
1120		if (dir == dotLast)
1121			continue;
1122		if ((file = DirLookupAbs(dir, name, base)) != NULL)
1123			goto found;
1124	}
1125
1126	if (seenDotLast && cur != NULL &&
1127	    ((file = DirLookupAbs(cur, name, base)) != NULL))
1128		goto found;
1129
1130	return false;
1131
1132found:
1133	if (file[0] == '\0') {
1134		free(file);
1135		file = NULL;
1136	}
1137	*out_file = file;
1138	return true;
1139}
1140
1141/*
1142 * Find the file with the given name along the given search path.
1143 *
1144 * Input:
1145 *	name		the file to find
1146 *	path		the directories to search, or NULL
1147 *	isinclude	if true, do not search .CURDIR at all
1148 *
1149 * Results:
1150 *	The freshly allocated path to the file, or NULL.
1151 */
1152static char *
1153FindFile(const char *name, SearchPath *path, bool isinclude)
1154{
1155	char *file;		/* the current filename to check */
1156	bool seenDotLast = isinclude; /* true if we should search dot last */
1157	struct cached_stat cst;
1158	const char *trailing_dot = ".";
1159	const char *base = str_basename(name);
1160
1161	DEBUG1(DIR, "Searching for %s ...", name);
1162
1163	if (path == NULL) {
1164		DEBUG0(DIR, "couldn't open path, file not found\n");
1165		misses++;
1166		return NULL;
1167	}
1168
1169	if (!seenDotLast && path->dirs.first != NULL) {
1170		CachedDir *dir = path->dirs.first->datum;
1171		if (dir == dotLast) {
1172			seenDotLast = true;
1173			DEBUG0(DIR, "[dot last]...");
1174		}
1175	}
1176	DEBUG0(DIR, "\n");
1177
1178	/*
1179	 * If there's no leading directory components or if the leading
1180	 * directory component is exactly `./', consult the cached contents
1181	 * of each of the directories on the search path.
1182	 */
1183	if (base == name || (base - name == 2 && *name == '.')) {
1184		CachedDirListNode *ln;
1185
1186		/*
1187		 * Look through all the directories on the path seeking one
1188		 * which contains the final component of the given name.  If
1189		 * such a file is found, return its pathname.
1190		 * If there is no such file, go on to phase two.
1191		 *
1192		 * No matter what, always look for the file in the current
1193		 * directory before anywhere else (unless the path contains
1194		 * the magic '.DOTLAST', in which case search it last).
1195		 * This is so there are no conflicts between what the user
1196		 * specifies (fish.c) and what make finds (./fish.c).
1197		 */
1198		if (!seenDotLast && (file = DirFindDot(name, base)) != NULL)
1199			return file;
1200
1201		for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1202			CachedDir *dir = ln->datum;
1203			if (dir == dotLast)
1204				continue;
1205			if ((file = DirLookup(dir, base)) != NULL)
1206				return file;
1207		}
1208
1209		if (seenDotLast && (file = DirFindDot(name, base)) != NULL)
1210			return file;
1211	}
1212
1213	if (base == name) {
1214		DEBUG0(DIR, "   failed.\n");
1215		misses++;
1216		return NULL;
1217	}
1218
1219	if (*base == '\0')
1220		base = trailing_dot;	/* we were given a trailing "/" */
1221
1222	if (name[0] != '/') {
1223		if (FindFileRelative(path, seenDotLast, name, &file))
1224			return file;
1225	} else {
1226		if (FindFileAbsolute(path, seenDotLast, name, base, &file))
1227			return file;
1228	}
1229
1230	/*
1231	 * We cannot add the directory onto the search path because
1232	 * of this amusing case:
1233	 * $(INSTALLDIR)/$(FILE): $(FILE)
1234	 *
1235	 * $(FILE) exists in $(INSTALLDIR) but not in the current one.
1236	 * When searching for $(FILE), we will find it in $(INSTALLDIR)
1237	 * b/c we added it here. This is not good...
1238	 */
1239
1240	DEBUG1(DIR, "   Looking for \"%s\" ...\n", name);
1241
1242	bigmisses++;
1243	if (cached_stat(name, &cst) == 0)
1244		return bmake_strdup(name);
1245
1246	DEBUG0(DIR, "   failed. Returning NULL\n");
1247	return NULL;
1248}
1249
1250/*
1251 * Find the file with the given name along the given search path.
1252 *
1253 * Input:
1254 *	name		the file to find
1255 *	path		the directories to search, or NULL
1256 *
1257 * Results:
1258 *	The freshly allocated path to the file, or NULL.
1259 */
1260char *
1261Dir_FindFile(const char *name, SearchPath *path)
1262{
1263	return FindFile(name, path, false);
1264}
1265
1266/*
1267 * Find the include file with the given name along the given search path.
1268 *
1269 * Input:
1270 *	name		the file to find
1271 *	path		the directories to search, or NULL
1272 *
1273 * Results:
1274 *	The freshly allocated path to the file, or NULL.
1275 */
1276char *
1277Dir_FindInclude(const char *name, SearchPath *path)
1278{
1279	return FindFile(name, path, true);
1280}
1281
1282
1283/*
1284 * Search for 'needle' starting at the directory 'here' and then working our
1285 * way up towards the root directory. Return the allocated path, or NULL.
1286 */
1287char *
1288Dir_FindHereOrAbove(const char *here, const char *needle)
1289{
1290	struct cached_stat cst;
1291	char *dirbase, *dirbase_end;
1292	char *try, *try_end;
1293
1294	dirbase = bmake_strdup(here);
1295	dirbase_end = dirbase + strlen(dirbase);
1296
1297	for (;;) {
1298		try = str_concat3(dirbase, "/", needle);
1299		if (cached_stat(try, &cst) != -1) {
1300			if ((cst.cst_mode & S_IFMT) != S_IFDIR) {
1301				/*
1302				 * Chop off the filename, to return a
1303				 * directory.
1304				 */
1305				try_end = try + strlen(try);
1306				while (try_end > try && *try_end != '/')
1307					try_end--;
1308				if (try_end > try)
1309					*try_end = '\0';	/* chop! */
1310			}
1311
1312			free(dirbase);
1313			return try;
1314		}
1315		free(try);
1316
1317		if (dirbase_end == dirbase)
1318			break;	/* failed! */
1319
1320		/* Truncate dirbase from the end to move up a dir. */
1321		while (dirbase_end > dirbase && *dirbase_end != '/')
1322			dirbase_end--;
1323		*dirbase_end = '\0';	/* chop! */
1324	}
1325
1326	free(dirbase);
1327	return NULL;
1328}
1329
1330/*
1331 * This is an implied source, and it may have moved,
1332 * see if we can find it via the current .PATH
1333 */
1334static char *
1335ResolveMovedDepends(GNode *gn)
1336{
1337	char *fullName;
1338
1339	const char *base = str_basename(gn->name);
1340	if (base == gn->name)
1341		return NULL;
1342
1343	fullName = Dir_FindFile(base, Suff_FindPath(gn));
1344	if (fullName == NULL)
1345		return NULL;
1346
1347	/*
1348	 * Put the found file in gn->path so that we give that to the compiler.
1349	 */
1350	/*
1351	 * XXX: Better just reset gn->path to NULL; updating it is already done
1352	 * by Dir_UpdateMTime.
1353	 */
1354	gn->path = bmake_strdup(fullName);
1355	if (!Job_RunTarget(".STALE", gn->fname))
1356		fprintf(stdout,	/* XXX: Why stdout? */
1357		    "%s: %s, %u: ignoring stale %s for %s, found %s\n",
1358		    progname, gn->fname, gn->lineno,
1359		    makeDependfile, gn->name, fullName);
1360
1361	return fullName;
1362}
1363
1364static char *
1365ResolveFullName(GNode *gn)
1366{
1367	char *fullName;
1368
1369	fullName = gn->path;
1370	if (fullName == NULL && !(gn->type & OP_NOPATH)) {
1371
1372		fullName = Dir_FindFile(gn->name, Suff_FindPath(gn));
1373
1374		if (fullName == NULL && gn->flags.fromDepend &&
1375		    !Lst_IsEmpty(&gn->implicitParents))
1376			fullName = ResolveMovedDepends(gn);
1377
1378		DEBUG2(DIR, "Found '%s' as '%s'\n",
1379		    gn->name, fullName != NULL ? fullName : "(not found)");
1380	}
1381
1382	if (fullName == NULL)
1383		fullName = bmake_strdup(gn->name);
1384
1385	/* XXX: Is every piece of memory freed as it should? */
1386
1387	return fullName;
1388}
1389
1390/*
1391 * Search 'gn' along 'dirSearchPath' and store its modification time in
1392 * 'gn->mtime'. If no file is found, store 0 instead.
1393 *
1394 * The found file is stored in 'gn->path', unless the node already had a path.
1395 */
1396void
1397Dir_UpdateMTime(GNode *gn, bool forceRefresh)
1398{
1399	char *fullName;
1400	struct cached_stat cst;
1401
1402	if (gn->type & OP_ARCHV) {
1403		Arch_UpdateMTime(gn);
1404		return;
1405	}
1406
1407	if (gn->type & OP_PHONY) {
1408		gn->mtime = 0;
1409		return;
1410	}
1411
1412	fullName = ResolveFullName(gn);
1413
1414	if (cached_stats(fullName, &cst, false, forceRefresh) < 0) {
1415		if (gn->type & OP_MEMBER) {
1416			if (fullName != gn->path)
1417				free(fullName);
1418			Arch_UpdateMemberMTime(gn);
1419			return;
1420		}
1421
1422		cst.cst_mtime = 0;
1423	}
1424
1425	if (fullName != NULL && gn->path == NULL)
1426		gn->path = fullName;
1427	/* XXX: else free(fullName)? */
1428
1429	gn->mtime = cst.cst_mtime;
1430}
1431
1432/*
1433 * Read the directory and add it to the cache in openDirs.
1434 * If a path is given, add the directory to that path as well.
1435 */
1436static CachedDir *
1437CacheNewDir(const char *name, SearchPath *path)
1438{
1439	CachedDir *dir = NULL;
1440	DIR *d;
1441	struct dirent *dp;
1442
1443	if ((d = opendir(name)) == NULL) {
1444		DEBUG1(DIR, "Caching %s ... not found\n", name);
1445		return dir;
1446	}
1447
1448	DEBUG1(DIR, "Caching %s ...\n", name);
1449
1450	dir = CachedDir_New(name);
1451
1452	while ((dp = readdir(d)) != NULL) {
1453
1454#if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
1455		/*
1456		 * The sun directory library doesn't check for a 0 inode
1457		 * (0-inode slots just take up space), so we have to do
1458		 * it ourselves.
1459		 */
1460		if (dp->d_fileno == 0)
1461			continue;
1462#endif /* sun && d_ino */
1463
1464		(void)HashSet_Add(&dir->files, dp->d_name);
1465	}
1466	(void)closedir(d);
1467
1468	OpenDirs_Add(&openDirs, dir);
1469	if (path != NULL)
1470		Lst_Append(&path->dirs, CachedDir_Ref(dir));
1471
1472	DEBUG1(DIR, "Caching %s done\n", name);
1473	return dir;
1474}
1475
1476/*
1477 * Read the list of filenames in the directory 'name' and store the result
1478 * in 'openDirs'.
1479 *
1480 * If a search path is given, append the directory to that path.
1481 *
1482 * Input:
1483 *	path		The path to which the directory should be
1484 *			added, or NULL to only add the directory to openDirs.
1485 *	name		The name of the directory to add.
1486 *			The name is not normalized in any way.
1487 * Output:
1488 *	result		If no path is given and the directory exists, the
1489 *			returned CachedDir has a reference count of 0.  It
1490 *			must either be assigned to a variable using
1491 *			CachedDir_Assign or be appended to a SearchPath using
1492 *			Lst_Append and CachedDir_Ref.
1493 */
1494CachedDir *
1495SearchPath_Add(SearchPath *path, const char *name)
1496{
1497
1498	if (path != NULL && strcmp(name, ".DOTLAST") == 0) {
1499		CachedDirListNode *ln;
1500
1501		/* XXX: Linear search gets slow with thousands of entries. */
1502		for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1503			CachedDir *pathDir = ln->datum;
1504			if (strcmp(pathDir->name, name) == 0)
1505				return pathDir;
1506		}
1507
1508		Lst_Prepend(&path->dirs, CachedDir_Ref(dotLast));
1509	}
1510
1511	if (path != NULL) {
1512		/* XXX: Why is OpenDirs only checked if path != NULL? */
1513		CachedDir *dir = OpenDirs_Find(&openDirs, name);
1514		if (dir != NULL) {
1515			if (Lst_FindDatum(&path->dirs, dir) == NULL)
1516				Lst_Append(&path->dirs, CachedDir_Ref(dir));
1517			return dir;
1518		}
1519	}
1520
1521	return CacheNewDir(name, path);
1522}
1523
1524/*
1525 * Return a copy of dirSearchPath, incrementing the reference counts for
1526 * the contained directories.
1527 */
1528SearchPath *
1529Dir_CopyDirSearchPath(void)
1530{
1531	SearchPath *path = SearchPath_New();
1532	CachedDirListNode *ln;
1533	for (ln = dirSearchPath.dirs.first; ln != NULL; ln = ln->next) {
1534		CachedDir *dir = ln->datum;
1535		Lst_Append(&path->dirs, CachedDir_Ref(dir));
1536	}
1537	return path;
1538}
1539
1540/*
1541 * Make a string by taking all the directories in the given search path and
1542 * preceding them by the given flag. Used by the suffix module to create
1543 * variables for compilers based on suffix search paths. Note that there is no
1544 * space between the given flag and each directory.
1545 */
1546char *
1547SearchPath_ToFlags(SearchPath *path, const char *flag)
1548{
1549	Buffer buf;
1550	CachedDirListNode *ln;
1551
1552	Buf_Init(&buf);
1553
1554	if (path != NULL) {
1555		for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1556			CachedDir *dir = ln->datum;
1557			Buf_AddStr(&buf, " ");
1558			Buf_AddStr(&buf, flag);
1559			Buf_AddStr(&buf, dir->name);
1560		}
1561	}
1562
1563	return Buf_DoneData(&buf);
1564}
1565
1566/* Free the search path and all directories mentioned in it. */
1567void
1568SearchPath_Free(SearchPath *path)
1569{
1570	CachedDirListNode *ln;
1571
1572	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1573		CachedDir *dir = ln->datum;
1574		CachedDir_Unref(dir);
1575	}
1576	Lst_Done(&path->dirs);
1577	free(path);
1578}
1579
1580/*
1581 * Clear out all elements from the given search path.
1582 * The path is set to the empty list but is not destroyed.
1583 */
1584void
1585SearchPath_Clear(SearchPath *path)
1586{
1587	while (!Lst_IsEmpty(&path->dirs)) {
1588		CachedDir *dir = Lst_Dequeue(&path->dirs);
1589		CachedDir_Unref(dir);
1590	}
1591}
1592
1593
1594/*
1595 * Concatenate two paths, adding the second to the end of the first,
1596 * skipping duplicates.
1597 */
1598void
1599SearchPath_AddAll(SearchPath *dst, SearchPath *src)
1600{
1601	CachedDirListNode *ln;
1602
1603	for (ln = src->dirs.first; ln != NULL; ln = ln->next) {
1604		CachedDir *dir = ln->datum;
1605		if (Lst_FindDatum(&dst->dirs, dir) == NULL)
1606			Lst_Append(&dst->dirs, CachedDir_Ref(dir));
1607	}
1608}
1609
1610static int
1611percentage(int num, int den)
1612{
1613	return den != 0 ? num * 100 / den : 0;
1614}
1615
1616void
1617Dir_PrintDirectories(void)
1618{
1619	CachedDirListNode *ln;
1620
1621	debug_printf("#*** Directory Cache:\n");
1622	debug_printf(
1623	    "# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
1624	    hits, misses, nearmisses, bigmisses,
1625	    percentage(hits, hits + bigmisses + nearmisses));
1626	debug_printf("#  refs  hits  directory\n");
1627
1628	for (ln = openDirs.list.first; ln != NULL; ln = ln->next) {
1629		CachedDir *dir = ln->datum;
1630		debug_printf("#  %4d  %4d  %s\n",
1631		    dir->refCount, dir->hits, dir->name);
1632	}
1633}
1634
1635void
1636SearchPath_Print(const SearchPath *path)
1637{
1638	CachedDirListNode *ln;
1639
1640	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
1641		const CachedDir *dir = ln->datum;
1642		debug_printf("%s ", dir->name);
1643	}
1644}
1645