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