[BACK]Return to dir.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/dir.c, Revision 1.5

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