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

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

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

CVSweb <webmaster@jp.NetBSD.org>