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

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