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