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

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