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