1*84d9c625SLionel Sambuc /* $NetBSD: dir.c,v 1.67 2013/03/05 22:01:43 christos Exp $ */
22e2caf59SThomas Veerman
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
52e2caf59SThomas Veerman * All rights reserved.
62e2caf59SThomas Veerman *
72e2caf59SThomas Veerman * This code is derived from software contributed to Berkeley by
82e2caf59SThomas Veerman * Adam de Boor.
92e2caf59SThomas Veerman *
102e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without
112e2caf59SThomas Veerman * modification, are permitted provided that the following conditions
122e2caf59SThomas Veerman * are met:
132e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright
142e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer.
152e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
162e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the
172e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution.
182e2caf59SThomas Veerman * 3. Neither the name of the University nor the names of its contributors
192e2caf59SThomas Veerman * may be used to endorse or promote products derived from this software
202e2caf59SThomas Veerman * without specific prior written permission.
212e2caf59SThomas Veerman *
222e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232e2caf59SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242e2caf59SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252e2caf59SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262e2caf59SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272e2caf59SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282e2caf59SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292e2caf59SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302e2caf59SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312e2caf59SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322e2caf59SThomas Veerman * SUCH DAMAGE.
332e2caf59SThomas Veerman */
342e2caf59SThomas Veerman
352e2caf59SThomas Veerman /*
362e2caf59SThomas Veerman * Copyright (c) 1988, 1989 by Adam de Boor
372e2caf59SThomas Veerman * Copyright (c) 1989 by Berkeley Softworks
382e2caf59SThomas Veerman * All rights reserved.
392e2caf59SThomas Veerman *
402e2caf59SThomas Veerman * This code is derived from software contributed to Berkeley by
412e2caf59SThomas Veerman * Adam de Boor.
422e2caf59SThomas Veerman *
432e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without
442e2caf59SThomas Veerman * modification, are permitted provided that the following conditions
452e2caf59SThomas Veerman * are met:
462e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright
472e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer.
482e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
492e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the
502e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution.
512e2caf59SThomas Veerman * 3. All advertising materials mentioning features or use of this software
522e2caf59SThomas Veerman * must display the following acknowledgement:
532e2caf59SThomas Veerman * This product includes software developed by the University of
542e2caf59SThomas Veerman * California, Berkeley and its contributors.
552e2caf59SThomas Veerman * 4. Neither the name of the University nor the names of its contributors
562e2caf59SThomas Veerman * may be used to endorse or promote products derived from this software
572e2caf59SThomas Veerman * without specific prior written permission.
582e2caf59SThomas Veerman *
592e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
602e2caf59SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
612e2caf59SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
622e2caf59SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
632e2caf59SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
642e2caf59SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
652e2caf59SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
662e2caf59SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
672e2caf59SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
682e2caf59SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
692e2caf59SThomas Veerman * SUCH DAMAGE.
702e2caf59SThomas Veerman */
712e2caf59SThomas Veerman
722e2caf59SThomas Veerman #ifndef MAKE_NATIVE
73*84d9c625SLionel Sambuc static char rcsid[] = "$NetBSD: dir.c,v 1.67 2013/03/05 22:01:43 christos Exp $";
742e2caf59SThomas Veerman #else
752e2caf59SThomas Veerman #include <sys/cdefs.h>
762e2caf59SThomas Veerman #ifndef lint
772e2caf59SThomas Veerman #if 0
782e2caf59SThomas Veerman static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 1/2/94";
792e2caf59SThomas Veerman #else
80*84d9c625SLionel Sambuc __RCSID("$NetBSD: dir.c,v 1.67 2013/03/05 22:01:43 christos Exp $");
812e2caf59SThomas Veerman #endif
822e2caf59SThomas Veerman #endif /* not lint */
832e2caf59SThomas Veerman #endif
842e2caf59SThomas Veerman
852e2caf59SThomas Veerman /*-
862e2caf59SThomas Veerman * dir.c --
872e2caf59SThomas Veerman * Directory searching using wildcards and/or normal names...
882e2caf59SThomas Veerman * Used both for source wildcarding in the Makefile and for finding
892e2caf59SThomas Veerman * implicit sources.
902e2caf59SThomas Veerman *
912e2caf59SThomas Veerman * The interface for this module is:
922e2caf59SThomas Veerman * Dir_Init Initialize the module.
932e2caf59SThomas Veerman *
942e2caf59SThomas Veerman * Dir_InitCur Set the cur Path.
952e2caf59SThomas Veerman *
962e2caf59SThomas Veerman * Dir_InitDot Set the dot Path.
972e2caf59SThomas Veerman *
982e2caf59SThomas Veerman * Dir_End Cleanup the module.
992e2caf59SThomas Veerman *
1002e2caf59SThomas Veerman * Dir_SetPATH Set ${.PATH} to reflect state of dirSearchPath.
1012e2caf59SThomas Veerman *
1022e2caf59SThomas Veerman * Dir_HasWildcards Returns TRUE if the name given it needs to
1032e2caf59SThomas Veerman * be wildcard-expanded.
1042e2caf59SThomas Veerman *
1052e2caf59SThomas Veerman * Dir_Expand Given a pattern and a path, return a Lst of names
1062e2caf59SThomas Veerman * which match the pattern on the search path.
1072e2caf59SThomas Veerman *
1082e2caf59SThomas Veerman * Dir_FindFile Searches for a file on a given search path.
1092e2caf59SThomas Veerman * If it exists, the entire path is returned.
1102e2caf59SThomas Veerman * Otherwise NULL is returned.
1112e2caf59SThomas Veerman *
1122e2caf59SThomas Veerman * Dir_FindHereOrAbove Search for a path in the current directory and
1132e2caf59SThomas Veerman * then all the directories above it in turn until
1142e2caf59SThomas Veerman * the path is found or we reach the root ("/").
1152e2caf59SThomas Veerman *
1162e2caf59SThomas Veerman * Dir_MTime Return the modification time of a node. The file
1172e2caf59SThomas Veerman * is searched for along the default search path.
1182e2caf59SThomas Veerman * The path and mtime fields of the node are filled
1192e2caf59SThomas Veerman * in.
1202e2caf59SThomas Veerman *
1212e2caf59SThomas Veerman * Dir_AddDir Add a directory to a search path.
1222e2caf59SThomas Veerman *
1232e2caf59SThomas Veerman * Dir_MakeFlags Given a search path and a command flag, create
1242e2caf59SThomas Veerman * a string with each of the directories in the path
1252e2caf59SThomas Veerman * preceded by the command flag and all of them
1262e2caf59SThomas Veerman * separated by a space.
1272e2caf59SThomas Veerman *
1282e2caf59SThomas Veerman * Dir_Destroy Destroy an element of a search path. Frees up all
1292e2caf59SThomas Veerman * things that can be freed for the element as long
1302e2caf59SThomas Veerman * as the element is no longer referenced by any other
1312e2caf59SThomas Veerman * search path.
1322e2caf59SThomas Veerman * Dir_ClearPath Resets a search path to the empty list.
1332e2caf59SThomas Veerman *
1342e2caf59SThomas Veerman * For debugging:
1352e2caf59SThomas Veerman * Dir_PrintDirectories Print stats about the directory cache.
1362e2caf59SThomas Veerman */
1372e2caf59SThomas Veerman
1382e2caf59SThomas Veerman #include <sys/types.h>
1392e2caf59SThomas Veerman #include <sys/stat.h>
1402e2caf59SThomas Veerman
1412e2caf59SThomas Veerman #include <dirent.h>
1422e2caf59SThomas Veerman #include <errno.h>
1432e2caf59SThomas Veerman #include <stdio.h>
1442e2caf59SThomas Veerman
1452e2caf59SThomas Veerman #include "make.h"
1462e2caf59SThomas Veerman #include "hash.h"
1472e2caf59SThomas Veerman #include "dir.h"
148*84d9c625SLionel Sambuc #include "job.h"
1492e2caf59SThomas Veerman
1502e2caf59SThomas Veerman /*
1512e2caf59SThomas Veerman * A search path consists of a Lst of Path structures. A Path structure
1522e2caf59SThomas Veerman * has in it the name of the directory and a hash table of all the files
1532e2caf59SThomas Veerman * in the directory. This is used to cut down on the number of system
1542e2caf59SThomas Veerman * calls necessary to find implicit dependents and their like. Since
1552e2caf59SThomas Veerman * these searches are made before any actions are taken, we need not
1562e2caf59SThomas Veerman * worry about the directory changing due to creation commands. If this
1572e2caf59SThomas Veerman * hampers the style of some makefiles, they must be changed.
1582e2caf59SThomas Veerman *
1592e2caf59SThomas Veerman * A list of all previously-read directories is kept in the
1602e2caf59SThomas Veerman * openDirectories Lst. This list is checked first before a directory
1612e2caf59SThomas Veerman * is opened.
1622e2caf59SThomas Veerman *
1632e2caf59SThomas Veerman * The need for the caching of whole directories is brought about by
1642e2caf59SThomas Veerman * the multi-level transformation code in suff.c, which tends to search
1652e2caf59SThomas Veerman * for far more files than regular make does. In the initial
1662e2caf59SThomas Veerman * implementation, the amount of time spent performing "stat" calls was
1672e2caf59SThomas Veerman * truly astronomical. The problem with hashing at the start is,
1682e2caf59SThomas Veerman * of course, that pmake doesn't then detect changes to these directories
1692e2caf59SThomas Veerman * during the course of the make. Three possibilities suggest themselves:
1702e2caf59SThomas Veerman *
1712e2caf59SThomas Veerman * 1) just use stat to test for a file's existence. As mentioned
1722e2caf59SThomas Veerman * above, this is very inefficient due to the number of checks
1732e2caf59SThomas Veerman * engendered by the multi-level transformation code.
1742e2caf59SThomas Veerman * 2) use readdir() and company to search the directories, keeping
1752e2caf59SThomas Veerman * them open between checks. I have tried this and while it
1762e2caf59SThomas Veerman * didn't slow down the process too much, it could severely
1772e2caf59SThomas Veerman * affect the amount of parallelism available as each directory
1782e2caf59SThomas Veerman * open would take another file descriptor out of play for
1792e2caf59SThomas Veerman * handling I/O for another job. Given that it is only recently
1802e2caf59SThomas Veerman * that UNIX OS's have taken to allowing more than 20 or 32
1812e2caf59SThomas Veerman * file descriptors for a process, this doesn't seem acceptable
1822e2caf59SThomas Veerman * to me.
1832e2caf59SThomas Veerman * 3) record the mtime of the directory in the Path structure and
1842e2caf59SThomas Veerman * verify the directory hasn't changed since the contents were
1852e2caf59SThomas Veerman * hashed. This will catch the creation or deletion of files,
1862e2caf59SThomas Veerman * but not the updating of files. However, since it is the
1872e2caf59SThomas Veerman * creation and deletion that is the problem, this could be
1882e2caf59SThomas Veerman * a good thing to do. Unfortunately, if the directory (say ".")
1892e2caf59SThomas Veerman * were fairly large and changed fairly frequently, the constant
1902e2caf59SThomas Veerman * rehashing could seriously degrade performance. It might be
1912e2caf59SThomas Veerman * good in such cases to keep track of the number of rehashes
1922e2caf59SThomas Veerman * and if the number goes over a (small) limit, resort to using
1932e2caf59SThomas Veerman * stat in its place.
1942e2caf59SThomas Veerman *
1952e2caf59SThomas Veerman * An additional thing to consider is that pmake is used primarily
1962e2caf59SThomas Veerman * to create C programs and until recently pcc-based compilers refused
1972e2caf59SThomas Veerman * to allow you to specify where the resulting object file should be
1982e2caf59SThomas Veerman * placed. This forced all objects to be created in the current
1992e2caf59SThomas Veerman * directory. This isn't meant as a full excuse, just an explanation of
2002e2caf59SThomas Veerman * some of the reasons for the caching used here.
2012e2caf59SThomas Veerman *
2022e2caf59SThomas Veerman * One more note: the location of a target's file is only performed
2032e2caf59SThomas Veerman * on the downward traversal of the graph and then only for terminal
2042e2caf59SThomas Veerman * nodes in the graph. This could be construed as wrong in some cases,
2052e2caf59SThomas Veerman * but prevents inadvertent modification of files when the "installed"
2062e2caf59SThomas Veerman * directory for a file is provided in the search path.
2072e2caf59SThomas Veerman *
2082e2caf59SThomas Veerman * Another data structure maintained by this module is an mtime
2092e2caf59SThomas Veerman * cache used when the searching of cached directories fails to find
2102e2caf59SThomas Veerman * a file. In the past, Dir_FindFile would simply perform an access()
2112e2caf59SThomas Veerman * call in such a case to determine if the file could be found using
2122e2caf59SThomas Veerman * just the name given. When this hit, however, all that was gained
2132e2caf59SThomas Veerman * was the knowledge that the file existed. Given that an access() is
2142e2caf59SThomas Veerman * essentially a stat() without the copyout() call, and that the same
2152e2caf59SThomas Veerman * filesystem overhead would have to be incurred in Dir_MTime, it made
2162e2caf59SThomas Veerman * sense to replace the access() with a stat() and record the mtime
2172e2caf59SThomas Veerman * in a cache for when Dir_MTime was actually called.
2182e2caf59SThomas Veerman */
2192e2caf59SThomas Veerman
2202e2caf59SThomas Veerman Lst dirSearchPath; /* main search path */
2212e2caf59SThomas Veerman
2222e2caf59SThomas Veerman static Lst openDirectories; /* the list of all open directories */
2232e2caf59SThomas Veerman
2242e2caf59SThomas Veerman /*
2252e2caf59SThomas Veerman * Variables for gathering statistics on the efficiency of the hashing
2262e2caf59SThomas Veerman * mechanism.
2272e2caf59SThomas Veerman */
2282e2caf59SThomas Veerman static int hits, /* Found in directory cache */
2292e2caf59SThomas Veerman misses, /* Sad, but not evil misses */
2302e2caf59SThomas Veerman nearmisses, /* Found under search path */
2312e2caf59SThomas Veerman bigmisses; /* Sought by itself */
2322e2caf59SThomas Veerman
2332e2caf59SThomas Veerman static Path *dot; /* contents of current directory */
2342e2caf59SThomas Veerman static Path *cur; /* contents of current directory, if not dot */
2352e2caf59SThomas Veerman static Path *dotLast; /* a fake path entry indicating we need to
2362e2caf59SThomas Veerman * look for . last */
2372e2caf59SThomas Veerman static Hash_Table mtimes; /* Results of doing a last-resort stat in
2382e2caf59SThomas Veerman * Dir_FindFile -- if we have to go to the
2392e2caf59SThomas Veerman * system to find the file, we might as well
2402e2caf59SThomas Veerman * have its mtime on record. XXX: If this is done
2412e2caf59SThomas Veerman * way early, there's a chance other rules will
2422e2caf59SThomas Veerman * have already updated the file, in which case
2432e2caf59SThomas Veerman * we'll update it again. Generally, there won't
2442e2caf59SThomas Veerman * be two rules to update a single file, so this
2452e2caf59SThomas Veerman * should be ok, but... */
2462e2caf59SThomas Veerman
2472e2caf59SThomas Veerman
2482e2caf59SThomas Veerman static int DirFindName(const void *, const void *);
2492e2caf59SThomas Veerman static int DirMatchFiles(const char *, Path *, Lst);
2502e2caf59SThomas Veerman static void DirExpandCurly(const char *, const char *, Lst, Lst);
2512e2caf59SThomas Veerman static void DirExpandInt(const char *, Lst, Lst);
2522e2caf59SThomas Veerman static int DirPrintWord(void *, void *);
2532e2caf59SThomas Veerman static int DirPrintDir(void *, void *);
2542e2caf59SThomas Veerman static char *DirLookup(Path *, const char *, const char *, Boolean);
2552e2caf59SThomas Veerman static char *DirLookupSubdir(Path *, const char *);
2562e2caf59SThomas Veerman static char *DirFindDot(Boolean, const char *, const char *);
2572e2caf59SThomas Veerman static char *DirLookupAbs(Path *, const char *, const char *);
2582e2caf59SThomas Veerman
2592e2caf59SThomas Veerman /*-
2602e2caf59SThomas Veerman *-----------------------------------------------------------------------
2612e2caf59SThomas Veerman * Dir_Init --
2622e2caf59SThomas Veerman * initialize things for this module
2632e2caf59SThomas Veerman *
2642e2caf59SThomas Veerman * Results:
2652e2caf59SThomas Veerman * none
2662e2caf59SThomas Veerman *
2672e2caf59SThomas Veerman * Side Effects:
2682e2caf59SThomas Veerman * some directories may be opened.
2692e2caf59SThomas Veerman *-----------------------------------------------------------------------
2702e2caf59SThomas Veerman */
2712e2caf59SThomas Veerman void
Dir_Init(const char * cdname)2722e2caf59SThomas Veerman Dir_Init(const char *cdname)
2732e2caf59SThomas Veerman {
2742e2caf59SThomas Veerman dirSearchPath = Lst_Init(FALSE);
2752e2caf59SThomas Veerman openDirectories = Lst_Init(FALSE);
2762e2caf59SThomas Veerman Hash_InitTable(&mtimes, 0);
2772e2caf59SThomas Veerman
2782e2caf59SThomas Veerman Dir_InitCur(cdname);
2792e2caf59SThomas Veerman
2802e2caf59SThomas Veerman dotLast = bmake_malloc(sizeof(Path));
2812e2caf59SThomas Veerman dotLast->refCount = 1;
2822e2caf59SThomas Veerman dotLast->hits = 0;
2832e2caf59SThomas Veerman dotLast->name = bmake_strdup(".DOTLAST");
2842e2caf59SThomas Veerman Hash_InitTable(&dotLast->files, -1);
2852e2caf59SThomas Veerman }
2862e2caf59SThomas Veerman
2872e2caf59SThomas Veerman /*
2882e2caf59SThomas Veerman * Called by Dir_Init() and whenever .CURDIR is assigned to.
2892e2caf59SThomas Veerman */
2902e2caf59SThomas Veerman void
Dir_InitCur(const char * cdname)2912e2caf59SThomas Veerman Dir_InitCur(const char *cdname)
2922e2caf59SThomas Veerman {
2932e2caf59SThomas Veerman Path *p;
2942e2caf59SThomas Veerman
2952e2caf59SThomas Veerman if (cdname != NULL) {
2962e2caf59SThomas Veerman /*
2972e2caf59SThomas Veerman * Our build directory is not the same as our source directory.
2982e2caf59SThomas Veerman * Keep this one around too.
2992e2caf59SThomas Veerman */
3002e2caf59SThomas Veerman if ((p = Dir_AddDir(NULL, cdname))) {
3012e2caf59SThomas Veerman p->refCount += 1;
3022e2caf59SThomas Veerman if (cur && cur != p) {
3032e2caf59SThomas Veerman /*
3042e2caf59SThomas Veerman * We've been here before, cleanup.
3052e2caf59SThomas Veerman */
3062e2caf59SThomas Veerman cur->refCount -= 1;
3072e2caf59SThomas Veerman Dir_Destroy(cur);
3082e2caf59SThomas Veerman }
3092e2caf59SThomas Veerman cur = p;
3102e2caf59SThomas Veerman }
3112e2caf59SThomas Veerman }
3122e2caf59SThomas Veerman }
3132e2caf59SThomas Veerman
3142e2caf59SThomas Veerman /*-
3152e2caf59SThomas Veerman *-----------------------------------------------------------------------
3162e2caf59SThomas Veerman * Dir_InitDot --
3172e2caf59SThomas Veerman * (re)initialize "dot" (current/object directory) path hash
3182e2caf59SThomas Veerman *
3192e2caf59SThomas Veerman * Results:
3202e2caf59SThomas Veerman * none
3212e2caf59SThomas Veerman *
3222e2caf59SThomas Veerman * Side Effects:
3232e2caf59SThomas Veerman * some directories may be opened.
3242e2caf59SThomas Veerman *-----------------------------------------------------------------------
3252e2caf59SThomas Veerman */
3262e2caf59SThomas Veerman void
Dir_InitDot(void)3272e2caf59SThomas Veerman Dir_InitDot(void)
3282e2caf59SThomas Veerman {
3292e2caf59SThomas Veerman if (dot != NULL) {
3302e2caf59SThomas Veerman LstNode ln;
3312e2caf59SThomas Veerman
3322e2caf59SThomas Veerman /* Remove old entry from openDirectories, but do not destroy. */
3332e2caf59SThomas Veerman ln = Lst_Member(openDirectories, dot);
3342e2caf59SThomas Veerman (void)Lst_Remove(openDirectories, ln);
3352e2caf59SThomas Veerman }
3362e2caf59SThomas Veerman
3372e2caf59SThomas Veerman dot = Dir_AddDir(NULL, ".");
3382e2caf59SThomas Veerman
3392e2caf59SThomas Veerman if (dot == NULL) {
3402e2caf59SThomas Veerman Error("Cannot open `.' (%s)", strerror(errno));
3412e2caf59SThomas Veerman exit(1);
3422e2caf59SThomas Veerman }
3432e2caf59SThomas Veerman
3442e2caf59SThomas Veerman /*
3452e2caf59SThomas Veerman * We always need to have dot around, so we increment its reference count
3462e2caf59SThomas Veerman * to make sure it's not destroyed.
3472e2caf59SThomas Veerman */
3482e2caf59SThomas Veerman dot->refCount += 1;
3492e2caf59SThomas Veerman Dir_SetPATH(); /* initialize */
3502e2caf59SThomas Veerman }
3512e2caf59SThomas Veerman
3522e2caf59SThomas Veerman /*-
3532e2caf59SThomas Veerman *-----------------------------------------------------------------------
3542e2caf59SThomas Veerman * Dir_End --
3552e2caf59SThomas Veerman * cleanup things for this module
3562e2caf59SThomas Veerman *
3572e2caf59SThomas Veerman * Results:
3582e2caf59SThomas Veerman * none
3592e2caf59SThomas Veerman *
3602e2caf59SThomas Veerman * Side Effects:
3612e2caf59SThomas Veerman * none
3622e2caf59SThomas Veerman *-----------------------------------------------------------------------
3632e2caf59SThomas Veerman */
3642e2caf59SThomas Veerman void
Dir_End(void)3652e2caf59SThomas Veerman Dir_End(void)
3662e2caf59SThomas Veerman {
3672e2caf59SThomas Veerman #ifdef CLEANUP
3682e2caf59SThomas Veerman if (cur) {
3692e2caf59SThomas Veerman cur->refCount -= 1;
3702e2caf59SThomas Veerman Dir_Destroy(cur);
3712e2caf59SThomas Veerman }
3722e2caf59SThomas Veerman dot->refCount -= 1;
3732e2caf59SThomas Veerman dotLast->refCount -= 1;
3742e2caf59SThomas Veerman Dir_Destroy(dotLast);
3752e2caf59SThomas Veerman Dir_Destroy(dot);
3762e2caf59SThomas Veerman Dir_ClearPath(dirSearchPath);
3772e2caf59SThomas Veerman Lst_Destroy(dirSearchPath, NULL);
3782e2caf59SThomas Veerman Dir_ClearPath(openDirectories);
3792e2caf59SThomas Veerman Lst_Destroy(openDirectories, NULL);
3802e2caf59SThomas Veerman Hash_DeleteTable(&mtimes);
3812e2caf59SThomas Veerman #endif
3822e2caf59SThomas Veerman }
3832e2caf59SThomas Veerman
3842e2caf59SThomas Veerman /*
3852e2caf59SThomas Veerman * We want ${.PATH} to indicate the order in which we will actually
3862e2caf59SThomas Veerman * search, so we rebuild it after any .PATH: target.
3872e2caf59SThomas Veerman * This is the simplest way to deal with the effect of .DOTLAST.
3882e2caf59SThomas Veerman */
3892e2caf59SThomas Veerman void
Dir_SetPATH(void)3902e2caf59SThomas Veerman Dir_SetPATH(void)
3912e2caf59SThomas Veerman {
3922e2caf59SThomas Veerman LstNode ln; /* a list element */
3932e2caf59SThomas Veerman Path *p;
3942e2caf59SThomas Veerman Boolean hasLastDot = FALSE; /* true we should search dot last */
3952e2caf59SThomas Veerman
3962e2caf59SThomas Veerman Var_Delete(".PATH", VAR_GLOBAL);
3972e2caf59SThomas Veerman
3982e2caf59SThomas Veerman if (Lst_Open(dirSearchPath) == SUCCESS) {
3992e2caf59SThomas Veerman if ((ln = Lst_First(dirSearchPath)) != NULL) {
4002e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
4012e2caf59SThomas Veerman if (p == dotLast) {
4022e2caf59SThomas Veerman hasLastDot = TRUE;
4032e2caf59SThomas Veerman Var_Append(".PATH", dotLast->name, VAR_GLOBAL);
4042e2caf59SThomas Veerman }
4052e2caf59SThomas Veerman }
4062e2caf59SThomas Veerman
4072e2caf59SThomas Veerman if (!hasLastDot) {
4082e2caf59SThomas Veerman if (dot)
4092e2caf59SThomas Veerman Var_Append(".PATH", dot->name, VAR_GLOBAL);
4102e2caf59SThomas Veerman if (cur)
4112e2caf59SThomas Veerman Var_Append(".PATH", cur->name, VAR_GLOBAL);
4122e2caf59SThomas Veerman }
4132e2caf59SThomas Veerman
4142e2caf59SThomas Veerman while ((ln = Lst_Next(dirSearchPath)) != NULL) {
4152e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
4162e2caf59SThomas Veerman if (p == dotLast)
4172e2caf59SThomas Veerman continue;
4182e2caf59SThomas Veerman if (p == dot && hasLastDot)
4192e2caf59SThomas Veerman continue;
4202e2caf59SThomas Veerman Var_Append(".PATH", p->name, VAR_GLOBAL);
4212e2caf59SThomas Veerman }
4222e2caf59SThomas Veerman
4232e2caf59SThomas Veerman if (hasLastDot) {
4242e2caf59SThomas Veerman if (dot)
4252e2caf59SThomas Veerman Var_Append(".PATH", dot->name, VAR_GLOBAL);
4262e2caf59SThomas Veerman if (cur)
4272e2caf59SThomas Veerman Var_Append(".PATH", cur->name, VAR_GLOBAL);
4282e2caf59SThomas Veerman }
4292e2caf59SThomas Veerman Lst_Close(dirSearchPath);
4302e2caf59SThomas Veerman }
4312e2caf59SThomas Veerman }
4322e2caf59SThomas Veerman
4332e2caf59SThomas Veerman /*-
4342e2caf59SThomas Veerman *-----------------------------------------------------------------------
4352e2caf59SThomas Veerman * DirFindName --
4362e2caf59SThomas Veerman * See if the Path structure describes the same directory as the
4372e2caf59SThomas Veerman * given one by comparing their names. Called from Dir_AddDir via
4382e2caf59SThomas Veerman * Lst_Find when searching the list of open directories.
4392e2caf59SThomas Veerman *
4402e2caf59SThomas Veerman * Input:
4412e2caf59SThomas Veerman * p Current name
4422e2caf59SThomas Veerman * dname Desired name
4432e2caf59SThomas Veerman *
4442e2caf59SThomas Veerman * Results:
4452e2caf59SThomas Veerman * 0 if it is the same. Non-zero otherwise
4462e2caf59SThomas Veerman *
4472e2caf59SThomas Veerman * Side Effects:
4482e2caf59SThomas Veerman * None
4492e2caf59SThomas Veerman *-----------------------------------------------------------------------
4502e2caf59SThomas Veerman */
4512e2caf59SThomas Veerman static int
DirFindName(const void * p,const void * dname)4522e2caf59SThomas Veerman DirFindName(const void *p, const void *dname)
4532e2caf59SThomas Veerman {
4542e2caf59SThomas Veerman return (strcmp(((const Path *)p)->name, dname));
4552e2caf59SThomas Veerman }
4562e2caf59SThomas Veerman
4572e2caf59SThomas Veerman /*-
4582e2caf59SThomas Veerman *-----------------------------------------------------------------------
4592e2caf59SThomas Veerman * Dir_HasWildcards --
4602e2caf59SThomas Veerman * see if the given name has any wildcard characters in it
4612e2caf59SThomas Veerman * be careful not to expand unmatching brackets or braces.
4622e2caf59SThomas Veerman * XXX: This code is not 100% correct. ([^]] fails etc.)
4632e2caf59SThomas Veerman * I really don't think that make(1) should be expanding
4642e2caf59SThomas Veerman * patterns, because then you have to set a mechanism for
4652e2caf59SThomas Veerman * escaping the expansion!
4662e2caf59SThomas Veerman *
4672e2caf59SThomas Veerman * Input:
4682e2caf59SThomas Veerman * name name to check
4692e2caf59SThomas Veerman *
4702e2caf59SThomas Veerman * Results:
4712e2caf59SThomas Veerman * returns TRUE if the word should be expanded, FALSE otherwise
4722e2caf59SThomas Veerman *
4732e2caf59SThomas Veerman * Side Effects:
4742e2caf59SThomas Veerman * none
4752e2caf59SThomas Veerman *-----------------------------------------------------------------------
4762e2caf59SThomas Veerman */
4772e2caf59SThomas Veerman Boolean
Dir_HasWildcards(char * name)4782e2caf59SThomas Veerman Dir_HasWildcards(char *name)
4792e2caf59SThomas Veerman {
4802e2caf59SThomas Veerman char *cp;
4812e2caf59SThomas Veerman int wild = 0, brace = 0, bracket = 0;
4822e2caf59SThomas Veerman
4832e2caf59SThomas Veerman for (cp = name; *cp; cp++) {
4842e2caf59SThomas Veerman switch(*cp) {
4852e2caf59SThomas Veerman case '{':
4862e2caf59SThomas Veerman brace++;
4872e2caf59SThomas Veerman wild = 1;
4882e2caf59SThomas Veerman break;
4892e2caf59SThomas Veerman case '}':
4902e2caf59SThomas Veerman brace--;
4912e2caf59SThomas Veerman break;
4922e2caf59SThomas Veerman case '[':
4932e2caf59SThomas Veerman bracket++;
4942e2caf59SThomas Veerman wild = 1;
4952e2caf59SThomas Veerman break;
4962e2caf59SThomas Veerman case ']':
4972e2caf59SThomas Veerman bracket--;
4982e2caf59SThomas Veerman break;
4992e2caf59SThomas Veerman case '?':
5002e2caf59SThomas Veerman case '*':
5012e2caf59SThomas Veerman wild = 1;
5022e2caf59SThomas Veerman break;
5032e2caf59SThomas Veerman default:
5042e2caf59SThomas Veerman break;
5052e2caf59SThomas Veerman }
5062e2caf59SThomas Veerman }
5072e2caf59SThomas Veerman return wild && bracket == 0 && brace == 0;
5082e2caf59SThomas Veerman }
5092e2caf59SThomas Veerman
5102e2caf59SThomas Veerman /*-
5112e2caf59SThomas Veerman *-----------------------------------------------------------------------
5122e2caf59SThomas Veerman * DirMatchFiles --
5132e2caf59SThomas Veerman * Given a pattern and a Path structure, see if any files
5142e2caf59SThomas Veerman * match the pattern and add their names to the 'expansions' list if
5152e2caf59SThomas Veerman * any do. This is incomplete -- it doesn't take care of patterns like
5162e2caf59SThomas Veerman * src / *src / *.c properly (just *.c on any of the directories), but it
5172e2caf59SThomas Veerman * will do for now.
5182e2caf59SThomas Veerman *
5192e2caf59SThomas Veerman * Input:
5202e2caf59SThomas Veerman * pattern Pattern to look for
5212e2caf59SThomas Veerman * p Directory to search
5222e2caf59SThomas Veerman * expansion Place to store the results
5232e2caf59SThomas Veerman *
5242e2caf59SThomas Veerman * Results:
5252e2caf59SThomas Veerman * Always returns 0
5262e2caf59SThomas Veerman *
5272e2caf59SThomas Veerman * Side Effects:
5282e2caf59SThomas Veerman * File names are added to the expansions lst. The directory will be
5292e2caf59SThomas Veerman * fully hashed when this is done.
5302e2caf59SThomas Veerman *-----------------------------------------------------------------------
5312e2caf59SThomas Veerman */
5322e2caf59SThomas Veerman static int
DirMatchFiles(const char * pattern,Path * p,Lst expansions)5332e2caf59SThomas Veerman DirMatchFiles(const char *pattern, Path *p, Lst expansions)
5342e2caf59SThomas Veerman {
5352e2caf59SThomas Veerman Hash_Search search; /* Index into the directory's table */
5362e2caf59SThomas Veerman Hash_Entry *entry; /* Current entry in the table */
5372e2caf59SThomas Veerman Boolean isDot; /* TRUE if the directory being searched is . */
5382e2caf59SThomas Veerman
5392e2caf59SThomas Veerman isDot = (*p->name == '.' && p->name[1] == '\0');
5402e2caf59SThomas Veerman
5412e2caf59SThomas Veerman for (entry = Hash_EnumFirst(&p->files, &search);
5422e2caf59SThomas Veerman entry != NULL;
5432e2caf59SThomas Veerman entry = Hash_EnumNext(&search))
5442e2caf59SThomas Veerman {
5452e2caf59SThomas Veerman /*
5462e2caf59SThomas Veerman * See if the file matches the given pattern. Note we follow the UNIX
5472e2caf59SThomas Veerman * convention that dot files will only be found if the pattern
5482e2caf59SThomas Veerman * begins with a dot (note also that as a side effect of the hashing
5492e2caf59SThomas Veerman * scheme, .* won't match . or .. since they aren't hashed).
5502e2caf59SThomas Veerman */
5512e2caf59SThomas Veerman if (Str_Match(entry->name, pattern) &&
5522e2caf59SThomas Veerman ((entry->name[0] != '.') ||
5532e2caf59SThomas Veerman (pattern[0] == '.')))
5542e2caf59SThomas Veerman {
5552e2caf59SThomas Veerman (void)Lst_AtEnd(expansions,
5562e2caf59SThomas Veerman (isDot ? bmake_strdup(entry->name) :
5572e2caf59SThomas Veerman str_concat(p->name, entry->name,
5582e2caf59SThomas Veerman STR_ADDSLASH)));
5592e2caf59SThomas Veerman }
5602e2caf59SThomas Veerman }
5612e2caf59SThomas Veerman return (0);
5622e2caf59SThomas Veerman }
5632e2caf59SThomas Veerman
5642e2caf59SThomas Veerman /*-
5652e2caf59SThomas Veerman *-----------------------------------------------------------------------
5662e2caf59SThomas Veerman * DirExpandCurly --
5672e2caf59SThomas Veerman * Expand curly braces like the C shell. Does this recursively.
5682e2caf59SThomas Veerman * Note the special case: if after the piece of the curly brace is
5692e2caf59SThomas Veerman * done there are no wildcard characters in the result, the result is
5702e2caf59SThomas Veerman * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.
5712e2caf59SThomas Veerman *
5722e2caf59SThomas Veerman * Input:
5732e2caf59SThomas Veerman * word Entire word to expand
5742e2caf59SThomas Veerman * brace First curly brace in it
5752e2caf59SThomas Veerman * path Search path to use
5762e2caf59SThomas Veerman * expansions Place to store the expansions
5772e2caf59SThomas Veerman *
5782e2caf59SThomas Veerman * Results:
5792e2caf59SThomas Veerman * None.
5802e2caf59SThomas Veerman *
5812e2caf59SThomas Veerman * Side Effects:
5822e2caf59SThomas Veerman * The given list is filled with the expansions...
5832e2caf59SThomas Veerman *
5842e2caf59SThomas Veerman *-----------------------------------------------------------------------
5852e2caf59SThomas Veerman */
5862e2caf59SThomas Veerman static void
DirExpandCurly(const char * word,const char * brace,Lst path,Lst expansions)5872e2caf59SThomas Veerman DirExpandCurly(const char *word, const char *brace, Lst path, Lst expansions)
5882e2caf59SThomas Veerman {
5892e2caf59SThomas Veerman const char *end; /* Character after the closing brace */
5902e2caf59SThomas Veerman const char *cp; /* Current position in brace clause */
5912e2caf59SThomas Veerman const char *start; /* Start of current piece of brace clause */
5922e2caf59SThomas Veerman int bracelevel; /* Number of braces we've seen. If we see a
5932e2caf59SThomas Veerman * right brace when this is 0, we've hit the
5942e2caf59SThomas Veerman * end of the clause. */
5952e2caf59SThomas Veerman char *file; /* Current expansion */
5962e2caf59SThomas Veerman int otherLen; /* The length of the other pieces of the
5972e2caf59SThomas Veerman * expansion (chars before and after the
5982e2caf59SThomas Veerman * clause in 'word') */
5992e2caf59SThomas Veerman char *cp2; /* Pointer for checking for wildcards in
6002e2caf59SThomas Veerman * expansion before calling Dir_Expand */
6012e2caf59SThomas Veerman
6022e2caf59SThomas Veerman start = brace+1;
6032e2caf59SThomas Veerman
6042e2caf59SThomas Veerman /*
6052e2caf59SThomas Veerman * Find the end of the brace clause first, being wary of nested brace
6062e2caf59SThomas Veerman * clauses.
6072e2caf59SThomas Veerman */
6082e2caf59SThomas Veerman for (end = start, bracelevel = 0; *end != '\0'; end++) {
6092e2caf59SThomas Veerman if (*end == '{') {
6102e2caf59SThomas Veerman bracelevel++;
6112e2caf59SThomas Veerman } else if ((*end == '}') && (bracelevel-- == 0)) {
6122e2caf59SThomas Veerman break;
6132e2caf59SThomas Veerman }
6142e2caf59SThomas Veerman }
6152e2caf59SThomas Veerman if (*end == '\0') {
6162e2caf59SThomas Veerman Error("Unterminated {} clause \"%s\"", start);
6172e2caf59SThomas Veerman return;
6182e2caf59SThomas Veerman } else {
6192e2caf59SThomas Veerman end++;
6202e2caf59SThomas Veerman }
6212e2caf59SThomas Veerman otherLen = brace - word + strlen(end);
6222e2caf59SThomas Veerman
6232e2caf59SThomas Veerman for (cp = start; cp < end; cp++) {
6242e2caf59SThomas Veerman /*
6252e2caf59SThomas Veerman * Find the end of this piece of the clause.
6262e2caf59SThomas Veerman */
6272e2caf59SThomas Veerman bracelevel = 0;
6282e2caf59SThomas Veerman while (*cp != ',') {
6292e2caf59SThomas Veerman if (*cp == '{') {
6302e2caf59SThomas Veerman bracelevel++;
6312e2caf59SThomas Veerman } else if ((*cp == '}') && (bracelevel-- <= 0)) {
6322e2caf59SThomas Veerman break;
6332e2caf59SThomas Veerman }
6342e2caf59SThomas Veerman cp++;
6352e2caf59SThomas Veerman }
6362e2caf59SThomas Veerman /*
6372e2caf59SThomas Veerman * Allocate room for the combination and install the three pieces.
6382e2caf59SThomas Veerman */
6392e2caf59SThomas Veerman file = bmake_malloc(otherLen + cp - start + 1);
6402e2caf59SThomas Veerman if (brace != word) {
6412e2caf59SThomas Veerman strncpy(file, word, brace-word);
6422e2caf59SThomas Veerman }
6432e2caf59SThomas Veerman if (cp != start) {
6442e2caf59SThomas Veerman strncpy(&file[brace-word], start, cp-start);
6452e2caf59SThomas Veerman }
6462e2caf59SThomas Veerman strcpy(&file[(brace-word)+(cp-start)], end);
6472e2caf59SThomas Veerman
6482e2caf59SThomas Veerman /*
6492e2caf59SThomas Veerman * See if the result has any wildcards in it. If we find one, call
6502e2caf59SThomas Veerman * Dir_Expand right away, telling it to place the result on our list
6512e2caf59SThomas Veerman * of expansions.
6522e2caf59SThomas Veerman */
6532e2caf59SThomas Veerman for (cp2 = file; *cp2 != '\0'; cp2++) {
6542e2caf59SThomas Veerman switch(*cp2) {
6552e2caf59SThomas Veerman case '*':
6562e2caf59SThomas Veerman case '?':
6572e2caf59SThomas Veerman case '{':
6582e2caf59SThomas Veerman case '[':
6592e2caf59SThomas Veerman Dir_Expand(file, path, expansions);
6602e2caf59SThomas Veerman goto next;
6612e2caf59SThomas Veerman }
6622e2caf59SThomas Veerman }
6632e2caf59SThomas Veerman if (*cp2 == '\0') {
6642e2caf59SThomas Veerman /*
6652e2caf59SThomas Veerman * Hit the end w/o finding any wildcards, so stick the expansion
6662e2caf59SThomas Veerman * on the end of the list.
6672e2caf59SThomas Veerman */
6682e2caf59SThomas Veerman (void)Lst_AtEnd(expansions, file);
6692e2caf59SThomas Veerman } else {
6702e2caf59SThomas Veerman next:
6712e2caf59SThomas Veerman free(file);
6722e2caf59SThomas Veerman }
6732e2caf59SThomas Veerman start = cp+1;
6742e2caf59SThomas Veerman }
6752e2caf59SThomas Veerman }
6762e2caf59SThomas Veerman
6772e2caf59SThomas Veerman
6782e2caf59SThomas Veerman /*-
6792e2caf59SThomas Veerman *-----------------------------------------------------------------------
6802e2caf59SThomas Veerman * DirExpandInt --
6812e2caf59SThomas Veerman * Internal expand routine. Passes through the directories in the
6822e2caf59SThomas Veerman * path one by one, calling DirMatchFiles for each. NOTE: This still
6832e2caf59SThomas Veerman * doesn't handle patterns in directories...
6842e2caf59SThomas Veerman *
6852e2caf59SThomas Veerman * Input:
6862e2caf59SThomas Veerman * word Word to expand
6872e2caf59SThomas Veerman * path Path on which to look
6882e2caf59SThomas Veerman * expansions Place to store the result
6892e2caf59SThomas Veerman *
6902e2caf59SThomas Veerman * Results:
6912e2caf59SThomas Veerman * None.
6922e2caf59SThomas Veerman *
6932e2caf59SThomas Veerman * Side Effects:
6942e2caf59SThomas Veerman * Things are added to the expansions list.
6952e2caf59SThomas Veerman *
6962e2caf59SThomas Veerman *-----------------------------------------------------------------------
6972e2caf59SThomas Veerman */
6982e2caf59SThomas Veerman static void
DirExpandInt(const char * word,Lst path,Lst expansions)6992e2caf59SThomas Veerman DirExpandInt(const char *word, Lst path, Lst expansions)
7002e2caf59SThomas Veerman {
7012e2caf59SThomas Veerman LstNode ln; /* Current node */
7022e2caf59SThomas Veerman Path *p; /* Directory in the node */
7032e2caf59SThomas Veerman
7042e2caf59SThomas Veerman if (Lst_Open(path) == SUCCESS) {
7052e2caf59SThomas Veerman while ((ln = Lst_Next(path)) != NULL) {
7062e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
7072e2caf59SThomas Veerman DirMatchFiles(word, p, expansions);
7082e2caf59SThomas Veerman }
7092e2caf59SThomas Veerman Lst_Close(path);
7102e2caf59SThomas Veerman }
7112e2caf59SThomas Veerman }
7122e2caf59SThomas Veerman
7132e2caf59SThomas Veerman /*-
7142e2caf59SThomas Veerman *-----------------------------------------------------------------------
7152e2caf59SThomas Veerman * DirPrintWord --
7162e2caf59SThomas Veerman * Print a word in the list of expansions. Callback for Dir_Expand
7172e2caf59SThomas Veerman * when DEBUG(DIR), via Lst_ForEach.
7182e2caf59SThomas Veerman *
7192e2caf59SThomas Veerman * Results:
7202e2caf59SThomas Veerman * === 0
7212e2caf59SThomas Veerman *
7222e2caf59SThomas Veerman * Side Effects:
7232e2caf59SThomas Veerman * The passed word is printed, followed by a space.
7242e2caf59SThomas Veerman *
7252e2caf59SThomas Veerman *-----------------------------------------------------------------------
7262e2caf59SThomas Veerman */
7272e2caf59SThomas Veerman static int
DirPrintWord(void * word,void * dummy)7282e2caf59SThomas Veerman DirPrintWord(void *word, void *dummy)
7292e2caf59SThomas Veerman {
7302e2caf59SThomas Veerman fprintf(debug_file, "%s ", (char *)word);
7312e2caf59SThomas Veerman
7322e2caf59SThomas Veerman return(dummy ? 0 : 0);
7332e2caf59SThomas Veerman }
7342e2caf59SThomas Veerman
7352e2caf59SThomas Veerman /*-
7362e2caf59SThomas Veerman *-----------------------------------------------------------------------
7372e2caf59SThomas Veerman * Dir_Expand --
7382e2caf59SThomas Veerman * Expand the given word into a list of words by globbing it looking
7392e2caf59SThomas Veerman * in the directories on the given search path.
7402e2caf59SThomas Veerman *
7412e2caf59SThomas Veerman * Input:
7422e2caf59SThomas Veerman * word the word to expand
7432e2caf59SThomas Veerman * path the list of directories in which to find the
7442e2caf59SThomas Veerman * resulting files
7452e2caf59SThomas Veerman * expansions the list on which to place the results
7462e2caf59SThomas Veerman *
7472e2caf59SThomas Veerman * Results:
7482e2caf59SThomas Veerman * A list of words consisting of the files which exist along the search
7492e2caf59SThomas Veerman * path matching the given pattern.
7502e2caf59SThomas Veerman *
7512e2caf59SThomas Veerman * Side Effects:
7522e2caf59SThomas Veerman * Directories may be opened. Who knows?
7532e2caf59SThomas Veerman *-----------------------------------------------------------------------
7542e2caf59SThomas Veerman */
7552e2caf59SThomas Veerman void
Dir_Expand(const char * word,Lst path,Lst expansions)7562e2caf59SThomas Veerman Dir_Expand(const char *word, Lst path, Lst expansions)
7572e2caf59SThomas Veerman {
7582e2caf59SThomas Veerman const char *cp;
7592e2caf59SThomas Veerman
7602e2caf59SThomas Veerman if (DEBUG(DIR)) {
7612e2caf59SThomas Veerman fprintf(debug_file, "Expanding \"%s\"... ", word);
7622e2caf59SThomas Veerman }
7632e2caf59SThomas Veerman
7642e2caf59SThomas Veerman cp = strchr(word, '{');
7652e2caf59SThomas Veerman if (cp) {
7662e2caf59SThomas Veerman DirExpandCurly(word, cp, path, expansions);
7672e2caf59SThomas Veerman } else {
7682e2caf59SThomas Veerman cp = strchr(word, '/');
7692e2caf59SThomas Veerman if (cp) {
7702e2caf59SThomas Veerman /*
7712e2caf59SThomas Veerman * The thing has a directory component -- find the first wildcard
7722e2caf59SThomas Veerman * in the string.
7732e2caf59SThomas Veerman */
7742e2caf59SThomas Veerman for (cp = word; *cp; cp++) {
7752e2caf59SThomas Veerman if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') {
7762e2caf59SThomas Veerman break;
7772e2caf59SThomas Veerman }
7782e2caf59SThomas Veerman }
7792e2caf59SThomas Veerman if (*cp == '{') {
7802e2caf59SThomas Veerman /*
7812e2caf59SThomas Veerman * This one will be fun.
7822e2caf59SThomas Veerman */
7832e2caf59SThomas Veerman DirExpandCurly(word, cp, path, expansions);
7842e2caf59SThomas Veerman return;
7852e2caf59SThomas Veerman } else if (*cp != '\0') {
7862e2caf59SThomas Veerman /*
7872e2caf59SThomas Veerman * Back up to the start of the component
7882e2caf59SThomas Veerman */
7892e2caf59SThomas Veerman char *dirpath;
7902e2caf59SThomas Veerman
7912e2caf59SThomas Veerman while (cp > word && *cp != '/') {
7922e2caf59SThomas Veerman cp--;
7932e2caf59SThomas Veerman }
7942e2caf59SThomas Veerman if (cp != word) {
7952e2caf59SThomas Veerman char sc;
7962e2caf59SThomas Veerman /*
7972e2caf59SThomas Veerman * If the glob isn't in the first component, try and find
7982e2caf59SThomas Veerman * all the components up to the one with a wildcard.
7992e2caf59SThomas Veerman */
8002e2caf59SThomas Veerman sc = cp[1];
8012e2caf59SThomas Veerman ((char *)UNCONST(cp))[1] = '\0';
8022e2caf59SThomas Veerman dirpath = Dir_FindFile(word, path);
8032e2caf59SThomas Veerman ((char *)UNCONST(cp))[1] = sc;
8042e2caf59SThomas Veerman /*
8052e2caf59SThomas Veerman * dirpath is null if can't find the leading component
8062e2caf59SThomas Veerman * XXX: Dir_FindFile won't find internal components.
8072e2caf59SThomas Veerman * i.e. if the path contains ../Etc/Object and we're
8082e2caf59SThomas Veerman * looking for Etc, it won't be found. Ah well.
8092e2caf59SThomas Veerman * Probably not important.
8102e2caf59SThomas Veerman */
8112e2caf59SThomas Veerman if (dirpath != NULL) {
8122e2caf59SThomas Veerman char *dp = &dirpath[strlen(dirpath) - 1];
8132e2caf59SThomas Veerman if (*dp == '/')
8142e2caf59SThomas Veerman *dp = '\0';
8152e2caf59SThomas Veerman path = Lst_Init(FALSE);
8162e2caf59SThomas Veerman (void)Dir_AddDir(path, dirpath);
8172e2caf59SThomas Veerman DirExpandInt(cp+1, path, expansions);
8182e2caf59SThomas Veerman Lst_Destroy(path, NULL);
8192e2caf59SThomas Veerman }
8202e2caf59SThomas Veerman } else {
8212e2caf59SThomas Veerman /*
8222e2caf59SThomas Veerman * Start the search from the local directory
8232e2caf59SThomas Veerman */
8242e2caf59SThomas Veerman DirExpandInt(word, path, expansions);
8252e2caf59SThomas Veerman }
8262e2caf59SThomas Veerman } else {
8272e2caf59SThomas Veerman /*
8282e2caf59SThomas Veerman * Return the file -- this should never happen.
8292e2caf59SThomas Veerman */
8302e2caf59SThomas Veerman DirExpandInt(word, path, expansions);
8312e2caf59SThomas Veerman }
8322e2caf59SThomas Veerman } else {
8332e2caf59SThomas Veerman /*
8342e2caf59SThomas Veerman * First the files in dot
8352e2caf59SThomas Veerman */
8362e2caf59SThomas Veerman DirMatchFiles(word, dot, expansions);
8372e2caf59SThomas Veerman
8382e2caf59SThomas Veerman /*
8392e2caf59SThomas Veerman * Then the files in every other directory on the path.
8402e2caf59SThomas Veerman */
8412e2caf59SThomas Veerman DirExpandInt(word, path, expansions);
8422e2caf59SThomas Veerman }
8432e2caf59SThomas Veerman }
8442e2caf59SThomas Veerman if (DEBUG(DIR)) {
8452e2caf59SThomas Veerman Lst_ForEach(expansions, DirPrintWord, NULL);
8462e2caf59SThomas Veerman fprintf(debug_file, "\n");
8472e2caf59SThomas Veerman }
8482e2caf59SThomas Veerman }
8492e2caf59SThomas Veerman
8502e2caf59SThomas Veerman /*-
8512e2caf59SThomas Veerman *-----------------------------------------------------------------------
8522e2caf59SThomas Veerman * DirLookup --
8532e2caf59SThomas Veerman * Find if the file with the given name exists in the given path.
8542e2caf59SThomas Veerman *
8552e2caf59SThomas Veerman * Results:
8562e2caf59SThomas Veerman * The path to the file or NULL. This path is guaranteed to be in a
8572e2caf59SThomas Veerman * different part of memory than name and so may be safely free'd.
8582e2caf59SThomas Veerman *
8592e2caf59SThomas Veerman * Side Effects:
8602e2caf59SThomas Veerman * None.
8612e2caf59SThomas Veerman *-----------------------------------------------------------------------
8622e2caf59SThomas Veerman */
8632e2caf59SThomas Veerman static char *
DirLookup(Path * p,const char * name MAKE_ATTR_UNUSED,const char * cp,Boolean hasSlash MAKE_ATTR_UNUSED)8642bc7c627SLionel Sambuc DirLookup(Path *p, const char *name MAKE_ATTR_UNUSED, const char *cp,
8652bc7c627SLionel Sambuc Boolean hasSlash MAKE_ATTR_UNUSED)
8662e2caf59SThomas Veerman {
8672e2caf59SThomas Veerman char *file; /* the current filename to check */
8682e2caf59SThomas Veerman
8692e2caf59SThomas Veerman if (DEBUG(DIR)) {
8702e2caf59SThomas Veerman fprintf(debug_file, " %s ...\n", p->name);
8712e2caf59SThomas Veerman }
8722e2caf59SThomas Veerman
8732e2caf59SThomas Veerman if (Hash_FindEntry(&p->files, cp) == NULL)
8742e2caf59SThomas Veerman return NULL;
8752e2caf59SThomas Veerman
8762e2caf59SThomas Veerman file = str_concat(p->name, cp, STR_ADDSLASH);
8772e2caf59SThomas Veerman if (DEBUG(DIR)) {
8782e2caf59SThomas Veerman fprintf(debug_file, " returning %s\n", file);
8792e2caf59SThomas Veerman }
8802e2caf59SThomas Veerman p->hits += 1;
8812e2caf59SThomas Veerman hits += 1;
8822e2caf59SThomas Veerman return file;
8832e2caf59SThomas Veerman }
8842e2caf59SThomas Veerman
8852e2caf59SThomas Veerman
8862e2caf59SThomas Veerman /*-
8872e2caf59SThomas Veerman *-----------------------------------------------------------------------
8882e2caf59SThomas Veerman * DirLookupSubdir --
8892e2caf59SThomas Veerman * Find if the file with the given name exists in the given path.
8902e2caf59SThomas Veerman *
8912e2caf59SThomas Veerman * Results:
8922e2caf59SThomas Veerman * The path to the file or NULL. This path is guaranteed to be in a
8932e2caf59SThomas Veerman * different part of memory than name and so may be safely free'd.
8942e2caf59SThomas Veerman *
8952e2caf59SThomas Veerman * Side Effects:
8962e2caf59SThomas Veerman * If the file is found, it is added in the modification times hash
8972e2caf59SThomas Veerman * table.
8982e2caf59SThomas Veerman *-----------------------------------------------------------------------
8992e2caf59SThomas Veerman */
9002e2caf59SThomas Veerman static char *
DirLookupSubdir(Path * p,const char * name)9012e2caf59SThomas Veerman DirLookupSubdir(Path *p, const char *name)
9022e2caf59SThomas Veerman {
9032e2caf59SThomas Veerman struct stat stb; /* Buffer for stat, if necessary */
9042e2caf59SThomas Veerman Hash_Entry *entry; /* Entry for mtimes table */
9052e2caf59SThomas Veerman char *file; /* the current filename to check */
9062e2caf59SThomas Veerman
9072e2caf59SThomas Veerman if (p != dot) {
9082e2caf59SThomas Veerman file = str_concat(p->name, name, STR_ADDSLASH);
9092e2caf59SThomas Veerman } else {
9102e2caf59SThomas Veerman /*
9112e2caf59SThomas Veerman * Checking in dot -- DON'T put a leading ./ on the thing.
9122e2caf59SThomas Veerman */
9132e2caf59SThomas Veerman file = bmake_strdup(name);
9142e2caf59SThomas Veerman }
9152e2caf59SThomas Veerman
9162e2caf59SThomas Veerman if (DEBUG(DIR)) {
9172e2caf59SThomas Veerman fprintf(debug_file, "checking %s ...\n", file);
9182e2caf59SThomas Veerman }
9192e2caf59SThomas Veerman
9202e2caf59SThomas Veerman if (stat(file, &stb) == 0) {
9212e2caf59SThomas Veerman if (stb.st_mtime == 0)
9222e2caf59SThomas Veerman stb.st_mtime = 1;
9232e2caf59SThomas Veerman /*
9242e2caf59SThomas Veerman * Save the modification time so if it's needed, we don't have
9252e2caf59SThomas Veerman * to fetch it again.
9262e2caf59SThomas Veerman */
9272e2caf59SThomas Veerman if (DEBUG(DIR)) {
9282e2caf59SThomas Veerman fprintf(debug_file, " Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
9292e2caf59SThomas Veerman file);
9302e2caf59SThomas Veerman }
9312e2caf59SThomas Veerman entry = Hash_CreateEntry(&mtimes, file, NULL);
9322e2caf59SThomas Veerman Hash_SetTimeValue(entry, stb.st_mtime);
9332e2caf59SThomas Veerman nearmisses += 1;
9342e2caf59SThomas Veerman return (file);
9352e2caf59SThomas Veerman }
9362e2caf59SThomas Veerman free(file);
9372e2caf59SThomas Veerman return NULL;
9382e2caf59SThomas Veerman }
9392e2caf59SThomas Veerman
9402e2caf59SThomas Veerman /*-
9412e2caf59SThomas Veerman *-----------------------------------------------------------------------
9422e2caf59SThomas Veerman * DirLookupAbs --
9432e2caf59SThomas Veerman * Find if the file with the given name exists in the given path.
9442e2caf59SThomas Veerman *
9452e2caf59SThomas Veerman * Results:
9462e2caf59SThomas Veerman * The path to the file, the empty string or NULL. If the file is
9472e2caf59SThomas Veerman * the empty string, the search should be terminated.
9482e2caf59SThomas Veerman * This path is guaranteed to be in a different part of memory
9492e2caf59SThomas Veerman * than name and so may be safely free'd.
9502e2caf59SThomas Veerman *
9512e2caf59SThomas Veerman * Side Effects:
9522e2caf59SThomas Veerman * None.
9532e2caf59SThomas Veerman *-----------------------------------------------------------------------
9542e2caf59SThomas Veerman */
9552e2caf59SThomas Veerman static char *
DirLookupAbs(Path * p,const char * name,const char * cp)9562e2caf59SThomas Veerman DirLookupAbs(Path *p, const char *name, const char *cp)
9572e2caf59SThomas Veerman {
9582e2caf59SThomas Veerman char *p1; /* pointer into p->name */
9592e2caf59SThomas Veerman const char *p2; /* pointer into name */
9602e2caf59SThomas Veerman
9612e2caf59SThomas Veerman if (DEBUG(DIR)) {
9622e2caf59SThomas Veerman fprintf(debug_file, " %s ...\n", p->name);
9632e2caf59SThomas Veerman }
9642e2caf59SThomas Veerman
9652e2caf59SThomas Veerman /*
9662e2caf59SThomas Veerman * If the file has a leading path component and that component
9672e2caf59SThomas Veerman * exactly matches the entire name of the current search
9682e2caf59SThomas Veerman * directory, we can attempt another cache lookup. And if we don't
9692e2caf59SThomas Veerman * have a hit, we can safely assume the file does not exist at all.
9702e2caf59SThomas Veerman */
9712e2caf59SThomas Veerman for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) {
9722e2caf59SThomas Veerman continue;
9732e2caf59SThomas Veerman }
9742e2caf59SThomas Veerman if (*p1 != '\0' || p2 != cp - 1) {
9752e2caf59SThomas Veerman return NULL;
9762e2caf59SThomas Veerman }
9772e2caf59SThomas Veerman
9782e2caf59SThomas Veerman if (Hash_FindEntry(&p->files, cp) == NULL) {
9792e2caf59SThomas Veerman if (DEBUG(DIR)) {
9802e2caf59SThomas Veerman fprintf(debug_file, " must be here but isn't -- returning\n");
9812e2caf59SThomas Veerman }
9822e2caf59SThomas Veerman /* Return empty string: terminates search */
9832e2caf59SThomas Veerman return bmake_strdup("");
9842e2caf59SThomas Veerman }
9852e2caf59SThomas Veerman
9862e2caf59SThomas Veerman p->hits += 1;
9872e2caf59SThomas Veerman hits += 1;
9882e2caf59SThomas Veerman if (DEBUG(DIR)) {
9892e2caf59SThomas Veerman fprintf(debug_file, " returning %s\n", name);
9902e2caf59SThomas Veerman }
9912e2caf59SThomas Veerman return (bmake_strdup(name));
9922e2caf59SThomas Veerman }
9932e2caf59SThomas Veerman
9942e2caf59SThomas Veerman /*-
9952e2caf59SThomas Veerman *-----------------------------------------------------------------------
9962e2caf59SThomas Veerman * DirFindDot --
9972e2caf59SThomas Veerman * Find the file given on "." or curdir
9982e2caf59SThomas Veerman *
9992e2caf59SThomas Veerman * Results:
10002e2caf59SThomas Veerman * The path to the file or NULL. This path is guaranteed to be in a
10012e2caf59SThomas Veerman * different part of memory than name and so may be safely free'd.
10022e2caf59SThomas Veerman *
10032e2caf59SThomas Veerman * Side Effects:
10042e2caf59SThomas Veerman * Hit counts change
10052e2caf59SThomas Veerman *-----------------------------------------------------------------------
10062e2caf59SThomas Veerman */
10072e2caf59SThomas Veerman static char *
DirFindDot(Boolean hasSlash MAKE_ATTR_UNUSED,const char * name,const char * cp)10082bc7c627SLionel Sambuc DirFindDot(Boolean hasSlash MAKE_ATTR_UNUSED, const char *name, const char *cp)
10092e2caf59SThomas Veerman {
10102e2caf59SThomas Veerman
10112e2caf59SThomas Veerman if (Hash_FindEntry(&dot->files, cp) != NULL) {
10122e2caf59SThomas Veerman if (DEBUG(DIR)) {
10132e2caf59SThomas Veerman fprintf(debug_file, " in '.'\n");
10142e2caf59SThomas Veerman }
10152e2caf59SThomas Veerman hits += 1;
10162e2caf59SThomas Veerman dot->hits += 1;
10172e2caf59SThomas Veerman return (bmake_strdup(name));
10182e2caf59SThomas Veerman }
10192e2caf59SThomas Veerman if (cur &&
10202e2caf59SThomas Veerman Hash_FindEntry(&cur->files, cp) != NULL) {
10212e2caf59SThomas Veerman if (DEBUG(DIR)) {
10222e2caf59SThomas Veerman fprintf(debug_file, " in ${.CURDIR} = %s\n", cur->name);
10232e2caf59SThomas Veerman }
10242e2caf59SThomas Veerman hits += 1;
10252e2caf59SThomas Veerman cur->hits += 1;
10262e2caf59SThomas Veerman return str_concat(cur->name, cp, STR_ADDSLASH);
10272e2caf59SThomas Veerman }
10282e2caf59SThomas Veerman
10292e2caf59SThomas Veerman return NULL;
10302e2caf59SThomas Veerman }
10312e2caf59SThomas Veerman
10322e2caf59SThomas Veerman /*-
10332e2caf59SThomas Veerman *-----------------------------------------------------------------------
10342e2caf59SThomas Veerman * Dir_FindFile --
10352e2caf59SThomas Veerman * Find the file with the given name along the given search path.
10362e2caf59SThomas Veerman *
10372e2caf59SThomas Veerman * Input:
10382e2caf59SThomas Veerman * name the file to find
10392e2caf59SThomas Veerman * path the Lst of directories to search
10402e2caf59SThomas Veerman *
10412e2caf59SThomas Veerman * Results:
10422e2caf59SThomas Veerman * The path to the file or NULL. This path is guaranteed to be in a
10432e2caf59SThomas Veerman * different part of memory than name and so may be safely free'd.
10442e2caf59SThomas Veerman *
10452e2caf59SThomas Veerman * Side Effects:
10462e2caf59SThomas Veerman * If the file is found in a directory which is not on the path
10472e2caf59SThomas Veerman * already (either 'name' is absolute or it is a relative path
10482e2caf59SThomas Veerman * [ dir1/.../dirn/file ] which exists below one of the directories
10492e2caf59SThomas Veerman * already on the search path), its directory is added to the end
10502e2caf59SThomas Veerman * of the path on the assumption that there will be more files in
10512e2caf59SThomas Veerman * that directory later on. Sometimes this is true. Sometimes not.
10522e2caf59SThomas Veerman *-----------------------------------------------------------------------
10532e2caf59SThomas Veerman */
10542e2caf59SThomas Veerman char *
Dir_FindFile(const char * name,Lst path)10552e2caf59SThomas Veerman Dir_FindFile(const char *name, Lst path)
10562e2caf59SThomas Veerman {
10572e2caf59SThomas Veerman LstNode ln; /* a list element */
10582e2caf59SThomas Veerman char *file; /* the current filename to check */
10592e2caf59SThomas Veerman Path *p; /* current path member */
10602e2caf59SThomas Veerman const char *cp; /* Terminal name of file */
10612e2caf59SThomas Veerman Boolean hasLastDot = FALSE; /* true we should search dot last */
10622e2caf59SThomas Veerman Boolean hasSlash; /* true if 'name' contains a / */
10632e2caf59SThomas Veerman struct stat stb; /* Buffer for stat, if necessary */
10642e2caf59SThomas Veerman Hash_Entry *entry; /* Entry for mtimes table */
10652e2caf59SThomas Veerman const char *trailing_dot = ".";
10662e2caf59SThomas Veerman
10672e2caf59SThomas Veerman /*
10682e2caf59SThomas Veerman * Find the final component of the name and note whether it has a
10692e2caf59SThomas Veerman * slash in it (the name, I mean)
10702e2caf59SThomas Veerman */
10712e2caf59SThomas Veerman cp = strrchr(name, '/');
10722e2caf59SThomas Veerman if (cp) {
10732e2caf59SThomas Veerman hasSlash = TRUE;
10742e2caf59SThomas Veerman cp += 1;
10752e2caf59SThomas Veerman } else {
10762e2caf59SThomas Veerman hasSlash = FALSE;
10772e2caf59SThomas Veerman cp = name;
10782e2caf59SThomas Veerman }
10792e2caf59SThomas Veerman
10802e2caf59SThomas Veerman if (DEBUG(DIR)) {
10812e2caf59SThomas Veerman fprintf(debug_file, "Searching for %s ...", name);
10822e2caf59SThomas Veerman }
10832e2caf59SThomas Veerman
10842e2caf59SThomas Veerman if (Lst_Open(path) == FAILURE) {
10852e2caf59SThomas Veerman if (DEBUG(DIR)) {
10862e2caf59SThomas Veerman fprintf(debug_file, "couldn't open path, file not found\n");
10872e2caf59SThomas Veerman }
10882e2caf59SThomas Veerman misses += 1;
10892e2caf59SThomas Veerman return NULL;
10902e2caf59SThomas Veerman }
10912e2caf59SThomas Veerman
10922e2caf59SThomas Veerman if ((ln = Lst_First(path)) != NULL) {
10932e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
10942e2caf59SThomas Veerman if (p == dotLast) {
10952e2caf59SThomas Veerman hasLastDot = TRUE;
10962e2caf59SThomas Veerman if (DEBUG(DIR))
10972e2caf59SThomas Veerman fprintf(debug_file, "[dot last]...");
10982e2caf59SThomas Veerman }
10992e2caf59SThomas Veerman }
11002e2caf59SThomas Veerman if (DEBUG(DIR)) {
11012e2caf59SThomas Veerman fprintf(debug_file, "\n");
11022e2caf59SThomas Veerman }
11032e2caf59SThomas Veerman
11042e2caf59SThomas Veerman /*
11052e2caf59SThomas Veerman * If there's no leading directory components or if the leading
11062e2caf59SThomas Veerman * directory component is exactly `./', consult the cached contents
11072e2caf59SThomas Veerman * of each of the directories on the search path.
11082e2caf59SThomas Veerman */
11092e2caf59SThomas Veerman if (!hasSlash || (cp - name == 2 && *name == '.')) {
11102e2caf59SThomas Veerman /*
11112e2caf59SThomas Veerman * We look through all the directories on the path seeking one which
11122e2caf59SThomas Veerman * contains the final component of the given name. If such a beast
11132e2caf59SThomas Veerman * is found, we concatenate the directory name and the final
11142e2caf59SThomas Veerman * component and return the resulting string. If we don't find any
11152e2caf59SThomas Veerman * such thing, we go on to phase two...
11162e2caf59SThomas Veerman *
11172e2caf59SThomas Veerman * No matter what, we always look for the file in the current
11182e2caf59SThomas Veerman * directory before anywhere else (unless we found the magic
11192e2caf59SThomas Veerman * DOTLAST path, in which case we search it last) and we *do not*
11202e2caf59SThomas Veerman * add the ./ to it if it exists.
11212e2caf59SThomas Veerman * This is so there are no conflicts between what the user
11222e2caf59SThomas Veerman * specifies (fish.c) and what pmake finds (./fish.c).
11232e2caf59SThomas Veerman */
11242e2caf59SThomas Veerman if (!hasLastDot &&
11252e2caf59SThomas Veerman (file = DirFindDot(hasSlash, name, cp)) != NULL) {
11262e2caf59SThomas Veerman Lst_Close(path);
11272e2caf59SThomas Veerman return file;
11282e2caf59SThomas Veerman }
11292e2caf59SThomas Veerman
11302e2caf59SThomas Veerman while ((ln = Lst_Next(path)) != NULL) {
11312e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
11322e2caf59SThomas Veerman if (p == dotLast)
11332e2caf59SThomas Veerman continue;
11342e2caf59SThomas Veerman if ((file = DirLookup(p, name, cp, hasSlash)) != NULL) {
11352e2caf59SThomas Veerman Lst_Close(path);
11362e2caf59SThomas Veerman return file;
11372e2caf59SThomas Veerman }
11382e2caf59SThomas Veerman }
11392e2caf59SThomas Veerman
11402e2caf59SThomas Veerman if (hasLastDot &&
11412e2caf59SThomas Veerman (file = DirFindDot(hasSlash, name, cp)) != NULL) {
11422e2caf59SThomas Veerman Lst_Close(path);
11432e2caf59SThomas Veerman return file;
11442e2caf59SThomas Veerman }
11452e2caf59SThomas Veerman }
11462e2caf59SThomas Veerman Lst_Close(path);
11472e2caf59SThomas Veerman
11482e2caf59SThomas Veerman /*
11492e2caf59SThomas Veerman * We didn't find the file on any directory in the search path.
11502e2caf59SThomas Veerman * If the name doesn't contain a slash, that means it doesn't exist.
11512e2caf59SThomas Veerman * If it *does* contain a slash, however, there is still hope: it
11522e2caf59SThomas Veerman * could be in a subdirectory of one of the members of the search
11532e2caf59SThomas Veerman * path. (eg. /usr/include and sys/types.h. The above search would
11542e2caf59SThomas Veerman * fail to turn up types.h in /usr/include, but it *is* in
11552e2caf59SThomas Veerman * /usr/include/sys/types.h).
11562e2caf59SThomas Veerman * [ This no longer applies: If we find such a beast, we assume there
11572e2caf59SThomas Veerman * will be more (what else can we assume?) and add all but the last
11582e2caf59SThomas Veerman * component of the resulting name onto the search path (at the
11592e2caf59SThomas Veerman * end).]
11602e2caf59SThomas Veerman * This phase is only performed if the file is *not* absolute.
11612e2caf59SThomas Veerman */
11622e2caf59SThomas Veerman if (!hasSlash) {
11632e2caf59SThomas Veerman if (DEBUG(DIR)) {
11642e2caf59SThomas Veerman fprintf(debug_file, " failed.\n");
11652e2caf59SThomas Veerman }
11662e2caf59SThomas Veerman misses += 1;
11672e2caf59SThomas Veerman return NULL;
11682e2caf59SThomas Veerman }
11692e2caf59SThomas Veerman
11702e2caf59SThomas Veerman if (*cp == '\0') {
11712e2caf59SThomas Veerman /* we were given a trailing "/" */
11722e2caf59SThomas Veerman cp = trailing_dot;
11732e2caf59SThomas Veerman }
11742e2caf59SThomas Veerman
11752e2caf59SThomas Veerman if (name[0] != '/') {
11762e2caf59SThomas Veerman Boolean checkedDot = FALSE;
11772e2caf59SThomas Veerman
11782e2caf59SThomas Veerman if (DEBUG(DIR)) {
11792e2caf59SThomas Veerman fprintf(debug_file, " Trying subdirectories...\n");
11802e2caf59SThomas Veerman }
11812e2caf59SThomas Veerman
11822e2caf59SThomas Veerman if (!hasLastDot) {
11832e2caf59SThomas Veerman if (dot) {
11842e2caf59SThomas Veerman checkedDot = TRUE;
11852e2caf59SThomas Veerman if ((file = DirLookupSubdir(dot, name)) != NULL)
11862e2caf59SThomas Veerman return file;
11872e2caf59SThomas Veerman }
11882e2caf59SThomas Veerman if (cur && (file = DirLookupSubdir(cur, name)) != NULL)
11892e2caf59SThomas Veerman return file;
11902e2caf59SThomas Veerman }
11912e2caf59SThomas Veerman
11922e2caf59SThomas Veerman (void)Lst_Open(path);
11932e2caf59SThomas Veerman while ((ln = Lst_Next(path)) != NULL) {
11942e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
11952e2caf59SThomas Veerman if (p == dotLast)
11962e2caf59SThomas Veerman continue;
11972e2caf59SThomas Veerman if (p == dot) {
11982e2caf59SThomas Veerman if (checkedDot)
11992e2caf59SThomas Veerman continue;
12002e2caf59SThomas Veerman checkedDot = TRUE;
12012e2caf59SThomas Veerman }
12022e2caf59SThomas Veerman if ((file = DirLookupSubdir(p, name)) != NULL) {
12032e2caf59SThomas Veerman Lst_Close(path);
12042e2caf59SThomas Veerman return file;
12052e2caf59SThomas Veerman }
12062e2caf59SThomas Veerman }
12072e2caf59SThomas Veerman Lst_Close(path);
12082e2caf59SThomas Veerman
12092e2caf59SThomas Veerman if (hasLastDot) {
12102e2caf59SThomas Veerman if (dot && !checkedDot) {
12112e2caf59SThomas Veerman checkedDot = TRUE;
12122e2caf59SThomas Veerman if ((file = DirLookupSubdir(dot, name)) != NULL)
12132e2caf59SThomas Veerman return file;
12142e2caf59SThomas Veerman }
12152e2caf59SThomas Veerman if (cur && (file = DirLookupSubdir(cur, name)) != NULL)
12162e2caf59SThomas Veerman return file;
12172e2caf59SThomas Veerman }
12182e2caf59SThomas Veerman
12192e2caf59SThomas Veerman if (checkedDot) {
12202e2caf59SThomas Veerman /*
12212e2caf59SThomas Veerman * Already checked by the given name, since . was in the path,
12222e2caf59SThomas Veerman * so no point in proceeding...
12232e2caf59SThomas Veerman */
12242e2caf59SThomas Veerman if (DEBUG(DIR)) {
12252e2caf59SThomas Veerman fprintf(debug_file, " Checked . already, returning NULL\n");
12262e2caf59SThomas Veerman }
12272e2caf59SThomas Veerman return NULL;
12282e2caf59SThomas Veerman }
12292e2caf59SThomas Veerman
12302e2caf59SThomas Veerman } else { /* name[0] == '/' */
12312e2caf59SThomas Veerman
12322e2caf59SThomas Veerman /*
12332e2caf59SThomas Veerman * For absolute names, compare directory path prefix against the
12342e2caf59SThomas Veerman * the directory path of each member on the search path for an exact
12352e2caf59SThomas Veerman * match. If we have an exact match on any member of the search path,
12362e2caf59SThomas Veerman * use the cached contents of that member to lookup the final file
12372e2caf59SThomas Veerman * component. If that lookup fails we can safely assume that the
12382e2caf59SThomas Veerman * file does not exist at all. This is signified by DirLookupAbs()
12392e2caf59SThomas Veerman * returning an empty string.
12402e2caf59SThomas Veerman */
12412e2caf59SThomas Veerman if (DEBUG(DIR)) {
12422e2caf59SThomas Veerman fprintf(debug_file, " Trying exact path matches...\n");
12432e2caf59SThomas Veerman }
12442e2caf59SThomas Veerman
12452e2caf59SThomas Veerman if (!hasLastDot && cur && (file = DirLookupAbs(cur, name, cp)) != NULL)
12462e2caf59SThomas Veerman return *file?file:NULL;
12472e2caf59SThomas Veerman
12482e2caf59SThomas Veerman (void)Lst_Open(path);
12492e2caf59SThomas Veerman while ((ln = Lst_Next(path)) != NULL) {
12502e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
12512e2caf59SThomas Veerman if (p == dotLast)
12522e2caf59SThomas Veerman continue;
12532e2caf59SThomas Veerman if ((file = DirLookupAbs(p, name, cp)) != NULL) {
12542e2caf59SThomas Veerman Lst_Close(path);
12552e2caf59SThomas Veerman return *file?file:NULL;
12562e2caf59SThomas Veerman }
12572e2caf59SThomas Veerman }
12582e2caf59SThomas Veerman Lst_Close(path);
12592e2caf59SThomas Veerman
12602e2caf59SThomas Veerman if (hasLastDot && cur && (file = DirLookupAbs(cur, name, cp)) != NULL)
12612e2caf59SThomas Veerman return *file?file:NULL;
12622e2caf59SThomas Veerman }
12632e2caf59SThomas Veerman
12642e2caf59SThomas Veerman /*
12652e2caf59SThomas Veerman * Didn't find it that way, either. Sigh. Phase 3. Add its directory
12662e2caf59SThomas Veerman * onto the search path in any case, just in case, then look for the
12672e2caf59SThomas Veerman * thing in the hash table. If we find it, grand. We return a new
12682e2caf59SThomas Veerman * copy of the name. Otherwise we sadly return a NULL pointer. Sigh.
12692e2caf59SThomas Veerman * Note that if the directory holding the file doesn't exist, this will
12702e2caf59SThomas Veerman * do an extra search of the final directory on the path. Unless something
12712e2caf59SThomas Veerman * weird happens, this search won't succeed and life will be groovy.
12722e2caf59SThomas Veerman *
12732e2caf59SThomas Veerman * Sigh. We cannot add the directory onto the search path because
12742e2caf59SThomas Veerman * of this amusing case:
12752e2caf59SThomas Veerman * $(INSTALLDIR)/$(FILE): $(FILE)
12762e2caf59SThomas Veerman *
12772e2caf59SThomas Veerman * $(FILE) exists in $(INSTALLDIR) but not in the current one.
12782e2caf59SThomas Veerman * When searching for $(FILE), we will find it in $(INSTALLDIR)
12792e2caf59SThomas Veerman * b/c we added it here. This is not good...
12802e2caf59SThomas Veerman */
12812e2caf59SThomas Veerman #ifdef notdef
12822e2caf59SThomas Veerman if (cp == traling_dot) {
12832e2caf59SThomas Veerman cp = strrchr(name, '/');
12842e2caf59SThomas Veerman cp += 1;
12852e2caf59SThomas Veerman }
12862e2caf59SThomas Veerman cp[-1] = '\0';
12872e2caf59SThomas Veerman (void)Dir_AddDir(path, name);
12882e2caf59SThomas Veerman cp[-1] = '/';
12892e2caf59SThomas Veerman
12902e2caf59SThomas Veerman bigmisses += 1;
12912e2caf59SThomas Veerman ln = Lst_Last(path);
12922e2caf59SThomas Veerman if (ln == NULL) {
12932e2caf59SThomas Veerman return NULL;
12942e2caf59SThomas Veerman } else {
12952e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
12962e2caf59SThomas Veerman }
12972e2caf59SThomas Veerman
12982e2caf59SThomas Veerman if (Hash_FindEntry(&p->files, cp) != NULL) {
12992e2caf59SThomas Veerman return (bmake_strdup(name));
13002e2caf59SThomas Veerman } else {
13012e2caf59SThomas Veerman return NULL;
13022e2caf59SThomas Veerman }
13032e2caf59SThomas Veerman #else /* !notdef */
13042e2caf59SThomas Veerman if (DEBUG(DIR)) {
13052e2caf59SThomas Veerman fprintf(debug_file, " Looking for \"%s\" ...\n", name);
13062e2caf59SThomas Veerman }
13072e2caf59SThomas Veerman
13082e2caf59SThomas Veerman bigmisses += 1;
13092e2caf59SThomas Veerman entry = Hash_FindEntry(&mtimes, name);
13102e2caf59SThomas Veerman if (entry != NULL) {
13112e2caf59SThomas Veerman if (DEBUG(DIR)) {
13122e2caf59SThomas Veerman fprintf(debug_file, " got it (in mtime cache)\n");
13132e2caf59SThomas Veerman }
13142e2caf59SThomas Veerman return(bmake_strdup(name));
13152e2caf59SThomas Veerman } else if (stat(name, &stb) == 0) {
13162e2caf59SThomas Veerman if (stb.st_mtime == 0)
13172e2caf59SThomas Veerman stb.st_mtime = 1;
13182e2caf59SThomas Veerman entry = Hash_CreateEntry(&mtimes, name, NULL);
13192e2caf59SThomas Veerman if (DEBUG(DIR)) {
13202e2caf59SThomas Veerman fprintf(debug_file, " Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
13212e2caf59SThomas Veerman name);
13222e2caf59SThomas Veerman }
13232e2caf59SThomas Veerman Hash_SetTimeValue(entry, stb.st_mtime);
13242e2caf59SThomas Veerman return (bmake_strdup(name));
13252e2caf59SThomas Veerman } else {
13262e2caf59SThomas Veerman if (DEBUG(DIR)) {
13272e2caf59SThomas Veerman fprintf(debug_file, " failed. Returning NULL\n");
13282e2caf59SThomas Veerman }
13292e2caf59SThomas Veerman return NULL;
13302e2caf59SThomas Veerman }
13312e2caf59SThomas Veerman #endif /* notdef */
13322e2caf59SThomas Veerman }
13332e2caf59SThomas Veerman
13342e2caf59SThomas Veerman
13352e2caf59SThomas Veerman /*-
13362e2caf59SThomas Veerman *-----------------------------------------------------------------------
13372e2caf59SThomas Veerman * Dir_FindHereOrAbove --
13382e2caf59SThomas Veerman * search for a path starting at a given directory and then working
13392e2caf59SThomas Veerman * our way up towards the root.
13402e2caf59SThomas Veerman *
13412e2caf59SThomas Veerman * Input:
13422e2caf59SThomas Veerman * here starting directory
13432e2caf59SThomas Veerman * search_path the path we are looking for
13442e2caf59SThomas Veerman * result the result of a successful search is placed here
13452e2caf59SThomas Veerman * rlen the length of the result buffer
13462e2caf59SThomas Veerman * (typically MAXPATHLEN + 1)
13472e2caf59SThomas Veerman *
13482e2caf59SThomas Veerman * Results:
13492e2caf59SThomas Veerman * 0 on failure, 1 on success [in which case the found path is put
13502e2caf59SThomas Veerman * in the result buffer].
13512e2caf59SThomas Veerman *
13522e2caf59SThomas Veerman * Side Effects:
13532e2caf59SThomas Veerman *-----------------------------------------------------------------------
13542e2caf59SThomas Veerman */
13552e2caf59SThomas Veerman int
Dir_FindHereOrAbove(char * here,char * search_path,char * result,int rlen)13562e2caf59SThomas Veerman Dir_FindHereOrAbove(char *here, char *search_path, char *result, int rlen) {
13572e2caf59SThomas Veerman
13582e2caf59SThomas Veerman struct stat st;
13592e2caf59SThomas Veerman char dirbase[MAXPATHLEN + 1], *db_end;
13602e2caf59SThomas Veerman char try[MAXPATHLEN + 1], *try_end;
13612e2caf59SThomas Veerman
13622e2caf59SThomas Veerman /* copy out our starting point */
13632e2caf59SThomas Veerman snprintf(dirbase, sizeof(dirbase), "%s", here);
13642e2caf59SThomas Veerman db_end = dirbase + strlen(dirbase);
13652e2caf59SThomas Veerman
13662e2caf59SThomas Veerman /* loop until we determine a result */
13672e2caf59SThomas Veerman while (1) {
13682e2caf59SThomas Veerman
13692e2caf59SThomas Veerman /* try and stat(2) it ... */
13702e2caf59SThomas Veerman snprintf(try, sizeof(try), "%s/%s", dirbase, search_path);
13712e2caf59SThomas Veerman if (stat(try, &st) != -1) {
13722e2caf59SThomas Veerman /*
13732e2caf59SThomas Veerman * success! if we found a file, chop off
13742e2caf59SThomas Veerman * the filename so we return a directory.
13752e2caf59SThomas Veerman */
13762e2caf59SThomas Veerman if ((st.st_mode & S_IFMT) != S_IFDIR) {
13772e2caf59SThomas Veerman try_end = try + strlen(try);
13782e2caf59SThomas Veerman while (try_end > try && *try_end != '/')
13792e2caf59SThomas Veerman try_end--;
13802e2caf59SThomas Veerman if (try_end > try)
13812e2caf59SThomas Veerman *try_end = 0; /* chop! */
13822e2caf59SThomas Veerman }
13832e2caf59SThomas Veerman
13842e2caf59SThomas Veerman /*
13852e2caf59SThomas Veerman * done!
13862e2caf59SThomas Veerman */
13872e2caf59SThomas Veerman snprintf(result, rlen, "%s", try);
13882e2caf59SThomas Veerman return(1);
13892e2caf59SThomas Veerman }
13902e2caf59SThomas Veerman
13912e2caf59SThomas Veerman /*
13922e2caf59SThomas Veerman * nope, we didn't find it. if we used up dirbase we've
13932e2caf59SThomas Veerman * reached the root and failed.
13942e2caf59SThomas Veerman */
13952e2caf59SThomas Veerman if (db_end == dirbase)
13962e2caf59SThomas Veerman break; /* failed! */
13972e2caf59SThomas Veerman
13982e2caf59SThomas Veerman /*
13992e2caf59SThomas Veerman * truncate dirbase from the end to move up a dir
14002e2caf59SThomas Veerman */
14012e2caf59SThomas Veerman while (db_end > dirbase && *db_end != '/')
14022e2caf59SThomas Veerman db_end--;
14032e2caf59SThomas Veerman *db_end = 0; /* chop! */
14042e2caf59SThomas Veerman
14052e2caf59SThomas Veerman } /* while (1) */
14062e2caf59SThomas Veerman
14072e2caf59SThomas Veerman /*
14082e2caf59SThomas Veerman * we failed...
14092e2caf59SThomas Veerman */
14102e2caf59SThomas Veerman return(0);
14112e2caf59SThomas Veerman }
14122e2caf59SThomas Veerman
14132e2caf59SThomas Veerman /*-
14142e2caf59SThomas Veerman *-----------------------------------------------------------------------
14152e2caf59SThomas Veerman * Dir_MTime --
14162e2caf59SThomas Veerman * Find the modification time of the file described by gn along the
14172e2caf59SThomas Veerman * search path dirSearchPath.
14182e2caf59SThomas Veerman *
14192e2caf59SThomas Veerman * Input:
14202e2caf59SThomas Veerman * gn the file whose modification time is desired
14212e2caf59SThomas Veerman *
14222e2caf59SThomas Veerman * Results:
14232e2caf59SThomas Veerman * The modification time or 0 if it doesn't exist
14242e2caf59SThomas Veerman *
14252e2caf59SThomas Veerman * Side Effects:
14262e2caf59SThomas Veerman * The modification time is placed in the node's mtime slot.
14272e2caf59SThomas Veerman * If the node didn't have a path entry before, and Dir_FindFile
14282e2caf59SThomas Veerman * found one for it, the full name is placed in the path slot.
14292e2caf59SThomas Veerman *-----------------------------------------------------------------------
14302e2caf59SThomas Veerman */
14312e2caf59SThomas Veerman int
Dir_MTime(GNode * gn,Boolean recheck)14322e2caf59SThomas Veerman Dir_MTime(GNode *gn, Boolean recheck)
14332e2caf59SThomas Veerman {
14342e2caf59SThomas Veerman char *fullName; /* the full pathname of name */
14352e2caf59SThomas Veerman struct stat stb; /* buffer for finding the mod time */
14362e2caf59SThomas Veerman Hash_Entry *entry;
14372e2caf59SThomas Veerman
14382e2caf59SThomas Veerman if (gn->type & OP_ARCHV) {
14392e2caf59SThomas Veerman return Arch_MTime(gn);
14402e2caf59SThomas Veerman } else if (gn->type & OP_PHONY) {
14412e2caf59SThomas Veerman gn->mtime = 0;
14422e2caf59SThomas Veerman return 0;
14432e2caf59SThomas Veerman } else if (gn->path == NULL) {
14442e2caf59SThomas Veerman if (gn->type & OP_NOPATH)
14452e2caf59SThomas Veerman fullName = NULL;
14462e2caf59SThomas Veerman else {
14472e2caf59SThomas Veerman fullName = Dir_FindFile(gn->name, Suff_FindPath(gn));
14482e2caf59SThomas Veerman if (fullName == NULL && gn->flags & FROM_DEPEND &&
14492e2caf59SThomas Veerman !Lst_IsEmpty(gn->iParents)) {
14502e2caf59SThomas Veerman char *cp;
14512e2caf59SThomas Veerman
14522e2caf59SThomas Veerman cp = strrchr(gn->name, '/');
14532e2caf59SThomas Veerman if (cp) {
14542e2caf59SThomas Veerman /*
14552e2caf59SThomas Veerman * This is an implied source, and it may have moved,
14562e2caf59SThomas Veerman * see if we can find it via the current .PATH
14572e2caf59SThomas Veerman */
14582e2caf59SThomas Veerman cp++;
14592e2caf59SThomas Veerman
14602e2caf59SThomas Veerman fullName = Dir_FindFile(cp, Suff_FindPath(gn));
14612e2caf59SThomas Veerman if (fullName) {
14622e2caf59SThomas Veerman /*
14632e2caf59SThomas Veerman * Put the found file in gn->path
14642e2caf59SThomas Veerman * so that we give that to the compiler.
14652e2caf59SThomas Veerman */
14662e2caf59SThomas Veerman gn->path = bmake_strdup(fullName);
1467*84d9c625SLionel Sambuc if (!Job_RunTarget(".STALE", gn->fname))
14682e2caf59SThomas Veerman fprintf(stdout,
1469*84d9c625SLionel Sambuc "%s: %s, %d: ignoring stale %s for %s, "
1470*84d9c625SLionel Sambuc "found %s\n", progname, gn->fname, gn->lineno,
1471*84d9c625SLionel Sambuc makeDependfile, gn->name, fullName);
14722e2caf59SThomas Veerman }
14732e2caf59SThomas Veerman }
14742e2caf59SThomas Veerman }
14752e2caf59SThomas Veerman if (DEBUG(DIR))
14762e2caf59SThomas Veerman fprintf(debug_file, "Found '%s' as '%s'\n",
14772e2caf59SThomas Veerman gn->name, fullName ? fullName : "(not found)" );
14782e2caf59SThomas Veerman }
14792e2caf59SThomas Veerman } else {
14802e2caf59SThomas Veerman fullName = gn->path;
14812e2caf59SThomas Veerman }
14822e2caf59SThomas Veerman
14832e2caf59SThomas Veerman if (fullName == NULL) {
14842e2caf59SThomas Veerman fullName = bmake_strdup(gn->name);
14852e2caf59SThomas Veerman }
14862e2caf59SThomas Veerman
14872e2caf59SThomas Veerman if (!recheck)
14882e2caf59SThomas Veerman entry = Hash_FindEntry(&mtimes, fullName);
14892e2caf59SThomas Veerman else
14902e2caf59SThomas Veerman entry = NULL;
14912e2caf59SThomas Veerman if (entry != NULL) {
14922e2caf59SThomas Veerman if (DEBUG(DIR)) {
14932e2caf59SThomas Veerman fprintf(debug_file, "Using cached time %s for %s\n",
14942e2caf59SThomas Veerman Targ_FmtTime(Hash_GetTimeValue(entry)), fullName);
14952e2caf59SThomas Veerman }
14962e2caf59SThomas Veerman stb.st_mtime = Hash_GetTimeValue(entry);
14972e2caf59SThomas Veerman } else if (stat(fullName, &stb) < 0) {
14982e2caf59SThomas Veerman if (gn->type & OP_MEMBER) {
14992e2caf59SThomas Veerman if (fullName != gn->path)
15002e2caf59SThomas Veerman free(fullName);
15012e2caf59SThomas Veerman return Arch_MemMTime(gn);
15022e2caf59SThomas Veerman } else {
15032e2caf59SThomas Veerman stb.st_mtime = 0;
15042e2caf59SThomas Veerman }
15052e2caf59SThomas Veerman } else {
15062e2caf59SThomas Veerman if (stb.st_mtime == 0) {
15072e2caf59SThomas Veerman /*
15082e2caf59SThomas Veerman * 0 handled specially by the code, if the time is really 0,
15092e2caf59SThomas Veerman * return something else instead
15102e2caf59SThomas Veerman */
15112e2caf59SThomas Veerman stb.st_mtime = 1;
15122e2caf59SThomas Veerman }
15132e2caf59SThomas Veerman entry = Hash_CreateEntry(&mtimes, fullName, NULL);
15142e2caf59SThomas Veerman Hash_SetTimeValue(entry, stb.st_mtime);
15152e2caf59SThomas Veerman }
15162e2caf59SThomas Veerman
15172e2caf59SThomas Veerman if (fullName && gn->path == NULL) {
15182e2caf59SThomas Veerman gn->path = fullName;
15192e2caf59SThomas Veerman }
15202e2caf59SThomas Veerman
15212e2caf59SThomas Veerman gn->mtime = stb.st_mtime;
15222e2caf59SThomas Veerman return (gn->mtime);
15232e2caf59SThomas Veerman }
15242e2caf59SThomas Veerman
15252e2caf59SThomas Veerman /*-
15262e2caf59SThomas Veerman *-----------------------------------------------------------------------
15272e2caf59SThomas Veerman * Dir_AddDir --
15282e2caf59SThomas Veerman * Add the given name to the end of the given path. The order of
15292e2caf59SThomas Veerman * the arguments is backwards so ParseDoDependency can do a
15302e2caf59SThomas Veerman * Lst_ForEach of its list of paths...
15312e2caf59SThomas Veerman *
15322e2caf59SThomas Veerman * Input:
15332e2caf59SThomas Veerman * path the path to which the directory should be
15342e2caf59SThomas Veerman * added
15352e2caf59SThomas Veerman * name the name of the directory to add
15362e2caf59SThomas Veerman *
15372e2caf59SThomas Veerman * Results:
15382e2caf59SThomas Veerman * none
15392e2caf59SThomas Veerman *
15402e2caf59SThomas Veerman * Side Effects:
15412e2caf59SThomas Veerman * A structure is added to the list and the directory is
15422e2caf59SThomas Veerman * read and hashed.
15432e2caf59SThomas Veerman *-----------------------------------------------------------------------
15442e2caf59SThomas Veerman */
15452e2caf59SThomas Veerman Path *
Dir_AddDir(Lst path,const char * name)15462e2caf59SThomas Veerman Dir_AddDir(Lst path, const char *name)
15472e2caf59SThomas Veerman {
15482e2caf59SThomas Veerman LstNode ln = NULL; /* node in case Path structure is found */
15492e2caf59SThomas Veerman Path *p = NULL; /* pointer to new Path structure */
15502e2caf59SThomas Veerman DIR *d; /* for reading directory */
15512e2caf59SThomas Veerman struct dirent *dp; /* entry in directory */
15522e2caf59SThomas Veerman
15532e2caf59SThomas Veerman if (strcmp(name, ".DOTLAST") == 0) {
15542e2caf59SThomas Veerman ln = Lst_Find(path, name, DirFindName);
15552e2caf59SThomas Veerman if (ln != NULL)
15562e2caf59SThomas Veerman return (Path *)Lst_Datum(ln);
15572e2caf59SThomas Veerman else {
15582e2caf59SThomas Veerman dotLast->refCount += 1;
15592e2caf59SThomas Veerman (void)Lst_AtFront(path, dotLast);
15602e2caf59SThomas Veerman }
15612e2caf59SThomas Veerman }
15622e2caf59SThomas Veerman
15632e2caf59SThomas Veerman if (path)
15642e2caf59SThomas Veerman ln = Lst_Find(openDirectories, name, DirFindName);
15652e2caf59SThomas Veerman if (ln != NULL) {
15662e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
15672e2caf59SThomas Veerman if (path && Lst_Member(path, p) == NULL) {
15682e2caf59SThomas Veerman p->refCount += 1;
15692e2caf59SThomas Veerman (void)Lst_AtEnd(path, p);
15702e2caf59SThomas Veerman }
15712e2caf59SThomas Veerman } else {
15722e2caf59SThomas Veerman if (DEBUG(DIR)) {
15732e2caf59SThomas Veerman fprintf(debug_file, "Caching %s ...", name);
15742e2caf59SThomas Veerman }
15752e2caf59SThomas Veerman
15762e2caf59SThomas Veerman if ((d = opendir(name)) != NULL) {
15772e2caf59SThomas Veerman p = bmake_malloc(sizeof(Path));
15782e2caf59SThomas Veerman p->name = bmake_strdup(name);
15792e2caf59SThomas Veerman p->hits = 0;
15802e2caf59SThomas Veerman p->refCount = 1;
15812e2caf59SThomas Veerman Hash_InitTable(&p->files, -1);
15822e2caf59SThomas Veerman
15832e2caf59SThomas Veerman while ((dp = readdir(d)) != NULL) {
15842e2caf59SThomas Veerman #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
15852e2caf59SThomas Veerman /*
15862e2caf59SThomas Veerman * The sun directory library doesn't check for a 0 inode
15872e2caf59SThomas Veerman * (0-inode slots just take up space), so we have to do
15882e2caf59SThomas Veerman * it ourselves.
15892e2caf59SThomas Veerman */
15902e2caf59SThomas Veerman if (dp->d_fileno == 0) {
15912e2caf59SThomas Veerman continue;
15922e2caf59SThomas Veerman }
15932e2caf59SThomas Veerman #endif /* sun && d_ino */
15942e2caf59SThomas Veerman (void)Hash_CreateEntry(&p->files, dp->d_name, NULL);
15952e2caf59SThomas Veerman }
15962e2caf59SThomas Veerman (void)closedir(d);
15972e2caf59SThomas Veerman (void)Lst_AtEnd(openDirectories, p);
15982e2caf59SThomas Veerman if (path != NULL)
15992e2caf59SThomas Veerman (void)Lst_AtEnd(path, p);
16002e2caf59SThomas Veerman }
16012e2caf59SThomas Veerman if (DEBUG(DIR)) {
16022e2caf59SThomas Veerman fprintf(debug_file, "done\n");
16032e2caf59SThomas Veerman }
16042e2caf59SThomas Veerman }
16052e2caf59SThomas Veerman return p;
16062e2caf59SThomas Veerman }
16072e2caf59SThomas Veerman
16082e2caf59SThomas Veerman /*-
16092e2caf59SThomas Veerman *-----------------------------------------------------------------------
16102e2caf59SThomas Veerman * Dir_CopyDir --
16112e2caf59SThomas Veerman * Callback function for duplicating a search path via Lst_Duplicate.
16122e2caf59SThomas Veerman * Ups the reference count for the directory.
16132e2caf59SThomas Veerman *
16142e2caf59SThomas Veerman * Results:
16152e2caf59SThomas Veerman * Returns the Path it was given.
16162e2caf59SThomas Veerman *
16172e2caf59SThomas Veerman * Side Effects:
16182e2caf59SThomas Veerman * The refCount of the path is incremented.
16192e2caf59SThomas Veerman *
16202e2caf59SThomas Veerman *-----------------------------------------------------------------------
16212e2caf59SThomas Veerman */
16222e2caf59SThomas Veerman void *
Dir_CopyDir(void * p)16232e2caf59SThomas Veerman Dir_CopyDir(void *p)
16242e2caf59SThomas Veerman {
16252e2caf59SThomas Veerman ((Path *)p)->refCount += 1;
16262e2caf59SThomas Veerman
16272e2caf59SThomas Veerman return (p);
16282e2caf59SThomas Veerman }
16292e2caf59SThomas Veerman
16302e2caf59SThomas Veerman /*-
16312e2caf59SThomas Veerman *-----------------------------------------------------------------------
16322e2caf59SThomas Veerman * Dir_MakeFlags --
16332e2caf59SThomas Veerman * Make a string by taking all the directories in the given search
16342e2caf59SThomas Veerman * path and preceding them by the given flag. Used by the suffix
16352e2caf59SThomas Veerman * module to create variables for compilers based on suffix search
16362e2caf59SThomas Veerman * paths.
16372e2caf59SThomas Veerman *
16382e2caf59SThomas Veerman * Input:
16392e2caf59SThomas Veerman * flag flag which should precede each directory
16402e2caf59SThomas Veerman * path list of directories
16412e2caf59SThomas Veerman *
16422e2caf59SThomas Veerman * Results:
16432e2caf59SThomas Veerman * The string mentioned above. Note that there is no space between
16442e2caf59SThomas Veerman * the given flag and each directory. The empty string is returned if
16452e2caf59SThomas Veerman * Things don't go well.
16462e2caf59SThomas Veerman *
16472e2caf59SThomas Veerman * Side Effects:
16482e2caf59SThomas Veerman * None
16492e2caf59SThomas Veerman *-----------------------------------------------------------------------
16502e2caf59SThomas Veerman */
16512e2caf59SThomas Veerman char *
Dir_MakeFlags(const char * flag,Lst path)16522e2caf59SThomas Veerman Dir_MakeFlags(const char *flag, Lst path)
16532e2caf59SThomas Veerman {
16542e2caf59SThomas Veerman char *str; /* the string which will be returned */
16552e2caf59SThomas Veerman char *s1, *s2;/* the current directory preceded by 'flag' */
16562e2caf59SThomas Veerman LstNode ln; /* the node of the current directory */
16572e2caf59SThomas Veerman Path *p; /* the structure describing the current directory */
16582e2caf59SThomas Veerman
16592e2caf59SThomas Veerman str = bmake_strdup("");
16602e2caf59SThomas Veerman
16612e2caf59SThomas Veerman if (Lst_Open(path) == SUCCESS) {
16622e2caf59SThomas Veerman while ((ln = Lst_Next(path)) != NULL) {
16632e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
16642e2caf59SThomas Veerman s2 = str_concat(flag, p->name, 0);
16652e2caf59SThomas Veerman str = str_concat(s1 = str, s2, STR_ADDSPACE);
16662e2caf59SThomas Veerman free(s1);
16672e2caf59SThomas Veerman free(s2);
16682e2caf59SThomas Veerman }
16692e2caf59SThomas Veerman Lst_Close(path);
16702e2caf59SThomas Veerman }
16712e2caf59SThomas Veerman
16722e2caf59SThomas Veerman return (str);
16732e2caf59SThomas Veerman }
16742e2caf59SThomas Veerman
16752e2caf59SThomas Veerman /*-
16762e2caf59SThomas Veerman *-----------------------------------------------------------------------
16772e2caf59SThomas Veerman * Dir_Destroy --
16782e2caf59SThomas Veerman * Nuke a directory descriptor, if possible. Callback procedure
16792e2caf59SThomas Veerman * for the suffixes module when destroying a search path.
16802e2caf59SThomas Veerman *
16812e2caf59SThomas Veerman * Input:
16822e2caf59SThomas Veerman * pp The directory descriptor to nuke
16832e2caf59SThomas Veerman *
16842e2caf59SThomas Veerman * Results:
16852e2caf59SThomas Veerman * None.
16862e2caf59SThomas Veerman *
16872e2caf59SThomas Veerman * Side Effects:
16882e2caf59SThomas Veerman * If no other path references this directory (refCount == 0),
16892e2caf59SThomas Veerman * the Path and all its data are freed.
16902e2caf59SThomas Veerman *
16912e2caf59SThomas Veerman *-----------------------------------------------------------------------
16922e2caf59SThomas Veerman */
16932e2caf59SThomas Veerman void
Dir_Destroy(void * pp)16942e2caf59SThomas Veerman Dir_Destroy(void *pp)
16952e2caf59SThomas Veerman {
16962e2caf59SThomas Veerman Path *p = (Path *)pp;
16972e2caf59SThomas Veerman p->refCount -= 1;
16982e2caf59SThomas Veerman
16992e2caf59SThomas Veerman if (p->refCount == 0) {
17002e2caf59SThomas Veerman LstNode ln;
17012e2caf59SThomas Veerman
17022e2caf59SThomas Veerman ln = Lst_Member(openDirectories, p);
17032e2caf59SThomas Veerman (void)Lst_Remove(openDirectories, ln);
17042e2caf59SThomas Veerman
17052e2caf59SThomas Veerman Hash_DeleteTable(&p->files);
17062e2caf59SThomas Veerman free(p->name);
17072e2caf59SThomas Veerman free(p);
17082e2caf59SThomas Veerman }
17092e2caf59SThomas Veerman }
17102e2caf59SThomas Veerman
17112e2caf59SThomas Veerman /*-
17122e2caf59SThomas Veerman *-----------------------------------------------------------------------
17132e2caf59SThomas Veerman * Dir_ClearPath --
17142e2caf59SThomas Veerman * Clear out all elements of the given search path. This is different
17152e2caf59SThomas Veerman * from destroying the list, notice.
17162e2caf59SThomas Veerman *
17172e2caf59SThomas Veerman * Input:
17182e2caf59SThomas Veerman * path Path to clear
17192e2caf59SThomas Veerman *
17202e2caf59SThomas Veerman * Results:
17212e2caf59SThomas Veerman * None.
17222e2caf59SThomas Veerman *
17232e2caf59SThomas Veerman * Side Effects:
17242e2caf59SThomas Veerman * The path is set to the empty list.
17252e2caf59SThomas Veerman *
17262e2caf59SThomas Veerman *-----------------------------------------------------------------------
17272e2caf59SThomas Veerman */
17282e2caf59SThomas Veerman void
Dir_ClearPath(Lst path)17292e2caf59SThomas Veerman Dir_ClearPath(Lst path)
17302e2caf59SThomas Veerman {
17312e2caf59SThomas Veerman Path *p;
17322e2caf59SThomas Veerman while (!Lst_IsEmpty(path)) {
17332e2caf59SThomas Veerman p = (Path *)Lst_DeQueue(path);
17342e2caf59SThomas Veerman Dir_Destroy(p);
17352e2caf59SThomas Veerman }
17362e2caf59SThomas Veerman }
17372e2caf59SThomas Veerman
17382e2caf59SThomas Veerman
17392e2caf59SThomas Veerman /*-
17402e2caf59SThomas Veerman *-----------------------------------------------------------------------
17412e2caf59SThomas Veerman * Dir_Concat --
17422e2caf59SThomas Veerman * Concatenate two paths, adding the second to the end of the first.
17432e2caf59SThomas Veerman * Makes sure to avoid duplicates.
17442e2caf59SThomas Veerman *
17452e2caf59SThomas Veerman * Input:
17462e2caf59SThomas Veerman * path1 Dest
17472e2caf59SThomas Veerman * path2 Source
17482e2caf59SThomas Veerman *
17492e2caf59SThomas Veerman * Results:
17502e2caf59SThomas Veerman * None
17512e2caf59SThomas Veerman *
17522e2caf59SThomas Veerman * Side Effects:
17532e2caf59SThomas Veerman * Reference counts for added dirs are upped.
17542e2caf59SThomas Veerman *
17552e2caf59SThomas Veerman *-----------------------------------------------------------------------
17562e2caf59SThomas Veerman */
17572e2caf59SThomas Veerman void
Dir_Concat(Lst path1,Lst path2)17582e2caf59SThomas Veerman Dir_Concat(Lst path1, Lst path2)
17592e2caf59SThomas Veerman {
17602e2caf59SThomas Veerman LstNode ln;
17612e2caf59SThomas Veerman Path *p;
17622e2caf59SThomas Veerman
17632e2caf59SThomas Veerman for (ln = Lst_First(path2); ln != NULL; ln = Lst_Succ(ln)) {
17642e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
17652e2caf59SThomas Veerman if (Lst_Member(path1, p) == NULL) {
17662e2caf59SThomas Veerman p->refCount += 1;
17672e2caf59SThomas Veerman (void)Lst_AtEnd(path1, p);
17682e2caf59SThomas Veerman }
17692e2caf59SThomas Veerman }
17702e2caf59SThomas Veerman }
17712e2caf59SThomas Veerman
17722e2caf59SThomas Veerman /********** DEBUG INFO **********/
17732e2caf59SThomas Veerman void
Dir_PrintDirectories(void)17742e2caf59SThomas Veerman Dir_PrintDirectories(void)
17752e2caf59SThomas Veerman {
17762e2caf59SThomas Veerman LstNode ln;
17772e2caf59SThomas Veerman Path *p;
17782e2caf59SThomas Veerman
17792e2caf59SThomas Veerman fprintf(debug_file, "#*** Directory Cache:\n");
17802e2caf59SThomas Veerman fprintf(debug_file, "# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
17812e2caf59SThomas Veerman hits, misses, nearmisses, bigmisses,
17822e2caf59SThomas Veerman (hits+bigmisses+nearmisses ?
17832e2caf59SThomas Veerman hits * 100 / (hits + bigmisses + nearmisses) : 0));
17842e2caf59SThomas Veerman fprintf(debug_file, "# %-20s referenced\thits\n", "directory");
17852e2caf59SThomas Veerman if (Lst_Open(openDirectories) == SUCCESS) {
17862e2caf59SThomas Veerman while ((ln = Lst_Next(openDirectories)) != NULL) {
17872e2caf59SThomas Veerman p = (Path *)Lst_Datum(ln);
17882e2caf59SThomas Veerman fprintf(debug_file, "# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
17892e2caf59SThomas Veerman }
17902e2caf59SThomas Veerman Lst_Close(openDirectories);
17912e2caf59SThomas Veerman }
17922e2caf59SThomas Veerman }
17932e2caf59SThomas Veerman
17942e2caf59SThomas Veerman static int
DirPrintDir(void * p,void * dummy)17952e2caf59SThomas Veerman DirPrintDir(void *p, void *dummy)
17962e2caf59SThomas Veerman {
17972e2caf59SThomas Veerman fprintf(debug_file, "%s ", ((Path *)p)->name);
17982e2caf59SThomas Veerman return (dummy ? 0 : 0);
17992e2caf59SThomas Veerman }
18002e2caf59SThomas Veerman
18012e2caf59SThomas Veerman void
Dir_PrintPath(Lst path)18022e2caf59SThomas Veerman Dir_PrintPath(Lst path)
18032e2caf59SThomas Veerman {
18042e2caf59SThomas Veerman Lst_ForEach(path, DirPrintDir, NULL);
18052e2caf59SThomas Veerman }
1806