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

Annotation of src/usr.bin/make/arch.c, Revision 1.44

1.44    ! espie       1: /*     $OpenPackages$ */
        !             2: /*     $OpenBSD: arch.c,v 1.16 1999/10/05 22:06:23 espie Exp $ */
1.9       millert     3: /*     $NetBSD: arch.c,v 1.17 1996/11/06 17:58:59 christos Exp $       */
1.1       deraadt     4:
                      5: /*
1.44    ! espie       6:  * Copyright (c) 1999,2000 Marc Espie.
1.38      espie       7:  *
                      8:  * Extensive code changes for the OpenBSD project.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     21:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     22:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     23:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     24:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     25:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     29:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31: /*
1.9       millert    32:  * Copyright (c) 1988, 1989, 1990, 1993
                     33:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt    34:  * Copyright (c) 1989 by Berkeley Softworks
                     35:  * All rights reserved.
                     36:  *
                     37:  * This code is derived from software contributed to Berkeley by
                     38:  * Adam de Boor.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
                     48:  * 3. All advertising materials mentioning features or use of this software
                     49:  *    must display the following acknowledgement:
                     50:  *     This product includes software developed by the University of
                     51:  *     California, Berkeley and its contributors.
                     52:  * 4. Neither the name of the University nor the names of its contributors
                     53:  *    may be used to endorse or promote products derived from this software
                     54:  *    without specific prior written permission.
                     55:  *
                     56:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     57:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     58:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     59:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     60:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     61:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     62:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     63:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     64:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     65:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     66:  * SUCH DAMAGE.
                     67:  */
                     68:
                     69: /*-
                     70:  * arch.c --
                     71:  *     Functions to manipulate libraries, archives and their members.
                     72:  *
                     73:  *     Once again, cacheing/hashing comes into play in the manipulation
                     74:  * of archives. The first time an archive is referenced, all of its members'
                     75:  * headers are read and hashed and the archive closed again. All hashed
                     76:  * archives are kept on a list which is searched each time an archive member
                     77:  * is referenced.
                     78:  *
                     79:  * The interface to this module is:
1.44    ! espie      80:  *     Arch_ParseArchive       Given an archive specification, return a list
        !            81:  *                             of GNode's, one for each member in the spec.
        !            82:  *                             FAILURE is returned if the specification is
        !            83:  *                             invalid for some reason.
        !            84:  *
        !            85:  *     Arch_Touch              Alter the modification time of the archive
        !            86:  *                             member described by the given node to be
        !            87:  *                             the current time.
        !            88:  *
        !            89:  *     Arch_TouchLib           Update the modification time of the library
        !            90:  *                             described by the given node. This is special
        !            91:  *                             because it also updates the modification time
        !            92:  *                             of the library's table of contents.
        !            93:  *
        !            94:  *     Arch_MTime              Find the modification time of a member of
        !            95:  *                             an archive *in the archive*. The time is also
        !            96:  *                             placed in the member's GNode. Returns the
        !            97:  *                             modification time.
        !            98:  *
        !            99:  *     Arch_MemMTime           Find the modification time of a member of
        !           100:  *                             an archive. Called when the member doesn't
        !           101:  *                             already exist. Looks in the archive for the
        !           102:  *                             modification time. Returns the modification
        !           103:  *                             time.
        !           104:  *
        !           105:  *     Arch_FindLib            Search for a library along a path. The
        !           106:  *                             library name in the GNode should be in
        !           107:  *                             -l<name> format.
1.1       deraadt   108:  *
1.44    ! espie     109:  *     Arch_LibOODate          Special function to decide if a library node
        !           110:  *                             is out-of-date.
1.1       deraadt   111:  *
1.44    ! espie     112:  *     Arch_Init               Initialize this module.
1.1       deraadt   113:  *
1.44    ! espie     114:  *     Arch_End                Cleanup this module.
1.1       deraadt   115:  */
                    116:
                    117: #include    <sys/types.h>
                    118: #include    <sys/stat.h>
                    119: #include    <sys/time.h>
                    120: #include    <sys/param.h>
1.44    ! espie     121: #include    <assert.h>
1.1       deraadt   122: #include    <ctype.h>
                    123: #include    <ar.h>
1.3       niklas    124: #include    <utime.h>
1.1       deraadt   125: #include    <stdio.h>
1.44    ! espie     126: #include    <stddef.h>
1.1       deraadt   127: #include    <stdlib.h>
1.9       millert   128: #include    <fcntl.h>
1.1       deraadt   129: #include    "make.h"
1.33      espie     130: #include    "ohash.h"
1.1       deraadt   131: #include    "dir.h"
                    132: #include    "config.h"
                    133:
1.32      espie     134: #ifndef lint
                    135: #if 0
                    136: static char sccsid[] = "@(#)arch.c     8.2 (Berkeley) 1/2/94";
                    137: #else
                    138: UNUSED
1.44    ! espie     139: static char rcsid[] = "$OpenBSD: arch.c,v 1.16 1999/10/05 22:06:23 espie Exp $";
1.32      espie     140: #endif
                    141: #endif /* not lint */
                    142:
1.44    ! espie     143:
1.14      espie     144: #ifdef TARGET_MACHINE
                    145: #undef MACHINE
                    146: #define MACHINE TARGET_MACHINE
                    147: #endif
                    148: #ifdef TARGET_MACHINE_ARCH
                    149: #undef MACHINE_ARCH
                    150: #define MACHINE_ARCH TARGET_MACHINE_ARCH
                    151: #endif
                    152:
1.44    ! espie     153: static struct ohash      archives;   /* Archives we've already examined.  */
1.1       deraadt   154:
1.35      espie     155: typedef struct Arch_ {
1.44    ! espie     156:     struct ohash   members;    /* All the members of this archive, as
        !           157:                               * struct arch_member entries.  */
        !           158:     char         name[1];    /* Archive name.  */
1.1       deraadt   159: } Arch;
                    160:
1.35      espie     161: /* Used to get to ar's field sizes.  */
                    162: static struct ar_hdr *dummy;
1.44    ! espie     163: #define AR_NAME_SIZE           (sizeof(dummy->ar_name))
1.35      espie     164: #define AR_DATE_SIZE           (sizeof(dummy->ar_date))
                    165:
1.44    ! espie     166: /* Each archive member is tied to an arch_member structure,
1.35      espie     167:  * suitable for hashing.  */
                    168: struct arch_member {
                    169:     TIMESTAMP    mtime;        /* Member modification date.  */
1.44    ! espie     170:     char         date[AR_DATE_SIZE+1];
        !           171:                                /* Same, before conversion to numeric value.  */
1.35      espie     172:     char         name[1];      /* Member name.  */
                    173: };
                    174:
1.43      espie     175: static struct ohash_info members_info = {
1.44    ! espie     176:     offsetof(struct arch_member, name), NULL,
        !           177:     hash_alloc, hash_free, element_alloc
1.35      espie     178: };
                    179:
1.43      espie     180: static struct ohash_info arch_info = {
1.44    ! espie     181:     offsetof(Arch, name), NULL, hash_alloc, hash_free, element_alloc
1.38      espie     182: };
                    183:
1.44    ! espie     184:
        !           185:
        !           186: static struct arch_member *new_arch_member(struct ar_hdr *, const char *);
        !           187: static TIMESTAMP mtime_of_member(struct arch_member *);
        !           188: static long field2long(const char *, size_t);
        !           189: static Arch *read_archive(const char *, const char *);
1.35      espie     190:
1.16      espie     191: #ifdef CLEANUP
1.44    ! espie     192: static void ArchFree(Arch *);
1.16      espie     193: #endif
1.44    ! espie     194: static TIMESTAMP ArchMTimeMember(const char *, const char *, Boolean);
        !           195: static FILE *ArchFindMember(const char *, const char *, struct ar_hdr *, const char *);
        !           196: static void ArchTouch(const char *, const char *);
1.7       niklas    197: #if defined(__svr4__) || defined(__SVR4) || \
1.15      pefo      198:     (defined(__OpenBSD__) && defined(__mips__)) || \
1.11      pefo      199:     (defined(__OpenBSD__) && defined(__powerpc))
1.6       briggs    200: #define SVR4ARCHIVES
1.40      espie     201: #endif
                    202:
                    203: #ifdef SVR4ARCHIVES
                    204: struct SVR4namelist {
                    205:     char         *fnametab;  /* Extended name table strings */
                    206:     size_t       fnamesize;  /* Size of the string table */
                    207: };
                    208:
                    209: static const char *svr4list = "Archive list";
                    210:
1.44    ! espie     211: static char *ArchSVR4Entry(struct SVR4namelist *, char *, size_t, FILE *);
1.6       briggs    212: #endif
1.1       deraadt   213:
1.35      espie     214: static struct arch_member *
                    215: new_arch_member(hdr, name)
                    216:     struct ar_hdr *hdr;
                    217:     const char *name;
                    218: {
                    219:     const char *end = NULL;
                    220:     struct arch_member *n;
                    221:
1.43      espie     222:     n = ohash_create_entry(&members_info, name, &end);
1.44    ! espie     223:     /* XXX ar entries are NOT null terminated. */
1.35      espie     224:     memcpy(n->date, &(hdr->ar_date), AR_DATE_SIZE);
                    225:     n->date[AR_DATE_SIZE] = '\0';
                    226:     /* Don't compute mtime before it is needed. */
                    227:     set_out_of_date(n->mtime);
                    228:     return n;
                    229: }
                    230:
                    231: static TIMESTAMP
                    232: mtime_of_member(m)
                    233:     struct arch_member *m;
                    234: {
                    235:     if (is_out_of_date(m->mtime))
1.44    ! espie     236:        grab_date((time_t) strtol(m->date, NULL, 10), m->mtime);
1.35      espie     237:     return m->mtime;
                    238: }
                    239:
1.16      espie     240: #ifdef CLEANUP
1.1       deraadt   241: /*-
                    242:  *-----------------------------------------------------------------------
                    243:  * ArchFree --
                    244:  *     Free memory used by an archive
                    245:  *-----------------------------------------------------------------------
                    246:  */
                    247: static void
1.44    ! espie     248: ArchFree(a)
        !           249:     Arch         *a;
1.1       deraadt   250: {
1.35      espie     251:     struct arch_member *mem;
1.38      espie     252:     unsigned int i;
1.9       millert   253:
                    254:     /* Free memory from hash entries */
1.43      espie     255:     for (mem = ohash_first(&a->members, &i); mem != NULL;
1.44    ! espie     256:        mem = ohash_next(&a->members, &i))
1.35      espie     257:        free(mem);
1.1       deraadt   258:
1.43      espie     259:     ohash_delete(&a->members);
1.27      espie     260:     free(a);
1.1       deraadt   261: }
1.16      espie     262: #endif
1.9       millert   263:
1.1       deraadt   264:
                    265:
                    266: /*-
                    267:  *-----------------------------------------------------------------------
                    268:  * Arch_ParseArchive --
                    269:  *     Parse the archive specification in the given line and find/create
                    270:  *     the nodes for the specified archive members, placing their nodes
                    271:  *     on the given list.
                    272:  *
                    273:  * Results:
                    274:  *     SUCCESS if it was a valid specification. The linePtr is updated
                    275:  *     to point to the first non-space after the archive spec. The
                    276:  *     nodes for the members are placed on the given list.
                    277:  *
                    278:  * Side Effects:
                    279:  *     Some nodes may be created. The given list is extended.
                    280:  *-----------------------------------------------------------------------
                    281:  */
                    282: ReturnStatus
1.31      espie     283: Arch_ParseArchive(linePtr, nodeLst, ctxt)
1.44    ! espie     284:     char           **linePtr;      /* Pointer to start of specification */
        !           285:     Lst            nodeLst;        /* Lst on which to place the nodes */
        !           286:     SymTable       *ctxt;          /* Context in which to expand variables */
1.1       deraadt   287: {
1.44    ! espie     288:     char           *cp;            /* Pointer into line */
        !           289:     GNode          *gn;            /* New node */
        !           290:     char           *libName;       /* Library-part of specification */
        !           291:     char           *memName;       /* Member-part of specification */
1.1       deraadt   292:     char           nameBuf[MAKE_BSIZE]; /* temporary place for node name */
1.44    ! espie     293:     char           saveChar;       /* Ending delimiter of member-name */
        !           294:     Boolean        subLibName;     /* TRUE if libName should have/had
1.1       deraadt   295:                                     * variable substitution performed on it */
                    296:
                    297:     libName = *linePtr;
1.9       millert   298:
1.1       deraadt   299:     subLibName = FALSE;
                    300:
1.44    ! espie     301:     for (cp = libName; *cp != '(' && *cp != '\0';) {
1.1       deraadt   302:        if (*cp == '$') {
1.44    ! espie     303:            ReturnStatus result;
1.9       millert   304:
1.44    ! espie     305:            cp += Var_ParseSkip(cp, ctxt, &result);
        !           306:            if (result == FAILURE)
        !           307:                return FAILURE;
        !           308:            subLibName = TRUE;
        !           309:        } else
        !           310:            cp++;
1.1       deraadt   311:     }
                    312:
                    313:     *cp++ = '\0';
1.44    ! espie     314:     if (subLibName)
1.18      espie     315:        libName = Var_Subst(libName, ctxt, TRUE);
1.1       deraadt   316:
                    317:     for (;;) {
1.44    ! espie     318:        /* First skip to the start of the member's name, mark that
1.1       deraadt   319:         * place and skip to the end of it (either white-space or
1.44    ! espie     320:         * a close paren).  */
        !           321:        Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
1.1       deraadt   322:
1.44    ! espie     323:        while (*cp != '\0' && *cp != ')' && isspace(*cp))
1.1       deraadt   324:            cp++;
                    325:        memName = cp;
1.44    ! espie     326:        while (*cp != '\0' && *cp != ')' && !isspace(*cp)) {
1.1       deraadt   327:            if (*cp == '$') {
1.44    ! espie     328:                ReturnStatus result;
        !           329:                cp += Var_ParseSkip(cp, ctxt, &result);
        !           330:                if (result == FAILURE)
        !           331:                    return FAILURE;
        !           332:                doSubst = TRUE;
        !           333:            } else
1.1       deraadt   334:                cp++;
                    335:        }
                    336:
1.44    ! espie     337:        /* If the specification ends without a closing parenthesis,
1.1       deraadt   338:         * chances are there's something wrong (like a missing backslash),
1.44    ! espie     339:         * so it's better to return failure than allow such things to
        !           340:         * happen.  */
1.1       deraadt   341:        if (*cp == '\0') {
                    342:            printf("No closing parenthesis in archive specification\n");
1.44    ! espie     343:            return FAILURE;
1.1       deraadt   344:        }
                    345:
1.44    ! espie     346:        /* If we didn't move anywhere, we must be done.  */
        !           347:        if (cp == memName)
1.1       deraadt   348:            break;
                    349:
                    350:        saveChar = *cp;
                    351:        *cp = '\0';
                    352:
1.44    ! espie     353:        /* XXX: This should be taken care of intelligently by
        !           354:         * SuffExpandChildren, both for the archive and the member portions.  */
        !           355:
        !           356:        /* If member contains variables, try and substitute for them.
1.1       deraadt   357:         * This will slow down archive specs with dynamic sources, of course,
                    358:         * since we'll be (non-)substituting them three times, but them's
                    359:         * the breaks -- we need to do this since SuffExpandChildren calls
                    360:         * us, otherwise we could assume the thing would be taken care of
1.44    ! espie     361:         * later.  */
1.1       deraadt   362:        if (doSubst) {
                    363:            char    *buf;
                    364:            char    *sacrifice;
                    365:            char    *oldMemName = memName;
1.9       millert   366:
1.18      espie     367:            memName = Var_Subst(memName, ctxt, TRUE);
1.1       deraadt   368:
1.44    ! espie     369:            /* Now form an archive spec and recurse to deal with nested
1.1       deraadt   370:             * variables and multi-word variable values.... The results
1.44    ! espie     371:             * are just placed at the end of the nodeLst we're returning.  */
1.1       deraadt   372:            buf = sacrifice = emalloc(strlen(memName)+strlen(libName)+3);
                    373:
                    374:            sprintf(buf, "%s(%s)", libName, memName);
                    375:
                    376:            if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
1.44    ! espie     377:                /* Must contain dynamic sources, so we can't deal with it now.
1.1       deraadt   378:                 * Just create an ARCHV node for the thing and let
1.44    ! espie     379:                 * SuffExpandChildren handle it...  */
        !           380:                gn = Targ_FindNode(buf, NULL, TARG_CREATE);
1.1       deraadt   381:
1.20      espie     382:                if (gn == NULL) {
1.1       deraadt   383:                    free(buf);
1.44    ! espie     384:                    return FAILURE;
1.1       deraadt   385:                } else {
                    386:                    gn->type |= OP_ARCHV;
1.26      espie     387:                    Lst_AtEnd(nodeLst, gn);
1.1       deraadt   388:                }
                    389:            } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
1.44    ! espie     390:                /* Error in nested call -- free buffer and return FAILURE
        !           391:                 * ourselves.  */
1.1       deraadt   392:                free(buf);
1.44    ! espie     393:                return FAILURE;
1.1       deraadt   394:            }
1.44    ! espie     395:            /* Free buffer and continue with our work.  */
1.1       deraadt   396:            free(buf);
                    397:        } else if (Dir_HasWildcards(memName)) {
1.44    ! espie     398:            LIST  members;
1.1       deraadt   399:            char  *member;
                    400:
1.28      espie     401:            Lst_Init(&members);
1.44    ! espie     402:
1.28      espie     403:            Dir_Expand(memName, &dirSearchPath, &members);
                    404:            while ((member = (char *)Lst_DeQueue(&members)) != NULL) {
1.44    ! espie     405:                snprintf(nameBuf, MAKE_BSIZE, "%s(%s)", libName, member);
1.1       deraadt   406:                free(member);
1.44    ! espie     407:                gn = Targ_FindNode(nameBuf, NULL, TARG_CREATE);
        !           408:                /* We've found the node, but have to make sure the rest of
        !           409:                 * the world knows it's an archive member, without having
        !           410:                 * to constantly check for parentheses, so we type the
        !           411:                 * thing with the OP_ARCHV bit before we place it on the
        !           412:                 * end of the provided list.  */
1.1       deraadt   413:                gn->type |= OP_ARCHV;
1.26      espie     414:                Lst_AtEnd(nodeLst, gn);
1.1       deraadt   415:            }
1.44    ! espie     416:        } else {
        !           417:            snprintf(nameBuf, MAKE_BSIZE, "%s(%s)", libName, memName);
        !           418:            gn = Targ_FindNode(nameBuf, NULL, TARG_CREATE);
        !           419:            /* We've found the node, but have to make sure the rest of the
        !           420:             * world knows it's an archive member, without having to
        !           421:             * constantly check for parentheses, so we type the thing with
        !           422:             * the OP_ARCHV bit before we place it on the end of the
        !           423:             * provided list.  */
        !           424:            gn->type |= OP_ARCHV;
        !           425:            Lst_AtEnd(nodeLst, gn);
1.1       deraadt   426:        }
1.44    ! espie     427:        if (doSubst)
1.1       deraadt   428:            free(memName);
1.9       millert   429:
1.1       deraadt   430:        *cp = saveChar;
                    431:     }
                    432:
1.44    ! espie     433:     /* If substituted libName, free it now, since we need it no longer.  */
        !           434:     if (subLibName)
1.1       deraadt   435:        free(libName);
                    436:
1.44    ! espie     437:     /* We promised the pointer would be set up at the next non-space, so
1.1       deraadt   438:      * we must advance cp there before setting *linePtr... (note that on
1.44    ! espie     439:      * entrance to the loop, cp is guaranteed to point at a ')') */
1.1       deraadt   440:     do {
                    441:        cp++;
1.44    ! espie     442:     } while (*cp != '\0' && isspace(*cp));
1.1       deraadt   443:
                    444:     *linePtr = cp;
1.44    ! espie     445:     return SUCCESS;
1.1       deraadt   446: }
                    447:
1.44    ! espie     448: /* Helper function: ar fields are not null terminated. */
1.41      espie     449: static long
                    450: field2long(field, len)
                    451:     const char *field;
                    452:     size_t len;
                    453: {
                    454:     static char enough[32];
                    455:
                    456:     assert(len < sizeof(enough));
                    457:     memcpy(enough, field, len);
                    458:     enough[len] = '\0';
                    459:     return strtol(enough, NULL, 10);
                    460: }
                    461:
1.38      espie     462: static Arch *
                    463: read_archive(archive, end)
                    464:     const char *archive;
                    465:     const char *end;
1.1       deraadt   466: {
1.44    ! espie     467:     FILE *       arch;       /* Stream to archive */
        !           468:     char         magic[SARMAG];
        !           469:     Arch         *ar;
1.40      espie     470: #ifdef SVR4ARCHIVES
                    471:     struct SVR4namelist list;
                    472:
                    473:     list.fnametab = NULL;
                    474: #endif
1.35      espie     475:
1.38      espie     476:     /* When we encounter an archive for the first time, we read its
                    477:      * whole contents, to place it in the cache.  */
1.35      espie     478:     arch = fopen(archive, "r");
                    479:     if (arch == NULL)
1.38      espie     480:        return NULL;
1.9       millert   481:
1.38      espie     482:     /* Make sure this is an archive we can handle.  */
1.35      espie     483:     if ((fread(magic, SARMAG, 1, arch) != 1) ||
1.41      espie     484:        (strncmp(magic, ARMAG, SARMAG) != 0)) {
1.35      espie     485:            fclose(arch);
1.38      espie     486:            return NULL;
1.1       deraadt   487:     }
                    488:
1.43      espie     489:     ar = ohash_create_entry(&arch_info, archive, &end);
                    490:     ohash_init(&ar->members, 8, &members_info);
1.38      espie     491:
1.41      espie     492:     for (;;) {
1.44    ! espie     493:        size_t          n;
        !           494:        struct ar_hdr   arh;    /* Archive-member header for reading archive */
1.41      espie     495:        off_t           size;   /* Size of archive member */
1.40      espie     496:        char            buffer[MAXPATHLEN+1];
1.44    ! espie     497:        char            *memName;
1.40      espie     498:                                /* Current member name while hashing. */
1.44    ! espie     499:        char            *cp;    /* Useful character pointer */
1.41      espie     500:
1.40      espie     501:        memName = buffer;
1.44    ! espie     502:        n = fread(&arh, 1, sizeof(struct ar_hdr), arch);
1.41      espie     503:
                    504:        /*  Whole archive read ok.  */
                    505:        if (n == 0 && feof(arch)) {
                    506: #ifdef SVR4ARCHIVES
                    507:            efree(list.fnametab);
                    508: #endif
                    509:            fclose(arch);
                    510:            return ar;
                    511:        }
                    512:        if (n < sizeof(struct ar_hdr))
                    513:            break;
1.40      espie     514:
1.41      espie     515:        if (memcmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
                    516:            /* The header is bogus.  */
                    517:            break;
1.1       deraadt   518:        } else {
1.41      espie     519:            /* We need to advance the stream's pointer to the start of the
                    520:             * next header.  Records are padded with newlines to an even-byte
1.44    ! espie     521:             * boundary, so we need to extract the size of the record and
1.41      espie     522:             * round it up during the seek.  */
                    523:            size = (off_t) field2long(arh.ar_size, sizeof(arh.ar_size));
                    524:
                    525:            (void)memcpy(memName, arh.ar_name, AR_NAME_SIZE);
                    526:            /* Find real end of name (strip extranous ' ')  */
1.44    ! espie     527:            for (cp = memName + AR_NAME_SIZE - 1; *cp == ' ';)
1.41      espie     528:                cp--;
1.1       deraadt   529:            cp[1] = '\0';
                    530:
1.6       briggs    531: #ifdef SVR4ARCHIVES
1.41      espie     532:            /* SVR4 names are slash terminated.  Also svr4 extended AR format.
1.6       briggs    533:             */
                    534:            if (memName[0] == '/') {
1.40      espie     535:                /* SVR4 magic mode.  */
                    536:                memName = ArchSVR4Entry(&list, memName, size, arch);
1.44    ! espie     537:                if (memName == NULL)            /* Invalid data */
1.40      espie     538:                    break;
1.44    ! espie     539:                else if (memName == svr4list)   /* List of files entry */
1.6       briggs    540:                    continue;
1.40      espie     541:                /* Got the entry.  */
                    542:                /* XXX this assumes further processing, such as AR_EFMT1,
                    543:                 * also applies to SVR4ARCHIVES.  */
1.6       briggs    544:            }
                    545:            else {
                    546:                if (cp[0] == '/')
                    547:                    cp[0] = '\0';
                    548:            }
1.3       niklas    549: #endif
                    550:
1.1       deraadt   551: #ifdef AR_EFMT1
1.41      espie     552:            /* BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    553:             * first <namelen> bytes of the file.  */
                    554:            if (memcmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
1.1       deraadt   555:                isdigit(memName[sizeof(AR_EFMT1) - 1])) {
                    556:
1.41      espie     557:                int elen = atoi(memName + sizeof(AR_EFMT1)-1);
1.44    ! espie     558:
1.41      espie     559:                if (elen <= 0 || elen > MAXPATHLEN)
                    560:                        break;
                    561:                memName = buffer;
                    562:                if (fread(memName, elen, 1, arch) != 1)
                    563:                        break;
1.1       deraadt   564:                memName[elen] = '\0';
1.41      espie     565:                if (fseek(arch, -elen, SEEK_CUR) != 0)
                    566:                        break;
                    567:                if (DEBUG(ARCH) || DEBUG(MAKE))
1.1       deraadt   568:                    printf("ArchStat: Extended format entry for %s\n", memName);
                    569:            }
                    570: #endif
                    571:
1.43      espie     572:            ohash_insert(&ar->members,
                    573:                ohash_qlookup(&ar->members, memName),
1.35      espie     574:                    new_arch_member(&arh, memName));
1.1       deraadt   575:        }
1.41      espie     576:        if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
                    577:            break;
1.1       deraadt   578:     }
                    579:
1.27      espie     580:     fclose(arch);
1.43      espie     581:     ohash_delete(&ar->members);
1.40      espie     582: #ifdef SVR4ARCHIVES
                    583:     efree(list.fnametab);
                    584: #endif
1.27      espie     585:     free(ar);
1.38      espie     586:     return NULL;
                    587: }
                    588:
                    589: /*-
                    590:  *-----------------------------------------------------------------------
                    591:  * ArchMTimeMember --
                    592:  *     Find the modification time of an archive's member, given the
                    593:  *     path to the archive and the path to the desired member.
                    594:  *
                    595:  * Results:
1.44    ! espie     596:  *     The archive member's modification time, or OUT_OF_DATE if member
        !           597:  *     was not found (convenient, so that missing members are always
1.38      espie     598:  *     out of date).
                    599:  *
                    600:  * Side Effects:
                    601:  *     Cache the whole archive contents if hash is TRUE.
                    602:  *-----------------------------------------------------------------------
                    603:  */
                    604: static TIMESTAMP
                    605: ArchMTimeMember(archive, member, hash)
                    606:     const char   *archive;   /* Path to the archive */
                    607:     const char   *member;    /* Name of member. If it is a path, only the
                    608:                               * last component is used. */
1.44    ! espie     609:     Boolean      hash;       /* TRUE if archive should be hashed if not
        !           610:                               * already so. */
1.38      espie     611: {
1.44    ! espie     612:     FILE *       arch;       /* Stream to archive */
1.38      espie     613:     Arch         *ar;        /* Archive descriptor */
1.44    ! espie     614:     unsigned int  slot;       /* Place of archive in the archives hash */
        !           615:     const char   *end = NULL;
1.38      espie     616:     const char   *cp;
                    617:     TIMESTAMP    result;
                    618:
                    619:     set_out_of_date(result);
                    620:     /* Because of space constraints and similar things, files are archived
                    621:      * using their final path components, not the entire thing, so we need
                    622:      * to point 'member' to the final component, if there is one, to make
                    623:      * the comparisons easier...  */
                    624:     cp = strrchr(member, '/');
                    625:     if (cp != NULL)
                    626:        member = cp + 1;
                    627:
                    628:     /* Try to find archive in cache.  */
1.43      espie     629:     slot = ohash_qlookupi(&archives, archive, &end);
                    630:     ar = ohash_find(&archives, slot);
1.38      espie     631:
                    632:     /* If not found, get it now.  */
                    633:     if (ar == NULL) {
                    634:        if (!hash) {
                    635:            /* Quick path:  no need to hash the whole archive, just use
                    636:             * ArchFindMember to get the member's header and close the stream
                    637:             * again.  */
                    638:            struct ar_hdr       sarh;
                    639:
                    640:            arch = ArchFindMember(archive, member, &sarh, "r");
                    641:
                    642:            if (arch != NULL) {
                    643:                fclose(arch);
                    644:                grab_date( (time_t)strtol(sarh.ar_date, NULL, 10), result);
                    645:            }
                    646:            return result;
                    647:        }
                    648:        ar = read_archive(archive, end);
                    649:        if (ar != NULL)
1.43      espie     650:            ohash_insert(&archives, slot, ar);
1.38      espie     651:     }
1.44    ! espie     652:
1.38      espie     653:     /* If archive was found, get entry we seek.  */
                    654:     if (ar != NULL) {
1.44    ! espie     655:        struct arch_member *he;
1.38      espie     656:        end = NULL;
                    657:
1.43      espie     658:        he = ohash_find(&ar->members, ohash_qlookupi(&ar->members, member, &end));
1.38      espie     659:        if (he != NULL)
                    660:            return mtime_of_member(he);
                    661:        else {
                    662:            if (end - member > AR_NAME_SIZE) {
1.44    ! espie     663:                /* Try truncated name.  */
        !           664:                end = member + AR_NAME_SIZE;
1.43      espie     665:                he = ohash_find(&ar->members,
                    666:                    ohash_qlookupi(&ar->members, member, &end));
1.38      espie     667:                if (he != NULL)
                    668:                    return mtime_of_member(he);
                    669:            }
                    670:        }
                    671:     }
1.35      espie     672:     return result;
1.1       deraadt   673: }
1.6       briggs    674:
                    675: #ifdef SVR4ARCHIVES
                    676: /*-
                    677:  *-----------------------------------------------------------------------
                    678:  * ArchSVR4Entry --
                    679:  *     Parse an SVR4 style entry that begins with a slash.
                    680:  *     If it is "//", then load the table of filenames
                    681:  *     If it is "/<offset>", then try to substitute the long file name
                    682:  *     from offset of a table previously read.
                    683:  *
                    684:  * Results:
1.40      espie     685:  *     svr4list: just read a list of names
1.44    ! espie     686:  *     NULL:     error occured
1.40      espie     687:  *     extended name
1.6       briggs    688:  *
1.40      espie     689:  * Side-effect:
                    690:  *     For a list of names, store the list in l.
1.6       briggs    691:  *-----------------------------------------------------------------------
                    692:  */
1.44    ! espie     693:
1.40      espie     694: static char *
                    695: ArchSVR4Entry(l, name, size, arch)
                    696:        struct SVR4namelist *l;
1.6       briggs    697:        char *name;
                    698:        size_t size;
                    699:        FILE *arch;
                    700: {
1.40      espie     701: #define ARLONGNAMES1 "/"
                    702: #define ARLONGNAMES2 "ARFILENAMES"
1.6       briggs    703:     size_t entry;
                    704:     char *ptr, *eptr;
                    705:
1.40      espie     706:     assert(name[0] == '/');
                    707:     name++;
                    708:     /* First comes a table of archive names, to be used by subsequent calls.  */
                    709:     if (memcmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
                    710:        memcmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
1.6       briggs    711:
1.40      espie     712:        if (l->fnametab != NULL) {
                    713:            if (DEBUG(ARCH))
1.6       briggs    714:                printf("Attempted to redefine an SVR4 name table\n");
1.40      espie     715:            return NULL;
1.6       briggs    716:        }
                    717:
1.40      espie     718:        l->fnametab = emalloc(size);
                    719:        l->fnamesize = size;
1.6       briggs    720:
1.40      espie     721:        if (fread(l->fnametab, size, 1, arch) != 1) {
                    722:            if (DEBUG(ARCH))
1.6       briggs    723:                printf("Reading an SVR4 name table failed\n");
1.40      espie     724:            return NULL;
1.6       briggs    725:        }
1.44    ! espie     726:
1.40      espie     727:        eptr = l->fnametab + size;
                    728:        for (entry = 0, ptr = l->fnametab; ptr < eptr; ptr++)
1.6       briggs    729:            switch (*ptr) {
                    730:            case '/':
                    731:                entry++;
                    732:                *ptr = '\0';
                    733:                break;
                    734:
                    735:            case '\n':
                    736:                break;
                    737:
                    738:            default:
                    739:                break;
                    740:            }
1.40      espie     741:        if (DEBUG(ARCH))
1.44    ! espie     742:            printf("Found svr4 archive name table with %lu entries\n",
        !           743:                        (u_long)entry);
1.40      espie     744:        return (char *)svr4list;
1.6       briggs    745:     }
1.40      espie     746:     /* Then the names themselves are given as offsets in this table.  */
                    747:     if (*name == ' ' || *name == '\0')
                    748:        return NULL;
1.6       briggs    749:
1.40      espie     750:     entry = (size_t) strtol(name, &eptr, 0);
                    751:     if ((*eptr != ' ' && *eptr != '\0') || eptr == name) {
                    752:        if (DEBUG(ARCH))
1.44    ! espie     753:            printf("Could not parse SVR4 name /%s\n", name);
1.40      espie     754:        return NULL;
1.6       briggs    755:     }
1.40      espie     756:     if (entry >= l->fnamesize) {
                    757:        if (DEBUG(ARCH))
1.44    ! espie     758:            printf("SVR4 entry offset /%s is greater than %lu\n",
1.40      espie     759:                   name, (u_long)l->fnamesize);
                    760:        return NULL;
1.6       briggs    761:     }
                    762:
1.40      espie     763:     if (DEBUG(ARCH))
1.44    ! espie     764:        printf("Replaced /%s with %s\n", name, l->fnametab + entry);
1.6       briggs    765:
1.40      espie     766:     return l->fnametab + entry;
1.6       briggs    767: }
                    768: #endif
                    769:
1.1       deraadt   770:
                    771: /*-
                    772:  *-----------------------------------------------------------------------
                    773:  * ArchFindMember --
                    774:  *     Locate a member of an archive, given the path of the archive and
                    775:  *     the path of the desired member. If the archive is to be modified,
                    776:  *     the mode should be "r+", if not, it should be "r".
                    777:  *
                    778:  * Results:
1.42      espie     779:  *     A FILE *, opened for reading and writing, positioned right after
1.44    ! espie     780:  *     the member's header, or NULL if the member was nonexistent.
1.1       deraadt   781:  *
                    782:  * Side Effects:
1.42      espie     783:  *     Fill the struct ar_hdr pointed by arhPtr.
1.1       deraadt   784:  *-----------------------------------------------------------------------
                    785:  */
                    786: static FILE *
1.42      espie     787: ArchFindMember(archive, member, arhPtr, mode)
1.38      espie     788:     const char   *archive;   /* Path to the archive */
                    789:     const char   *member;    /* Name of member. If it is a path, only the
1.1       deraadt   790:                               * last component is used. */
                    791:     struct ar_hdr *arhPtr;    /* Pointer to header structure to be filled in */
1.38      espie     792:     const char   *mode;      /* The mode for opening the stream */
1.1       deraadt   793: {
1.44    ! espie     794:     FILE *       arch;       /* Stream to archive */
1.1       deraadt   795:     char         *cp;        /* Useful character pointer */
                    796:     char         magic[SARMAG];
1.42      espie     797:     size_t       len;
                    798: #ifdef SVR4ARCHIVES
                    799:     struct SVR4namelist list;
                    800:
                    801:     list.fnametab = NULL;
                    802: #endif
                    803:
                    804:     arch = fopen(archive, mode);
                    805:     if (arch == NULL)
                    806:        return NULL;
1.9       millert   807:
1.42      espie     808:     /* Make sure this is an archive we can handle.  */
                    809:     if (fread(magic, SARMAG, 1, arch) != 1 ||
                    810:        strncmp(magic, ARMAG, SARMAG) != 0) {
                    811:            fclose(arch);
                    812:            return NULL;
1.44    ! espie     813:     }
1.1       deraadt   814:
1.42      espie     815:     /* Because of space constraints and similar things, files are archived
1.1       deraadt   816:      * using their final path components, not the entire thing, so we need
                    817:      * to point 'member' to the final component, if there is one, to make
1.42      espie     818:      * the comparisons easier...  */
                    819:     cp = strrchr(member, '/');
                    820:     if (cp != NULL)
1.1       deraadt   821:        member = cp + 1;
1.44    ! espie     822:
1.42      espie     823:     len = strlen(member);
                    824:     if (len >= AR_NAME_SIZE)
                    825:        len = AR_NAME_SIZE;
                    826:
1.44    ! espie     827:     /* Error handling is simpler than for read_archive, since we just
1.42      espie     828:      * look for a given member.  */
                    829:     while (fread(arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
1.44    ! espie     830:        off_t             size;       /* Size of archive member */
        !           831:        char              *memName;
1.42      espie     832:
1.44    ! espie     833:        if (memcmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0)
1.42      espie     834:             /* The header is bogus, so the archive is bad.  */
                    835:             break;
1.44    ! espie     836:
1.42      espie     837:        memName = arhPtr->ar_name;
                    838:        if (memcmp(member, memName, len) == 0) {
                    839:            /* If the member's name doesn't take up the entire 'name' field,
1.44    ! espie     840:             * we have to be careful of matching prefixes. Names are space-
        !           841:             * padded to the right, so if the character in 'name' at the end
        !           842:             * of the matched string is anything but a space, this isn't the
1.42      espie     843:             * member we sought.  */
                    844: #ifdef SVR4ARCHIVES
                    845:            if (len < sizeof(arhPtr->ar_name) && memName[len] == '/')
1.44    ! espie     846:                len++;
1.42      espie     847: #endif
1.44    ! espie     848:            if (len == sizeof(arhPtr->ar_name) ||
        !           849:                memName[len] == ' ') {
1.42      espie     850: #ifdef SVR4ARCHIVES
                    851:                efree(list.fnametab);
                    852: #endif
                    853:                return arch;
                    854:            }
                    855:        }
                    856:
                    857:        size = (off_t) field2long(arhPtr->ar_size, sizeof(arhPtr->ar_size));
1.9       millert   858:
1.42      espie     859: #ifdef SVR4ARCHIVES
                    860:            /* svr4 names are slash terminated. Also svr4 extended AR format.
1.44    ! espie     861:             */
1.42      espie     862:            if (memName[0] == '/') {
                    863:                /* svr4 magic mode.  */
                    864:                memName = ArchSVR4Entry(&list, arhPtr->ar_name, size, arch);
1.44    ! espie     865:                if (memName == NULL)            /* Invalid data */
1.42      espie     866:                    break;
                    867:                else if (memName == svr4list)   /* List of files entry */
                    868:                    continue;
                    869:                /* Got the entry.  */
                    870:                if (strcmp(memName, member) == 0) {
                    871:                    efree(list.fnametab);
                    872:                    return arch;
                    873:                }
1.1       deraadt   874:            }
1.42      espie     875: #endif
                    876:
1.1       deraadt   877: #ifdef AR_EFMT1
1.42      espie     878:        /* BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    879:         * first <namelen> bytes of the file.  */
                    880:        if (memcmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
                    881:            isdigit(memName[sizeof(AR_EFMT1) - 1])) {
                    882:            char          ename[MAXPATHLEN+1];
                    883:
                    884:            int elen = atoi(memName + sizeof(AR_EFMT1)-1);
                    885:
1.44    ! espie     886:            if (elen <= 0 || elen > MAXPATHLEN)
        !           887:                break;
        !           888:            if (fread(ename, elen, 1, arch) != 1)
        !           889:                break;
1.42      espie     890:            if (fseek(arch, -elen, SEEK_CUR) != 0)
1.44    ! espie     891:                break;
1.42      espie     892:            ename[elen] = '\0';
1.44    ! espie     893:            if (DEBUG(ARCH) || DEBUG(MAKE))
1.42      espie     894:                printf("ArchFind: Extended format entry for %s\n", ename);
1.44    ! espie     895:            /* Found as extended name.  */
1.42      espie     896:            if (strcmp(ename, member) == 0) {
                    897: #ifdef SVR4ARCHIVES
                    898:                efree(list.fnametab);
                    899: #endif
                    900:                return arch;
1.1       deraadt   901:                }
1.42      espie     902:        }
1.1       deraadt   903: #endif
1.42      espie     904:        /* This isn't the member we're after, so we need to advance the
                    905:         * stream's pointer to the start of the next header.  */
                    906:        if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
                    907:            break;
1.1       deraadt   908:     }
                    909:
1.42      espie     910:     /* We did not find the member, or we ran into an error while reading
                    911:      * the archive.  */
                    912: #ifdef SVRARCHIVES
                    913:     efree(list.fnametab);
                    914: #endif
                    915:     fclose(arch);
                    916:     return NULL;
1.1       deraadt   917: }
                    918:
1.39      espie     919: static void
                    920: ArchTouch(archive, member)
                    921:     const char   *archive;   /* Path to the archive */
                    922:     const char   *member;    /* Name of member. */
                    923: {
                    924:     FILE *arch;
                    925:     struct ar_hdr arh;
                    926:
                    927:     arch = ArchFindMember(archive, member, &arh, "r+");
                    928:     if (arch != NULL) {
                    929:        snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)
                    930:            timestamp2time_t(now));
1.42      espie     931:        if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) == 0)
                    932:            (void)fwrite(&arh, sizeof(struct ar_hdr), 1, arch);
1.39      espie     933:        fclose(arch);
                    934:     }
                    935: }
                    936:
1.1       deraadt   937: /*-
                    938:  *-----------------------------------------------------------------------
                    939:  * Arch_Touch --
                    940:  *     Touch a member of an archive.
                    941:  *
1.39      espie     942:  * Side Effects:
1.1       deraadt   943:  *     The 'time' field of the member's header is updated.
                    944:  *     The modification time of the entire archive is also changed.
                    945:  *     For a library, this could necessitate the re-ranlib'ing of the
                    946:  *     whole thing.
                    947:  *-----------------------------------------------------------------------
                    948:  */
                    949: void
1.39      espie     950: Arch_Touch(gn)
1.1       deraadt   951:     GNode        *gn;    /* Node of member to touch */
                    952: {
1.39      espie     953:     ArchTouch(Varq_Value(ARCHIVE_INDEX, gn), Varq_Value(MEMBER_INDEX, gn));
1.1       deraadt   954: }
                    955:
                    956: /*-
                    957:  *-----------------------------------------------------------------------
                    958:  * Arch_TouchLib --
                    959:  *     Given a node which represents a library, touch the thing, making
                    960:  *     sure that the table of contents also is touched.
                    961:  *
                    962:  * Side Effects:
1.42      espie     963:  *     The modification time of the library and of the RANLIBMAG
1.1       deraadt   964:  *     member are set to 'now'.
                    965:  *-----------------------------------------------------------------------
                    966:  */
                    967: void
1.39      espie     968: Arch_TouchLib(gn)
1.44    ! espie     969:     GNode          *gn;        /* The node of the library to touch */
1.1       deraadt   970: {
1.3       niklas    971: #ifdef RANLIBMAG
1.39      espie     972:     if (gn->path != NULL) {
1.44    ! espie     973:        ArchTouch(gn->path, RANLIBMAG);
1.37      espie     974:        set_times(gn->path);
1.1       deraadt   975:     }
1.3       niklas    976: #endif
1.1       deraadt   977: }
                    978:
                    979: /*-
                    980:  *-----------------------------------------------------------------------
                    981:  * Arch_MTime --
                    982:  *     Return the modification time of a member of an archive.
                    983:  *
                    984:  * Results:
1.42      espie     985:  *     The modification time.
1.1       deraadt   986:  *
                    987:  * Side Effects:
                    988:  *     The mtime field of the given node is filled in with the value
                    989:  *     returned by the function.
                    990:  *-----------------------------------------------------------------------
                    991:  */
1.34      espie     992: TIMESTAMP
1.35      espie     993: Arch_MTime(gn)
1.1       deraadt   994:     GNode        *gn;        /* Node describing archive member */
                    995: {
1.35      espie     996:     gn->mtime = ArchMTimeMember(Varq_Value(ARCHIVE_INDEX, gn),
1.44    ! espie     997:             Varq_Value(MEMBER_INDEX, gn),
        !           998:             TRUE);
1.1       deraadt   999:
1.34      espie    1000:     return gn->mtime;
1.1       deraadt  1001: }
                   1002:
                   1003: /*-
                   1004:  *-----------------------------------------------------------------------
                   1005:  * Arch_MemMTime --
                   1006:  *     Given a non-existent archive member's node, get its modification
                   1007:  *     time from its archived form, if it exists.
                   1008:  *
1.44    ! espie    1009:  * Results:
        !          1010:  *     The modification time.
        !          1011:  *
        !          1012:  * Side Effects:
        !          1013:  *     The mtime field is filled in.
1.1       deraadt  1014:  *-----------------------------------------------------------------------
                   1015:  */
1.36      espie    1016: TIMESTAMP
1.44    ! espie    1017: Arch_MemMTime(gn)
        !          1018:     GNode        *gn;
1.1       deraadt  1019: {
1.44    ! espie    1020:     LstNode      ln;
1.1       deraadt  1021:
1.37      espie    1022:     for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                   1023:        GNode   *pgn;
                   1024:        char    *nameStart,
                   1025:                *nameEnd;
                   1026:
1.29      espie    1027:        pgn = (GNode *)Lst_Datum(ln);
1.1       deraadt  1028:
                   1029:        if (pgn->type & OP_ARCHV) {
1.37      espie    1030:            /* If the parent is an archive specification and is being made
1.1       deraadt  1031:             * and its member's name matches the name of the node we were
                   1032:             * given, record the modification time of the parent in the
                   1033:             * child. We keep searching its parents in case some other
1.37      espie    1034:             * parent requires this child to exist...  */
                   1035:            if ((nameStart = strchr(pgn->name, '(') ) != NULL) {
1.44    ! espie    1036:                nameStart++;
        !          1037:                nameEnd = strchr(nameStart, ')');
1.14      espie    1038:            } else
1.44    ! espie    1039:                nameEnd = NULL;
1.1       deraadt  1040:
1.14      espie    1041:            if (pgn->make && nameEnd != NULL &&
1.37      espie    1042:                strncmp(nameStart, gn->name, nameEnd - nameStart) == 0 &&
                   1043:                gn->name[nameEnd-nameStart] == '\0')
                   1044:                    gn->mtime = Arch_MTime(pgn);
1.1       deraadt  1045:        } else if (pgn->make) {
1.37      espie    1046:            /* Something which isn't a library depends on the existence of
                   1047:             * this target, so it needs to exist.  */
1.44    ! espie    1048:            set_out_of_date(gn->mtime);
1.1       deraadt  1049:            break;
                   1050:        }
                   1051:     }
1.36      espie    1052:     return gn->mtime;
1.1       deraadt  1053: }
                   1054:
                   1055: /*-
                   1056:  *-----------------------------------------------------------------------
                   1057:  * Arch_FindLib --
1.9       millert  1058:  *     Search for a library along the given search path.
1.1       deraadt  1059:  *
                   1060:  * Side Effects:
                   1061:  *     The node's 'path' field is set to the found path (including the
                   1062:  *     actual file name, not -l...). If the system can handle the -L
                   1063:  *     flag when linking (or we cannot find the library), we assume that
                   1064:  *     the user has placed the .LIBRARIES variable in the final linking
                   1065:  *     command (or the linker will know where to find it) and set the
                   1066:  *     TARGET variable for this node to be the node's name. Otherwise,
                   1067:  *     we set the TARGET variable to be the full path of the library,
                   1068:  *     as returned by Dir_FindFile.
                   1069:  *-----------------------------------------------------------------------
                   1070:  */
                   1071: void
1.44    ! espie    1072: Arch_FindLib(gn, path)
        !          1073:     GNode          *gn;        /* Node of library to find */
        !          1074:     Lst            path;       /* Search path */
1.1       deraadt  1075: {
1.44    ! espie    1076:     char           *libName;   /* file name for archive */
1.1       deraadt  1077:
1.44    ! espie    1078:     libName = emalloc(strlen(gn->name) + 6 - 2);
1.1       deraadt  1079:     sprintf(libName, "lib%s.a", &gn->name[2]);
                   1080:
1.44    ! espie    1081:     gn->path = Dir_FindFile(libName, path);
1.1       deraadt  1082:
1.44    ! espie    1083:     free(libName);
1.1       deraadt  1084:
                   1085: #ifdef LIBRARIES
1.30      espie    1086:     Varq_Set(TARGET_INDEX, gn->name, gn);
1.1       deraadt  1087: #else
1.30      espie    1088:     Varq_Set(TARGET_INDEX, gn->path == NULL ? gn->name : gn->path, gn);
1.3       niklas   1089: #endif /* LIBRARIES */
1.1       deraadt  1090: }
                   1091:
                   1092: /*-
                   1093:  *-----------------------------------------------------------------------
                   1094:  * Arch_LibOODate --
                   1095:  *     Decide if a node with the OP_LIB attribute is out-of-date. Called
                   1096:  *     from Make_OODate to make its life easier.
                   1097:  *
                   1098:  *     There are several ways for a library to be out-of-date that are
                   1099:  *     not available to ordinary files. In addition, there are ways
                   1100:  *     that are open to regular files that are not available to
                   1101:  *     libraries. A library that is only used as a source is never
                   1102:  *     considered out-of-date by itself. This does not preclude the
                   1103:  *     library's modification time from making its parent be out-of-date.
                   1104:  *     A library will be considered out-of-date for any of these reasons,
                   1105:  *     given that it is a target on a dependency line somewhere:
                   1106:  *         Its modification time is less than that of one of its
1.44    ! espie    1107:  *               sources (gn->mtime < gn->cmtime).
1.1       deraadt  1108:  *         Its modification time is greater than the time at which the
1.44    ! espie    1109:  *               make began (i.e. it's been modified in the course
        !          1110:  *               of the make, probably by archiving).
1.1       deraadt  1111:  *         The modification time of one of its sources is greater than
                   1112:  *               the one of its RANLIBMAG member (i.e. its table of contents
1.44    ! espie    1113:  *               is out-of-date). We don't compare of the archive time
1.1       deraadt  1114:  *               vs. TOC time because they can be too close. In my
                   1115:  *               opinion we should not bother with the TOC at all since
                   1116:  *               this is used by 'ar' rules that affect the data contents
                   1117:  *               of the archive, not by ranlib rules, which affect the
1.9       millert  1118:  *               TOC.
1.1       deraadt  1119:  *
                   1120:  * Results:
                   1121:  *     TRUE if the library is out-of-date. FALSE otherwise.
                   1122:  *
                   1123:  * Side Effects:
                   1124:  *     The library will be hashed if it hasn't been already.
                   1125:  *-----------------------------------------------------------------------
                   1126:  */
                   1127: Boolean
1.44    ! espie    1128: Arch_LibOODate(gn)
        !          1129:     GNode        *gn;          /* The library's graph node */
1.1       deraadt  1130: {
1.44    ! espie    1131:     TIMESTAMP    modTimeTOC;   /* mod time of __.SYMDEF */
1.9       millert  1132:
1.44    ! espie    1133:     if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->children))
1.37      espie    1134:        return FALSE;
                   1135:     if (is_before(now, gn->mtime) || is_before(gn->mtime, gn->cmtime) ||
1.44    ! espie    1136:        is_out_of_date(gn->mtime))
1.37      espie    1137:        return TRUE;
1.2       deraadt  1138: #ifdef RANLIBMAG
1.37      espie    1139:     /* non existent libraries are always out-of-date.  */
                   1140:     if (gn->path == NULL)
1.44    ! espie    1141:        return TRUE;
1.37      espie    1142:     modTimeTOC = ArchMTimeMember(gn->path, RANLIBMAG, FALSE);
                   1143:
                   1144:     if (!is_out_of_date(modTimeTOC)) {
                   1145:        if (DEBUG(ARCH) || DEBUG(MAKE))
                   1146:            printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
                   1147:        return is_before(modTimeTOC, gn->cmtime);
                   1148:     }
1.44    ! espie    1149:     /* A library w/o a table of contents is out-of-date.  */
1.37      espie    1150:     if (DEBUG(ARCH) || DEBUG(MAKE))
                   1151:        printf("No t.o.c....");
                   1152:     return TRUE;
1.2       deraadt  1153: #else
1.37      espie    1154:     return FALSE;
1.2       deraadt  1155: #endif
1.1       deraadt  1156: }
                   1157:
                   1158: /*-
                   1159:  *-----------------------------------------------------------------------
                   1160:  * Arch_Init --
                   1161:  *     Initialize things for this module.
                   1162:  *
                   1163:  * Side Effects:
1.38      espie    1164:  *     The 'archives' hash is initialized.
1.1       deraadt  1165:  *-----------------------------------------------------------------------
                   1166:  */
                   1167: void
1.28      espie    1168: Arch_Init()
1.1       deraadt  1169: {
1.43      espie    1170:     ohash_init(&archives, 4, &arch_info);
1.1       deraadt  1171: }
                   1172:
                   1173:
                   1174:
                   1175: /*-
                   1176:  *-----------------------------------------------------------------------
                   1177:  * Arch_End --
                   1178:  *     Cleanup things for this module.
                   1179:  *
                   1180:  * Side Effects:
1.38      espie    1181:  *     The 'archives' hash is freed
1.1       deraadt  1182:  *-----------------------------------------------------------------------
                   1183:  */
                   1184: void
1.44    ! espie    1185: Arch_End()
1.1       deraadt  1186: {
1.16      espie    1187: #ifdef CLEANUP
1.38      espie    1188:     Arch *e;
                   1189:     unsigned int i;
                   1190:
1.44    ! espie    1191:     for (e = ohash_first(&archives, &i); e != NULL;
        !          1192:        e = ohash_next(&archives, &i))
1.38      espie    1193:            ArchFree(e);
1.43      espie    1194:     ohash_delete(&archives);
1.16      espie    1195: #endif
1.9       millert  1196: }
                   1197:
                   1198: /*-
                   1199:  *-----------------------------------------------------------------------
                   1200:  * Arch_IsLib --
                   1201:  *     Check if the node is a library
                   1202:  *
                   1203:  * Results:
                   1204:  *     True or False.
                   1205:  *-----------------------------------------------------------------------
                   1206:  */
1.44    ! espie    1207: Boolean
1.9       millert  1208: Arch_IsLib(gn)
                   1209:     GNode *gn;
                   1210: {
1.44    ! espie    1211:     char buf[SARMAG];
1.9       millert  1212:     int fd;
                   1213:
1.44    ! espie    1214:     if (gn->path == NULL || (fd = open(gn->path, O_RDONLY)) == -1)
1.9       millert  1215:        return FALSE;
                   1216:
1.44    ! espie    1217:     if (read(fd, buf, SARMAG) != SARMAG) {
        !          1218:        (void)close(fd);
1.9       millert  1219:        return FALSE;
                   1220:     }
                   1221:
1.44    ! espie    1222:     (void)close(fd);
1.9       millert  1223:
1.44    ! espie    1224:     return memcmp(buf, ARMAG, SARMAG) == 0;
1.1       deraadt  1225: }