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

1.44      espie       1: /*     $OpenPackages$ */
1.52    ! espie       2: /*     $OpenBSD: arch.c,v 1.51 2002/01/30 18:40:26 matthieu 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.52    ! espie     316:            size_t  len;
1.9       millert   317:
1.45      espie     318:            memName = Var_Subst(memName, ctxt, true);
1.1       deraadt   319:
1.44      espie     320:            /* Now form an archive spec and recurse to deal with nested
1.1       deraadt   321:             * variables and multi-word variable values.... The results
1.44      espie     322:             * are just placed at the end of the nodeLst we're returning.  */
1.52    ! espie     323:            len = strlen(memName)+strlen(libName)+3;
        !           324:            buf = sacrifice = emalloc(len);
1.1       deraadt   325:
1.52    ! espie     326:            snprintf(buf, len, "%s(%s)", libName, memName);
1.1       deraadt   327:
                    328:            if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
1.44      espie     329:                /* Must contain dynamic sources, so we can't deal with it now.
1.1       deraadt   330:                 * Just create an ARCHV node for the thing and let
1.44      espie     331:                 * SuffExpandChildren handle it...  */
1.45      espie     332:                gn = Targ_FindNode(buf, TARG_CREATE);
1.1       deraadt   333:
1.20      espie     334:                if (gn == NULL) {
1.1       deraadt   335:                    free(buf);
1.45      espie     336:                    return false;
1.1       deraadt   337:                } else {
                    338:                    gn->type |= OP_ARCHV;
1.26      espie     339:                    Lst_AtEnd(nodeLst, gn);
1.1       deraadt   340:                }
1.45      espie     341:            } else if (!Arch_ParseArchive(&sacrifice, nodeLst, ctxt)) {
                    342:                /* Error in nested call -- free buffer and return false
1.44      espie     343:                 * ourselves.  */
1.1       deraadt   344:                free(buf);
1.45      espie     345:                return false;
1.1       deraadt   346:            }
1.44      espie     347:            /* Free buffer and continue with our work.  */
1.1       deraadt   348:            free(buf);
                    349:        } else if (Dir_HasWildcards(memName)) {
1.44      espie     350:            LIST  members;
1.1       deraadt   351:            char  *member;
                    352:
1.28      espie     353:            Lst_Init(&members);
1.44      espie     354:
1.45      espie     355:            Dir_Expand(memName, dirSearchPath, &members);
1.28      espie     356:            while ((member = (char *)Lst_DeQueue(&members)) != NULL) {
1.44      espie     357:                snprintf(nameBuf, MAKE_BSIZE, "%s(%s)", libName, member);
1.1       deraadt   358:                free(member);
1.45      espie     359:                gn = Targ_FindNode(nameBuf, TARG_CREATE);
1.44      espie     360:                /* We've found the node, but have to make sure the rest of
                    361:                 * the world knows it's an archive member, without having
                    362:                 * to constantly check for parentheses, so we type the
                    363:                 * thing with the OP_ARCHV bit before we place it on the
                    364:                 * end of the provided list.  */
1.1       deraadt   365:                gn->type |= OP_ARCHV;
1.26      espie     366:                Lst_AtEnd(nodeLst, gn);
1.1       deraadt   367:            }
1.44      espie     368:        } else {
                    369:            snprintf(nameBuf, MAKE_BSIZE, "%s(%s)", libName, memName);
1.45      espie     370:            gn = Targ_FindNode(nameBuf, TARG_CREATE);
1.44      espie     371:            /* We've found the node, but have to make sure the rest of the
                    372:             * world knows it's an archive member, without having to
                    373:             * constantly check for parentheses, so we type the thing with
                    374:             * the OP_ARCHV bit before we place it on the end of the
                    375:             * provided list.  */
                    376:            gn->type |= OP_ARCHV;
                    377:            Lst_AtEnd(nodeLst, gn);
1.1       deraadt   378:        }
1.44      espie     379:        if (doSubst)
1.1       deraadt   380:            free(memName);
1.9       millert   381:
1.1       deraadt   382:        *cp = saveChar;
                    383:     }
                    384:
1.44      espie     385:     /* If substituted libName, free it now, since we need it no longer.  */
                    386:     if (subLibName)
1.1       deraadt   387:        free(libName);
                    388:
1.44      espie     389:     /* We promised the pointer would be set up at the next non-space, so
1.1       deraadt   390:      * we must advance cp there before setting *linePtr... (note that on
1.44      espie     391:      * entrance to the loop, cp is guaranteed to point at a ')') */
1.1       deraadt   392:     do {
                    393:        cp++;
1.44      espie     394:     } while (*cp != '\0' && isspace(*cp));
1.1       deraadt   395:
                    396:     *linePtr = cp;
1.45      espie     397:     return true;
1.1       deraadt   398: }
                    399:
1.44      espie     400: /* Helper function: ar fields are not null terminated. */
1.41      espie     401: static long
                    402: field2long(field, len)
                    403:     const char *field;
                    404:     size_t len;
                    405: {
                    406:     static char enough[32];
                    407:
                    408:     assert(len < sizeof(enough));
                    409:     memcpy(enough, field, len);
                    410:     enough[len] = '\0';
                    411:     return strtol(enough, NULL, 10);
                    412: }
                    413:
1.38      espie     414: static Arch *
                    415: read_archive(archive, end)
                    416:     const char *archive;
                    417:     const char *end;
1.1       deraadt   418: {
1.44      espie     419:     FILE *       arch;       /* Stream to archive */
                    420:     char         magic[SARMAG];
                    421:     Arch         *ar;
1.40      espie     422: #ifdef SVR4ARCHIVES
                    423:     struct SVR4namelist list;
                    424:
                    425:     list.fnametab = NULL;
                    426: #endif
1.35      espie     427:
1.38      espie     428:     /* When we encounter an archive for the first time, we read its
                    429:      * whole contents, to place it in the cache.  */
1.35      espie     430:     arch = fopen(archive, "r");
                    431:     if (arch == NULL)
1.38      espie     432:        return NULL;
1.9       millert   433:
1.38      espie     434:     /* Make sure this is an archive we can handle.  */
1.35      espie     435:     if ((fread(magic, SARMAG, 1, arch) != 1) ||
1.41      espie     436:        (strncmp(magic, ARMAG, SARMAG) != 0)) {
1.35      espie     437:            fclose(arch);
1.38      espie     438:            return NULL;
1.1       deraadt   439:     }
                    440:
1.43      espie     441:     ar = ohash_create_entry(&arch_info, archive, &end);
                    442:     ohash_init(&ar->members, 8, &members_info);
1.38      espie     443:
1.41      espie     444:     for (;;) {
1.44      espie     445:        size_t          n;
                    446:        struct ar_hdr   arh;    /* Archive-member header for reading archive */
1.41      espie     447:        off_t           size;   /* Size of archive member */
1.48      espie     448:        char            buffer[PATH_MAX];
1.44      espie     449:        char            *memName;
1.40      espie     450:                                /* Current member name while hashing. */
1.44      espie     451:        char            *cp;    /* Useful character pointer */
1.41      espie     452:
1.40      espie     453:        memName = buffer;
1.44      espie     454:        n = fread(&arh, 1, sizeof(struct ar_hdr), arch);
1.41      espie     455:
                    456:        /*  Whole archive read ok.  */
                    457:        if (n == 0 && feof(arch)) {
                    458: #ifdef SVR4ARCHIVES
                    459:            efree(list.fnametab);
                    460: #endif
                    461:            fclose(arch);
                    462:            return ar;
                    463:        }
                    464:        if (n < sizeof(struct ar_hdr))
                    465:            break;
1.40      espie     466:
1.41      espie     467:        if (memcmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
                    468:            /* The header is bogus.  */
                    469:            break;
1.1       deraadt   470:        } else {
1.41      espie     471:            /* We need to advance the stream's pointer to the start of the
                    472:             * next header.  Records are padded with newlines to an even-byte
1.44      espie     473:             * boundary, so we need to extract the size of the record and
1.41      espie     474:             * round it up during the seek.  */
                    475:            size = (off_t) field2long(arh.ar_size, sizeof(arh.ar_size));
                    476:
                    477:            (void)memcpy(memName, arh.ar_name, AR_NAME_SIZE);
                    478:            /* Find real end of name (strip extranous ' ')  */
1.44      espie     479:            for (cp = memName + AR_NAME_SIZE - 1; *cp == ' ';)
1.41      espie     480:                cp--;
1.1       deraadt   481:            cp[1] = '\0';
                    482:
1.6       briggs    483: #ifdef SVR4ARCHIVES
1.41      espie     484:            /* SVR4 names are slash terminated.  Also svr4 extended AR format.
1.6       briggs    485:             */
                    486:            if (memName[0] == '/') {
1.40      espie     487:                /* SVR4 magic mode.  */
                    488:                memName = ArchSVR4Entry(&list, memName, size, arch);
1.44      espie     489:                if (memName == NULL)            /* Invalid data */
1.40      espie     490:                    break;
1.44      espie     491:                else if (memName == svr4list)   /* List of files entry */
1.6       briggs    492:                    continue;
1.40      espie     493:                /* Got the entry.  */
                    494:                /* XXX this assumes further processing, such as AR_EFMT1,
                    495:                 * also applies to SVR4ARCHIVES.  */
1.6       briggs    496:            }
                    497:            else {
                    498:                if (cp[0] == '/')
                    499:                    cp[0] = '\0';
                    500:            }
1.3       niklas    501: #endif
                    502:
1.1       deraadt   503: #ifdef AR_EFMT1
1.41      espie     504:            /* BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    505:             * first <namelen> bytes of the file.  */
                    506:            if (memcmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
1.1       deraadt   507:                isdigit(memName[sizeof(AR_EFMT1) - 1])) {
                    508:
1.41      espie     509:                int elen = atoi(memName + sizeof(AR_EFMT1)-1);
1.44      espie     510:
1.48      espie     511:                if (elen <= 0 || elen >= PATH_MAX)
1.41      espie     512:                        break;
                    513:                memName = buffer;
                    514:                if (fread(memName, elen, 1, arch) != 1)
                    515:                        break;
1.1       deraadt   516:                memName[elen] = '\0';
1.41      espie     517:                if (fseek(arch, -elen, SEEK_CUR) != 0)
                    518:                        break;
                    519:                if (DEBUG(ARCH) || DEBUG(MAKE))
1.1       deraadt   520:                    printf("ArchStat: Extended format entry for %s\n", memName);
                    521:            }
                    522: #endif
                    523:
1.43      espie     524:            ohash_insert(&ar->members,
                    525:                ohash_qlookup(&ar->members, memName),
1.35      espie     526:                    new_arch_member(&arh, memName));
1.1       deraadt   527:        }
1.41      espie     528:        if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
                    529:            break;
1.1       deraadt   530:     }
                    531:
1.27      espie     532:     fclose(arch);
1.43      espie     533:     ohash_delete(&ar->members);
1.40      espie     534: #ifdef SVR4ARCHIVES
                    535:     efree(list.fnametab);
                    536: #endif
1.27      espie     537:     free(ar);
1.38      espie     538:     return NULL;
                    539: }
                    540:
                    541: /*-
                    542:  *-----------------------------------------------------------------------
                    543:  * ArchMTimeMember --
                    544:  *     Find the modification time of an archive's member, given the
                    545:  *     path to the archive and the path to the desired member.
                    546:  *
                    547:  * Results:
1.44      espie     548:  *     The archive member's modification time, or OUT_OF_DATE if member
                    549:  *     was not found (convenient, so that missing members are always
1.38      espie     550:  *     out of date).
                    551:  *
                    552:  * Side Effects:
1.45      espie     553:  *     Cache the whole archive contents if hash is true.
1.38      espie     554:  *-----------------------------------------------------------------------
                    555:  */
                    556: static TIMESTAMP
                    557: ArchMTimeMember(archive, member, hash)
                    558:     const char   *archive;   /* Path to the archive */
                    559:     const char   *member;    /* Name of member. If it is a path, only the
                    560:                               * last component is used. */
1.45      espie     561:     bool         hash;       /* true if archive should be hashed if not
1.44      espie     562:                               * already so. */
1.38      espie     563: {
1.44      espie     564:     FILE *       arch;       /* Stream to archive */
1.38      espie     565:     Arch         *ar;        /* Archive descriptor */
1.44      espie     566:     unsigned int  slot;       /* Place of archive in the archives hash */
                    567:     const char   *end = NULL;
1.38      espie     568:     const char   *cp;
                    569:     TIMESTAMP    result;
                    570:
1.45      espie     571:     ts_set_out_of_date(result);
1.38      espie     572:     /* Because of space constraints and similar things, files are archived
                    573:      * using their final path components, not the entire thing, so we need
                    574:      * to point 'member' to the final component, if there is one, to make
                    575:      * the comparisons easier...  */
                    576:     cp = strrchr(member, '/');
                    577:     if (cp != NULL)
                    578:        member = cp + 1;
                    579:
                    580:     /* Try to find archive in cache.  */
1.43      espie     581:     slot = ohash_qlookupi(&archives, archive, &end);
                    582:     ar = ohash_find(&archives, slot);
1.38      espie     583:
                    584:     /* If not found, get it now.  */
                    585:     if (ar == NULL) {
                    586:        if (!hash) {
                    587:            /* Quick path:  no need to hash the whole archive, just use
                    588:             * ArchFindMember to get the member's header and close the stream
                    589:             * again.  */
                    590:            struct ar_hdr       sarh;
                    591:
                    592:            arch = ArchFindMember(archive, member, &sarh, "r");
                    593:
                    594:            if (arch != NULL) {
                    595:                fclose(arch);
1.45      espie     596:                ts_set_from_time_t( (time_t)strtol(sarh.ar_date, NULL, 10), result);
1.38      espie     597:            }
                    598:            return result;
                    599:        }
                    600:        ar = read_archive(archive, end);
                    601:        if (ar != NULL)
1.43      espie     602:            ohash_insert(&archives, slot, ar);
1.38      espie     603:     }
1.44      espie     604:
1.38      espie     605:     /* If archive was found, get entry we seek.  */
                    606:     if (ar != NULL) {
1.44      espie     607:        struct arch_member *he;
1.38      espie     608:        end = NULL;
                    609:
1.43      espie     610:        he = ohash_find(&ar->members, ohash_qlookupi(&ar->members, member, &end));
1.38      espie     611:        if (he != NULL)
                    612:            return mtime_of_member(he);
                    613:        else {
1.47      deraadt   614:            if ((size_t)(end - member) > AR_NAME_SIZE) {
1.44      espie     615:                /* Try truncated name.  */
                    616:                end = member + AR_NAME_SIZE;
1.43      espie     617:                he = ohash_find(&ar->members,
                    618:                    ohash_qlookupi(&ar->members, member, &end));
1.38      espie     619:                if (he != NULL)
                    620:                    return mtime_of_member(he);
                    621:            }
                    622:        }
                    623:     }
1.35      espie     624:     return result;
1.1       deraadt   625: }
1.6       briggs    626:
                    627: #ifdef SVR4ARCHIVES
                    628: /*-
                    629:  *-----------------------------------------------------------------------
                    630:  * ArchSVR4Entry --
                    631:  *     Parse an SVR4 style entry that begins with a slash.
                    632:  *     If it is "//", then load the table of filenames
                    633:  *     If it is "/<offset>", then try to substitute the long file name
                    634:  *     from offset of a table previously read.
                    635:  *
                    636:  * Results:
1.40      espie     637:  *     svr4list: just read a list of names
1.50      mpech     638:  *     NULL:     error occurred
1.40      espie     639:  *     extended name
1.6       briggs    640:  *
1.40      espie     641:  * Side-effect:
                    642:  *     For a list of names, store the list in l.
1.6       briggs    643:  *-----------------------------------------------------------------------
                    644:  */
1.44      espie     645:
1.40      espie     646: static char *
                    647: ArchSVR4Entry(l, name, size, arch)
                    648:        struct SVR4namelist *l;
1.6       briggs    649:        char *name;
                    650:        size_t size;
                    651:        FILE *arch;
                    652: {
1.40      espie     653: #define ARLONGNAMES1 "/"
                    654: #define ARLONGNAMES2 "ARFILENAMES"
1.6       briggs    655:     size_t entry;
                    656:     char *ptr, *eptr;
                    657:
1.40      espie     658:     assert(name[0] == '/');
                    659:     name++;
                    660:     /* First comes a table of archive names, to be used by subsequent calls.  */
                    661:     if (memcmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
                    662:        memcmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
1.6       briggs    663:
1.40      espie     664:        if (l->fnametab != NULL) {
                    665:            if (DEBUG(ARCH))
1.6       briggs    666:                printf("Attempted to redefine an SVR4 name table\n");
1.40      espie     667:            return NULL;
1.6       briggs    668:        }
                    669:
1.40      espie     670:        l->fnametab = emalloc(size);
                    671:        l->fnamesize = size;
1.6       briggs    672:
1.40      espie     673:        if (fread(l->fnametab, size, 1, arch) != 1) {
                    674:            if (DEBUG(ARCH))
1.6       briggs    675:                printf("Reading an SVR4 name table failed\n");
1.40      espie     676:            return NULL;
1.6       briggs    677:        }
1.44      espie     678:
1.40      espie     679:        eptr = l->fnametab + size;
                    680:        for (entry = 0, ptr = l->fnametab; ptr < eptr; ptr++)
1.6       briggs    681:            switch (*ptr) {
                    682:            case '/':
                    683:                entry++;
                    684:                *ptr = '\0';
                    685:                break;
                    686:
                    687:            case '\n':
                    688:                break;
                    689:
                    690:            default:
                    691:                break;
                    692:            }
1.40      espie     693:        if (DEBUG(ARCH))
1.44      espie     694:            printf("Found svr4 archive name table with %lu entries\n",
                    695:                        (u_long)entry);
1.40      espie     696:        return (char *)svr4list;
1.6       briggs    697:     }
1.40      espie     698:     /* Then the names themselves are given as offsets in this table.  */
                    699:     if (*name == ' ' || *name == '\0')
                    700:        return NULL;
1.6       briggs    701:
1.40      espie     702:     entry = (size_t) strtol(name, &eptr, 0);
                    703:     if ((*eptr != ' ' && *eptr != '\0') || eptr == name) {
                    704:        if (DEBUG(ARCH))
1.44      espie     705:            printf("Could not parse SVR4 name /%s\n", name);
1.40      espie     706:        return NULL;
1.6       briggs    707:     }
1.40      espie     708:     if (entry >= l->fnamesize) {
                    709:        if (DEBUG(ARCH))
1.44      espie     710:            printf("SVR4 entry offset /%s is greater than %lu\n",
1.40      espie     711:                   name, (u_long)l->fnamesize);
                    712:        return NULL;
1.6       briggs    713:     }
                    714:
1.40      espie     715:     if (DEBUG(ARCH))
1.44      espie     716:        printf("Replaced /%s with %s\n", name, l->fnametab + entry);
1.6       briggs    717:
1.40      espie     718:     return l->fnametab + entry;
1.6       briggs    719: }
                    720: #endif
                    721:
1.1       deraadt   722:
                    723: /*-
                    724:  *-----------------------------------------------------------------------
                    725:  * ArchFindMember --
                    726:  *     Locate a member of an archive, given the path of the archive and
                    727:  *     the path of the desired member. If the archive is to be modified,
                    728:  *     the mode should be "r+", if not, it should be "r".
                    729:  *
                    730:  * Results:
1.42      espie     731:  *     A FILE *, opened for reading and writing, positioned right after
1.44      espie     732:  *     the member's header, or NULL if the member was nonexistent.
1.1       deraadt   733:  *
                    734:  * Side Effects:
1.42      espie     735:  *     Fill the struct ar_hdr pointed by arhPtr.
1.1       deraadt   736:  *-----------------------------------------------------------------------
                    737:  */
                    738: static FILE *
1.42      espie     739: ArchFindMember(archive, member, arhPtr, mode)
1.38      espie     740:     const char   *archive;   /* Path to the archive */
                    741:     const char   *member;    /* Name of member. If it is a path, only the
1.1       deraadt   742:                               * last component is used. */
                    743:     struct ar_hdr *arhPtr;    /* Pointer to header structure to be filled in */
1.38      espie     744:     const char   *mode;      /* The mode for opening the stream */
1.1       deraadt   745: {
1.44      espie     746:     FILE *       arch;       /* Stream to archive */
1.1       deraadt   747:     char         *cp;        /* Useful character pointer */
                    748:     char         magic[SARMAG];
1.42      espie     749:     size_t       len;
                    750: #ifdef SVR4ARCHIVES
                    751:     struct SVR4namelist list;
                    752:
                    753:     list.fnametab = NULL;
                    754: #endif
                    755:
                    756:     arch = fopen(archive, mode);
                    757:     if (arch == NULL)
                    758:        return NULL;
1.9       millert   759:
1.42      espie     760:     /* Make sure this is an archive we can handle.  */
                    761:     if (fread(magic, SARMAG, 1, arch) != 1 ||
                    762:        strncmp(magic, ARMAG, SARMAG) != 0) {
                    763:            fclose(arch);
                    764:            return NULL;
1.44      espie     765:     }
1.1       deraadt   766:
1.42      espie     767:     /* Because of space constraints and similar things, files are archived
1.1       deraadt   768:      * using their final path components, not the entire thing, so we need
                    769:      * to point 'member' to the final component, if there is one, to make
1.42      espie     770:      * the comparisons easier...  */
                    771:     cp = strrchr(member, '/');
                    772:     if (cp != NULL)
1.1       deraadt   773:        member = cp + 1;
1.44      espie     774:
1.42      espie     775:     len = strlen(member);
                    776:     if (len >= AR_NAME_SIZE)
                    777:        len = AR_NAME_SIZE;
                    778:
1.44      espie     779:     /* Error handling is simpler than for read_archive, since we just
1.42      espie     780:      * look for a given member.  */
                    781:     while (fread(arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
1.44      espie     782:        off_t             size;       /* Size of archive member */
                    783:        char              *memName;
1.42      espie     784:
1.44      espie     785:        if (memcmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0)
1.42      espie     786:             /* The header is bogus, so the archive is bad.  */
                    787:             break;
1.44      espie     788:
1.42      espie     789:        memName = arhPtr->ar_name;
                    790:        if (memcmp(member, memName, len) == 0) {
                    791:            /* If the member's name doesn't take up the entire 'name' field,
1.44      espie     792:             * we have to be careful of matching prefixes. Names are space-
                    793:             * padded to the right, so if the character in 'name' at the end
                    794:             * of the matched string is anything but a space, this isn't the
1.42      espie     795:             * member we sought.  */
                    796: #ifdef SVR4ARCHIVES
                    797:            if (len < sizeof(arhPtr->ar_name) && memName[len] == '/')
1.44      espie     798:                len++;
1.42      espie     799: #endif
1.44      espie     800:            if (len == sizeof(arhPtr->ar_name) ||
                    801:                memName[len] == ' ') {
1.42      espie     802: #ifdef SVR4ARCHIVES
                    803:                efree(list.fnametab);
                    804: #endif
                    805:                return arch;
                    806:            }
                    807:        }
                    808:
                    809:        size = (off_t) field2long(arhPtr->ar_size, sizeof(arhPtr->ar_size));
1.9       millert   810:
1.42      espie     811: #ifdef SVR4ARCHIVES
                    812:            /* svr4 names are slash terminated. Also svr4 extended AR format.
1.44      espie     813:             */
1.42      espie     814:            if (memName[0] == '/') {
                    815:                /* svr4 magic mode.  */
                    816:                memName = ArchSVR4Entry(&list, arhPtr->ar_name, size, arch);
1.44      espie     817:                if (memName == NULL)            /* Invalid data */
1.42      espie     818:                    break;
                    819:                else if (memName == svr4list)   /* List of files entry */
                    820:                    continue;
                    821:                /* Got the entry.  */
                    822:                if (strcmp(memName, member) == 0) {
                    823:                    efree(list.fnametab);
                    824:                    return arch;
                    825:                }
1.1       deraadt   826:            }
1.42      espie     827: #endif
                    828:
1.1       deraadt   829: #ifdef AR_EFMT1
1.42      espie     830:        /* BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    831:         * first <namelen> bytes of the file.  */
                    832:        if (memcmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
                    833:            isdigit(memName[sizeof(AR_EFMT1) - 1])) {
1.48      espie     834:            char          ename[PATH_MAX];
1.42      espie     835:
                    836:            int elen = atoi(memName + sizeof(AR_EFMT1)-1);
                    837:
1.48      espie     838:            if (elen <= 0 || elen >= PATH_MAX)
1.44      espie     839:                break;
                    840:            if (fread(ename, elen, 1, arch) != 1)
                    841:                break;
1.42      espie     842:            if (fseek(arch, -elen, SEEK_CUR) != 0)
1.44      espie     843:                break;
1.42      espie     844:            ename[elen] = '\0';
1.44      espie     845:            if (DEBUG(ARCH) || DEBUG(MAKE))
1.42      espie     846:                printf("ArchFind: Extended format entry for %s\n", ename);
1.44      espie     847:            /* Found as extended name.  */
1.42      espie     848:            if (strcmp(ename, member) == 0) {
                    849: #ifdef SVR4ARCHIVES
                    850:                efree(list.fnametab);
                    851: #endif
                    852:                return arch;
1.1       deraadt   853:                }
1.42      espie     854:        }
1.1       deraadt   855: #endif
1.42      espie     856:        /* This isn't the member we're after, so we need to advance the
                    857:         * stream's pointer to the start of the next header.  */
                    858:        if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
                    859:            break;
1.1       deraadt   860:     }
                    861:
1.42      espie     862:     /* We did not find the member, or we ran into an error while reading
                    863:      * the archive.  */
                    864: #ifdef SVRARCHIVES
                    865:     efree(list.fnametab);
                    866: #endif
                    867:     fclose(arch);
                    868:     return NULL;
1.1       deraadt   869: }
                    870:
1.39      espie     871: static void
                    872: ArchTouch(archive, member)
                    873:     const char   *archive;   /* Path to the archive */
                    874:     const char   *member;    /* Name of member. */
                    875: {
                    876:     FILE *arch;
                    877:     struct ar_hdr arh;
                    878:
                    879:     arch = ArchFindMember(archive, member, &arh, "r+");
                    880:     if (arch != NULL) {
                    881:        snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)
                    882:            timestamp2time_t(now));
1.42      espie     883:        if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) == 0)
                    884:            (void)fwrite(&arh, sizeof(struct ar_hdr), 1, arch);
1.39      espie     885:        fclose(arch);
                    886:     }
                    887: }
                    888:
1.45      espie     889: /*
1.39      espie     890:  * Side Effects:
1.1       deraadt   891:  *     The modification time of the entire archive is also changed.
                    892:  *     For a library, this could necessitate the re-ranlib'ing of the
                    893:  *     whole thing.
                    894:  */
                    895: void
1.39      espie     896: Arch_Touch(gn)
1.1       deraadt   897:     GNode        *gn;    /* Node of member to touch */
                    898: {
1.39      espie     899:     ArchTouch(Varq_Value(ARCHIVE_INDEX, gn), Varq_Value(MEMBER_INDEX, gn));
1.1       deraadt   900: }
                    901:
1.47      deraadt   902: /*ARGSUSED*/
1.1       deraadt   903: void
1.39      espie     904: Arch_TouchLib(gn)
1.49      espie     905:     GNode          *gn UNUSED; /* The node of the library to touch */
                    906:                      /* ^          Non RANLIBMAG does nothing with it */
1.1       deraadt   907: {
1.3       niklas    908: #ifdef RANLIBMAG
1.39      espie     909:     if (gn->path != NULL) {
1.44      espie     910:        ArchTouch(gn->path, RANLIBMAG);
1.37      espie     911:        set_times(gn->path);
1.1       deraadt   912:     }
1.3       niklas    913: #endif
1.1       deraadt   914: }
                    915:
1.34      espie     916: TIMESTAMP
1.35      espie     917: Arch_MTime(gn)
1.1       deraadt   918:     GNode        *gn;        /* Node describing archive member */
                    919: {
1.35      espie     920:     gn->mtime = ArchMTimeMember(Varq_Value(ARCHIVE_INDEX, gn),
1.44      espie     921:             Varq_Value(MEMBER_INDEX, gn),
1.45      espie     922:             true);
1.1       deraadt   923:
1.34      espie     924:     return gn->mtime;
1.1       deraadt   925: }
                    926:
1.36      espie     927: TIMESTAMP
1.44      espie     928: Arch_MemMTime(gn)
                    929:     GNode        *gn;
1.1       deraadt   930: {
1.44      espie     931:     LstNode      ln;
1.1       deraadt   932:
1.37      espie     933:     for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    934:        GNode   *pgn;
                    935:        char    *nameStart,
                    936:                *nameEnd;
                    937:
1.29      espie     938:        pgn = (GNode *)Lst_Datum(ln);
1.1       deraadt   939:
                    940:        if (pgn->type & OP_ARCHV) {
1.37      espie     941:            /* If the parent is an archive specification and is being made
1.1       deraadt   942:             * and its member's name matches the name of the node we were
                    943:             * given, record the modification time of the parent in the
                    944:             * child. We keep searching its parents in case some other
1.37      espie     945:             * parent requires this child to exist...  */
                    946:            if ((nameStart = strchr(pgn->name, '(') ) != NULL) {
1.44      espie     947:                nameStart++;
                    948:                nameEnd = strchr(nameStart, ')');
1.14      espie     949:            } else
1.44      espie     950:                nameEnd = NULL;
1.1       deraadt   951:
1.14      espie     952:            if (pgn->make && nameEnd != NULL &&
1.37      espie     953:                strncmp(nameStart, gn->name, nameEnd - nameStart) == 0 &&
                    954:                gn->name[nameEnd-nameStart] == '\0')
                    955:                    gn->mtime = Arch_MTime(pgn);
1.1       deraadt   956:        } else if (pgn->make) {
1.37      espie     957:            /* Something which isn't a library depends on the existence of
                    958:             * this target, so it needs to exist.  */
1.45      espie     959:            ts_set_out_of_date(gn->mtime);
1.1       deraadt   960:            break;
                    961:        }
                    962:     }
1.36      espie     963:     return gn->mtime;
1.1       deraadt   964: }
                    965:
1.45      espie     966: /* If the system can handle the -L flag when linking (or we cannot find
                    967:  * the library), we assume that the user has placed the .LIBRARIES variable
                    968:  * in the final linking command (or the linker will know where to find it)
                    969:  * and set the TARGET variable for this node to be the node's name. Otherwise,
                    970:  * we set the TARGET variable to be the full path of the library,
                    971:  * as returned by Dir_FindFile.
1.1       deraadt   972:  */
                    973: void
1.44      espie     974: Arch_FindLib(gn, path)
                    975:     GNode          *gn;        /* Node of library to find */
                    976:     Lst            path;       /* Search path */
1.1       deraadt   977: {
1.44      espie     978:     char           *libName;   /* file name for archive */
1.52    ! espie     979:     size_t         len = strlen(gn->name) + 6 - 2;
1.1       deraadt   980:
1.52    ! espie     981:     libName = emalloc(len);
        !           982:     snprintf(libName, len, "lib%s.a", &gn->name[2]);
1.1       deraadt   983:
1.44      espie     984:     gn->path = Dir_FindFile(libName, path);
1.1       deraadt   985:
1.44      espie     986:     free(libName);
1.1       deraadt   987:
                    988: #ifdef LIBRARIES
1.30      espie     989:     Varq_Set(TARGET_INDEX, gn->name, gn);
1.1       deraadt   990: #else
1.30      espie     991:     Varq_Set(TARGET_INDEX, gn->path == NULL ? gn->name : gn->path, gn);
1.3       niklas    992: #endif /* LIBRARIES */
1.1       deraadt   993: }
                    994:
                    995: /*-
                    996:  *-----------------------------------------------------------------------
                    997:  * Arch_LibOODate --
                    998:  *     Decide if a node with the OP_LIB attribute is out-of-date. Called
                    999:  *     from Make_OODate to make its life easier.
                   1000:  *
                   1001:  *     There are several ways for a library to be out-of-date that are
                   1002:  *     not available to ordinary files. In addition, there are ways
                   1003:  *     that are open to regular files that are not available to
                   1004:  *     libraries. A library that is only used as a source is never
                   1005:  *     considered out-of-date by itself. This does not preclude the
                   1006:  *     library's modification time from making its parent be out-of-date.
                   1007:  *     A library will be considered out-of-date for any of these reasons,
                   1008:  *     given that it is a target on a dependency line somewhere:
                   1009:  *         Its modification time is less than that of one of its
1.44      espie    1010:  *               sources (gn->mtime < gn->cmtime).
1.1       deraadt  1011:  *         Its modification time is greater than the time at which the
1.44      espie    1012:  *               make began (i.e. it's been modified in the course
                   1013:  *               of the make, probably by archiving).
1.1       deraadt  1014:  *         The modification time of one of its sources is greater than
                   1015:  *               the one of its RANLIBMAG member (i.e. its table of contents
1.44      espie    1016:  *               is out-of-date). We don't compare of the archive time
1.1       deraadt  1017:  *               vs. TOC time because they can be too close. In my
                   1018:  *               opinion we should not bother with the TOC at all since
                   1019:  *               this is used by 'ar' rules that affect the data contents
                   1020:  *               of the archive, not by ranlib rules, which affect the
1.9       millert  1021:  *               TOC.
1.1       deraadt  1022:  *
                   1023:  * Results:
1.45      espie    1024:  *     true if the library is out-of-date. false otherwise.
1.1       deraadt  1025:  *
                   1026:  * Side Effects:
                   1027:  *     The library will be hashed if it hasn't been already.
                   1028:  *-----------------------------------------------------------------------
                   1029:  */
1.45      espie    1030: bool
1.44      espie    1031: Arch_LibOODate(gn)
                   1032:     GNode        *gn;          /* The library's graph node */
1.1       deraadt  1033: {
1.47      deraadt  1034: #ifdef RANLIBMAG
1.44      espie    1035:     TIMESTAMP    modTimeTOC;   /* mod time of __.SYMDEF */
1.47      deraadt  1036: #endif
1.9       millert  1037:
1.44      espie    1038:     if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->children))
1.45      espie    1039:        return false;
                   1040:     if (is_strictly_before(now, gn->mtime) || is_strictly_before(gn->mtime, gn->cmtime) ||
1.44      espie    1041:        is_out_of_date(gn->mtime))
1.45      espie    1042:        return true;
1.2       deraadt  1043: #ifdef RANLIBMAG
1.37      espie    1044:     /* non existent libraries are always out-of-date.  */
                   1045:     if (gn->path == NULL)
1.45      espie    1046:        return true;
                   1047:     modTimeTOC = ArchMTimeMember(gn->path, RANLIBMAG, false);
1.37      espie    1048:
                   1049:     if (!is_out_of_date(modTimeTOC)) {
                   1050:        if (DEBUG(ARCH) || DEBUG(MAKE))
                   1051:            printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1.45      espie    1052:        return is_strictly_before(modTimeTOC, gn->cmtime);
1.37      espie    1053:     }
1.44      espie    1054:     /* A library w/o a table of contents is out-of-date.  */
1.37      espie    1055:     if (DEBUG(ARCH) || DEBUG(MAKE))
                   1056:        printf("No t.o.c....");
1.45      espie    1057:     return true;
1.2       deraadt  1058: #else
1.45      espie    1059:     return false;
1.2       deraadt  1060: #endif
1.1       deraadt  1061: }
                   1062:
                   1063: void
1.28      espie    1064: Arch_Init()
1.1       deraadt  1065: {
1.43      espie    1066:     ohash_init(&archives, 4, &arch_info);
1.1       deraadt  1067: }
                   1068:
1.45      espie    1069: #ifdef CLEANUP
1.1       deraadt  1070: void
1.44      espie    1071: Arch_End()
1.1       deraadt  1072: {
1.38      espie    1073:     Arch *e;
                   1074:     unsigned int i;
                   1075:
1.44      espie    1076:     for (e = ohash_first(&archives, &i); e != NULL;
                   1077:        e = ohash_next(&archives, &i))
1.38      espie    1078:            ArchFree(e);
1.43      espie    1079:     ohash_delete(&archives);
1.45      espie    1080: }
1.16      espie    1081: #endif
1.9       millert  1082:
1.45      espie    1083: bool
1.9       millert  1084: Arch_IsLib(gn)
                   1085:     GNode *gn;
                   1086: {
1.44      espie    1087:     char buf[SARMAG];
1.9       millert  1088:     int fd;
                   1089:
1.44      espie    1090:     if (gn->path == NULL || (fd = open(gn->path, O_RDONLY)) == -1)
1.45      espie    1091:        return false;
1.9       millert  1092:
1.44      espie    1093:     if (read(fd, buf, SARMAG) != SARMAG) {
                   1094:        (void)close(fd);
1.45      espie    1095:        return false;
1.9       millert  1096:     }
                   1097:
1.44      espie    1098:     (void)close(fd);
1.9       millert  1099:
1.44      espie    1100:     return memcmp(buf, ARMAG, SARMAG) == 0;
1.1       deraadt  1101: }