[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.38

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