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