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