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