Home | History | Annotate | Line # | Download | only in make
dir.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1988, 1989, 1990, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  * Copyright (c) 1989 by Berkeley Softworks
      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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 static char sccsid[] = "@(#)dir.c	8.3 (Berkeley) 4/28/95";
     41 #endif /* not lint */
     42 
     43 /*-
     44  * dir.c --
     45  *	Directory searching using wildcards and/or normal names...
     46  *	Used both for source wildcarding in the Makefile and for finding
     47  *	implicit sources.
     48  *
     49  * The interface for this module is:
     50  *	Dir_Init  	    Initialize the module.
     51  *
     52  *	Dir_End  	    Cleanup the module.
     53  *
     54  *	Dir_HasWildcards    Returns TRUE if the name given it needs to
     55  *	    	  	    be wildcard-expanded.
     56  *
     57  *	Dir_Expand	    Given a pattern and a path, return a Lst of names
     58  *	    	  	    which match the pattern on the search path.
     59  *
     60  *	Dir_FindFile	    Searches for a file on a given search path.
     61  *	    	  	    If it exists, the entire path is returned.
     62  *	    	  	    Otherwise NULL is returned.
     63  *
     64  *	Dir_MTime 	    Return the modification time of a node. The file
     65  *	    	  	    is searched for along the default search path.
     66  *	    	  	    The path and mtime fields of the node are filled
     67  *	    	  	    in.
     68  *
     69  *	Dir_AddDir	    Add a directory to a search path.
     70  *
     71  *	Dir_MakeFlags	    Given a search path and a command flag, create
     72  *	    	  	    a string with each of the directories in the path
     73  *	    	  	    preceded by the command flag and all of them
     74  *	    	  	    separated by a space.
     75  *
     76  *	Dir_Destroy	    Destroy an element of a search path. Frees up all
     77  *	    	  	    things that can be freed for the element as long
     78  *	    	  	    as the element is no longer referenced by any other
     79  *	    	  	    search path.
     80  *	Dir_ClearPath	    Resets a search path to the empty list.
     81  *
     82  * For debugging:
     83  *	Dir_PrintDirectories	Print stats about the directory cache.
     84  */
     85 
     86 #include <stdio.h>
     87 #include <sys/types.h>
     88 #include <dirent.h>
     89 #include <sys/stat.h>
     90 #include "make.h"
     91 #include "hash.h"
     92 #include "dir.h"
     93 
     94 /*
     95  *	A search path consists of a Lst of Path structures. A Path structure
     96  *	has in it the name of the directory and a hash table of all the files
     97  *	in the directory. This is used to cut down on the number of system
     98  *	calls necessary to find implicit dependents and their like. Since
     99  *	these searches are made before any actions are taken, we need not
    100  *	worry about the directory changing due to creation commands. If this
    101  *	hampers the style of some makefiles, they must be changed.
    102  *
    103  *	A list of all previously-read directories is kept in the
    104  *	openDirectories Lst. This list is checked first before a directory
    105  *	is opened.
    106  *
    107  *	The need for the caching of whole directories is brought about by
    108  *	the multi-level transformation code in suff.c, which tends to search
    109  *	for far more files than regular make does. In the initial
    110  *	implementation, the amount of time spent performing "stat" calls was
    111  *	truly astronomical. The problem with hashing at the start is,
    112  *	of course, that pmake doesn't then detect changes to these directories
    113  *	during the course of the make. Three possibilities suggest themselves:
    114  *
    115  *	    1) just use stat to test for a file's existence. As mentioned
    116  *	       above, this is very inefficient due to the number of checks
    117  *	       engendered by the multi-level transformation code.
    118  *	    2) use readdir() and company to search the directories, keeping
    119  *	       them open between checks. I have tried this and while it
    120  *	       didn't slow down the process too much, it could severely
    121  *	       affect the amount of parallelism available as each directory
    122  *	       open would take another file descriptor out of play for
    123  *	       handling I/O for another job. Given that it is only recently
    124  *	       that UNIX OS's have taken to allowing more than 20 or 32
    125  *	       file descriptors for a process, this doesn't seem acceptable
    126  *	       to me.
    127  *	    3) record the mtime of the directory in the Path structure and
    128  *	       verify the directory hasn't changed since the contents were
    129  *	       hashed. This will catch the creation or deletion of files,
    130  *	       but not the updating of files. However, since it is the
    131  *	       creation and deletion that is the problem, this could be
    132  *	       a good thing to do. Unfortunately, if the directory (say ".")
    133  *	       were fairly large and changed fairly frequently, the constant
    134  *	       rehashing could seriously degrade performance. It might be
    135  *	       good in such cases to keep track of the number of rehashes
    136  *	       and if the number goes over a (small) limit, resort to using
    137  *	       stat in its place.
    138  *
    139  *	An additional thing to consider is that pmake is used primarily
    140  *	to create C programs and until recently pcc-based compilers refused
    141  *	to allow you to specify where the resulting object file should be
    142  *	placed. This forced all objects to be created in the current
    143  *	directory. This isn't meant as a full excuse, just an explanation of
    144  *	some of the reasons for the caching used here.
    145  *
    146  *	One more note: the location of a target's file is only performed
    147  *	on the downward traversal of the graph and then only for terminal
    148  *	nodes in the graph. This could be construed as wrong in some cases,
    149  *	but prevents inadvertent modification of files when the "installed"
    150  *	directory for a file is provided in the search path.
    151  *
    152  *	Another data structure maintained by this module is an mtime
    153  *	cache used when the searching of cached directories fails to find
    154  *	a file. In the past, Dir_FindFile would simply perform an access()
    155  *	call in such a case to determine if the file could be found using
    156  *	just the name given. When this hit, however, all that was gained
    157  *	was the knowledge that the file existed. Given that an access() is
    158  *	essentially a stat() without the copyout() call, and that the same
    159  *	filesystem overhead would have to be incurred in Dir_MTime, it made
    160  *	sense to replace the access() with a stat() and record the mtime
    161  *	in a cache for when Dir_MTime was actually called.
    162  */
    163 
    164 Lst          dirSearchPath;	/* main search path */
    165 
    166 static Lst   openDirectories;	/* the list of all open directories */
    167 
    168 /*
    169  * Variables for gathering statistics on the efficiency of the hashing
    170  * mechanism.
    171  */
    172 static int    hits,	      /* Found in directory cache */
    173 	      misses,	      /* Sad, but not evil misses */
    174 	      nearmisses,     /* Found under search path */
    175 	      bigmisses;      /* Sought by itself */
    176 
    177 static Path    	  *dot;	    /* contents of current directory */
    178 static Hash_Table mtimes;   /* Results of doing a last-resort stat in
    179 			     * Dir_FindFile -- if we have to go to the
    180 			     * system to find the file, we might as well
    181 			     * have its mtime on record. XXX: If this is done
    182 			     * way early, there's a chance other rules will
    183 			     * have already updated the file, in which case
    184 			     * we'll update it again. Generally, there won't
    185 			     * be two rules to update a single file, so this
    186 			     * should be ok, but... */
    187 
    188 
    189 static int DirFindName __P((ClientData, ClientData));
    190 static int DirMatchFiles __P((char *, Path *, Lst));
    191 static void DirExpandCurly __P((char *, char *, Lst, Lst));
    192 static void DirExpandInt __P((char *, Lst, Lst));
    193 static int DirPrintWord __P((ClientData, ClientData));
    194 static int DirPrintDir __P((ClientData, ClientData));
    195 
    196 /*-
    197  *-----------------------------------------------------------------------
    198  * Dir_Init --
    199  *	initialize things for this module
    200  *
    201  * Results:
    202  *	none
    203  *
    204  * Side Effects:
    205  *	some directories may be opened.
    206  *-----------------------------------------------------------------------
    207  */
    208 void
    209 Dir_Init ()
    210 {
    211     dirSearchPath = Lst_Init (FALSE);
    212     openDirectories = Lst_Init (FALSE);
    213     Hash_InitTable(&mtimes, 0);
    214 
    215     /*
    216      * Since the Path structure is placed on both openDirectories and
    217      * the path we give Dir_AddDir (which in this case is openDirectories),
    218      * we need to remove "." from openDirectories and what better time to
    219      * do it than when we have to fetch the thing anyway?
    220      */
    221     Dir_AddDir (openDirectories, ".");
    222     dot = (Path *) Lst_DeQueue (openDirectories);
    223 
    224     /*
    225      * We always need to have dot around, so we increment its reference count
    226      * to make sure it's not destroyed.
    227      */
    228     dot->refCount += 1;
    229 }
    230 
    231 /*-
    232  *-----------------------------------------------------------------------
    233  * Dir_End --
    234  *	cleanup things for this module
    235  *
    236  * Results:
    237  *	none
    238  *
    239  * Side Effects:
    240  *	none
    241  *-----------------------------------------------------------------------
    242  */
    243 void
    244 Dir_End()
    245 {
    246     dot->refCount -= 1;
    247     Dir_Destroy((ClientData) dot);
    248     Dir_ClearPath(dirSearchPath);
    249     Lst_Destroy(dirSearchPath, NOFREE);
    250     Dir_ClearPath(openDirectories);
    251     Lst_Destroy(openDirectories, NOFREE);
    252     Hash_DeleteTable(&mtimes);
    253 }
    254 
    255 /*-
    256  *-----------------------------------------------------------------------
    257  * DirFindName --
    258  *	See if the Path structure describes the same directory as the
    259  *	given one by comparing their names. Called from Dir_AddDir via
    260  *	Lst_Find when searching the list of open directories.
    261  *
    262  * Results:
    263  *	0 if it is the same. Non-zero otherwise
    264  *
    265  * Side Effects:
    266  *	None
    267  *-----------------------------------------------------------------------
    268  */
    269 static int
    270 DirFindName (p, dname)
    271     ClientData    p;	      /* Current name */
    272     ClientData	  dname;      /* Desired name */
    273 {
    274     return (strcmp (((Path *)p)->name, (char *) dname));
    275 }
    276 
    277 /*-
    278  *-----------------------------------------------------------------------
    279  * Dir_HasWildcards  --
    280  *	see if the given name has any wildcard characters in it
    281  *
    282  * Results:
    283  *	returns TRUE if the word should be expanded, FALSE otherwise
    284  *
    285  * Side Effects:
    286  *	none
    287  *-----------------------------------------------------------------------
    288  */
    289 Boolean
    290 Dir_HasWildcards (name)
    291     char          *name;	/* name to check */
    292 {
    293     register char *cp;
    294 
    295     for (cp = name; *cp; cp++) {
    296 	switch(*cp) {
    297 	case '{':
    298 	case '[':
    299 	case '?':
    300 	case '*':
    301 	    return (TRUE);
    302 	}
    303     }
    304     return (FALSE);
    305 }
    306 
    307 /*-
    308  *-----------------------------------------------------------------------
    309  * DirMatchFiles --
    310  * 	Given a pattern and a Path structure, see if any files
    311  *	match the pattern and add their names to the 'expansions' list if
    312  *	any do. This is incomplete -- it doesn't take care of patterns like
    313  *	src / *src / *.c properly (just *.c on any of the directories), but it
    314  *	will do for now.
    315  *
    316  * Results:
    317  *	Always returns 0
    318  *
    319  * Side Effects:
    320  *	File names are added to the expansions lst. The directory will be
    321  *	fully hashed when this is done.
    322  *-----------------------------------------------------------------------
    323  */
    324 static int
    325 DirMatchFiles (pattern, p, expansions)
    326     char	  *pattern;   	/* Pattern to look for */
    327     Path	  *p;         	/* Directory to search */
    328     Lst	    	  expansions;	/* Place to store the results */
    329 {
    330     Hash_Search	  search;   	/* Index into the directory's table */
    331     Hash_Entry	  *entry;   	/* Current entry in the table */
    332     Boolean 	  isDot;    	/* TRUE if the directory being searched is . */
    333 
    334     isDot = (*p->name == '.' && p->name[1] == '\0');
    335 
    336     for (entry = Hash_EnumFirst(&p->files, &search);
    337 	 entry != (Hash_Entry *)NULL;
    338 	 entry = Hash_EnumNext(&search))
    339     {
    340 	/*
    341 	 * See if the file matches the given pattern. Note we follow the UNIX
    342 	 * convention that dot files will only be found if the pattern
    343 	 * begins with a dot (note also that as a side effect of the hashing
    344 	 * scheme, .* won't match . or .. since they aren't hashed).
    345 	 */
    346 	if (Str_Match(entry->name, pattern) &&
    347 	    ((entry->name[0] != '.') ||
    348 	     (pattern[0] == '.')))
    349 	{
    350 	    (void)Lst_AtEnd(expansions,
    351 			    (isDot ? strdup(entry->name) :
    352 			     str_concat(p->name, entry->name,
    353 					STR_ADDSLASH)));
    354 	}
    355     }
    356     return (0);
    357 }
    358 
    359 /*-
    360  *-----------------------------------------------------------------------
    361  * DirExpandCurly --
    362  *	Expand curly braces like the C shell. Does this recursively.
    363  *	Note the special case: if after the piece of the curly brace is
    364  *	done there are no wildcard characters in the result, the result is
    365  *	placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.
    366  *
    367  * Results:
    368  *	None.
    369  *
    370  * Side Effects:
    371  *	The given list is filled with the expansions...
    372  *
    373  *-----------------------------------------------------------------------
    374  */
    375 static void
    376 DirExpandCurly(word, brace, path, expansions)
    377     char    	  *word;    	/* Entire word to expand */
    378     char    	  *brace;   	/* First curly brace in it */
    379     Lst	    	  path;	    	/* Search path to use */
    380     Lst	    	  expansions;	/* Place to store the expansions */
    381 {
    382     char    	  *end;	    	/* Character after the closing brace */
    383     char    	  *cp;	    	/* Current position in brace clause */
    384     char    	  *start;   	/* Start of current piece of brace clause */
    385     int	    	  bracelevel;	/* Number of braces we've seen. If we see a
    386 				 * right brace when this is 0, we've hit the
    387 				 * end of the clause. */
    388     char    	  *file;    	/* Current expansion */
    389     int	    	  otherLen; 	/* The length of the other pieces of the
    390 				 * expansion (chars before and after the
    391 				 * clause in 'word') */
    392     char    	  *cp2;	    	/* Pointer for checking for wildcards in
    393 				 * expansion before calling Dir_Expand */
    394 
    395     start = brace+1;
    396 
    397     /*
    398      * Find the end of the brace clause first, being wary of nested brace
    399      * clauses.
    400      */
    401     for (end = start, bracelevel = 0; *end != '\0'; end++) {
    402 	if (*end == '{') {
    403 	    bracelevel++;
    404 	} else if ((*end == '}') && (bracelevel-- == 0)) {
    405 	    break;
    406 	}
    407     }
    408     if (*end == '\0') {
    409 	Error("Unterminated {} clause \"%s\"", start);
    410 	return;
    411     } else {
    412 	end++;
    413     }
    414     otherLen = brace - word + strlen(end);
    415 
    416     for (cp = start; cp < end; cp++) {
    417 	/*
    418 	 * Find the end of this piece of the clause.
    419 	 */
    420 	bracelevel = 0;
    421 	while (*cp != ',') {
    422 	    if (*cp == '{') {
    423 		bracelevel++;
    424 	    } else if ((*cp == '}') && (bracelevel-- <= 0)) {
    425 		break;
    426 	    }
    427 	    cp++;
    428 	}
    429 	/*
    430 	 * Allocate room for the combination and install the three pieces.
    431 	 */
    432 	file = emalloc(otherLen + cp - start + 1);
    433 	if (brace != word) {
    434 	    strncpy(file, word, brace-word);
    435 	}
    436 	if (cp != start) {
    437 	    strncpy(&file[brace-word], start, cp-start);
    438 	}
    439 	strcpy(&file[(brace-word)+(cp-start)], end);
    440 
    441 	/*
    442 	 * See if the result has any wildcards in it. If we find one, call
    443 	 * Dir_Expand right away, telling it to place the result on our list
    444 	 * of expansions.
    445 	 */
    446 	for (cp2 = file; *cp2 != '\0'; cp2++) {
    447 	    switch(*cp2) {
    448 	    case '*':
    449 	    case '?':
    450 	    case '{':
    451 	    case '[':
    452 		Dir_Expand(file, path, expansions);
    453 		goto next;
    454 	    }
    455 	}
    456 	if (*cp2 == '\0') {
    457 	    /*
    458 	     * Hit the end w/o finding any wildcards, so stick the expansion
    459 	     * on the end of the list.
    460 	     */
    461 	    (void)Lst_AtEnd(expansions, file);
    462 	} else {
    463 	next:
    464 	    free(file);
    465 	}
    466 	start = cp+1;
    467     }
    468 }
    469 
    470 
    471 /*-
    472  *-----------------------------------------------------------------------
    473  * DirExpandInt --
    474  *	Internal expand routine. Passes through the directories in the
    475  *	path one by one, calling DirMatchFiles for each. NOTE: This still
    476  *	doesn't handle patterns in directories...
    477  *
    478  * Results:
    479  *	None.
    480  *
    481  * Side Effects:
    482  *	Things are added to the expansions list.
    483  *
    484  *-----------------------------------------------------------------------
    485  */
    486 static void
    487 DirExpandInt(word, path, expansions)
    488     char    	  *word;    	/* Word to expand */
    489     Lst	    	  path;	    	/* Path on which to look */
    490     Lst	    	  expansions;	/* Place to store the result */
    491 {
    492     LstNode 	  ln;	    	/* Current node */
    493     Path	  *p;	    	/* Directory in the node */
    494 
    495     if (Lst_Open(path) == SUCCESS) {
    496 	while ((ln = Lst_Next(path)) != NILLNODE) {
    497 	    p = (Path *)Lst_Datum(ln);
    498 	    DirMatchFiles(word, p, expansions);
    499 	}
    500 	Lst_Close(path);
    501     }
    502 }
    503 
    504 /*-
    505  *-----------------------------------------------------------------------
    506  * DirPrintWord --
    507  *	Print a word in the list of expansions. Callback for Dir_Expand
    508  *	when DEBUG(DIR), via Lst_ForEach.
    509  *
    510  * Results:
    511  *	=== 0
    512  *
    513  * Side Effects:
    514  *	The passed word is printed, followed by a space.
    515  *
    516  *-----------------------------------------------------------------------
    517  */
    518 static int
    519 DirPrintWord(word, dummy)
    520     ClientData  word;
    521     ClientData  dummy;
    522 {
    523     printf("%s ", (char *) word);
    524 
    525     return(dummy ? 0 : 0);
    526 }
    527 
    528 /*-
    529  *-----------------------------------------------------------------------
    530  * Dir_Expand  --
    531  *	Expand the given word into a list of words by globbing it looking
    532  *	in the directories on the given search path.
    533  *
    534  * Results:
    535  *	A list of words consisting of the files which exist along the search
    536  *	path matching the given pattern.
    537  *
    538  * Side Effects:
    539  *	Directories may be opened. Who knows?
    540  *-----------------------------------------------------------------------
    541  */
    542 void
    543 Dir_Expand (word, path, expansions)
    544     char    *word;      /* the word to expand */
    545     Lst     path;   	/* the list of directories in which to find
    546 			 * the resulting files */
    547     Lst	    expansions;	/* the list on which to place the results */
    548 {
    549     char    	  *cp;
    550 
    551     if (DEBUG(DIR)) {
    552 	printf("expanding \"%s\"...", word);
    553     }
    554 
    555     cp = strchr(word, '{');
    556     if (cp) {
    557 	DirExpandCurly(word, cp, path, expansions);
    558     } else {
    559 	cp = strchr(word, '/');
    560 	if (cp) {
    561 	    /*
    562 	     * The thing has a directory component -- find the first wildcard
    563 	     * in the string.
    564 	     */
    565 	    for (cp = word; *cp; cp++) {
    566 		if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') {
    567 		    break;
    568 		}
    569 	    }
    570 	    if (*cp == '{') {
    571 		/*
    572 		 * This one will be fun.
    573 		 */
    574 		DirExpandCurly(word, cp, path, expansions);
    575 		return;
    576 	    } else if (*cp != '\0') {
    577 		/*
    578 		 * Back up to the start of the component
    579 		 */
    580 		char  *dirpath;
    581 
    582 		while (cp > word && *cp != '/') {
    583 		    cp--;
    584 		}
    585 		if (cp != word) {
    586 		    char sc;
    587 		    /*
    588 		     * If the glob isn't in the first component, try and find
    589 		     * all the components up to the one with a wildcard.
    590 		     */
    591 		    sc = cp[1];
    592 		    cp[1] = '\0';
    593 		    dirpath = Dir_FindFile(word, path);
    594 		    cp[1] = sc;
    595 		    /*
    596 		     * dirpath is null if can't find the leading component
    597 		     * XXX: Dir_FindFile won't find internal components.
    598 		     * i.e. if the path contains ../Etc/Object and we're
    599 		     * looking for Etc, it won't be found. Ah well.
    600 		     * Probably not important.
    601 		     */
    602 		    if (dirpath != (char *)NULL) {
    603 			char *dp = &dirpath[strlen(dirpath) - 1];
    604 			if (*dp == '/')
    605 			    *dp = '\0';
    606 			path = Lst_Init(FALSE);
    607 			Dir_AddDir(path, dirpath);
    608 			DirExpandInt(cp+1, path, expansions);
    609 			Lst_Destroy(path, NOFREE);
    610 		    }
    611 		} else {
    612 		    /*
    613 		     * Start the search from the local directory
    614 		     */
    615 		    DirExpandInt(word, path, expansions);
    616 		}
    617 	    } else {
    618 		/*
    619 		 * Return the file -- this should never happen.
    620 		 */
    621 		DirExpandInt(word, path, expansions);
    622 	    }
    623 	} else {
    624 	    /*
    625 	     * First the files in dot
    626 	     */
    627 	    DirMatchFiles(word, dot, expansions);
    628 
    629 	    /*
    630 	     * Then the files in every other directory on the path.
    631 	     */
    632 	    DirExpandInt(word, path, expansions);
    633 	}
    634     }
    635     if (DEBUG(DIR)) {
    636 	Lst_ForEach(expansions, DirPrintWord, (ClientData) 0);
    637 	fputc('\n', stdout);
    638     }
    639 }
    640 
    641 /*-
    642  *-----------------------------------------------------------------------
    643  * Dir_FindFile  --
    644  *	Find the file with the given name along the given search path.
    645  *
    646  * Results:
    647  *	The path to the file or NULL. This path is guaranteed to be in a
    648  *	different part of memory than name and so may be safely free'd.
    649  *
    650  * Side Effects:
    651  *	If the file is found in a directory which is not on the path
    652  *	already (either 'name' is absolute or it is a relative path
    653  *	[ dir1/.../dirn/file ] which exists below one of the directories
    654  *	already on the search path), its directory is added to the end
    655  *	of the path on the assumption that there will be more files in
    656  *	that directory later on. Sometimes this is true. Sometimes not.
    657  *-----------------------------------------------------------------------
    658  */
    659 char *
    660 Dir_FindFile (name, path)
    661     char    	  *name;    /* the file to find */
    662     Lst           path;	    /* the Lst of directories to search */
    663 {
    664     register char *p1;	    /* pointer into p->name */
    665     register char *p2;	    /* pointer into name */
    666     LstNode       ln;	    /* a list element */
    667     register char *file;    /* the current filename to check */
    668     register Path *p;	    /* current path member */
    669     register char *cp;	    /* index of first slash, if any */
    670     Boolean	  hasSlash; /* true if 'name' contains a / */
    671     struct stat	  stb;	    /* Buffer for stat, if necessary */
    672     Hash_Entry	  *entry;   /* Entry for mtimes table */
    673 
    674     /*
    675      * Find the final component of the name and note whether it has a
    676      * slash in it (the name, I mean)
    677      */
    678     cp = strrchr (name, '/');
    679     if (cp) {
    680 	hasSlash = TRUE;
    681 	cp += 1;
    682     } else {
    683 	hasSlash = FALSE;
    684 	cp = name;
    685     }
    686 
    687     if (DEBUG(DIR)) {
    688 	printf("Searching for %s...", name);
    689     }
    690     /*
    691      * No matter what, we always look for the file in the current directory
    692      * before anywhere else and we *do not* add the ./ to it if it exists.
    693      * This is so there are no conflicts between what the user specifies
    694      * (fish.c) and what pmake finds (./fish.c).
    695      */
    696     if ((!hasSlash || (cp - name == 2 && *name == '.')) &&
    697 	(Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL)) {
    698 	    if (DEBUG(DIR)) {
    699 		printf("in '.'\n");
    700 	    }
    701 	    hits += 1;
    702 	    dot->hits += 1;
    703 	    return (strdup (name));
    704     }
    705 
    706     if (Lst_Open (path) == FAILURE) {
    707 	if (DEBUG(DIR)) {
    708 	    printf("couldn't open path, file not found\n");
    709 	}
    710 	misses += 1;
    711 	return ((char *) NULL);
    712     }
    713 
    714     /*
    715      * We look through all the directories on the path seeking one which
    716      * contains the final component of the given name and whose final
    717      * component(s) match the name's initial component(s). If such a beast
    718      * is found, we concatenate the directory name and the final component
    719      * and return the resulting string. If we don't find any such thing,
    720      * we go on to phase two...
    721      */
    722     while ((ln = Lst_Next (path)) != NILLNODE) {
    723 	p = (Path *) Lst_Datum (ln);
    724 	if (DEBUG(DIR)) {
    725 	    printf("%s...", p->name);
    726 	}
    727 	if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
    728 	    if (DEBUG(DIR)) {
    729 		printf("here...");
    730 	    }
    731 	    if (hasSlash) {
    732 		/*
    733 		 * If the name had a slash, its initial components and p's
    734 		 * final components must match. This is false if a mismatch
    735 		 * is encountered before all of the initial components
    736 		 * have been checked (p2 > name at the end of the loop), or
    737 		 * we matched only part of one of the components of p
    738 		 * along with all the rest of them (*p1 != '/').
    739 		 */
    740 		p1 = p->name + strlen (p->name) - 1;
    741 		p2 = cp - 2;
    742 		while (p2 >= name && p1 >= p->name && *p1 == *p2) {
    743 		    p1 -= 1; p2 -= 1;
    744 		}
    745 		if (p2 >= name || (p1 >= p->name && *p1 != '/')) {
    746 		    if (DEBUG(DIR)) {
    747 			printf("component mismatch -- continuing...");
    748 		    }
    749 		    continue;
    750 		}
    751 	    }
    752 	    file = str_concat (p->name, cp, STR_ADDSLASH);
    753 	    if (DEBUG(DIR)) {
    754 		printf("returning %s\n", file);
    755 	    }
    756 	    Lst_Close (path);
    757 	    p->hits += 1;
    758 	    hits += 1;
    759 	    return (file);
    760 	} else if (hasSlash) {
    761 	    /*
    762 	     * If the file has a leading path component and that component
    763 	     * exactly matches the entire name of the current search
    764 	     * directory, we assume the file doesn't exist and return NULL.
    765 	     */
    766 	    for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) {
    767 		continue;
    768 	    }
    769 	    if (*p1 == '\0' && p2 == cp - 1) {
    770 		if (DEBUG(DIR)) {
    771 		    printf("must be here but isn't -- returing NULL\n");
    772 		}
    773 		Lst_Close (path);
    774 		return ((char *) NULL);
    775 	    }
    776 	}
    777     }
    778 
    779     /*
    780      * We didn't find the file on any existing members of the directory.
    781      * If the name doesn't contain a slash, that means it doesn't exist.
    782      * If it *does* contain a slash, however, there is still hope: it
    783      * could be in a subdirectory of one of the members of the search
    784      * path. (eg. /usr/include and sys/types.h. The above search would
    785      * fail to turn up types.h in /usr/include, but it *is* in
    786      * /usr/include/sys/types.h) If we find such a beast, we assume there
    787      * will be more (what else can we assume?) and add all but the last
    788      * component of the resulting name onto the search path (at the
    789      * end). This phase is only performed if the file is *not* absolute.
    790      */
    791     if (!hasSlash) {
    792 	if (DEBUG(DIR)) {
    793 	    printf("failed.\n");
    794 	}
    795 	misses += 1;
    796 	return ((char *) NULL);
    797     }
    798 
    799     if (*name != '/') {
    800 	Boolean	checkedDot = FALSE;
    801 
    802 	if (DEBUG(DIR)) {
    803 	    printf("failed. Trying subdirectories...");
    804 	}
    805 	(void) Lst_Open (path);
    806 	while ((ln = Lst_Next (path)) != NILLNODE) {
    807 	    p = (Path *) Lst_Datum (ln);
    808 	    if (p != dot) {
    809 		file = str_concat (p->name, name, STR_ADDSLASH);
    810 	    } else {
    811 		/*
    812 		 * Checking in dot -- DON'T put a leading ./ on the thing.
    813 		 */
    814 		file = strdup(name);
    815 		checkedDot = TRUE;
    816 	    }
    817 	    if (DEBUG(DIR)) {
    818 		printf("checking %s...", file);
    819 	    }
    820 
    821 
    822 	    if (stat (file, &stb) == 0) {
    823 		if (DEBUG(DIR)) {
    824 		    printf("got it.\n");
    825 		}
    826 
    827 		Lst_Close (path);
    828 
    829 		/*
    830 		 * We've found another directory to search. We know there's
    831 		 * a slash in 'file' because we put one there. We nuke it after
    832 		 * finding it and call Dir_AddDir to add this new directory
    833 		 * onto the existing search path. Once that's done, we restore
    834 		 * the slash and triumphantly return the file name, knowing
    835 		 * that should a file in this directory every be referenced
    836 		 * again in such a manner, we will find it without having to do
    837 		 * numerous numbers of access calls. Hurrah!
    838 		 */
    839 		cp = strrchr (file, '/');
    840 		*cp = '\0';
    841 		Dir_AddDir (path, file);
    842 		*cp = '/';
    843 
    844 		/*
    845 		 * Save the modification time so if it's needed, we don't have
    846 		 * to fetch it again.
    847 		 */
    848 		if (DEBUG(DIR)) {
    849 		    printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
    850 			    file);
    851 		}
    852 		entry = Hash_CreateEntry(&mtimes, (char *) file,
    853 					 (Boolean *)NULL);
    854 		Hash_SetValue(entry, (long)stb.st_mtime);
    855 		nearmisses += 1;
    856 		return (file);
    857 	    } else {
    858 		free (file);
    859 	    }
    860 	}
    861 
    862 	if (DEBUG(DIR)) {
    863 	    printf("failed. ");
    864 	}
    865 	Lst_Close (path);
    866 
    867 	if (checkedDot) {
    868 	    /*
    869 	     * Already checked by the given name, since . was in the path,
    870 	     * so no point in proceeding...
    871 	     */
    872 	    if (DEBUG(DIR)) {
    873 		printf("Checked . already, returning NULL\n");
    874 	    }
    875 	    return(NULL);
    876 	}
    877     }
    878 
    879     /*
    880      * Didn't find it that way, either. Sigh. Phase 3. Add its directory
    881      * onto the search path in any case, just in case, then look for the
    882      * thing in the hash table. If we find it, grand. We return a new
    883      * copy of the name. Otherwise we sadly return a NULL pointer. Sigh.
    884      * Note that if the directory holding the file doesn't exist, this will
    885      * do an extra search of the final directory on the path. Unless something
    886      * weird happens, this search won't succeed and life will be groovy.
    887      *
    888      * Sigh. We cannot add the directory onto the search path because
    889      * of this amusing case:
    890      * $(INSTALLDIR)/$(FILE): $(FILE)
    891      *
    892      * $(FILE) exists in $(INSTALLDIR) but not in the current one.
    893      * When searching for $(FILE), we will find it in $(INSTALLDIR)
    894      * b/c we added it here. This is not good...
    895      */
    896 #ifdef notdef
    897     cp[-1] = '\0';
    898     Dir_AddDir (path, name);
    899     cp[-1] = '/';
    900 
    901     bigmisses += 1;
    902     ln = Lst_Last (path);
    903     if (ln == NILLNODE) {
    904 	return ((char *) NULL);
    905     } else {
    906 	p = (Path *) Lst_Datum (ln);
    907     }
    908 
    909     if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
    910 	return (strdup (name));
    911     } else {
    912 	return ((char *) NULL);
    913     }
    914 #else /* !notdef */
    915     if (DEBUG(DIR)) {
    916 	printf("Looking for \"%s\"...", name);
    917     }
    918 
    919     bigmisses += 1;
    920     entry = Hash_FindEntry(&mtimes, name);
    921     if (entry != (Hash_Entry *)NULL) {
    922 	if (DEBUG(DIR)) {
    923 	    printf("got it (in mtime cache)\n");
    924 	}
    925 	return(strdup(name));
    926     } else if (stat (name, &stb) == 0) {
    927 	entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL);
    928 	if (DEBUG(DIR)) {
    929 	    printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
    930 		    name);
    931 	}
    932 	Hash_SetValue(entry, (long)stb.st_mtime);
    933 	return (strdup (name));
    934     } else {
    935 	if (DEBUG(DIR)) {
    936 	    printf("failed. Returning NULL\n");
    937 	}
    938 	return ((char *)NULL);
    939     }
    940 #endif /* notdef */
    941 }
    942 
    943 /*-
    944  *-----------------------------------------------------------------------
    945  * Dir_MTime  --
    946  *	Find the modification time of the file described by gn along the
    947  *	search path dirSearchPath.
    948  *
    949  * Results:
    950  *	The modification time or 0 if it doesn't exist
    951  *
    952  * Side Effects:
    953  *	The modification time is placed in the node's mtime slot.
    954  *	If the node didn't have a path entry before, and Dir_FindFile
    955  *	found one for it, the full name is placed in the path slot.
    956  *-----------------------------------------------------------------------
    957  */
    958 int
    959 Dir_MTime (gn)
    960     GNode         *gn;	      /* the file whose modification time is
    961 			       * desired */
    962 {
    963     char          *fullName;  /* the full pathname of name */
    964     struct stat	  stb;	      /* buffer for finding the mod time */
    965     Hash_Entry	  *entry;
    966 
    967     if (gn->type & OP_ARCHV) {
    968 	return Arch_MTime (gn);
    969     } else if (gn->path == (char *)NULL) {
    970 	fullName = Dir_FindFile (gn->name, dirSearchPath);
    971     } else {
    972 	fullName = gn->path;
    973     }
    974 
    975     if (fullName == (char *)NULL) {
    976 	fullName = strdup(gn->name);
    977     }
    978 
    979     entry = Hash_FindEntry(&mtimes, fullName);
    980     if (entry != (Hash_Entry *)NULL) {
    981 	/*
    982 	 * Only do this once -- the second time folks are checking to
    983 	 * see if the file was actually updated, so we need to actually go
    984 	 * to the file system.
    985 	 */
    986 	if (DEBUG(DIR)) {
    987 	    printf("Using cached time %s for %s\n",
    988 		    Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName);
    989 	}
    990 	stb.st_mtime = (time_t)(long)Hash_GetValue(entry);
    991 	Hash_DeleteEntry(&mtimes, entry);
    992     } else if (stat (fullName, &stb) < 0) {
    993 	if (gn->type & OP_MEMBER) {
    994 	    if (fullName != gn->path)
    995 		free(fullName);
    996 	    return Arch_MemMTime (gn);
    997 	} else {
    998 	    stb.st_mtime = 0;
    999 	}
   1000     }
   1001     if (fullName && gn->path == (char *)NULL) {
   1002 	gn->path = fullName;
   1003     }
   1004 
   1005     gn->mtime = stb.st_mtime;
   1006     return (gn->mtime);
   1007 }
   1008 
   1009 /*-
   1010  *-----------------------------------------------------------------------
   1011  * Dir_AddDir --
   1012  *	Add the given name to the end of the given path. The order of
   1013  *	the arguments is backwards so ParseDoDependency can do a
   1014  *	Lst_ForEach of its list of paths...
   1015  *
   1016  * Results:
   1017  *	none
   1018  *
   1019  * Side Effects:
   1020  *	A structure is added to the list and the directory is
   1021  *	read and hashed.
   1022  *-----------------------------------------------------------------------
   1023  */
   1024 void
   1025 Dir_AddDir (path, name)
   1026     Lst           path;	      /* the path to which the directory should be
   1027 			       * added */
   1028     char          *name;      /* the name of the directory to add */
   1029 {
   1030     LstNode       ln;	      /* node in case Path structure is found */
   1031     register Path *p;	      /* pointer to new Path structure */
   1032     DIR     	  *d;	      /* for reading directory */
   1033     register struct dirent *dp; /* entry in directory */
   1034 
   1035     ln = Lst_Find (openDirectories, (ClientData)name, DirFindName);
   1036     if (ln != NILLNODE) {
   1037 	p = (Path *)Lst_Datum (ln);
   1038 	if (Lst_Member(path, (ClientData)p) == NILLNODE) {
   1039 	    p->refCount += 1;
   1040 	    (void)Lst_AtEnd (path, (ClientData)p);
   1041 	}
   1042     } else {
   1043 	if (DEBUG(DIR)) {
   1044 	    printf("Caching %s...", name);
   1045 	    fflush(stdout);
   1046 	}
   1047 
   1048 	if ((d = opendir (name)) != (DIR *) NULL) {
   1049 	    p = (Path *) emalloc (sizeof (Path));
   1050 	    p->name = strdup (name);
   1051 	    p->hits = 0;
   1052 	    p->refCount = 1;
   1053 	    Hash_InitTable (&p->files, -1);
   1054 
   1055 	    /*
   1056 	     * Skip the first two entries -- these will *always* be . and ..
   1057 	     */
   1058 	    (void)readdir(d);
   1059 	    (void)readdir(d);
   1060 
   1061 	    while ((dp = readdir (d)) != (struct dirent *) NULL) {
   1062 #ifdef sun
   1063 		/*
   1064 		 * The sun directory library doesn't check for a 0 inode
   1065 		 * (0-inode slots just take up space), so we have to do
   1066 		 * it ourselves.
   1067 		 */
   1068 		if (dp->d_fileno == 0) {
   1069 		    continue;
   1070 		}
   1071 #endif /* sun */
   1072 		(void)Hash_CreateEntry(&p->files, dp->d_name, (Boolean *)NULL);
   1073 	    }
   1074 	    (void) closedir (d);
   1075 	    (void)Lst_AtEnd (openDirectories, (ClientData)p);
   1076 	    (void)Lst_AtEnd (path, (ClientData)p);
   1077 	}
   1078 	if (DEBUG(DIR)) {
   1079 	    printf("done\n");
   1080 	}
   1081     }
   1082 }
   1083 
   1084 /*-
   1085  *-----------------------------------------------------------------------
   1086  * Dir_CopyDir --
   1087  *	Callback function for duplicating a search path via Lst_Duplicate.
   1088  *	Ups the reference count for the directory.
   1089  *
   1090  * Results:
   1091  *	Returns the Path it was given.
   1092  *
   1093  * Side Effects:
   1094  *	The refCount of the path is incremented.
   1095  *
   1096  *-----------------------------------------------------------------------
   1097  */
   1098 ClientData
   1099 Dir_CopyDir(p)
   1100     ClientData p;
   1101 {
   1102     ((Path *) p)->refCount += 1;
   1103 
   1104     return ((ClientData)p);
   1105 }
   1106 
   1107 /*-
   1108  *-----------------------------------------------------------------------
   1109  * Dir_MakeFlags --
   1110  *	Make a string by taking all the directories in the given search
   1111  *	path and preceding them by the given flag. Used by the suffix
   1112  *	module to create variables for compilers based on suffix search
   1113  *	paths.
   1114  *
   1115  * Results:
   1116  *	The string mentioned above. Note that there is no space between
   1117  *	the given flag and each directory. The empty string is returned if
   1118  *	Things don't go well.
   1119  *
   1120  * Side Effects:
   1121  *	None
   1122  *-----------------------------------------------------------------------
   1123  */
   1124 char *
   1125 Dir_MakeFlags (flag, path)
   1126     char	  *flag;  /* flag which should precede each directory */
   1127     Lst	    	  path;	  /* list of directories */
   1128 {
   1129     char	  *str;	  /* the string which will be returned */
   1130     char	  *tstr;  /* the current directory preceded by 'flag' */
   1131     LstNode	  ln;	  /* the node of the current directory */
   1132     Path	  *p;	  /* the structure describing the current directory */
   1133 
   1134     str = strdup ("");
   1135 
   1136     if (Lst_Open (path) == SUCCESS) {
   1137 	while ((ln = Lst_Next (path)) != NILLNODE) {
   1138 	    p = (Path *) Lst_Datum (ln);
   1139 	    tstr = str_concat (flag, p->name, 0);
   1140 	    str = str_concat (str, tstr, STR_ADDSPACE | STR_DOFREE);
   1141 	}
   1142 	Lst_Close (path);
   1143     }
   1144 
   1145     return (str);
   1146 }
   1147 
   1148 /*-
   1149  *-----------------------------------------------------------------------
   1150  * Dir_Destroy --
   1151  *	Nuke a directory descriptor, if possible. Callback procedure
   1152  *	for the suffixes module when destroying a search path.
   1153  *
   1154  * Results:
   1155  *	None.
   1156  *
   1157  * Side Effects:
   1158  *	If no other path references this directory (refCount == 0),
   1159  *	the Path and all its data are freed.
   1160  *
   1161  *-----------------------------------------------------------------------
   1162  */
   1163 void
   1164 Dir_Destroy (pp)
   1165     ClientData 	  pp;	    /* The directory descriptor to nuke */
   1166 {
   1167     Path    	  *p = (Path *) pp;
   1168     p->refCount -= 1;
   1169 
   1170     if (p->refCount == 0) {
   1171 	LstNode	ln;
   1172 
   1173 	ln = Lst_Member (openDirectories, (ClientData)p);
   1174 	(void) Lst_Remove (openDirectories, ln);
   1175 
   1176 	Hash_DeleteTable (&p->files);
   1177 	free((Address)p->name);
   1178 	free((Address)p);
   1179     }
   1180 }
   1181 
   1182 /*-
   1183  *-----------------------------------------------------------------------
   1184  * Dir_ClearPath --
   1185  *	Clear out all elements of the given search path. This is different
   1186  *	from destroying the list, notice.
   1187  *
   1188  * Results:
   1189  *	None.
   1190  *
   1191  * Side Effects:
   1192  *	The path is set to the empty list.
   1193  *
   1194  *-----------------------------------------------------------------------
   1195  */
   1196 void
   1197 Dir_ClearPath(path)
   1198     Lst	    path; 	/* Path to clear */
   1199 {
   1200     Path    *p;
   1201     while (!Lst_IsEmpty(path)) {
   1202 	p = (Path *)Lst_DeQueue(path);
   1203 	Dir_Destroy((ClientData) p);
   1204     }
   1205 }
   1206 
   1207 
   1208 /*-
   1209  *-----------------------------------------------------------------------
   1210  * Dir_Concat --
   1211  *	Concatenate two paths, adding the second to the end of the first.
   1212  *	Makes sure to avoid duplicates.
   1213  *
   1214  * Results:
   1215  *	None
   1216  *
   1217  * Side Effects:
   1218  *	Reference counts for added dirs are upped.
   1219  *
   1220  *-----------------------------------------------------------------------
   1221  */
   1222 void
   1223 Dir_Concat(path1, path2)
   1224     Lst	    path1;  	/* Dest */
   1225     Lst	    path2;  	/* Source */
   1226 {
   1227     LstNode ln;
   1228     Path    *p;
   1229 
   1230     for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) {
   1231 	p = (Path *)Lst_Datum(ln);
   1232 	if (Lst_Member(path1, (ClientData)p) == NILLNODE) {
   1233 	    p->refCount += 1;
   1234 	    (void)Lst_AtEnd(path1, (ClientData)p);
   1235 	}
   1236     }
   1237 }
   1238 
   1239 /********** DEBUG INFO **********/
   1240 void
   1241 Dir_PrintDirectories()
   1242 {
   1243     LstNode	ln;
   1244     Path	*p;
   1245 
   1246     printf ("#*** Directory Cache:\n");
   1247     printf ("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
   1248 	      hits, misses, nearmisses, bigmisses,
   1249 	      (hits+bigmisses+nearmisses ?
   1250 	       hits * 100 / (hits + bigmisses + nearmisses) : 0));
   1251     printf ("# %-20s referenced\thits\n", "directory");
   1252     if (Lst_Open (openDirectories) == SUCCESS) {
   1253 	while ((ln = Lst_Next (openDirectories)) != NILLNODE) {
   1254 	    p = (Path *) Lst_Datum (ln);
   1255 	    printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
   1256 	}
   1257 	Lst_Close (openDirectories);
   1258     }
   1259 }
   1260 
   1261 static int DirPrintDir (p, dummy)
   1262     ClientData	p;
   1263     ClientData	dummy;
   1264 {
   1265     printf ("%s ", ((Path *) p)->name);
   1266     return (dummy ? 0 : 0);
   1267 }
   1268 
   1269 void
   1270 Dir_PrintPath (path)
   1271     Lst	path;
   1272 {
   1273     Lst_ForEach (path, DirPrintDir, (ClientData)0);
   1274 }
   1275