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

1.44      espie       1: /*     $OpenPackages$ */
1.70    ! espie       2: /*     $OpenBSD: arch.c,v 1.69 2007/09/17 09:28:36 espie Exp $ */
1.9       millert     3: /*     $NetBSD: arch.c,v 1.17 1996/11/06 17:58:59 christos Exp $       */
1.1       deraadt     4:
                      5: /*
1.44      espie       6:  * Copyright (c) 1999,2000 Marc Espie.
1.38      espie       7:  *
                      8:  * Extensive code changes for the OpenBSD project.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     21:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     22:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     23:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     24:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     25:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     29:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31: /*
1.9       millert    32:  * Copyright (c) 1988, 1989, 1990, 1993
                     33:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt    34:  * Copyright (c) 1989 by Berkeley Softworks
                     35:  * All rights reserved.
                     36:  *
                     37:  * This code is derived from software contributed to Berkeley by
                     38:  * Adam de Boor.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
1.53      millert    48:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    49:  *    may be used to endorse or promote products derived from this software
                     50:  *    without specific prior written permission.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  */
                     64:
1.45      espie      65: /*
1.1       deraadt    66:  *     Once again, cacheing/hashing comes into play in the manipulation
                     67:  * of archives. The first time an archive is referenced, all of its members'
                     68:  * headers are read and hashed and the archive closed again. All hashed
1.69      espie      69:  * archives are kept in a hash (archives) which is searched each time
1.45      espie      70:  * an archive member is referenced.
1.1       deraadt    71:  *
                     72:  */
                     73:
1.46      espie      74: #include <sys/param.h>
1.45      espie      75: #include <ar.h>
                     76: #include <assert.h>
                     77: #include <ctype.h>
                     78: #include <fcntl.h>
                     79: #include <limits.h>
                     80: #include <stddef.h>
                     81: #include <stdio.h>
1.55      espie      82: #include <stdint.h>
1.45      espie      83: #include <stdlib.h>
                     84: #include <string.h>
                     85: #include <unistd.h>
                     86: #include "ohash.h"
                     87: #include "config.h"
                     88: #include "defines.h"
1.70    ! espie      89: #include "buf.h"
1.45      espie      90: #include "dir.h"
1.63      espie      91: #include "direxpand.h"
1.45      espie      92: #include "arch.h"
                     93: #include "var.h"
                     94: #include "targ.h"
                     95: #include "memory.h"
                     96: #include "gnode.h"
                     97: #include "timestamp.h"
                     98: #include "lst.h"
1.44      espie      99:
1.48      espie     100: #ifndef PATH_MAX
                    101: # ifdef MAXPATHLEN
                    102: #  define PATH_MAX (MAXPATHLEN+1)
                    103: # else
                    104: #  define PATH_MAX     1024
                    105: # endif
                    106: #endif
                    107:
1.14      espie     108: #ifdef TARGET_MACHINE
                    109: #undef MACHINE
                    110: #define MACHINE TARGET_MACHINE
                    111: #endif
                    112: #ifdef TARGET_MACHINE_ARCH
                    113: #undef MACHINE_ARCH
                    114: #define MACHINE_ARCH TARGET_MACHINE_ARCH
                    115: #endif
                    116:
1.70    ! espie     117: static struct ohash archives;  /* Archives we've already examined.  */
1.1       deraadt   118:
1.35      espie     119: typedef struct Arch_ {
1.64      espie     120:        struct ohash members;   /* All the members of this archive, as
1.70    ! espie     121:                                 * struct arch_member entries.  */
1.64      espie     122:        char name[1];           /* Archive name. */
1.1       deraadt   123: } Arch;
                    124:
1.35      espie     125: /* Used to get to ar's field sizes.  */
                    126: static struct ar_hdr *dummy;
1.44      espie     127: #define AR_NAME_SIZE           (sizeof(dummy->ar_name))
1.35      espie     128: #define AR_DATE_SIZE           (sizeof(dummy->ar_date))
                    129:
1.44      espie     130: /* Each archive member is tied to an arch_member structure,
1.35      espie     131:  * suitable for hashing.  */
                    132: struct arch_member {
1.70    ! espie     133:        TIMESTAMP mtime;                /* Member modification date.  */
        !           134:        char date[AR_DATE_SIZE+1];      /* Same, before conversion to numeric
        !           135:                                         * value.  */
        !           136:        char name[1];                   /* Member name.  */
1.35      espie     137: };
                    138:
1.43      espie     139: static struct ohash_info members_info = {
1.64      espie     140:        offsetof(struct arch_member, name), NULL,
                    141:        hash_alloc, hash_free, element_alloc
1.35      espie     142: };
                    143:
1.43      espie     144: static struct ohash_info arch_info = {
1.64      espie     145:        offsetof(Arch, name), NULL, hash_alloc, hash_free, element_alloc
1.38      espie     146: };
                    147:
1.44      espie     148:
                    149:
                    150: static struct arch_member *new_arch_member(struct ar_hdr *, const char *);
                    151: static TIMESTAMP mtime_of_member(struct arch_member *);
                    152: static long field2long(const char *, size_t);
                    153: static Arch *read_archive(const char *, const char *);
1.35      espie     154:
1.16      espie     155: #ifdef CLEANUP
1.44      espie     156: static void ArchFree(Arch *);
1.16      espie     157: #endif
1.45      espie     158: static TIMESTAMP ArchMTimeMember(const char *, const char *, bool);
1.44      espie     159: static FILE *ArchFindMember(const char *, const char *, struct ar_hdr *, const char *);
                    160: static void ArchTouch(const char *, const char *);
1.7       niklas    161: #if defined(__svr4__) || defined(__SVR4) || \
1.51      matthieu  162:     (defined(__OpenBSD__) && defined(__ELF__))
1.6       briggs    163: #define SVR4ARCHIVES
1.40      espie     164: #endif
1.70    ! espie     165: static bool parse_archive(Buffer, const char **, Lst, SymTable *);
        !           166: static void add_archive_node(Lst, const char *);
1.40      espie     167:
                    168: #ifdef SVR4ARCHIVES
                    169: struct SVR4namelist {
1.70    ! espie     170:        char *fnametab;         /* Extended name table strings */
        !           171:        size_t fnamesize;       /* Size of the string table */
1.40      espie     172: };
                    173:
                    174: static const char *svr4list = "Archive list";
                    175:
1.54      espie     176: static char *ArchSVR4Entry(struct SVR4namelist *, const char *, size_t, FILE *);
1.6       briggs    177: #endif
1.1       deraadt   178:
1.35      espie     179: static struct arch_member *
1.54      espie     180: new_arch_member(struct ar_hdr *hdr, const char *name)
1.35      espie     181: {
1.64      espie     182:        const char *end = NULL;
                    183:        struct arch_member *n;
1.35      espie     184:
1.64      espie     185:        n = ohash_create_entry(&members_info, name, &end);
                    186:        /* XXX ar entries are NOT null terminated.      */
                    187:        memcpy(n->date, &(hdr->ar_date), AR_DATE_SIZE);
                    188:        n->date[AR_DATE_SIZE] = '\0';
                    189:        /* Don't compute mtime before it is needed. */
                    190:        ts_set_out_of_date(n->mtime);
                    191:        return n;
1.35      espie     192: }
                    193:
                    194: static TIMESTAMP
1.54      espie     195: mtime_of_member(struct arch_member *m)
1.35      espie     196: {
1.64      espie     197:        if (is_out_of_date(m->mtime))
1.69      espie     198:                ts_set_from_time_t((time_t) strtol(m->date, NULL, 10),
1.64      espie     199:                    m->mtime);
                    200:        return m->mtime;
1.35      espie     201: }
                    202:
1.16      espie     203: #ifdef CLEANUP
1.1       deraadt   204: static void
1.54      espie     205: ArchFree(Arch *a)
1.1       deraadt   206: {
1.64      espie     207:        /* Free memory from hash entries */
1.65      espie     208:        free_hash(&a->members);
1.64      espie     209:        free(a);
1.1       deraadt   210: }
1.16      espie     211: #endif
1.9       millert   212:
1.70    ! espie     213: bool
        !           214: Arch_ParseArchive(const char **line, Lst nodes, SymTable *ctxt)
        !           215: {
        !           216:        bool result;
        !           217:        BUFFER expand;
1.1       deraadt   218:
1.70    ! espie     219:        Buf_Init(&expand, MAKE_BSIZE);
        !           220:        result = parse_archive(&expand, line, nodes, ctxt);
        !           221:        Buf_Destroy(&expand);
        !           222:        return result;
        !           223: }
        !           224:
        !           225: static void
        !           226: add_archive_node(Lst nodes, const char *name)
        !           227: {
        !           228:        GNode *gn;
1.1       deraadt   229:
1.70    ! espie     230:        gn = Targ_FindNode(name, TARG_CREATE);
        !           231:        gn->type |= OP_ARCHV;
        !           232:        Lst_AtEnd(nodes, gn);
        !           233: }
1.64      espie     234:
1.70    ! espie     235: static bool
        !           236: parse_archive(Buffer expand, const char **linePtr, Lst nodeLst, SymTable *ctxt)
        !           237: {
        !           238:        const char *cp;         /* Pointer into line */
        !           239:        const char *lib;        /* Library-part of specification */
        !           240:        const char *elib;
        !           241:        const char *member;     /* Member-part of specification */
        !           242:        const char *emember;
        !           243:        bool subst_lib;
1.64      espie     244:
1.70    ! espie     245:        /* figure out the library name part */
        !           246:        lib = *linePtr;
        !           247:        subst_lib = false;
1.64      espie     248:
1.70    ! espie     249:        for (cp = lib; *cp != '(' && *cp != '\0';) {
1.64      espie     250:                if (*cp == '$') {
                    251:                        if (!Var_ParseSkip(&cp, ctxt))
                    252:                                return false;
1.70    ! espie     253:                        subst_lib = true;
1.64      espie     254:                } else
                    255:                        cp++;
                    256:        }
                    257:
1.70    ! espie     258:        elib = cp;
        !           259:        if (subst_lib) {
        !           260:                lib = Var_Substi(lib, elib, ctxt, true);
        !           261:                elib = lib + strlen(lib);
        !           262:        }
1.64      espie     263:
1.70    ! espie     264:        cp++;
        !           265:        /* iterate on members, that may be separated by spaces */
1.64      espie     266:        for (;;) {
                    267:                /* First skip to the start of the member's name, mark that
                    268:                 * place and skip to the end of it (either white-space or
                    269:                 * a close paren).  */
1.70    ! espie     270:                bool subst_member = false;
1.64      espie     271:
                    272:                while (isspace(*cp))
                    273:                        cp++;
1.70    ! espie     274:                member = cp;
1.64      espie     275:                while (*cp != '\0' && *cp != ')' && !isspace(*cp)) {
                    276:                        if (*cp == '$') {
                    277:                                if (!Var_ParseSkip(&cp, ctxt))
                    278:                                        return false;
1.70    ! espie     279:                                subst_member = true;
1.64      espie     280:                        } else
                    281:                                cp++;
                    282:                }
                    283:
                    284:                /* If the specification ends without a closing parenthesis,
                    285:                 * chances are there's something wrong (like a missing
                    286:                 * backslash), so it's better to return failure than allow such
                    287:                 * things to happen.  */
                    288:                if (*cp == '\0') {
                    289:                        printf("No closing parenthesis in archive specification\n");
                    290:                        return false;
                    291:                }
1.1       deraadt   292:
1.64      espie     293:                /* If we didn't move anywhere, we must be done.  */
1.70    ! espie     294:                if (cp == member)
1.64      espie     295:                        break;
1.1       deraadt   296:
1.70    ! espie     297:                emember = cp;
1.1       deraadt   298:
1.64      espie     299:                /* XXX: This should be taken care of intelligently by
                    300:                 * SuffExpandChildren, both for the archive and the member
                    301:                 * portions.  */
                    302:
                    303:                /* If member contains variables, try and substitute for them.
                    304:                 * This will slow down archive specs with dynamic sources, of
                    305:                 * course, since we'll be (non-)substituting them three times,
                    306:                 * but them's the breaks -- we need to do this since
                    307:                 * SuffExpandChildren calls us, otherwise we could assume the
                    308:                 * thing would be taken care of later.  */
1.70    ! espie     309:                if (subst_member) {
        !           310:                        const char *oldMemberName = member;
        !           311:                        const char *result;
1.64      espie     312:
1.70    ! espie     313:                        member = Var_Substi(member, emember, ctxt, true);
1.64      espie     314:
                    315:                        /* Now form an archive spec and recurse to deal with
                    316:                         * nested variables and multi-word variable values....
                    317:                         * The results are just placed at the end of the
                    318:                         * nodeLst we're returning.  */
1.70    ! espie     319:                        Buf_Addi(expand, lib, elib);
        !           320:                        Buf_AddChar(expand, '(');
        !           321:                        Buf_AddString(expand, member);
        !           322:                        Buf_AddChar(expand, ')');
        !           323:                        result = Buf_Retrieve(expand);
        !           324:
        !           325:                        if (strchr(member, '$') &&
        !           326:                            memcmp(member, oldMemberName,
        !           327:                                emember - oldMemberName) == 0) {
1.64      espie     328:                                /* Must contain dynamic sources, so we can't
1.70    ! espie     329:                                 * deal with it now.  let SuffExpandChildren
        !           330:                                 * handle it later  */
        !           331:                                add_archive_node(nodeLst, result);
        !           332:                        } else if (!Arch_ParseArchive(&result, nodeLst, ctxt))
1.64      espie     333:                                return false;
1.70    ! espie     334:                        Buf_Reset(expand);
        !           335:                } else if (Dir_HasWildcardsi(member, emember)) {
        !           336:                        LIST  members;
        !           337:                        char  *m;
1.64      espie     338:
                    339:                        Lst_Init(&members);
                    340:
1.70    ! espie     341:                        Dir_Expandi(member, emember, defaultPath, &members);
        !           342:                        while ((m = (char *)Lst_DeQueue(&members)) != NULL) {
        !           343:                                Buf_Addi(expand, lib, elib);
        !           344:                                Buf_AddChar(expand, '(');
        !           345:                                Buf_AddString(expand, m);
        !           346:                                Buf_AddChar(expand, ')');
        !           347:                                free(m);
        !           348:                                add_archive_node(nodeLst, Buf_Retrieve(expand));
        !           349:                                Buf_Reset(expand);
1.64      espie     350:                        }
1.1       deraadt   351:                } else {
1.70    ! espie     352:                        Buf_Addi(expand, lib, elib);
        !           353:                        Buf_AddChar(expand, '(');
        !           354:                        Buf_Addi(expand, member, emember);
        !           355:                        Buf_AddChar(expand, ')');
        !           356:                        add_archive_node(nodeLst, Buf_Retrieve(expand));
        !           357:                        Buf_Reset(expand);
        !           358:                }
        !           359:                if (subst_member)
        !           360:                        free((char *)member);
        !           361:
        !           362:        }
        !           363:
        !           364:        if (subst_lib)
        !           365:                free((char *)lib);
1.64      espie     366:
                    367:        /* We promised the pointer would be set up at the next non-space, so
                    368:         * we must advance cp there before setting *linePtr... (note that on
                    369:         * entrance to the loop, cp is guaranteed to point at a ')') */
                    370:        do {
                    371:                cp++;
                    372:        } while (isspace(*cp));
1.1       deraadt   373:
1.64      espie     374:        *linePtr = cp;
                    375:        return true;
1.1       deraadt   376: }
                    377:
1.44      espie     378: /* Helper function: ar fields are not null terminated. */
1.41      espie     379: static long
1.54      espie     380: field2long(const char *field, size_t length)
1.41      espie     381: {
1.64      espie     382:        static char enough[32];
1.41      espie     383:
1.64      espie     384:        assert(length < sizeof(enough));
                    385:        memcpy(enough, field, length);
                    386:        enough[length] = '\0';
                    387:        return strtol(enough, NULL, 10);
1.41      espie     388: }
                    389:
1.38      espie     390: static Arch *
1.54      espie     391: read_archive(const char *archive, const char *earchive)
1.1       deraadt   392: {
1.70    ! espie     393:        FILE *arch;       /* Stream to archive */
1.64      espie     394:        char magic[SARMAG];
                    395:        Arch *ar;
1.40      espie     396: #ifdef SVR4ARCHIVES
1.64      espie     397:        struct SVR4namelist list;
1.40      espie     398:
1.64      espie     399:        list.fnametab = NULL;
1.40      espie     400: #endif
1.35      espie     401:
1.64      espie     402:        /* When we encounter an archive for the first time, we read its
                    403:         * whole contents, to place it in the cache.  */
                    404:        arch = fopen(archive, "r");
                    405:        if (arch == NULL)
                    406:                return NULL;
1.9       millert   407:
1.64      espie     408:        /* Make sure this is an archive we can handle.  */
                    409:        if ((fread(magic, SARMAG, 1, arch) != 1) ||
                    410:            (strncmp(magic, ARMAG, SARMAG) != 0)) {
                    411:                fclose(arch);
                    412:                return NULL;
                    413:        }
1.1       deraadt   414:
1.64      espie     415:        ar = ohash_create_entry(&arch_info, archive, &earchive);
                    416:        ohash_init(&ar->members, 8, &members_info);
1.38      espie     417:
1.64      espie     418:        for (;;) {
                    419:                size_t n;
1.70    ! espie     420:                struct ar_hdr arHeader; /* Archive-member header */
        !           421:                off_t size;             /* Size of archive member */
1.64      espie     422:                char buffer[PATH_MAX];
1.70    ! espie     423:                char *memberName;       /* Current member name while hashing. */
1.64      espie     424:                char *cp;
1.41      espie     425:
1.64      espie     426:                memberName = buffer;
                    427:                n = fread(&arHeader, 1, sizeof(struct ar_hdr), arch);
1.41      espie     428:
1.64      espie     429:                /*  Whole archive read ok.  */
                    430:                if (n == 0 && feof(arch)) {
1.41      espie     431: #ifdef SVR4ARCHIVES
1.64      espie     432:                        efree(list.fnametab);
1.41      espie     433: #endif
1.64      espie     434:                        fclose(arch);
                    435:                        return ar;
                    436:                }
                    437:                if (n < sizeof(struct ar_hdr))
                    438:                        break;
1.40      espie     439:
1.69      espie     440:                if (memcmp(arHeader.ar_fmag, ARFMAG, sizeof(arHeader.ar_fmag))
1.64      espie     441:                    != 0) {
1.70    ! espie     442:                        /* header is bogus.  */
1.64      espie     443:                        break;
                    444:                } else {
                    445:                        /* We need to advance the stream's pointer to the start
                    446:                         * of the next header.  Records are padded with
                    447:                         * newlines to an even-byte boundary, so we need to
                    448:                         * extract the size of the record and round it up
                    449:                         * during the seek.  */
1.69      espie     450:                        size = (off_t) field2long(arHeader.ar_size,
1.64      espie     451:                            sizeof(arHeader.ar_size));
                    452:
1.69      espie     453:                        (void)memcpy(memberName, arHeader.ar_name,
1.64      espie     454:                            AR_NAME_SIZE);
                    455:                        /* Find real end of name (strip extranous ' ')  */
                    456:                        for (cp = memberName + AR_NAME_SIZE - 1; *cp == ' ';)
                    457:                                cp--;
                    458:                        cp[1] = '\0';
1.1       deraadt   459:
1.6       briggs    460: #ifdef SVR4ARCHIVES
1.64      espie     461:                        /* SVR4 names are slash terminated.  Also svr4 extended
                    462:                         * AR format.
                    463:                         */
                    464:                        if (memberName[0] == '/') {
                    465:                                /* SVR4 magic mode.  */
1.69      espie     466:                                memberName = ArchSVR4Entry(&list, memberName,
1.64      espie     467:                                    size, arch);
1.70    ! espie     468:                                if (memberName == NULL)
        !           469:                                        /* Invalid data */
1.64      espie     470:                                        break;
                    471:                                else if (memberName == svr4list)
1.70    ! espie     472:                                        /* List of files entry */
1.64      espie     473:                                        continue;
                    474:                                /* Got the entry.  */
                    475:                                /* XXX this assumes further processing, such as
                    476:                                 * AR_EFMT1, also applies to SVR4ARCHIVES.  */
                    477:                        }
                    478:                        else {
                    479:                                if (cp[0] == '/')
                    480:                                        cp[0] = '\0';
                    481:                        }
1.3       niklas    482: #endif
                    483:
1.1       deraadt   484: #ifdef AR_EFMT1
1.64      espie     485:                        /* BSD 4.4 extended AR format: #1/<namelen>, with name
                    486:                         * as the first <namelen> bytes of the file.  */
1.69      espie     487:                        if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1)
1.64      espie     488:                            == 0 && isdigit(memberName[sizeof(AR_EFMT1) - 1])) {
                    489:
1.69      espie     490:                                int elen = atoi(memberName +
1.64      espie     491:                                    sizeof(AR_EFMT1)-1);
                    492:
                    493:                                if (elen <= 0 || elen >= PATH_MAX)
                    494:                                        break;
                    495:                                memberName = buffer;
                    496:                                if (fread(memberName, elen, 1, arch) != 1)
                    497:                                        break;
                    498:                                memberName[elen] = '\0';
                    499:                                if (fseek(arch, -elen, SEEK_CUR) != 0)
                    500:                                        break;
                    501:                                if (DEBUG(ARCH) || DEBUG(MAKE))
1.69      espie     502:                                        printf("ArchStat: Extended format entry for %s\n",
1.64      espie     503:                                            memberName);
                    504:                        }
                    505: #endif
                    506:
                    507:                        ohash_insert(&ar->members,
                    508:                            ohash_qlookup(&ar->members, memberName),
                    509:                                new_arch_member(&arHeader, memberName));
                    510:                }
                    511:                if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
1.41      espie     512:                        break;
1.1       deraadt   513:        }
                    514:
1.64      espie     515:        fclose(arch);
                    516:        ohash_delete(&ar->members);
1.40      espie     517: #ifdef SVR4ARCHIVES
1.64      espie     518:        efree(list.fnametab);
1.40      espie     519: #endif
1.64      espie     520:        free(ar);
                    521:        return NULL;
1.38      espie     522: }
                    523:
                    524: /*-
                    525:  *-----------------------------------------------------------------------
                    526:  * ArchMTimeMember --
                    527:  *     Find the modification time of an archive's member, given the
                    528:  *     path to the archive and the path to the desired member.
                    529:  *
                    530:  * Results:
1.44      espie     531:  *     The archive member's modification time, or OUT_OF_DATE if member
                    532:  *     was not found (convenient, so that missing members are always
1.38      espie     533:  *     out of date).
                    534:  *
                    535:  * Side Effects:
1.45      espie     536:  *     Cache the whole archive contents if hash is true.
1.38      espie     537:  *-----------------------------------------------------------------------
                    538:  */
                    539: static TIMESTAMP
1.54      espie     540: ArchMTimeMember(
1.70    ! espie     541:     const char *archive,       /* Path to the archive */
        !           542:     const char *member,        /* Name of member. If it is a path, only the
        !           543:                                 * last component is used. */
        !           544:     bool hash)                 /* true if archive should be hashed if not
        !           545:                                 * already so. */
1.38      espie     546: {
1.70    ! espie     547:        FILE *arch;             /* Stream to archive */
        !           548:        Arch *ar;               /* Archive descriptor */
1.64      espie     549:        unsigned int slot;      /* Place of archive in the archives hash */
                    550:        const char *end = NULL;
                    551:        const char *cp;
                    552:        TIMESTAMP result;
                    553:
                    554:        ts_set_out_of_date(result);
                    555:        /* Because of space constraints and similar things, files are archived
                    556:         * using their final path components, not the entire thing, so we need
                    557:         * to point 'member' to the final component, if there is one, to make
                    558:         * the comparisons easier...  */
                    559:        cp = strrchr(member, '/');
                    560:        if (cp != NULL)
                    561:                member = cp + 1;
                    562:
                    563:        /* Try to find archive in cache.  */
                    564:        slot = ohash_qlookupi(&archives, archive, &end);
                    565:        ar = ohash_find(&archives, slot);
                    566:
                    567:        /* If not found, get it now.  */
                    568:        if (ar == NULL) {
                    569:                if (!hash) {
1.67      espie     570:                        /* Quick path:  no need to hash the whole archive, just
                    571:                         * use ArchFindMember to get the member's header and
                    572:                         * close the stream again.  */
                    573:                        struct ar_hdr arHeader;
                    574:
                    575:                        arch = ArchFindMember(archive, member, &arHeader, "r");
                    576:
                    577:                        if (arch != NULL) {
                    578:                                fclose(arch);
1.69      espie     579:                                ts_set_from_time_t(
                    580:                                    (time_t)strtol(arHeader.ar_date, NULL, 10),
1.67      espie     581:                                    result);
                    582:                        }
                    583:                        return result;
1.64      espie     584:                }
                    585:                ar = read_archive(archive, end);
                    586:                if (ar != NULL)
                    587:                        ohash_insert(&archives, slot, ar);
                    588:        }
1.38      espie     589:
1.64      espie     590:        /* If archive was found, get entry we seek.  */
                    591:        if (ar != NULL) {
                    592:                struct arch_member *he;
                    593:                end = NULL;
1.38      espie     594:
1.70    ! espie     595:                he = ohash_find(&ar->members, ohash_qlookupi(&ar->members,
        !           596:                    member, &end));
1.38      espie     597:                if (he != NULL)
1.64      espie     598:                        return mtime_of_member(he);
                    599:                else {
                    600:                        if ((size_t)(end - member) > AR_NAME_SIZE) {
                    601:                                /* Try truncated name.  */
                    602:                                end = member + AR_NAME_SIZE;
                    603:                                he = ohash_find(&ar->members,
                    604:                                    ohash_qlookupi(&ar->members, member, &end));
                    605:                                if (he != NULL)
                    606:                                        return mtime_of_member(he);
                    607:                        }
                    608:                }
1.38      espie     609:        }
1.64      espie     610:        return result;
1.1       deraadt   611: }
1.6       briggs    612:
                    613: #ifdef SVR4ARCHIVES
                    614: /*-
                    615:  *-----------------------------------------------------------------------
                    616:  * ArchSVR4Entry --
                    617:  *     Parse an SVR4 style entry that begins with a slash.
                    618:  *     If it is "//", then load the table of filenames
                    619:  *     If it is "/<offset>", then try to substitute the long file name
                    620:  *     from offset of a table previously read.
                    621:  *
                    622:  * Results:
1.40      espie     623:  *     svr4list: just read a list of names
1.50      mpech     624:  *     NULL:     error occurred
1.40      espie     625:  *     extended name
1.6       briggs    626:  *
1.40      espie     627:  * Side-effect:
                    628:  *     For a list of names, store the list in l.
1.6       briggs    629:  *-----------------------------------------------------------------------
                    630:  */
1.44      espie     631:
1.40      espie     632: static char *
1.54      espie     633: ArchSVR4Entry(struct SVR4namelist *l, const char *name, size_t size, FILE *arch)
1.6       briggs    634: {
1.40      espie     635: #define ARLONGNAMES1 "/"
                    636: #define ARLONGNAMES2 "ARFILENAMES"
1.64      espie     637:        size_t entry;
                    638:        char *ptr, *eptr;
1.6       briggs    639:
1.64      espie     640:        assert(name[0] == '/');
                    641:        name++;
                    642:        /* First comes a table of archive names, to be used by subsequent
                    643:         * calls.  */
                    644:        if (memcmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
                    645:            memcmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
                    646:
                    647:                if (l->fnametab != NULL) {
                    648:                        if (DEBUG(ARCH))
                    649:                                printf("Attempted to redefine an SVR4 name table\n");
                    650:                        return NULL;
                    651:                }
1.6       briggs    652:
1.64      espie     653:                l->fnametab = emalloc(size);
                    654:                l->fnamesize = size;
1.6       briggs    655:
1.64      espie     656:                if (fread(l->fnametab, size, 1, arch) != 1) {
                    657:                        if (DEBUG(ARCH))
                    658:                                printf("Reading an SVR4 name table failed\n");
                    659:                        return NULL;
                    660:                }
                    661:
                    662:                eptr = l->fnametab + size;
                    663:                for (entry = 0, ptr = l->fnametab; ptr < eptr; ptr++)
                    664:                        switch (*ptr) {
                    665:                        case '/':
                    666:                                entry++;
                    667:                                *ptr = '\0';
                    668:                                break;
                    669:
                    670:                        case '\n':
                    671:                                break;
                    672:
                    673:                        default:
                    674:                                break;
                    675:                        }
                    676:                if (DEBUG(ARCH))
                    677:                        printf("Found svr4 archive name table with %lu entries\n",
                    678:                            (u_long)entry);
                    679:                return (char *)svr4list;
                    680:        }
                    681:        /* Then the names themselves are given as offsets in this table.  */
                    682:        if (*name == ' ' || *name == '\0')
                    683:                return NULL;
                    684:
                    685:        entry = (size_t) strtol(name, &eptr, 0);
                    686:        if ((*eptr != ' ' && *eptr != '\0') || eptr == name) {
                    687:                if (DEBUG(ARCH))
                    688:                        printf("Could not parse SVR4 name /%s\n", name);
                    689:                return NULL;
                    690:        }
                    691:        if (entry >= l->fnamesize) {
                    692:                if (DEBUG(ARCH))
                    693:                        printf("SVR4 entry offset /%s is greater than %lu\n",
1.70    ! espie     694:                            name, (u_long)l->fnamesize);
1.64      espie     695:                return NULL;
1.6       briggs    696:        }
1.44      espie     697:
1.40      espie     698:        if (DEBUG(ARCH))
1.64      espie     699:                printf("Replaced /%s with %s\n", name, l->fnametab + entry);
1.6       briggs    700:
1.64      espie     701:        return l->fnametab + entry;
1.6       briggs    702: }
                    703: #endif
                    704:
1.1       deraadt   705:
                    706: /*-
                    707:  *-----------------------------------------------------------------------
                    708:  * ArchFindMember --
                    709:  *     Locate a member of an archive, given the path of the archive and
                    710:  *     the path of the desired member. If the archive is to be modified,
                    711:  *     the mode should be "r+", if not, it should be "r".
                    712:  *
                    713:  * Results:
1.42      espie     714:  *     A FILE *, opened for reading and writing, positioned right after
1.44      espie     715:  *     the member's header, or NULL if the member was nonexistent.
1.1       deraadt   716:  *
                    717:  * Side Effects:
1.54      espie     718:  *     Fill the struct ar_hdr pointed by arHeaderPtr.
1.1       deraadt   719:  *-----------------------------------------------------------------------
                    720:  */
                    721: static FILE *
1.54      espie     722: ArchFindMember(
                    723:     const char   *archive,   /* Path to the archive */
                    724:     const char   *member,    /* Name of member. If it is a path, only the
1.1       deraadt   725:                               * last component is used. */
1.54      espie     726:     struct ar_hdr *arHeaderPtr,/* Pointer to header structure to be filled in */
                    727:     const char   *mode)      /* mode for opening the stream */
1.1       deraadt   728: {
1.70    ! espie     729:        FILE *    arch;       /* Stream to archive */
        !           730:        char      *cp;
        !           731:        char      magic[SARMAG];
        !           732:        size_t    length;
1.42      espie     733: #ifdef SVR4ARCHIVES
1.64      espie     734:        struct SVR4namelist list;
1.42      espie     735:
1.64      espie     736:        list.fnametab = NULL;
1.42      espie     737: #endif
                    738:
1.64      espie     739:        arch = fopen(archive, mode);
                    740:        if (arch == NULL)
                    741:                return NULL;
1.9       millert   742:
1.64      espie     743:        /* Make sure this is an archive we can handle.  */
                    744:        if (fread(magic, SARMAG, 1, arch) != 1 ||
                    745:            strncmp(magic, ARMAG, SARMAG) != 0) {
                    746:                fclose(arch);
                    747:                return NULL;
                    748:        }
1.1       deraadt   749:
1.64      espie     750:        /* Because of space constraints and similar things, files are archived
                    751:         * using their final path components, not the entire thing, so we need
                    752:         * to point 'member' to the final component, if there is one, to make
                    753:         * the comparisons easier...  */
                    754:        cp = strrchr(member, '/');
                    755:        if (cp != NULL)
                    756:                member = cp + 1;
                    757:
                    758:        length = strlen(member);
                    759:        if (length >= AR_NAME_SIZE)
                    760:                length = AR_NAME_SIZE;
                    761:
                    762:        /* Error handling is simpler than for read_archive, since we just
                    763:         * look for a given member.  */
                    764:        while (fread(arHeaderPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
1.70    ! espie     765:                off_t size;       /* Size of archive member */
1.64      espie     766:                char *memberName;
                    767:
1.69      espie     768:                if (memcmp(arHeaderPtr->ar_fmag, ARFMAG,
1.64      espie     769:                    sizeof(arHeaderPtr->ar_fmag) ) != 0)
                    770:                         /* The header is bogus, so the archive is bad.  */
                    771:                         break;
                    772:
                    773:                memberName = arHeaderPtr->ar_name;
                    774:                if (memcmp(member, memberName, length) == 0) {
                    775:                        /* If the member's name doesn't take up the entire
                    776:                         * 'name' field, we have to be careful of matching
                    777:                         * prefixes. Names are space- padded to the right, so
                    778:                         * if the character in 'name' at the end of the matched
                    779:                         * string is anything but a space, this isn't the
                    780:                         * member we sought.  */
1.42      espie     781: #ifdef SVR4ARCHIVES
1.69      espie     782:                        if (length < sizeof(arHeaderPtr->ar_name) &&
1.64      espie     783:                            memberName[length] == '/')
                    784:                                length++;
1.42      espie     785: #endif
1.64      espie     786:                        if (length == sizeof(arHeaderPtr->ar_name) ||
                    787:                            memberName[length] == ' ') {
1.42      espie     788: #ifdef SVR4ARCHIVES
1.64      espie     789:                                efree(list.fnametab);
1.42      espie     790: #endif
1.64      espie     791:                                return arch;
                    792:                        }
                    793:                }
1.42      espie     794:
1.69      espie     795:                size = (off_t) field2long(arHeaderPtr->ar_size,
1.64      espie     796:                    sizeof(arHeaderPtr->ar_size));
1.9       millert   797:
1.42      espie     798: #ifdef SVR4ARCHIVES
1.67      espie     799:                /* svr4 names are slash terminated. Also svr4 extended AR
                    800:                 * format.
                    801:                 */
                    802:                if (memberName[0] == '/') {
                    803:                        /* svr4 magic mode.  */
1.70    ! espie     804:                        memberName = ArchSVR4Entry(&list, arHeaderPtr->ar_name,
        !           805:                            size, arch);
        !           806:                        if (memberName == NULL)
        !           807:                                /* Invalid data */
1.67      espie     808:                                break;
                    809:                        else if (memberName == svr4list)
1.70    ! espie     810:                                /* List of files entry */
1.67      espie     811:                                continue;
                    812:                        /* Got the entry.  */
                    813:                        if (strcmp(memberName, member) == 0) {
                    814:                                efree(list.fnametab);
                    815:                                return arch;
                    816:                        }
                    817:                }
1.42      espie     818: #endif
                    819:
1.1       deraadt   820: #ifdef AR_EFMT1
1.64      espie     821:                /* BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    822:                 * first <namelen> bytes of the file.  */
                    823:                if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
                    824:                    isdigit(memberName[sizeof(AR_EFMT1) - 1])) {
                    825:                        char ename[PATH_MAX];
                    826:
                    827:                        int elength = atoi(memberName + sizeof(AR_EFMT1)-1);
                    828:
                    829:                        if (elength <= 0 || elength >= PATH_MAX)
                    830:                                break;
                    831:                        if (fread(ename, elength, 1, arch) != 1)
                    832:                                break;
                    833:                        if (fseek(arch, -elength, SEEK_CUR) != 0)
                    834:                                break;
                    835:                        ename[elength] = '\0';
                    836:                        if (DEBUG(ARCH) || DEBUG(MAKE))
                    837:                                printf("ArchFind: Extended format entry for %s\n", ename);
                    838:                        /* Found as extended name.      */
                    839:                        if (strcmp(ename, member) == 0) {
1.42      espie     840: #ifdef SVR4ARCHIVES
1.64      espie     841:                                efree(list.fnametab);
1.42      espie     842: #endif
1.64      espie     843:                                return arch;
                    844:                        }
1.1       deraadt   845:                }
1.64      espie     846: #endif
                    847:                /* This isn't the member we're after, so we need to advance the
                    848:                 * stream's pointer to the start of the next header.  */
                    849:                if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
                    850:                        break;
1.42      espie     851:        }
1.1       deraadt   852:
1.64      espie     853:        /* We did not find the member, or we ran into an error while reading
                    854:         * the archive.  */
1.42      espie     855: #ifdef SVRARCHIVES
1.64      espie     856:        efree(list.fnametab);
1.42      espie     857: #endif
1.64      espie     858:        fclose(arch);
                    859:        return NULL;
1.1       deraadt   860: }
                    861:
1.39      espie     862: static void
1.54      espie     863: ArchTouch(const char *archive, const char *member)
1.39      espie     864: {
1.67      espie     865:        FILE *arch;
                    866:        struct ar_hdr arHeader;
1.39      espie     867:
1.64      espie     868:        arch = ArchFindMember(archive, member, &arHeader, "r+");
                    869:        if (arch != NULL) {
1.70    ! espie     870:                snprintf(arHeader.ar_date, sizeof(arHeader.ar_date),
        !           871:                    "%-12ld", (long) timestamp2time_t(now));
1.64      espie     872:                if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) == 0)
                    873:                        (void)fwrite(&arHeader, sizeof(struct ar_hdr), 1, arch);
                    874:                fclose(arch);
                    875:        }
1.39      espie     876: }
                    877:
1.45      espie     878: /*
1.39      espie     879:  * Side Effects:
1.1       deraadt   880:  *     The modification time of the entire archive is also changed.
                    881:  *     For a library, this could necessitate the re-ranlib'ing of the
                    882:  *     whole thing.
                    883:  */
                    884: void
1.54      espie     885: Arch_Touch(GNode *gn)
1.1       deraadt   886: {
1.64      espie     887:        ArchTouch(Varq_Value(ARCHIVE_INDEX, gn), Varq_Value(MEMBER_INDEX, gn));
1.1       deraadt   888: }
                    889:
1.47      deraadt   890: /*ARGSUSED*/
1.1       deraadt   891: void
1.54      espie     892: Arch_TouchLib(GNode *gn UNUSED)
1.49      espie     893:                      /* ^          Non RANLIBMAG does nothing with it */
1.1       deraadt   894: {
1.3       niklas    895: #ifdef RANLIBMAG
1.64      espie     896:        if (gn->path != NULL) {
                    897:                ArchTouch(gn->path, RANLIBMAG);
                    898:                set_times(gn->path);
1.70    ! espie     899:        }
1.3       niklas    900: #endif
1.1       deraadt   901: }
                    902:
1.34      espie     903: TIMESTAMP
1.54      espie     904: Arch_MTime(GNode *gn)
1.1       deraadt   905: {
1.64      espie     906:        gn->mtime = ArchMTimeMember(Varq_Value(ARCHIVE_INDEX, gn),
                    907:             Varq_Value(MEMBER_INDEX, gn), true);
1.1       deraadt   908:
1.64      espie     909:        return gn->mtime;
1.1       deraadt   910: }
                    911:
1.36      espie     912: TIMESTAMP
1.54      espie     913: Arch_MemMTime(GNode *gn)
1.1       deraadt   914: {
1.64      espie     915:        LstNode ln;
1.1       deraadt   916:
1.64      espie     917:        for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    918:                GNode *pgn;
                    919:                char *nameStart;
                    920:                char *nameEnd;
                    921:
                    922:                pgn = (GNode *)Lst_Datum(ln);
                    923:
                    924:                if (pgn->type & OP_ARCHV) {
                    925:                        /* If the parent is an archive specification and is
                    926:                         * being made and its member's name matches the name of
                    927:                         * the node we were given, record the modification time
                    928:                         * of the parent in the child. We keep searching its
                    929:                         * parents in case some other parent requires this
                    930:                         * child to exist...  */
                    931:                        if ((nameStart = strchr(pgn->name, '(') ) != NULL) {
                    932:                                nameStart++;
                    933:                                nameEnd = strchr(nameStart, ')');
                    934:                        } else
                    935:                                nameEnd = NULL;
                    936:
                    937:                        if (pgn->make && nameEnd != NULL &&
1.69      espie     938:                            strncmp(nameStart, gn->name, nameEnd - nameStart)
1.64      espie     939:                            == 0 && gn->name[nameEnd-nameStart] == '\0')
                    940:                                gn->mtime = Arch_MTime(pgn);
                    941:                } else if (pgn->make) {
                    942:                        /* Something which isn't a library depends on the
                    943:                         * existence of this target, so it needs to exist.  */
                    944:                        ts_set_out_of_date(gn->mtime);
                    945:                        break;
                    946:                }
1.1       deraadt   947:        }
1.64      espie     948:        return gn->mtime;
1.1       deraadt   949: }
                    950:
1.69      espie     951: /* If the system can handle the -L flag when linking (or we cannot find
                    952:  * the library), we assume that the user has placed the .LIBRARIES variable
                    953:  * in the final linking command (or the linker will know where to find it)
1.45      espie     954:  * and set the TARGET variable for this node to be the node's name. Otherwise,
                    955:  * we set the TARGET variable to be the full path of the library,
                    956:  * as returned by Dir_FindFile.
1.1       deraadt   957:  */
                    958: void
1.54      espie     959: Arch_FindLib(GNode *gn, Lst path)
1.1       deraadt   960: {
1.64      espie     961:        char *libName;  /* file name for archive */
                    962:        size_t length = strlen(gn->name) + 6 - 2;
1.1       deraadt   963:
1.64      espie     964:        libName = emalloc(length);
                    965:        snprintf(libName, length, "lib%s.a", &gn->name[2]);
1.1       deraadt   966:
1.64      espie     967:        gn->path = Dir_FindFile(libName, path);
1.1       deraadt   968:
1.64      espie     969:        free(libName);
1.1       deraadt   970:
1.64      espie     971:        Varq_Set(TARGET_INDEX, gn->name, gn);
1.1       deraadt   972: }
                    973:
                    974: /*-
                    975:  *-----------------------------------------------------------------------
                    976:  * Arch_LibOODate --
                    977:  *     Decide if a node with the OP_LIB attribute is out-of-date. Called
                    978:  *     from Make_OODate to make its life easier.
                    979:  *
                    980:  *     There are several ways for a library to be out-of-date that are
                    981:  *     not available to ordinary files. In addition, there are ways
                    982:  *     that are open to regular files that are not available to
                    983:  *     libraries. A library that is only used as a source is never
                    984:  *     considered out-of-date by itself. This does not preclude the
                    985:  *     library's modification time from making its parent be out-of-date.
                    986:  *     A library will be considered out-of-date for any of these reasons,
                    987:  *     given that it is a target on a dependency line somewhere:
                    988:  *         Its modification time is less than that of one of its
1.44      espie     989:  *               sources (gn->mtime < gn->cmtime).
1.1       deraadt   990:  *         Its modification time is greater than the time at which the
1.44      espie     991:  *               make began (i.e. it's been modified in the course
                    992:  *               of the make, probably by archiving).
1.1       deraadt   993:  *         The modification time of one of its sources is greater than
                    994:  *               the one of its RANLIBMAG member (i.e. its table of contents
1.44      espie     995:  *               is out-of-date). We don't compare of the archive time
1.1       deraadt   996:  *               vs. TOC time because they can be too close. In my
                    997:  *               opinion we should not bother with the TOC at all since
                    998:  *               this is used by 'ar' rules that affect the data contents
                    999:  *               of the archive, not by ranlib rules, which affect the
1.9       millert  1000:  *               TOC.
1.1       deraadt  1001:  *
                   1002:  * Results:
1.45      espie    1003:  *     true if the library is out-of-date. false otherwise.
1.1       deraadt  1004:  *
                   1005:  * Side Effects:
                   1006:  *     The library will be hashed if it hasn't been already.
                   1007:  *-----------------------------------------------------------------------
                   1008:  */
1.45      espie    1009: bool
1.54      espie    1010: Arch_LibOODate(GNode *gn)
1.1       deraadt  1011: {
1.47      deraadt  1012: #ifdef RANLIBMAG
1.64      espie    1013:        TIMESTAMP modTimeTOC;   /* mod time of __.SYMDEF */
1.47      deraadt  1014: #endif
1.9       millert  1015:
1.64      espie    1016:        if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->children))
                   1017:                return false;
1.69      espie    1018:        if (is_strictly_before(now, gn->mtime) ||
1.64      espie    1019:            is_strictly_before(gn->mtime, gn->cmtime) ||
                   1020:            is_out_of_date(gn->mtime))
                   1021:                return true;
1.2       deraadt  1022: #ifdef RANLIBMAG
1.64      espie    1023:        /* non existent libraries are always out-of-date.  */
                   1024:        if (gn->path == NULL)
                   1025:                return true;
                   1026:        modTimeTOC = ArchMTimeMember(gn->path, RANLIBMAG, false);
1.37      espie    1027:
1.64      espie    1028:        if (!is_out_of_date(modTimeTOC)) {
                   1029:                if (DEBUG(ARCH) || DEBUG(MAKE))
1.69      espie    1030:                        printf("%s modified %s...", RANLIBMAG,
1.66      espie    1031:                            time_to_string(modTimeTOC));
1.64      espie    1032:                return is_strictly_before(modTimeTOC, gn->cmtime);
                   1033:        }
                   1034:        /* A library w/o a table of contents is out-of-date.  */
1.37      espie    1035:        if (DEBUG(ARCH) || DEBUG(MAKE))
1.64      espie    1036:                printf("No t.o.c....");
                   1037:        return true;
1.2       deraadt  1038: #else
1.64      espie    1039:        return false;
1.2       deraadt  1040: #endif
1.1       deraadt  1041: }
                   1042:
                   1043: void
1.54      espie    1044: Arch_Init(void)
1.1       deraadt  1045: {
1.64      espie    1046:        ohash_init(&archives, 4, &arch_info);
1.1       deraadt  1047: }
                   1048:
1.45      espie    1049: #ifdef CLEANUP
1.1       deraadt  1050: void
1.54      espie    1051: Arch_End(void)
1.1       deraadt  1052: {
1.64      espie    1053:        Arch *e;
                   1054:        unsigned int i;
1.38      espie    1055:
1.64      espie    1056:        for (e = ohash_first(&archives, &i); e != NULL;
                   1057:            e = ohash_next(&archives, &i))
                   1058:                ArchFree(e);
                   1059:        ohash_delete(&archives);
1.45      espie    1060: }
1.16      espie    1061: #endif
1.9       millert  1062:
1.45      espie    1063: bool
1.54      espie    1064: Arch_IsLib(GNode *gn)
1.9       millert  1065: {
1.64      espie    1066:        char buf[SARMAG];
                   1067:        int fd;
                   1068:
                   1069:        if (gn->path == NULL || (fd = open(gn->path, O_RDONLY)) == -1)
                   1070:                return false;
1.9       millert  1071:
1.64      espie    1072:        if (read(fd, buf, SARMAG) != SARMAG) {
                   1073:                (void)close(fd);
                   1074:                return false;
                   1075:        }
1.9       millert  1076:
1.44      espie    1077:        (void)close(fd);
1.9       millert  1078:
1.64      espie    1079:        return memcmp(buf, ARMAG, SARMAG) == 0;
1.1       deraadt  1080: }