Ignore:
Timestamp:
Nov 26, 2002, 10:24:54 PM (23 years ago)
Author:
bird
Message:

Import of RELENG_4_7_0_RELEASE

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FREEBSD/src/kmk/dir.c

    r10 r24  
    11/*
    2  * Copyright (c) 1988, 1989, 1990, 1993
    3  *      The Regents of the University of California.  All rights reserved.
     2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
    43 * Copyright (c) 1988, 1989 by Adam de Boor
    54 * Copyright (c) 1989 by Berkeley Softworks
     
    3635 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    3736 * SUCH DAMAGE.
    38  *
    39  * @(#)dir.c    8.2 (Berkeley) 1/2/94
    40  */
    41 
    42 #include <sys/cdefs.h>
    43 __FBSDID("$FreeBSD: src/usr.bin/make/dir.c,v 1.30 2002/10/09 03:42:10 jmallett Exp $");
     37 */
     38
     39#ifndef lint
     40#if 0
     41static char sccsid[] = "@(#)dir.c       8.2 (Berkeley) 1/2/94";
     42#else
     43static const char rcsid[] =
     44  "$FreeBSD: src/usr.bin/make/dir.c,v 1.10.2.1 2001/02/13 03:13:57 will Exp $";
     45#endif
     46#endif /* not lint */
    4447
    4548/*-
     
    8891#include <stdio.h>
    8992#include <sys/types.h>
     93#include <dirent.h>
    9094#include <sys/stat.h>
    91 #include <dirent.h>
    92 #include <err.h>
    9395#include "make.h"
    9496#include "hash.h"
     
    190192
    191193
    192 static int DirFindName(void *, void *);
    193 static int DirMatchFiles(char *, Path *, Lst);
    194 static void DirExpandCurly(char *, char *, Lst, Lst);
    195 static void DirExpandInt(char *, Lst, Lst);
    196 static int DirPrintWord(void *, void *);
    197 static int DirPrintDir(void *, void *);
     194static int DirFindName __P((ClientData, ClientData));
     195static int DirMatchFiles __P((char *, Path *, Lst));
     196static void DirExpandCurly __P((char *, char *, Lst, Lst));
     197static void DirExpandInt __P((char *, Lst, Lst));
     198static int DirPrintWord __P((ClientData, ClientData));
     199static int DirPrintDir __P((ClientData, ClientData));
    198200
    199201/*-
     
    210212 */
    211213void
    212 Dir_Init (void)
     214Dir_Init ()
    213215{
    214216    dirSearchPath = Lst_Init (FALSE);
     
    247249 */
    248250void
    249 Dir_End(void)
     251Dir_End()
    250252{
    251253    dot->refCount -= 1;
    252     Dir_Destroy((void *) dot);
     254    Dir_Destroy((ClientData) dot);
    253255    Dir_ClearPath(dirSearchPath);
    254256    Lst_Destroy(dirSearchPath, NOFREE);
     
    273275 */
    274276static int
    275 DirFindName (void *p, void *dname)
     277DirFindName (p, dname)
     278    ClientData    p;          /* Current name */
     279    ClientData    dname;      /* Desired name */
    276280{
    277281    return (strcmp (((Path *)p)->name, (char *) dname));
     
    281285 *-----------------------------------------------------------------------
    282286 * Dir_HasWildcards  --
    283  *      See if the given name has any wildcard characters in it.
     287 *      see if the given name has any wildcard characters in it
    284288 *
    285289 * Results:
     
    291295 */
    292296Boolean
    293 Dir_HasWildcards (char *name)
    294 {
    295     char *cp;
    296     int wild = 0, brace = 0, bracket = 0;
     297Dir_HasWildcards (name)
     298    char          *name;        /* name to check */
     299{
     300    register char *cp;
    297301
    298302    for (cp = name; *cp; cp++) {
    299303        switch(*cp) {
    300304        case '{':
    301                 brace++;
    302                 wild = 1;
    303                 break;
    304         case '}':
    305                 brace--;
    306                 break;
    307305        case '[':
    308                 bracket++;
    309                 wild = 1;
    310                 break;
    311         case ']':
    312                 bracket--;
    313                 break;
    314306        case '?':
    315307        case '*':
    316                 wild = 1;
    317                 break;
    318         default:
    319                 break;
    320         }
    321     }
    322     return wild && bracket == 0 && brace == 0;
     308            return (TRUE);
     309        }
     310    }
     311    return (FALSE);
    323312}
    324313
     
    341330 */
    342331static int
    343 DirMatchFiles (char *pattern, Path *p, Lst expansions)
     332DirMatchFiles (pattern, p, expansions)
     333    char          *pattern;     /* Pattern to look for */
     334    Path          *p;           /* Directory to search */
     335    Lst           expansions;   /* Place to store the results */
    344336{
    345337    Hash_Search   search;       /* Index into the directory's table */
     
    378370 *      Note the special case: if after the piece of the curly brace is
    379371 *      done there are no wildcard characters in the result, the result is
    380  *      placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.  The
    381  *      given arguments are the entire word to expand, the first curly
    382  *      brace in the word, the search path, and the list to store the
    383  *      expansions in.
     372 *      placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.
    384373 *
    385374 * Results:
     
    392381 */
    393382static void
    394 DirExpandCurly(char *word, char *brace, Lst path, Lst expansions)
     383DirExpandCurly(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 */
    395388{
    396389    char          *end;         /* Character after the closing brace */
     
    466459                Dir_Expand(file, path, expansions);
    467460                goto next;
    468             default:
    469                 break;
    470461            }
    471462        }
     
    490481 *      Internal expand routine. Passes through the directories in the
    491482 *      path one by one, calling DirMatchFiles for each. NOTE: This still
    492  *      doesn't handle patterns in directories...  Works given a word to
    493  *      expand, a path to look in, and a list to store expansions in.
     483 *      doesn't handle patterns in directories...
    494484 *
    495485 * Results:
     
    502492 */
    503493static void
    504 DirExpandInt(char *word, Lst path, Lst expansions)
     494DirExpandInt(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 */
    505498{
    506499    LstNode       ln;           /* Current node */
     
    508501
    509502    if (Lst_Open(path) == SUCCESS) {
    510         while ((ln = Lst_Next(path)) != NULL) {
     503        while ((ln = Lst_Next(path)) != NILLNODE) {
    511504            p = (Path *)Lst_Datum(ln);
    512505            DirMatchFiles(word, p, expansions);
     
    531524 */
    532525static int
    533 DirPrintWord(void *word, void *dummy __unused)
    534 {
    535     DEBUGF(DIR, ("%s ", (char *) word));
    536 
    537     return (0);
     526DirPrintWord(word, dummy)
     527    ClientData  word;
     528    ClientData  dummy;
     529{
     530    printf("%s ", (char *) word);
     531
     532    return(dummy ? 0 : 0);
    538533}
    539534
     
    546541 * Results:
    547542 *      A list of words consisting of the files which exist along the search
    548  *      path matching the given pattern is placed in expansions.
     543 *      path matching the given pattern.
    549544 *
    550545 * Side Effects:
     
    553548 */
    554549void
    555 Dir_Expand (char *word, Lst path, Lst expansions)
     550Dir_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 */
    556555{
    557556    char          *cp;
    558557
    559     DEBUGF(DIR, ("expanding \"%s\"...", word));
     558    if (DEBUG(DIR)) {
     559        printf("expanding \"%s\"...", word);
     560    }
    560561
    561562    cp = strchr(word, '{');
     
    640641    }
    641642    if (DEBUG(DIR)) {
    642         Lst_ForEach(expansions, DirPrintWord, (void *) 0);
    643         DEBUGF(DIR, ("\n"));
     643        Lst_ForEach(expansions, DirPrintWord, (ClientData) 0);
     644        fputc('\n', stdout);
    644645    }
    645646}
     
    664665 */
    665666char *
    666 Dir_FindFile (char *name, Lst path)
    667 {
    668     char          *p1;      /* pointer into p->name */
    669     char          *p2;      /* pointer into name */
     667Dir_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 */
    670673    LstNode       ln;       /* a list element */
    671     char          *file;    /* the current filename to check */
    672     Path          *p;       /* current path member */
    673     char          *cp;      /* index of first slash, if any */
     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 */
    674677    Boolean       hasSlash; /* true if 'name' contains a / */
    675678    struct stat   stb;      /* Buffer for stat, if necessary */
     
    689692    }
    690693
    691     DEBUGF(DIR, ("Searching for %s...", name));
     694    if (DEBUG(DIR)) {
     695        printf("Searching for %s...", name);
     696    }
    692697    /*
    693698     * No matter what, we always look for the file in the current directory
     
    698703    if ((!hasSlash || (cp - name == 2 && *name == '.')) &&
    699704        (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL)) {
    700             DEBUGF(DIR, ("in '.'\n"));
     705            if (DEBUG(DIR)) {
     706                printf("in '.'\n");
     707            }
    701708            hits += 1;
    702709            dot->hits += 1;
     
    705712
    706713    if (Lst_Open (path) == FAILURE) {
    707         DEBUGF(DIR, ("couldn't open path, file not found\n"));
     714        if (DEBUG(DIR)) {
     715            printf("couldn't open path, file not found\n");
     716        }
    708717        misses += 1;
    709718        return ((char *) NULL);
     
    718727     * we go on to phase two...
    719728     */
    720     while ((ln = Lst_Next (path)) != NULL) {
     729    while ((ln = Lst_Next (path)) != NILLNODE) {
    721730        p = (Path *) Lst_Datum (ln);
    722         DEBUGF(DIR, ("%s...", p->name));
     731        if (DEBUG(DIR)) {
     732            printf("%s...", p->name);
     733        }
    723734        if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
    724             DEBUGF(DIR, ("here..."));
     735            if (DEBUG(DIR)) {
     736                printf("here...");
     737            }
    725738            if (hasSlash) {
    726739                /*
     
    738751                }
    739752                if (p2 >= name || (p1 >= p->name && *p1 != '/')) {
    740                     DEBUGF(DIR, ("component mismatch -- continuing..."));
     753                    if (DEBUG(DIR)) {
     754                        printf("component mismatch -- continuing...");
     755                    }
    741756                    continue;
    742757                }
    743758            }
    744759            file = str_concat (p->name, cp, STR_ADDSLASH);
    745             DEBUGF(DIR, ("returning %s\n", file));
     760            if (DEBUG(DIR)) {
     761                printf("returning %s\n", file);
     762            }
    746763            Lst_Close (path);
    747764            p->hits += 1;
     
    758775            }
    759776            if (*p1 == '\0' && p2 == cp - 1) {
    760                 DEBUGF(DIR, ("must be here but isn't -- returing NULL\n"));
     777                if (DEBUG(DIR)) {
     778                    printf("must be here but isn't -- returing NULL\n");
     779                }
    761780                Lst_Close (path);
    762781                return ((char *) NULL);
     
    778797     */
    779798    if (!hasSlash) {
    780         DEBUGF(DIR, ("failed.\n"));
     799        if (DEBUG(DIR)) {
     800            printf("failed.\n");
     801        }
    781802        misses += 1;
    782803        return ((char *) NULL);
     
    786807        Boolean checkedDot = FALSE;
    787808
    788         DEBUGF(DIR, ("failed. Trying subdirectories..."));
     809        if (DEBUG(DIR)) {
     810            printf("failed. Trying subdirectories...");
     811        }
    789812        (void) Lst_Open (path);
    790         while ((ln = Lst_Next (path)) != NULL) {
     813        while ((ln = Lst_Next (path)) != NILLNODE) {
    791814            p = (Path *) Lst_Datum (ln);
    792815            if (p != dot) {
     
    799822                checkedDot = TRUE;
    800823            }
    801             DEBUGF(DIR, ("checking %s...", file));
     824            if (DEBUG(DIR)) {
     825                printf("checking %s...", file);
     826            }
     827
    802828
    803829            if (stat (file, &stb) == 0) {
    804                 DEBUGF(DIR, ("got it.\n"));
     830                if (DEBUG(DIR)) {
     831                    printf("got it.\n");
     832                }
    805833
    806834                Lst_Close (path);
     
    825853                 * to fetch it again.
    826854                 */
    827                 DEBUGF(DIR, ("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), file));
     855                if (DEBUG(DIR)) {
     856                    printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
     857                            file);
     858                }
    828859                entry = Hash_CreateEntry(&mtimes, (char *) file,
    829860                                         (Boolean *)NULL);
     
    836867        }
    837868
    838         DEBUGF(DIR, ("failed. "));
     869        if (DEBUG(DIR)) {
     870            printf("failed. ");
     871        }
    839872        Lst_Close (path);
    840873
     
    844877             * so no point in proceeding...
    845878             */
    846             DEBUGF(DIR, ("Checked . already, returning NULL\n"));
     879            if (DEBUG(DIR)) {
     880                printf("Checked . already, returning NULL\n");
     881            }
    847882            return(NULL);
    848883        }
     
    873908    bigmisses += 1;
    874909    ln = Lst_Last (path);
    875     if (ln == NULL) {
     910    if (ln == NILLNODE) {
    876911        return ((char *) NULL);
    877912    } else {
     
    885920    }
    886921#else /* !notdef */
    887     DEBUGF(DIR, ("Looking for \"%s\"...", name));
     922    if (DEBUG(DIR)) {
     923        printf("Looking for \"%s\"...", name);
     924    }
    888925
    889926    bigmisses += 1;
    890927    entry = Hash_FindEntry(&mtimes, name);
    891928    if (entry != (Hash_Entry *)NULL) {
    892         DEBUGF(DIR, ("got it (in mtime cache)\n"));
    893         return (estrdup(name));
     929        if (DEBUG(DIR)) {
     930            printf("got it (in mtime cache)\n");
     931        }
     932        return(estrdup(name));
    894933    } else if (stat (name, &stb) == 0) {
    895934        entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL);
    896         DEBUGF(DIR, ("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), name));
     935        if (DEBUG(DIR)) {
     936            printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
     937                    name);
     938        }
    897939        Hash_SetValue(entry, (long)stb.st_mtime);
    898940        return (estrdup (name));
    899941    } else {
    900         DEBUGF(DIR, ("failed. Returning NULL\n"));
     942        if (DEBUG(DIR)) {
     943            printf("failed. Returning NULL\n");
     944        }
    901945        return ((char *)NULL);
    902946    }
     
    920964 */
    921965int
    922 Dir_MTime (GNode *gn)
     966Dir_MTime (gn)
     967    GNode         *gn;        /* the file whose modification time is
     968                               * desired */
    923969{
    924970    char          *fullName;  /* the full pathname of name */
     
    943989         * Only do this once -- the second time folks are checking to
    944990         * see if the file was actually updated, so we need to actually go
    945          * to the filesystem.
     991         * to the file system.
    946992         */
    947         DEBUGF(DIR, ("Using cached time %s for %s\n",
    948                Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName));
     993        if (DEBUG(DIR)) {
     994            printf("Using cached time %s for %s\n",
     995                    Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName);
     996        }
    949997        stb.st_mtime = (time_t)(long)Hash_GetValue(entry);
    950998        Hash_DeleteEntry(&mtimes, entry);
     
    9821030 */
    9831031void
    984 Dir_AddDir (Lst path, char *name)
     1032Dir_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 */
    9851036{
    9861037    LstNode       ln;         /* node in case Path structure is found */
    987     Path          *p;         /* pointer to new Path structure */
     1038    register Path *p;         /* pointer to new Path structure */
    9881039    DIR           *d;         /* for reading directory */
    989     struct dirent *dp;        /* entry in directory */
    990 
    991     ln = Lst_Find (openDirectories, (void *)name, DirFindName);
    992     if (ln != NULL) {
     1040    register struct dirent *dp; /* entry in directory */
     1041
     1042    ln = Lst_Find (openDirectories, (ClientData)name, DirFindName);
     1043    if (ln != NILLNODE) {
    9931044        p = (Path *)Lst_Datum (ln);
    994         if (Lst_Member(path, (void *)p) == NULL) {
     1045        if (Lst_Member(path, (ClientData)p) == NILLNODE) {
    9951046            p->refCount += 1;
    996             (void)Lst_AtEnd (path, (void *)p);
     1047            (void)Lst_AtEnd (path, (ClientData)p);
    9971048        }
    9981049    } else {
    999         DEBUGF(DIR, ("Caching %s...", name));
     1050        if (DEBUG(DIR)) {
     1051            printf("Caching %s...", name);
     1052            fflush(stdout);
     1053        }
    10001054
    10011055        if ((d = opendir (name)) != (DIR *) NULL) {
     
    10071061
    10081062            while ((dp = readdir (d)) != (struct dirent *) NULL) {
    1009 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
     1063#if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
    10101064                /*
    10111065                 * The sun directory library doesn't check for a 0 inode
     
    10221076                 * that order when first going through a directory.  This is
    10231077                 * needed for XFS over NFS filesystems since SGI does not
    1024                  * guarantee that these are the first two entries returned
     1078                 * guarantee that these are * the first two entries returned
    10251079                 * from readdir().
    10261080                 */
     
    10311085            }
    10321086            (void) closedir (d);
    1033             (void)Lst_AtEnd (openDirectories, (void *)p);
    1034             (void)Lst_AtEnd (path, (void *)p);
    1035         }
    1036         DEBUGF(DIR, ("done\n"));
     1087            (void)Lst_AtEnd (openDirectories, (ClientData)p);
     1088            (void)Lst_AtEnd (path, (ClientData)p);
     1089        }
     1090        if (DEBUG(DIR)) {
     1091            printf("done\n");
     1092        }
    10371093    }
    10381094}
     
    10521108 *-----------------------------------------------------------------------
    10531109 */
    1054 void *
    1055 Dir_CopyDir(void *p)
     1110ClientData
     1111Dir_CopyDir(p)
     1112    ClientData p;
    10561113{
    10571114    ((Path *) p)->refCount += 1;
    10581115
    1059     return ((void *)p);
     1116    return ((ClientData)p);
    10601117}
    10611118
     
    10781135 */
    10791136char *
    1080 Dir_MakeFlags (char *flag, Lst path)
     1137Dir_MakeFlags (flag, path)
     1138    char          *flag;  /* flag which should precede each directory */
     1139    Lst           path;   /* list of directories */
    10811140{
    10821141    char          *str;   /* the string which will be returned */
     
    10881147
    10891148    if (Lst_Open (path) == SUCCESS) {
    1090         while ((ln = Lst_Next (path)) != NULL) {
     1149        while ((ln = Lst_Next (path)) != NILLNODE) {
    10911150            p = (Path *) Lst_Datum (ln);
    10921151            tstr = str_concat (flag, p->name, 0);
     
    11151174 */
    11161175void
    1117 Dir_Destroy (void *pp)
     1176Dir_Destroy (pp)
     1177    ClientData    pp;       /* The directory descriptor to nuke */
    11181178{
    11191179    Path          *p = (Path *) pp;
     
    11231183        LstNode ln;
    11241184
    1125         ln = Lst_Member (openDirectories, (void *)p);
     1185        ln = Lst_Member (openDirectories, (ClientData)p);
    11261186        (void) Lst_Remove (openDirectories, ln);
    11271187
    11281188        Hash_DeleteTable (&p->files);
    1129         free(p->name);
    1130         free(p);
     1189        free((Address)p->name);
     1190        free((Address)p);
    11311191    }
    11321192}
     
    11471207 */
    11481208void
    1149 Dir_ClearPath(Lst path)
     1209Dir_ClearPath(path)
     1210    Lst     path;       /* Path to clear */
    11501211{
    11511212    Path    *p;
    11521213    while (!Lst_IsEmpty(path)) {
    11531214        p = (Path *)Lst_DeQueue(path);
    1154         Dir_Destroy((void *) p);
     1215        Dir_Destroy((ClientData) p);
    11551216    }
    11561217}
     
    11721233 */
    11731234void
    1174 Dir_Concat(Lst path1, Lst path2)
     1235Dir_Concat(path1, path2)
     1236    Lst     path1;      /* Dest */
     1237    Lst     path2;      /* Source */
    11751238{
    11761239    LstNode ln;
    11771240    Path    *p;
    11781241
    1179     for (ln = Lst_First(path2); ln != NULL; ln = Lst_Succ(ln)) {
     1242    for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) {
    11801243        p = (Path *)Lst_Datum(ln);
    1181         if (Lst_Member(path1, (void *)p) == NULL) {
     1244        if (Lst_Member(path1, (ClientData)p) == NILLNODE) {
    11821245            p->refCount += 1;
    1183             (void)Lst_AtEnd(path1, (void *)p);
     1246            (void)Lst_AtEnd(path1, (ClientData)p);
    11841247        }
    11851248    }
     
    11881251/********** DEBUG INFO **********/
    11891252void
    1190 Dir_PrintDirectories(void)
     1253Dir_PrintDirectories()
    11911254{
    11921255    LstNode     ln;
     
    12001263    printf ("# %-20s referenced\thits\n", "directory");
    12011264    if (Lst_Open (openDirectories) == SUCCESS) {
    1202         while ((ln = Lst_Next (openDirectories)) != NULL) {
     1265        while ((ln = Lst_Next (openDirectories)) != NILLNODE) {
    12031266            p = (Path *) Lst_Datum (ln);
    12041267            printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
     
    12081271}
    12091272
    1210 static int
    1211 DirPrintDir (void *p, void *dummy __unused)
     1273static int DirPrintDir (p, dummy)
     1274    ClientData  p;
     1275    ClientData  dummy;
    12121276{
    12131277    printf ("%s ", ((Path *) p)->name);
    1214 
    1215     return (0);
     1278    return (dummy ? 0 : 0);
    12161279}
    12171280
    12181281void
    1219 Dir_PrintPath (Lst path)
    1220 {
    1221     Lst_ForEach (path, DirPrintDir, (void *)0);
    1222 }
     1282Dir_PrintPath (path)
     1283    Lst path;
     1284{
     1285    Lst_ForEach (path, DirPrintDir, (ClientData)0);
     1286}
Note: See TracChangeset for help on using the changeset viewer.