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