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