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