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

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