Home | History | Annotate | Line # | Download | only in make
      1 /*	$NetBSD: dir.c,v 1.297 2025/06/12 18:51:05 rillig 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"	*/
    135 MAKE_RCSID("$NetBSD: dir.c,v 1.297 2025/06/12 18:51:05 rillig 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. */
    202 struct 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 
    227 typedef List CachedDirList;
    228 typedef ListNode CachedDirListNode;
    229 
    230 /* A list of cached directories, with fast lookup by directory name. */
    231 typedef struct OpenDirs {
    232 	CachedDirList list;
    233 	HashTable /* of CachedDirListNode */ table;
    234 } OpenDirs;
    235 
    236 
    237 SearchPath dirSearchPath = { LST_INIT }; /* main search path */
    238 
    239 static OpenDirs openDirs;	/* all cached directories */
    240 
    241 /*
    242  * Variables for gathering statistics on the efficiency of the caching
    243  * mechanism.
    244  */
    245 static int hits;		/* Found in directory cache */
    246 static int misses;		/* Sad, but not evil misses */
    247 static int nearmisses;		/* Found under search path */
    248 static int bigmisses;		/* Sought by itself */
    249 
    250 /* The cached contents of ".", the relative current directory. */
    251 static CachedDir *dot = NULL;
    252 /* The cached contents of the absolute current directory. */
    253 static CachedDir *cur = NULL;
    254 /* A fake path entry indicating we need to look for '.' last. */
    255 static 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  */
    265 static HashTable mtimes;
    266 
    267 static HashTable lmtimes;	/* same as mtimes but for lstat */
    268 
    269 
    270 static void OpenDirs_Remove(OpenDirs *, const char *);
    271 
    272 
    273 static CachedDir *
    274 CachedDir_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 
    290 static CachedDir *
    291 CachedDir_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 
    303 static void
    304 CachedDir_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. */
    328 static void
    329 CachedDir_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 
    341 static void
    342 OpenDirs_Init(OpenDirs *odirs)
    343 {
    344 	Lst_Init(&odirs->list);
    345 	HashTable_Init(&odirs->table);
    346 }
    347 
    348 #ifdef CLEANUP
    349 static void
    350 OpenDirs_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 
    368 static CachedDir *
    369 OpenDirs_Find(OpenDirs *odirs, const char *name)
    370 {
    371 	CachedDirListNode *ln = HashTable_FindValue(&odirs->table, name);
    372 	return ln != NULL ? ln->datum : NULL;
    373 }
    374 
    375 static void
    376 OpenDirs_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 
    384 static void
    385 OpenDirs_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  */
    400 static int
    401 cached_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 
    442 int
    443 cached_stat(const char *pathname, struct cached_stat *cst)
    444 {
    445 	return cached_stats(pathname, cst, false, false);
    446 }
    447 
    448 int
    449 cached_lstat(const char *pathname, struct cached_stat *cst)
    450 {
    451 	return cached_stats(pathname, cst, true, false);
    452 }
    453 
    454 /* Initialize the directories module. */
    455 void
    456 Dir_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. */
    465 void
    466 Dir_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  */
    488 void
    489 Dir_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 #ifdef CLEANUP
    505 static void
    506 FreeCachedTable(HashTable *tbl)
    507 {
    508 	HashIter hi;
    509 	HashIter_Init(&hi, tbl);
    510 	while (HashIter_Next(&hi))
    511 		free(hi.entry->value);
    512 	HashTable_Done(tbl);
    513 }
    514 
    515 /* Clean up the directories module. */
    516 void
    517 Dir_End(void)
    518 {
    519 	CachedDir_Assign(&cur, NULL);
    520 	CachedDir_Assign(&dot, NULL);
    521 	CachedDir_Assign(&dotLast, NULL);
    522 	SearchPath_Clear(&dirSearchPath);
    523 	OpenDirs_Done(&openDirs);
    524 	FreeCachedTable(&mtimes);
    525 	FreeCachedTable(&lmtimes);
    526 }
    527 #endif
    528 
    529 /*
    530  * We want ${.PATH} to indicate the order in which we will actually
    531  * search, so we rebuild it after any .PATH: target.
    532  * This is the simplest way to deal with the effect of .DOTLAST.
    533  */
    534 void
    535 Dir_SetPATH(void)
    536 {
    537 	CachedDirListNode *ln;
    538 	bool seenDotLast = false;	/* true if we should search '.' last */
    539 
    540 	Global_Delete(".PATH");
    541 
    542 	if ((ln = dirSearchPath.dirs.first) != NULL) {
    543 		CachedDir *dir = ln->datum;
    544 		if (dir == dotLast) {
    545 			seenDotLast = true;
    546 			Global_Append(".PATH", dotLast->name);
    547 		}
    548 	}
    549 
    550 	if (!seenDotLast) {
    551 		if (dot != NULL)
    552 			Global_Append(".PATH", dot->name);
    553 		if (cur != NULL)
    554 			Global_Append(".PATH", cur->name);
    555 	}
    556 
    557 	for (ln = dirSearchPath.dirs.first; ln != NULL; ln = ln->next) {
    558 		CachedDir *dir = ln->datum;
    559 		if (dir == dotLast)
    560 			continue;
    561 		if (dir == dot && seenDotLast)
    562 			continue;
    563 		Global_Append(".PATH", dir->name);
    564 	}
    565 
    566 	if (seenDotLast) {
    567 		if (dot != NULL)
    568 			Global_Append(".PATH", dot->name);
    569 		if (cur != NULL)
    570 			Global_Append(".PATH", cur->name);
    571 	}
    572 }
    573 
    574 
    575 void
    576 Dir_SetSYSPATH(void)
    577 {
    578 	CachedDirListNode *ln;
    579 	SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
    580 		? defSysIncPath : sysIncPath;
    581 
    582 	Var_ReadOnly(".SYSPATH", false);
    583 	Global_Delete(".SYSPATH");
    584 	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
    585 		CachedDir *dir = ln->datum;
    586 		Global_Append(".SYSPATH", dir->name);
    587 	}
    588 	Var_ReadOnly(".SYSPATH", true);
    589 }
    590 
    591 /*
    592  * See if the given name has any wildcard characters in it and all braces and
    593  * brackets are properly balanced.
    594  *
    595  * XXX: This code is not 100% correct ([^]] fails etc.). I really don't think
    596  * that make(1) should be expanding patterns, because then you have to set a
    597  * mechanism for escaping the expansion!
    598  */
    599 bool
    600 Dir_HasWildcards(const char *name)
    601 {
    602 	const char *p;
    603 	bool wild = false;
    604 	int braces = 0, brackets = 0;
    605 
    606 	for (p = name; *p != '\0'; p++) {
    607 		switch (*p) {
    608 		case '{':
    609 			braces++;
    610 			wild = true;
    611 			break;
    612 		case '}':
    613 			braces--;
    614 			break;
    615 		case '[':
    616 			brackets++;
    617 			wild = true;
    618 			break;
    619 		case ']':
    620 			brackets--;
    621 			break;
    622 		case '?':
    623 		case '*':
    624 			wild = true;
    625 			break;
    626 		default:
    627 			break;
    628 		}
    629 	}
    630 	return wild && brackets == 0 && braces == 0;
    631 }
    632 
    633 /*
    634  * See if any files as seen from 'dir' match 'pattern', and add their names
    635  * to 'expansions' if they do.
    636  *
    637  * Wildcards are only expanded in the final path component, but not in
    638  * directories like src/lib*c/file*.c. To expand these wildcards,
    639  * delegate the work to the shell, using the '!=' variable assignment
    640  * operator, the ':sh' variable modifier or the ':!...!' variable modifier,
    641  * such as in ${:!echo src/lib*c/file*.c!}.
    642  */
    643 static void
    644 DirMatchFiles(const char *pattern, CachedDir *dir, StringList *expansions)
    645 {
    646 	const char *dirName = dir->name;
    647 	bool isDot = dirName[0] == '.' && dirName[1] == '\0';
    648 	HashIter hi;
    649 
    650 	/*
    651 	 * XXX: Iterating over all hash entries is inefficient.  If the
    652 	 * pattern is a plain string without any wildcards, a direct lookup
    653 	 * is faster.
    654 	 */
    655 
    656 	HashIter_InitSet(&hi, &dir->files);
    657 	while (HashIter_Next(&hi)) {
    658 		const char *base = hi.entry->key;
    659 		StrMatchResult res = Str_Match(base, pattern);
    660 		/* TODO: handle errors from res.error */
    661 
    662 		if (!res.matched)
    663 			continue;
    664 
    665 		/*
    666 		 * Follow the UNIX convention that dot files are only found
    667 		 * if the pattern begins with a dot. The pattern '.*' does
    668 		 * not match '.' or '..' since these are not included in the
    669 		 * directory cache.
    670 		 *
    671 		 * This means that the pattern '[a-z.]*' does not find
    672 		 * '.file', which is consistent with NetBSD sh, NetBSD ksh,
    673 		 * bash, dash, csh and probably many other shells as well.
    674 		 */
    675 		if (base[0] == '.' && pattern[0] != '.')
    676 			continue;
    677 
    678 		{
    679 			char *fullName = isDot
    680 			    ? bmake_strdup(base)
    681 			    : str_concat3(dirName, "/", base);
    682 			Lst_Append(expansions, fullName);
    683 		}
    684 	}
    685 }
    686 
    687 /* Find the next closing brace in 'p', taking nested braces into account. */
    688 static const char *
    689 closing_brace(const char *p)
    690 {
    691 	int depth = 0;
    692 	while (*p != '\0') {
    693 		if (*p == '}' && depth == 0)
    694 			break;
    695 		if (*p == '{')
    696 			depth++;
    697 		if (*p == '}')
    698 			depth--;
    699 		p++;
    700 	}
    701 	return p;
    702 }
    703 
    704 /*
    705  * Find the next closing brace or comma in the string, taking nested braces
    706  * into account.
    707  */
    708 static const char *
    709 separator_comma(const char *p)
    710 {
    711 	int depth = 0;
    712 	while (*p != '\0') {
    713 		if ((*p == '}' || *p == ',') && depth == 0)
    714 			break;
    715 		if (*p == '{')
    716 			depth++;
    717 		if (*p == '}')
    718 			depth--;
    719 		p++;
    720 	}
    721 	return p;
    722 }
    723 
    724 static bool
    725 contains_wildcard(const char *p)
    726 {
    727 	for (; *p != '\0'; p++) {
    728 		switch (*p) {
    729 		case '*':
    730 		case '?':
    731 		case '{':
    732 		case '[':
    733 			return true;
    734 		}
    735 	}
    736 	return false;
    737 }
    738 
    739 static char *
    740 concat3(const char *a, size_t a_len, const char *b, size_t b_len,
    741 	const char *c, size_t c_len)
    742 {
    743 	size_t s_len = a_len + b_len + c_len;
    744 	char *s = bmake_malloc(s_len + 1);
    745 	memcpy(s, a, a_len);
    746 	memcpy(s + a_len, b, b_len);
    747 	memcpy(s + a_len + b_len, c, c_len);
    748 	s[s_len] = '\0';
    749 	return s;
    750 }
    751 
    752 /*
    753  * Expand curly braces like the C shell. Brace expansion by itself is purely
    754  * textual, the expansions are not looked up in the file system. But if an
    755  * expanded word contains wildcard characters, it is expanded further,
    756  * matching only the actually existing files.
    757  *
    758  * Example: "{a{b,c}}" expands to "ab" and "ac".
    759  * Example: "{a}" expands to "a".
    760  * Example: "{a,*.c}" expands to "a" and all "*.c" files that exist.
    761  *
    762  * Input:
    763  *	word		Entire word to expand
    764  *	brace		First curly brace in it
    765  *	path		Search path to use
    766  *	expansions	Place to store the expansions
    767  */
    768 static void
    769 DirExpandCurly(const char *word, const char *brace, SearchPath *path,
    770 	       StringList *expansions)
    771 {
    772 	const char *prefix, *middle, *piece, *middle_end, *suffix;
    773 	size_t prefix_len, suffix_len;
    774 
    775 	/* Split the word into prefix, '{', middle, '}' and suffix. */
    776 
    777 	middle = brace + 1;
    778 	middle_end = closing_brace(middle);
    779 	if (*middle_end == '\0') {
    780 		Error("Unterminated {} clause \"%s\"", middle);
    781 		return;
    782 	}
    783 
    784 	prefix = word;
    785 	prefix_len = (size_t)(brace - prefix);
    786 	suffix = middle_end + 1;
    787 	suffix_len = strlen(suffix);
    788 
    789 	/* Split the middle into pieces, separated by commas. */
    790 
    791 	piece = middle;
    792 	while (piece < middle_end + 1) {
    793 		const char *piece_end = separator_comma(piece);
    794 		size_t piece_len = (size_t)(piece_end - piece);
    795 
    796 		char *file = concat3(prefix, prefix_len, piece, piece_len,
    797 		    suffix, suffix_len);
    798 
    799 		if (contains_wildcard(file)) {
    800 			SearchPath_Expand(path, file, expansions);
    801 			free(file);
    802 		} else {
    803 			Lst_Append(expansions, file);
    804 		}
    805 
    806 		/* skip over the comma or closing brace */
    807 		piece = piece_end + 1;
    808 	}
    809 }
    810 
    811 
    812 /* Expand 'pattern' in each of the directories from 'path'. */
    813 static void
    814 DirExpandPath(const char *pattern, SearchPath *path, StringList *expansions)
    815 {
    816 	CachedDirListNode *ln;
    817 	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
    818 		CachedDir *dir = ln->datum;
    819 		DirMatchFiles(pattern, dir, expansions);
    820 	}
    821 }
    822 
    823 static void
    824 PrintExpansions(StringList *expansions)
    825 {
    826 	const char *sep = "";
    827 	StringListNode *ln;
    828 	for (ln = expansions->first; ln != NULL; ln = ln->next) {
    829 		const char *word = ln->datum;
    830 		debug_printf("%s%s", sep, word);
    831 		sep = " ";
    832 	}
    833 	debug_printf("\n");
    834 }
    835 
    836 /*
    837  * The wildcard isn't in the first component.
    838  * Find all the components up to the one with the wildcard.
    839  */
    840 static void
    841 SearchPath_ExpandMiddle(SearchPath *path, const char *pattern,
    842 			const char *wildcardComponent, StringList *expansions)
    843 {
    844 	char *prefix, *dirpath, *end;
    845 	SearchPath *partPath;
    846 
    847 	prefix = bmake_strsedup(pattern, wildcardComponent + 1);
    848 	/*
    849 	 * XXX: Only the first match of the prefix in the path is
    850 	 * taken, any others are ignored.  The expectation may be
    851 	 * that the pattern is expanded in the whole path.
    852 	 */
    853 	dirpath = Dir_FindFile(prefix, path);
    854 	free(prefix);
    855 
    856 	/*
    857 	 * dirpath is null if can't find the leading component
    858 	 *
    859 	 * XXX: Dir_FindFile won't find internal components.  i.e. if the
    860 	 * path contains ../Etc/Object and we're looking for Etc, it won't
    861 	 * be found.  Ah well.  Probably not important.
    862 	 *
    863 	 * TODO: Check whether the above comment is still true.
    864 	 */
    865 	if (dirpath == NULL)
    866 		return;
    867 
    868 	end = &dirpath[strlen(dirpath) - 1];
    869 	/* XXX: What about multiple trailing slashes? */
    870 	if (*end == '/')
    871 		*end = '\0';
    872 
    873 	partPath = SearchPath_New();
    874 	(void)SearchPath_Add(partPath, dirpath);
    875 	DirExpandPath(wildcardComponent + 1, partPath, expansions);
    876 	SearchPath_Free(partPath);
    877 	free(dirpath);
    878 }
    879 
    880 /*
    881  * Expand the given pattern into a list of existing filenames by globbing it,
    882  * looking in each directory from the search path.
    883  *
    884  * Input:
    885  *	path		the directories in which to find the files
    886  *	pattern		the pattern to expand
    887  *	expansions	the list on which to place the results
    888  */
    889 void
    890 SearchPath_Expand(SearchPath *path, const char *pattern, StringList *expansions)
    891 {
    892 	const char *brace, *slash, *wildcard, *wildcardComponent;
    893 
    894 	assert(path != NULL);
    895 	assert(expansions != NULL);
    896 
    897 	DEBUG1(DIR, "Expanding \"%s\"... ", pattern);
    898 
    899 	brace = strchr(pattern, '{');
    900 	if (brace != NULL) {
    901 		DirExpandCurly(pattern, brace, path, expansions);
    902 		goto done;
    903 	}
    904 
    905 	slash = strchr(pattern, '/');
    906 	if (slash == NULL) {
    907 		DirMatchFiles(pattern, dot, expansions);
    908 		DirExpandPath(pattern, path, expansions);
    909 		goto done;
    910 	}
    911 
    912 	/* At this point, the pattern has a directory component. */
    913 
    914 	/* Find the first wildcard in the pattern. */
    915 	for (wildcard = pattern; *wildcard != '\0'; wildcard++)
    916 		if (*wildcard == '?' || *wildcard == '[' || *wildcard == '*')
    917 			break;
    918 
    919 	if (*wildcard == '\0') {
    920 		/*
    921 		 * No directory component and no wildcard at all -- this
    922 		 * should never happen as in such a simple case there is no
    923 		 * need to expand anything.
    924 		 */
    925 		DirExpandPath(pattern, path, expansions);
    926 		goto done;
    927 	}
    928 
    929 	/* Back up to the start of the component containing the wildcard. */
    930 	/* XXX: This handles '///' and '/' differently. */
    931 	wildcardComponent = wildcard;
    932 	while (wildcardComponent > pattern && *wildcardComponent != '/')
    933 		wildcardComponent--;
    934 
    935 	if (wildcardComponent == pattern) {
    936 		/* The first component contains the wildcard. */
    937 		/* Start the search from the local directory */
    938 		DirExpandPath(pattern, path, expansions);
    939 	} else {
    940 		SearchPath_ExpandMiddle(path, pattern, wildcardComponent,
    941 		    expansions);
    942 	}
    943 
    944 done:
    945 	if (DEBUG(DIR))
    946 		PrintExpansions(expansions);
    947 }
    948 
    949 /*
    950  * Find if 'base' exists in 'dir'.
    951  * Return the freshly allocated path to the file, or NULL.
    952  */
    953 static char *
    954 DirLookup(CachedDir *dir, const char *base)
    955 {
    956 	char *file;
    957 
    958 	DEBUG1(DIR, "   %s ...\n", dir->name);
    959 
    960 	if (!HashSet_Contains(&dir->files, base))
    961 		return NULL;
    962 
    963 	file = str_concat3(dir->name, "/", base);
    964 	DEBUG1(DIR, "   returning %s\n", file);
    965 	dir->hits++;
    966 	hits++;
    967 	return file;
    968 }
    969 
    970 
    971 /*
    972  * Find if 'name' exists in 'dir'.
    973  * Return the freshly allocated path to the file, or NULL.
    974  */
    975 static char *
    976 DirLookupSubdir(CachedDir *dir, const char *name)
    977 {
    978 	struct cached_stat cst;
    979 	char *file = dir == dot
    980 	    ? bmake_strdup(name)
    981 	    : str_concat3(dir->name, "/", name);
    982 
    983 	DEBUG1(DIR, "checking %s ...\n", file);
    984 
    985 	if (cached_stat(file, &cst) == 0) {
    986 		nearmisses++;
    987 		return file;
    988 	}
    989 	free(file);
    990 	return NULL;
    991 }
    992 
    993 /*
    994  * Find if 'name' (which has basename 'base') exists in 'dir'.
    995  * Return the freshly allocated path to the file, an empty string, or NULL.
    996  * Returning an empty string means that the search should be terminated.
    997  */
    998 static char *
    999 DirLookupAbs(CachedDir *dir, const char *name, const char *base)
   1000 {
   1001 	const char *dnp;	/* pointer into dir->name */
   1002 	const char *np;		/* pointer into name */
   1003 
   1004 	DEBUG1(DIR, "   %s ...\n", dir->name);
   1005 
   1006 	/*
   1007 	 * If the file has a leading path component and that component
   1008 	 * exactly matches the entire name of the current search
   1009 	 * directory, we can attempt another cache lookup. And if we don't
   1010 	 * have a hit, we can safely assume the file does not exist at all.
   1011 	 */
   1012 	for (dnp = dir->name, np = name;
   1013 	     *dnp != '\0' && *dnp == *np; dnp++, np++)
   1014 		continue;
   1015 	if (*dnp != '\0' || np != base - 1)
   1016 		return NULL;
   1017 
   1018 	if (!HashSet_Contains(&dir->files, base)) {
   1019 		DEBUG0(DIR, "   must be here but isn't -- returning\n");
   1020 		return bmake_strdup("");	/* to terminate the search */
   1021 	}
   1022 
   1023 	dir->hits++;
   1024 	hits++;
   1025 	DEBUG1(DIR, "   returning %s\n", name);
   1026 	return bmake_strdup(name);
   1027 }
   1028 
   1029 /*
   1030  * Find the given file in "." or curdir.
   1031  * Return the freshly allocated path to the file, or NULL.
   1032  */
   1033 static char *
   1034 DirFindDot(const char *name, const char *base)
   1035 {
   1036 
   1037 	if (HashSet_Contains(&dot->files, base)) {
   1038 		DEBUG0(DIR, "   in '.'\n");
   1039 		hits++;
   1040 		dot->hits++;
   1041 		return bmake_strdup(name);
   1042 	}
   1043 
   1044 	if (cur != NULL && HashSet_Contains(&cur->files, base)) {
   1045 		DEBUG1(DIR, "   in ${.CURDIR} = %s\n", cur->name);
   1046 		hits++;
   1047 		cur->hits++;
   1048 		return str_concat3(cur->name, "/", base);
   1049 	}
   1050 
   1051 	return NULL;
   1052 }
   1053 
   1054 static bool
   1055 FindFileRelative(SearchPath *path, bool seenDotLast,
   1056 		 const char *name, char **out_file)
   1057 {
   1058 	CachedDirListNode *ln;
   1059 	bool checkedDot = false;
   1060 	char *file;
   1061 
   1062 	DEBUG0(DIR, "   Trying subdirectories...\n");
   1063 
   1064 	if (!seenDotLast) {
   1065 		if (dot != NULL) {
   1066 			checkedDot = true;
   1067 			if ((file = DirLookupSubdir(dot, name)) != NULL)
   1068 				goto done;
   1069 		}
   1070 		if (cur != NULL &&
   1071 		    (file = DirLookupSubdir(cur, name)) != NULL)
   1072 			goto done;
   1073 	}
   1074 
   1075 	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1076 		CachedDir *dir = ln->datum;
   1077 		if (dir == dotLast)
   1078 			continue;
   1079 		if (dir == dot) {
   1080 			if (checkedDot)
   1081 				continue;
   1082 			checkedDot = true;
   1083 		}
   1084 		if ((file = DirLookupSubdir(dir, name)) != NULL)
   1085 			goto done;
   1086 	}
   1087 
   1088 	if (seenDotLast) {
   1089 		if (dot != NULL && !checkedDot) {
   1090 			checkedDot = true;
   1091 			if ((file = DirLookupSubdir(dot, name)) != NULL)
   1092 				goto done;
   1093 		}
   1094 		if (cur != NULL &&
   1095 		    (file = DirLookupSubdir(cur, name)) != NULL)
   1096 			goto done;
   1097 	}
   1098 
   1099 	if (checkedDot) {
   1100 		/*
   1101 		 * Already checked by the given name, since . was in
   1102 		 * the path, so no point in proceeding.
   1103 		 */
   1104 		DEBUG0(DIR, "   Checked . already, returning NULL\n");
   1105 		file = NULL;
   1106 		goto done;
   1107 	}
   1108 
   1109 	return false;
   1110 
   1111 done:
   1112 	*out_file = file;
   1113 	return true;
   1114 }
   1115 
   1116 static bool
   1117 FindFileAbsolute(SearchPath *path, bool seenDotLast,
   1118 		 const char *name, const char *base, char **out_file)
   1119 {
   1120 	char *file;
   1121 	CachedDirListNode *ln;
   1122 
   1123 	DEBUG0(DIR, "   Trying exact path matches...\n");
   1124 
   1125 	if (!seenDotLast && cur != NULL &&
   1126 	    ((file = DirLookupAbs(cur, name, base)) != NULL))
   1127 		goto found;
   1128 
   1129 	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1130 		CachedDir *dir = ln->datum;
   1131 		if (dir == dotLast)
   1132 			continue;
   1133 		if ((file = DirLookupAbs(dir, name, base)) != NULL)
   1134 			goto found;
   1135 	}
   1136 
   1137 	if (seenDotLast && cur != NULL &&
   1138 	    ((file = DirLookupAbs(cur, name, base)) != NULL))
   1139 		goto found;
   1140 
   1141 	return false;
   1142 
   1143 found:
   1144 	if (file[0] == '\0') {
   1145 		free(file);
   1146 		file = NULL;
   1147 	}
   1148 	*out_file = file;
   1149 	return true;
   1150 }
   1151 
   1152 /*
   1153  * Find the file with the given name along the given search path.
   1154  *
   1155  * Input:
   1156  *	name		the file to find
   1157  *	path		the directories to search, or NULL
   1158  *	isinclude	if true, do not search .CURDIR at all
   1159  *
   1160  * Results:
   1161  *	The freshly allocated path to the file, or NULL.
   1162  */
   1163 static char *
   1164 FindFile(const char *name, SearchPath *path, bool isinclude)
   1165 {
   1166 	char *file;		/* the current filename to check */
   1167 	bool seenDotLast = isinclude; /* true if we should search dot last */
   1168 	struct cached_stat cst;
   1169 	const char *trailing_dot = ".";
   1170 	const char *base = str_basename(name);
   1171 
   1172 	DEBUG1(DIR, "Searching for %s ...", name);
   1173 
   1174 	if (path == NULL) {
   1175 		DEBUG0(DIR, "couldn't open path, file not found\n");
   1176 		misses++;
   1177 		return NULL;
   1178 	}
   1179 
   1180 	if (!seenDotLast && path->dirs.first != NULL) {
   1181 		CachedDir *dir = path->dirs.first->datum;
   1182 		if (dir == dotLast) {
   1183 			seenDotLast = true;
   1184 			DEBUG0(DIR, "[dot last]...");
   1185 		}
   1186 	}
   1187 	DEBUG0(DIR, "\n");
   1188 
   1189 	/*
   1190 	 * If there's no leading directory components or if the leading
   1191 	 * directory component is exactly `./', consult the cached contents
   1192 	 * of each of the directories on the search path.
   1193 	 */
   1194 	if (base == name || (base - name == 2 && *name == '.')) {
   1195 		CachedDirListNode *ln;
   1196 
   1197 		/*
   1198 		 * Look through all the directories on the path seeking one
   1199 		 * which contains the final component of the given name.  If
   1200 		 * such a file is found, return its pathname.
   1201 		 * If there is no such file, go on to phase two.
   1202 		 *
   1203 		 * No matter what, always look for the file in the current
   1204 		 * directory before anywhere else (unless the path contains
   1205 		 * the magic '.DOTLAST', in which case search it last).
   1206 		 * This is so there are no conflicts between what the user
   1207 		 * specifies (fish.c) and what make finds (./fish.c).
   1208 		 */
   1209 		if (!seenDotLast && (file = DirFindDot(name, base)) != NULL)
   1210 			return file;
   1211 
   1212 		for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1213 			CachedDir *dir = ln->datum;
   1214 			if (dir == dotLast)
   1215 				continue;
   1216 			if ((file = DirLookup(dir, base)) != NULL)
   1217 				return file;
   1218 		}
   1219 
   1220 		if (seenDotLast && (file = DirFindDot(name, base)) != NULL)
   1221 			return file;
   1222 	}
   1223 
   1224 	if (base == name) {
   1225 		DEBUG0(DIR, "   failed.\n");
   1226 		misses++;
   1227 		return NULL;
   1228 	}
   1229 
   1230 	if (*base == '\0')
   1231 		base = trailing_dot;	/* we were given a trailing "/" */
   1232 
   1233 	if (name[0] != '/') {
   1234 		if (FindFileRelative(path, seenDotLast, name, &file))
   1235 			return file;
   1236 	} else {
   1237 		if (FindFileAbsolute(path, seenDotLast, name, base, &file))
   1238 			return file;
   1239 	}
   1240 
   1241 	/*
   1242 	 * We cannot add the directory onto the search path because
   1243 	 * of this amusing case:
   1244 	 * $(INSTALLDIR)/$(FILE): $(FILE)
   1245 	 *
   1246 	 * $(FILE) exists in $(INSTALLDIR) but not in the current one.
   1247 	 * When searching for $(FILE), we will find it in $(INSTALLDIR)
   1248 	 * b/c we added it here. This is not good...
   1249 	 */
   1250 
   1251 	DEBUG1(DIR, "   Looking for \"%s\" ...\n", name);
   1252 
   1253 	bigmisses++;
   1254 	if (cached_stat(name, &cst) == 0)
   1255 		return bmake_strdup(name);
   1256 
   1257 	DEBUG0(DIR, "   failed. Returning NULL\n");
   1258 	return NULL;
   1259 }
   1260 
   1261 /*
   1262  * Find the file with the given name along the given search path.
   1263  *
   1264  * Input:
   1265  *	name		the file to find
   1266  *	path		the directories to search, or NULL
   1267  *
   1268  * Results:
   1269  *	The freshly allocated path to the file, or NULL.
   1270  */
   1271 char *
   1272 Dir_FindFile(const char *name, SearchPath *path)
   1273 {
   1274 	return FindFile(name, path, false);
   1275 }
   1276 
   1277 /*
   1278  * Find the include file with the given name along the given search path.
   1279  *
   1280  * Input:
   1281  *	name		the file to find
   1282  *	path		the directories to search, or NULL
   1283  *
   1284  * Results:
   1285  *	The freshly allocated path to the file, or NULL.
   1286  */
   1287 char *
   1288 Dir_FindInclude(const char *name, SearchPath *path)
   1289 {
   1290 	return FindFile(name, path, true);
   1291 }
   1292 
   1293 
   1294 /*
   1295  * Search for 'needle' starting at the directory 'here' and then working our
   1296  * way up towards the root directory. Return the allocated path, or NULL.
   1297  */
   1298 char *
   1299 Dir_FindHereOrAbove(const char *here, const char *needle)
   1300 {
   1301 	struct cached_stat cst;
   1302 	char *dirbase, *dirbase_end;
   1303 	char *try, *try_end;
   1304 
   1305 	dirbase = bmake_strdup(here);
   1306 	dirbase_end = dirbase + strlen(dirbase);
   1307 
   1308 	for (;;) {
   1309 		try = str_concat3(dirbase, "/", needle);
   1310 		if (cached_stat(try, &cst) != -1) {
   1311 			if ((cst.cst_mode & S_IFMT) != S_IFDIR) {
   1312 				/*
   1313 				 * Chop off the filename, to return a
   1314 				 * directory.
   1315 				 */
   1316 				try_end = try + strlen(try);
   1317 				while (try_end > try && *try_end != '/')
   1318 					try_end--;
   1319 				if (try_end > try)
   1320 					*try_end = '\0';	/* chop! */
   1321 			}
   1322 
   1323 			free(dirbase);
   1324 			return try;
   1325 		}
   1326 		free(try);
   1327 
   1328 		if (dirbase_end == dirbase)
   1329 			break;	/* failed! */
   1330 
   1331 		/* Truncate dirbase from the end to move up a dir. */
   1332 		while (dirbase_end > dirbase && *dirbase_end != '/')
   1333 			dirbase_end--;
   1334 		*dirbase_end = '\0';	/* chop! */
   1335 	}
   1336 
   1337 	free(dirbase);
   1338 	return NULL;
   1339 }
   1340 
   1341 /*
   1342  * This is an implied source, and it may have moved,
   1343  * see if we can find it via the current .PATH
   1344  */
   1345 static char *
   1346 ResolveMovedDepends(GNode *gn)
   1347 {
   1348 	char *fullName;
   1349 
   1350 	const char *base = str_basename(gn->name);
   1351 	if (base == gn->name)
   1352 		return NULL;
   1353 
   1354 	fullName = Dir_FindFile(base, Suff_FindPath(gn));
   1355 	if (fullName == NULL)
   1356 		return NULL;
   1357 
   1358 	/*
   1359 	 * Put the found file in gn->path so that we give that to the compiler.
   1360 	 */
   1361 	/*
   1362 	 * XXX: Better just reset gn->path to NULL; updating it is already done
   1363 	 * by Dir_UpdateMTime.
   1364 	 */
   1365 	gn->path = bmake_strdup(fullName);
   1366 	if (!Job_RunTarget(".STALE", gn->fname))
   1367 		fprintf(stdout,	/* XXX: Why stdout? */
   1368 		    "%s: %s:%u: ignoring stale %s for %s, found %s\n",
   1369 		    progname, gn->fname, gn->lineno,
   1370 		    makeDependfile, gn->name, fullName);
   1371 
   1372 	return fullName;
   1373 }
   1374 
   1375 static char *
   1376 ResolveFullName(GNode *gn)
   1377 {
   1378 	char *fullName;
   1379 
   1380 	fullName = gn->path;
   1381 	if (fullName == NULL && !(gn->type & OP_NOPATH)) {
   1382 
   1383 		fullName = Dir_FindFile(gn->name, Suff_FindPath(gn));
   1384 
   1385 		if (fullName == NULL && gn->flags.fromDepend &&
   1386 		    !Lst_IsEmpty(&gn->implicitParents))
   1387 			fullName = ResolveMovedDepends(gn);
   1388 
   1389 		DEBUG2(DIR, "Found '%s' as '%s'\n",
   1390 		    gn->name, fullName != NULL ? fullName : "(not found)");
   1391 	}
   1392 
   1393 	if (fullName == NULL)
   1394 		fullName = bmake_strdup(gn->name);
   1395 
   1396 	/* XXX: Is every piece of memory freed as it should? */
   1397 
   1398 	return fullName;
   1399 }
   1400 
   1401 /*
   1402  * Search 'gn' along 'dirSearchPath' and store its modification time in
   1403  * 'gn->mtime'. If no file is found, store 0 instead.
   1404  *
   1405  * The found file is stored in 'gn->path', unless the node already had a path.
   1406  */
   1407 void
   1408 Dir_UpdateMTime(GNode *gn, bool forceRefresh)
   1409 {
   1410 	char *fullName;
   1411 	struct cached_stat cst;
   1412 
   1413 	if (gn->type & OP_ARCHV) {
   1414 		Arch_UpdateMTime(gn);
   1415 		return;
   1416 	}
   1417 
   1418 	if (gn->type & OP_PHONY) {
   1419 		gn->mtime = 0;
   1420 		return;
   1421 	}
   1422 
   1423 	fullName = ResolveFullName(gn);
   1424 
   1425 	if (cached_stats(fullName, &cst, false, forceRefresh) < 0) {
   1426 		if (gn->type & OP_MEMBER) {
   1427 			if (fullName != gn->path)
   1428 				free(fullName);
   1429 			Arch_UpdateMemberMTime(gn);
   1430 			return;
   1431 		}
   1432 
   1433 		cst.cst_mtime = 0;
   1434 	}
   1435 
   1436 	if (fullName != NULL && gn->path == NULL)
   1437 		gn->path = fullName;
   1438 	/* XXX: else free(fullName)? */
   1439 
   1440 	gn->mtime = cst.cst_mtime;
   1441 }
   1442 
   1443 /*
   1444  * Read the directory and add it to the cache in openDirs.
   1445  * If a path is given, add the directory to that path as well.
   1446  */
   1447 static CachedDir *
   1448 CacheNewDir(const char *name, SearchPath *path)
   1449 {
   1450 	CachedDir *dir = NULL;
   1451 	DIR *d;
   1452 	struct dirent *dp;
   1453 
   1454 	if ((d = opendir(name)) == NULL) {
   1455 		DEBUG1(DIR, "Caching %s ... not found\n", name);
   1456 		return dir;
   1457 	}
   1458 
   1459 	DEBUG1(DIR, "Caching %s ...\n", name);
   1460 
   1461 	dir = CachedDir_New(name);
   1462 
   1463 	while ((dp = readdir(d)) != NULL) {
   1464 
   1465 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
   1466 		/*
   1467 		 * The sun directory library doesn't check for a 0 inode
   1468 		 * (0-inode slots just take up space), so we have to do
   1469 		 * it ourselves.
   1470 		 */
   1471 		if (dp->d_fileno == 0)
   1472 			continue;
   1473 #endif /* sun && d_ino */
   1474 
   1475 		(void)HashSet_Add(&dir->files, dp->d_name);
   1476 	}
   1477 	(void)closedir(d);
   1478 
   1479 	OpenDirs_Add(&openDirs, dir);
   1480 	if (path != NULL)
   1481 		Lst_Append(&path->dirs, CachedDir_Ref(dir));
   1482 
   1483 	DEBUG1(DIR, "Caching %s done\n", name);
   1484 	return dir;
   1485 }
   1486 
   1487 /*
   1488  * Read the list of filenames in the directory 'name' and store the result
   1489  * in 'openDirs'.
   1490  *
   1491  * If a search path is given, append the directory to that path.
   1492  *
   1493  * Input:
   1494  *	path		The path to which the directory should be
   1495  *			added, or NULL to only add the directory to openDirs.
   1496  *	name		The name of the directory to add.
   1497  *			The name is not normalized in any way.
   1498  * Output:
   1499  *	result		If no path is given and the directory exists, the
   1500  *			returned CachedDir has a reference count of 0.  It
   1501  *			must either be assigned to a variable using
   1502  *			CachedDir_Assign or be appended to a SearchPath using
   1503  *			Lst_Append and CachedDir_Ref.
   1504  */
   1505 CachedDir *
   1506 SearchPath_Add(SearchPath *path, const char *name)
   1507 {
   1508 
   1509 	if (path != NULL && strcmp(name, ".DOTLAST") == 0) {
   1510 		CachedDirListNode *ln;
   1511 
   1512 		/* XXX: Linear search gets slow with thousands of entries. */
   1513 		for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1514 			CachedDir *pathDir = ln->datum;
   1515 			if (strcmp(pathDir->name, name) == 0)
   1516 				return pathDir;
   1517 		}
   1518 
   1519 		Lst_Prepend(&path->dirs, CachedDir_Ref(dotLast));
   1520 	}
   1521 
   1522 	if (path != NULL) {
   1523 		/* XXX: Why is OpenDirs only checked if path != NULL? */
   1524 		CachedDir *dir = OpenDirs_Find(&openDirs, name);
   1525 		if (dir != NULL) {
   1526 			if (Lst_FindDatum(&path->dirs, dir) == NULL)
   1527 				Lst_Append(&path->dirs, CachedDir_Ref(dir));
   1528 			return dir;
   1529 		}
   1530 	}
   1531 
   1532 	return CacheNewDir(name, path);
   1533 }
   1534 
   1535 /*
   1536  * Return a copy of dirSearchPath, incrementing the reference counts for
   1537  * the contained directories.
   1538  */
   1539 SearchPath *
   1540 Dir_CopyDirSearchPath(void)
   1541 {
   1542 	SearchPath *path = SearchPath_New();
   1543 	CachedDirListNode *ln;
   1544 	for (ln = dirSearchPath.dirs.first; ln != NULL; ln = ln->next) {
   1545 		CachedDir *dir = ln->datum;
   1546 		Lst_Append(&path->dirs, CachedDir_Ref(dir));
   1547 	}
   1548 	return path;
   1549 }
   1550 
   1551 /*
   1552  * Make a string by taking all the directories in the given search path and
   1553  * preceding them by the given flag. Used by the suffix module to create
   1554  * variables for compilers based on suffix search paths. Note that there is no
   1555  * space between the given flag and each directory.
   1556  */
   1557 char *
   1558 SearchPath_ToFlags(SearchPath *path, const char *flag)
   1559 {
   1560 	Buffer buf;
   1561 	CachedDirListNode *ln;
   1562 
   1563 	Buf_Init(&buf);
   1564 
   1565 	if (path != NULL) {
   1566 		for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1567 			CachedDir *dir = ln->datum;
   1568 			Buf_AddStr(&buf, " ");
   1569 			Buf_AddStr(&buf, flag);
   1570 			Buf_AddStr(&buf, dir->name);
   1571 		}
   1572 	}
   1573 
   1574 	return Buf_DoneData(&buf);
   1575 }
   1576 
   1577 /* Free the search path and all directories mentioned in it. */
   1578 void
   1579 SearchPath_Free(SearchPath *path)
   1580 {
   1581 	CachedDirListNode *ln;
   1582 
   1583 	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1584 		CachedDir *dir = ln->datum;
   1585 		CachedDir_Unref(dir);
   1586 	}
   1587 	Lst_Done(&path->dirs);
   1588 	free(path);
   1589 }
   1590 
   1591 /*
   1592  * Clear out all elements from the given search path.
   1593  * The path is set to the empty list but is not destroyed.
   1594  */
   1595 void
   1596 SearchPath_Clear(SearchPath *path)
   1597 {
   1598 	while (!Lst_IsEmpty(&path->dirs)) {
   1599 		CachedDir *dir = Lst_Dequeue(&path->dirs);
   1600 		CachedDir_Unref(dir);
   1601 	}
   1602 }
   1603 
   1604 
   1605 /*
   1606  * Concatenate two paths, adding the second to the end of the first,
   1607  * skipping duplicates.
   1608  */
   1609 void
   1610 SearchPath_AddAll(SearchPath *dst, SearchPath *src)
   1611 {
   1612 	CachedDirListNode *ln;
   1613 
   1614 	for (ln = src->dirs.first; ln != NULL; ln = ln->next) {
   1615 		CachedDir *dir = ln->datum;
   1616 		if (Lst_FindDatum(&dst->dirs, dir) == NULL)
   1617 			Lst_Append(&dst->dirs, CachedDir_Ref(dir));
   1618 	}
   1619 }
   1620 
   1621 static int
   1622 percentage(int num, int den)
   1623 {
   1624 	return den != 0 ? num * 100 / den : 0;
   1625 }
   1626 
   1627 void
   1628 Dir_PrintDirectories(void)
   1629 {
   1630 	CachedDirListNode *ln;
   1631 
   1632 	debug_printf("#*** Directory Cache:\n");
   1633 	debug_printf(
   1634 	    "# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
   1635 	    hits, misses, nearmisses, bigmisses,
   1636 	    percentage(hits, hits + bigmisses + nearmisses));
   1637 	debug_printf("#  refs  hits  directory\n");
   1638 
   1639 	for (ln = openDirs.list.first; ln != NULL; ln = ln->next) {
   1640 		CachedDir *dir = ln->datum;
   1641 		debug_printf("#  %4d  %4d  %s\n",
   1642 		    dir->refCount, dir->hits, dir->name);
   1643 	}
   1644 }
   1645 
   1646 void
   1647 SearchPath_Print(const SearchPath *path)
   1648 {
   1649 	CachedDirListNode *ln;
   1650 
   1651 	for (ln = path->dirs.first; ln != NULL; ln = ln->next) {
   1652 		const CachedDir *dir = ln->datum;
   1653 		debug_printf("%s ", dir->name);
   1654 	}
   1655 }
   1656