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

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