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

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