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

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