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