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

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