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

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