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