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

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