~ubuntu-branches/ubuntu/raring/pmake/raring-proposed

« back to all changes in this revision

Viewing changes to dir.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hocevar (Debian packages)
  • Date: 2005-07-07 10:20:56 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050707102056-3nh411zy3wbwuwyr
Tags: 1.111-1
* New upstream snapshot.
* This version properly parses arguments and does not crash when parameters
  are missing (Closes: #287336, #316394).
* debian/control:
  + Set policy to 3.6.2.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      $NetBSD: dir.c,v 1.40 2004/02/03 19:25:29 chuck Exp $   */
 
1
/*      $NetBSD: dir.c,v 1.45 2005/02/16 15:11:52 christos Exp $        */
2
2
 
3
3
/*
4
4
 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
69
69
 * SUCH DAMAGE.
70
70
 */
71
71
 
72
 
#ifdef MAKE_BOOTSTRAP
73
 
static char rcsid[] = "$NetBSD: dir.c,v 1.40 2004/02/03 19:25:29 chuck Exp $";
 
72
#ifndef MAKE_NATIVE
 
73
static char rcsid[] = "$NetBSD: dir.c,v 1.45 2005/02/16 15:11:52 christos Exp $";
74
74
#else
75
75
#include <sys/cdefs.h>
76
76
#ifndef lint
77
77
#if 0
78
78
static char sccsid[] = "@(#)dir.c       8.2 (Berkeley) 1/2/94";
79
79
#else
80
 
__RCSID("$NetBSD: dir.c,v 1.40 2004/02/03 19:25:29 chuck Exp $");
 
80
__RCSID("$NetBSD: dir.c,v 1.45 2005/02/16 15:11:52 christos Exp $");
81
81
#endif
82
82
#endif /* not lint */
83
83
#endif
268
268
 *-----------------------------------------------------------------------
269
269
 */
270
270
void
271
 
Dir_Init (const char *cdname)
 
271
Dir_Init(const char *cdname)
272
272
{
273
 
    dirSearchPath = Lst_Init (FALSE);
274
 
    openDirectories = Lst_Init (FALSE);
 
273
    dirSearchPath = Lst_Init(FALSE);
 
274
    openDirectories = Lst_Init(FALSE);
275
275
    Hash_InitTable(&mtimes, 0);
276
276
 
277
277
    Dir_InitCur(cdname);
278
278
 
279
 
    dotLast = (Path *) emalloc (sizeof (Path));
 
279
    dotLast = emalloc(sizeof(Path));
280
280
    dotLast->refCount = 1;
281
281
    dotLast->hits = 0;
282
282
    dotLast->name = estrdup(".DOTLAST");
283
 
    Hash_InitTable (&dotLast->files, -1);
 
283
    Hash_InitTable(&dotLast->files, -1);
284
284
}
285
285
 
286
286
/*
287
287
 * Called by Dir_Init() and whenever .CURDIR is assigned to.
288
288
 */
289
289
void
290
 
Dir_InitCur (const char *cdname)
 
290
Dir_InitCur(const char *cdname)
291
291
{
292
292
    Path *p;
293
293
    
329
329
        LstNode ln;
330
330
 
331
331
        /* Remove old entry from openDirectories, but do not destroy. */
332
 
        ln = Lst_Member (openDirectories, (ClientData)dot);
333
 
        (void) Lst_Remove (openDirectories, ln);
 
332
        ln = Lst_Member(openDirectories, (ClientData)dot);
 
333
        (void) Lst_Remove(openDirectories, ln);
334
334
    }
335
335
 
336
 
    dot = Dir_AddDir (NULL, ".");
 
336
    dot = Dir_AddDir(NULL, ".");
337
337
 
338
338
    if (dot == NULL) {
339
339
        Error("Cannot open `.' (%s)", strerror(errno));
386
386
 * This is the simplest way to deal with the effect of .DOTLAST.
387
387
 */
388
388
void
389
 
Dir_SetPATH (void)
 
389
Dir_SetPATH(void)
390
390
{
391
391
    LstNode       ln;           /* a list element */
392
392
    Path *p;
394
394
 
395
395
    Var_Delete(".PATH", VAR_GLOBAL);
396
396
    
397
 
    if (Lst_Open (dirSearchPath) == SUCCESS) {
398
 
        if ((ln = Lst_First (dirSearchPath)) != NILLNODE) {
399
 
            p = (Path *) Lst_Datum (ln);
 
397
    if (Lst_Open(dirSearchPath) == SUCCESS) {
 
398
        if ((ln = Lst_First(dirSearchPath)) != NILLNODE) {
 
399
            p = (Path *) Lst_Datum(ln);
400
400
            if (p == dotLast) {
401
401
                hasLastDot = TRUE;
402
402
                Var_Append(".PATH", dotLast->name, VAR_GLOBAL);
410
410
                Var_Append(".PATH", cur->name, VAR_GLOBAL);
411
411
        }
412
412
 
413
 
        while ((ln = Lst_Next (dirSearchPath)) != NILLNODE) {
414
 
            p = (Path *) Lst_Datum (ln);
 
413
        while ((ln = Lst_Next(dirSearchPath)) != NILLNODE) {
 
414
            p = (Path *) Lst_Datum(ln);
415
415
            if (p == dotLast)
416
416
                continue;
417
417
            if (p == dot && hasLastDot)
860
860
 *-----------------------------------------------------------------------
861
861
 */
862
862
static char *
863
 
DirLookup(Path *p, const char *name, const char *cp, Boolean hasSlash)
 
863
DirLookup(Path *p, const char *name __unused, const char *cp, 
 
864
          Boolean hasSlash __unused)
864
865
{
865
866
    char *file;         /* the current filename to check */
866
867
 
868
869
        printf("   %s ...\n", p->name);
869
870
    }
870
871
 
871
 
    if (Hash_FindEntry (&p->files, cp) == (Hash_Entry *)NULL)
 
872
    if (Hash_FindEntry(&p->files, cp) == (Hash_Entry *)NULL)
872
873
        return NULL;
873
874
 
874
 
    file = str_concat (p->name, cp, STR_ADDSLASH);
 
875
    file = str_concat(p->name, cp, STR_ADDSLASH);
875
876
    if (DEBUG(DIR)) {
876
877
        printf("   returning %s\n", file);
877
878
    }
903
904
    char         *file;         /* the current filename to check */
904
905
 
905
906
    if (p != dot) {
906
 
        file = str_concat (p->name, name, STR_ADDSLASH);
 
907
        file = str_concat(p->name, name, STR_ADDSLASH);
907
908
    } else {
908
909
        /*
909
910
         * Checking in dot -- DON'T put a leading ./ on the thing.
972
973
                return NULL;
973
974
        }
974
975
 
975
 
        if (Hash_FindEntry (&p->files, cp) == (Hash_Entry *)NULL) {
 
976
        if (Hash_FindEntry(&p->files, cp) == (Hash_Entry *)NULL) {
976
977
                if (DEBUG(DIR)) {
977
978
                        printf("   must be here but isn't -- returning\n");
978
979
                }
985
986
        if (DEBUG(DIR)) {
986
987
                printf("   returning %s\n", name);
987
988
        }
988
 
        return (estrdup (name));
 
989
        return (estrdup(name));
989
990
}
990
991
 
991
992
/*-
1002
1003
 *-----------------------------------------------------------------------
1003
1004
 */
1004
1005
static char *
1005
 
DirFindDot(Boolean hasSlash, const char *name, const char *cp)
 
1006
DirFindDot(Boolean hasSlash __unused, const char *name, const char *cp)
1006
1007
{
1007
1008
 
1008
 
        if (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL) {
 
1009
        if (Hash_FindEntry(&dot->files, cp) != (Hash_Entry *)NULL) {
1009
1010
            if (DEBUG(DIR)) {
1010
1011
                printf("   in '.'\n");
1011
1012
            }
1012
1013
            hits += 1;
1013
1014
            dot->hits += 1;
1014
 
            return (estrdup (name));
 
1015
            return (estrdup(name));
1015
1016
        }
1016
1017
        if (cur &&
1017
 
            Hash_FindEntry (&cur->files, cp) != (Hash_Entry *)NULL) {
 
1018
            Hash_FindEntry(&cur->files, cp) != (Hash_Entry *)NULL) {
1018
1019
            if (DEBUG(DIR)) {
1019
1020
                printf("   in ${.CURDIR} = %s\n", cur->name);
1020
1021
            }
1021
1022
            hits += 1;
1022
1023
            cur->hits += 1;
1023
 
            return str_concat (cur->name, cp, STR_ADDSLASH);
 
1024
            return str_concat(cur->name, cp, STR_ADDSLASH);
1024
1025
        }
1025
1026
 
1026
1027
        return NULL;
1077
1078
        printf("Searching for %s ...", name);
1078
1079
    }
1079
1080
 
1080
 
    if (Lst_Open (path) == FAILURE) {
 
1081
    if (Lst_Open(path) == FAILURE) {
1081
1082
        if (DEBUG(DIR)) {
1082
1083
            printf("couldn't open path, file not found\n");
1083
1084
        }
1085
1086
        return ((char *) NULL);
1086
1087
    }
1087
1088
 
1088
 
    if ((ln = Lst_First (path)) != NILLNODE) {
1089
 
        p = (Path *) Lst_Datum (ln);
 
1089
    if ((ln = Lst_First(path)) != NILLNODE) {
 
1090
        p = (Path *) Lst_Datum(ln);
1090
1091
        if (p == dotLast) {
1091
1092
            hasLastDot = TRUE;
1092
1093
            if (DEBUG(DIR))
1119
1120
             */
1120
1121
            if (!hasLastDot &&
1121
1122
                        (file = DirFindDot(hasSlash, name, cp)) != NULL) {
1122
 
                    Lst_Close (path);
 
1123
                    Lst_Close(path);
1123
1124
                    return file;
1124
1125
            }
1125
1126
 
1126
 
            while ((ln = Lst_Next (path)) != NILLNODE) {
1127
 
                p = (Path *) Lst_Datum (ln);
 
1127
            while ((ln = Lst_Next(path)) != NILLNODE) {
 
1128
                p = (Path *) Lst_Datum(ln);
1128
1129
                if (p == dotLast)
1129
1130
                    continue;
1130
1131
                if ((file = DirLookup(p, name, cp, hasSlash)) != NULL) {
1131
 
                    Lst_Close (path);
 
1132
                    Lst_Close(path);
1132
1133
                        return file;
1133
1134
                }
1134
1135
            }
1135
1136
 
1136
1137
            if (hasLastDot &&
1137
1138
                        (file = DirFindDot(hasSlash, name, cp)) != NULL) {
1138
 
                    Lst_Close (path);
 
1139
                    Lst_Close(path);
1139
1140
                    return file;
1140
1141
            }
1141
1142
    }
1142
 
    Lst_Close (path);
 
1143
    Lst_Close(path);
1143
1144
 
1144
1145
    /*
1145
1146
     * We didn't find the file on any directory in the search path.
1180
1181
                        return file;
1181
1182
        }
1182
1183
 
1183
 
        (void) Lst_Open (path);
1184
 
        while ((ln = Lst_Next (path)) != NILLNODE) {
1185
 
            p = (Path *) Lst_Datum (ln);
 
1184
        (void) Lst_Open(path);
 
1185
        while ((ln = Lst_Next(path)) != NILLNODE) {
 
1186
            p = (Path *) Lst_Datum(ln);
1186
1187
            if (p == dotLast)
1187
1188
                continue;
1188
1189
            if (p == dot) {
1191
1192
                checkedDot = TRUE;
1192
1193
            }
1193
1194
            if ((file = DirLookupSubdir(p, name)) != NULL) {
1194
 
                Lst_Close (path);
 
1195
                Lst_Close(path);
1195
1196
                return file;
1196
1197
            }
1197
1198
        }
1198
 
        Lst_Close (path);
 
1199
        Lst_Close(path);
1199
1200
 
1200
1201
        if (hasLastDot) {
1201
1202
                if (dot && !checkedDot) {
1236
1237
        if (!hasLastDot && cur && (file = DirLookupAbs(cur, name, cp)) != NULL)
1237
1238
            return *file?file:NULL;
1238
1239
 
1239
 
        (void) Lst_Open (path);
1240
 
        while ((ln = Lst_Next (path)) != NILLNODE) {
1241
 
            p = (Path *) Lst_Datum (ln);
 
1240
        (void) Lst_Open(path);
 
1241
        while ((ln = Lst_Next(path)) != NILLNODE) {
 
1242
            p = (Path *) Lst_Datum(ln);
1242
1243
            if (p == dotLast)
1243
1244
                continue;
1244
1245
            if ((file = DirLookupAbs(p, name, cp)) != NULL) {
1245
 
                Lst_Close (path);
 
1246
                Lst_Close(path);
1246
1247
                return *file?file:NULL;
1247
1248
            }
1248
1249
        }
1249
 
        Lst_Close (path);
 
1250
        Lst_Close(path);
1250
1251
 
1251
1252
        if (hasLastDot && cur && (file = DirLookupAbs(cur, name, cp)) != NULL)
1252
1253
            return *file?file:NULL;
1271
1272
     */
1272
1273
#ifdef notdef
1273
1274
    cp[-1] = '\0';
1274
 
    (void) Dir_AddDir (path, name);
 
1275
    (void) Dir_AddDir(path, name);
1275
1276
    cp[-1] = '/';
1276
1277
 
1277
1278
    bigmisses += 1;
1278
 
    ln = Lst_Last (path);
 
1279
    ln = Lst_Last(path);
1279
1280
    if (ln == NILLNODE) {
1280
1281
        return ((char *) NULL);
1281
1282
    } else {
1282
 
        p = (Path *) Lst_Datum (ln);
 
1283
        p = (Path *) Lst_Datum(ln);
1283
1284
    }
1284
1285
 
1285
 
    if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
1286
 
        return (estrdup (name));
 
1286
    if (Hash_FindEntry(&p->files, cp) != (Hash_Entry *)NULL) {
 
1287
        return (estrdup(name));
1287
1288
    } else {
1288
1289
        return ((char *) NULL);
1289
1290
    }
1306
1307
                    name);
1307
1308
        }
1308
1309
        Hash_SetValue(entry, (long)stb.st_mtime);
1309
 
        return (estrdup (name));
 
1310
        return (estrdup(name));
1310
1311
    } else {
1311
1312
        if (DEBUG(DIR)) {
1312
1313
            printf("   failed. Returning NULL\n");
1421
1422
    Hash_Entry    *entry;
1422
1423
 
1423
1424
    if (gn->type & OP_ARCHV) {
1424
 
        return Arch_MTime (gn);
 
1425
        return Arch_MTime(gn);
1425
1426
    } else if (gn->type & OP_PHONY) {
1426
1427
        gn->mtime = 0;
1427
1428
        return 0;
1429
1430
        if (gn->type & OP_NOPATH)
1430
1431
            fullName = NULL;
1431
1432
        else
1432
 
            fullName = Dir_FindFile (gn->name, dirSearchPath);
 
1433
            fullName = Dir_FindFile(gn->name, Suff_FindPath(gn));
1433
1434
    } else {
1434
1435
        fullName = gn->path;
1435
1436
    }
1455
1456
        if (gn->type & OP_MEMBER) {
1456
1457
            if (fullName != gn->path)
1457
1458
                free(fullName);
1458
 
            return Arch_MemMTime (gn);
 
1459
            return Arch_MemMTime(gn);
1459
1460
        } else {
1460
1461
            stb.st_mtime = 0;
1461
1462
        }
1497
1498
    struct dirent *dp;        /* entry in directory */
1498
1499
 
1499
1500
    if (strcmp(name, ".DOTLAST") == 0) {
1500
 
        ln = Lst_Find (path, (ClientData)UNCONST(name), DirFindName);
 
1501
        ln = Lst_Find(path, (ClientData)UNCONST(name), DirFindName);
1501
1502
        if (ln != NILLNODE)
1502
1503
            return (Path *) Lst_Datum(ln);
1503
1504
        else {
1507
1508
    }
1508
1509
 
1509
1510
    if (path)
1510
 
        ln = Lst_Find (openDirectories, (ClientData)UNCONST(name), DirFindName);
 
1511
        ln = Lst_Find(openDirectories, (ClientData)UNCONST(name), DirFindName);
1511
1512
    if (ln != NILLNODE) {
1512
1513
        p = (Path *)Lst_Datum (ln);
1513
1514
        if (Lst_Member(path, (ClientData)p) == NILLNODE) {
1521
1522
        }
1522
1523
 
1523
1524
        if ((d = opendir (name)) != (DIR *) NULL) {
1524
 
            p = (Path *) emalloc (sizeof (Path));
1525
 
            p->name = estrdup (name);
 
1525
            p = emalloc(sizeof(Path));
 
1526
            p->name = estrdup(name);
1526
1527
            p->hits = 0;
1527
1528
            p->refCount = 1;
1528
 
            Hash_InitTable (&p->files, -1);
 
1529
            Hash_InitTable(&p->files, -1);
1529
1530
 
1530
1531
            while ((dp = readdir (d)) != (struct dirent *) NULL) {
1531
1532
#if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
1603
1604
    LstNode       ln;     /* the node of the current directory */
1604
1605
    Path          *p;     /* the structure describing the current directory */
1605
1606
 
1606
 
    str = estrdup ("");
 
1607
    str = estrdup("");
1607
1608
 
1608
 
    if (Lst_Open (path) == SUCCESS) {
1609
 
        while ((ln = Lst_Next (path)) != NILLNODE) {
1610
 
            p = (Path *) Lst_Datum (ln);
1611
 
            s2 = str_concat (flag, p->name, 0);
1612
 
            str = str_concat (s1 = str, s2, STR_ADDSPACE);
 
1609
    if (Lst_Open(path) == SUCCESS) {
 
1610
        while ((ln = Lst_Next(path)) != NILLNODE) {
 
1611
            p = (Path *) Lst_Datum(ln);
 
1612
            s2 = str_concat(flag, p->name, 0);
 
1613
            str = str_concat(s1 = str, s2, STR_ADDSPACE);
1613
1614
            free(s1);
1614
1615
            free(s2);
1615
1616
        }
1616
 
        Lst_Close (path);
 
1617
        Lst_Close(path);
1617
1618
    }
1618
1619
 
1619
1620
    return (str);
1646
1647
    if (p->refCount == 0) {
1647
1648
        LstNode ln;
1648
1649
 
1649
 
        ln = Lst_Member (openDirectories, (ClientData)p);
1650
 
        (void) Lst_Remove (openDirectories, ln);
 
1650
        ln = Lst_Member(openDirectories, (ClientData)p);
 
1651
        (void) Lst_Remove(openDirectories, ln);
1651
1652
 
1652
 
        Hash_DeleteTable (&p->files);
 
1653
        Hash_DeleteTable(&p->files);
1653
1654
        free((Address)p->name);
1654
1655
        free((Address)p);
1655
1656
    }
1729
1730
              (hits+bigmisses+nearmisses ?
1730
1731
               hits * 100 / (hits + bigmisses + nearmisses) : 0));
1731
1732
    printf ("# %-20s referenced\thits\n", "directory");
1732
 
    if (Lst_Open (openDirectories) == SUCCESS) {
1733
 
        while ((ln = Lst_Next (openDirectories)) != NILLNODE) {
1734
 
            p = (Path *) Lst_Datum (ln);
 
1733
    if (Lst_Open(openDirectories) == SUCCESS) {
 
1734
        while ((ln = Lst_Next(openDirectories)) != NILLNODE) {
 
1735
            p = (Path *) Lst_Datum(ln);
1735
1736
            printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
1736
1737
        }
1737
 
        Lst_Close (openDirectories);
 
1738
        Lst_Close(openDirectories);
1738
1739
    }
1739
1740
}
1740
1741
 
1748
1749
void
1749
1750
Dir_PrintPath(Lst path)
1750
1751
{
1751
 
    Lst_ForEach (path, DirPrintDir, (ClientData)0);
 
1752
    Lst_ForEach(path, DirPrintDir, (ClientData)0);
1752
1753
}