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