Home | History | Annotate | Line # | Download | only in make
arch.c revision 1.95
      1 /*	$NetBSD: arch.c,v 1.95 2020/08/23 18:57:32 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      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  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1989 by Berkeley Softworks
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Adam de Boor.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #ifndef MAKE_NATIVE
     72 static char rcsid[] = "$NetBSD: arch.c,v 1.95 2020/08/23 18:57:32 rillig Exp $";
     73 #else
     74 #include <sys/cdefs.h>
     75 #ifndef lint
     76 #if 0
     77 static char sccsid[] = "@(#)arch.c	8.2 (Berkeley) 1/2/94";
     78 #else
     79 __RCSID("$NetBSD: arch.c,v 1.95 2020/08/23 18:57:32 rillig Exp $");
     80 #endif
     81 #endif /* not lint */
     82 #endif
     83 
     84 /*-
     85  * arch.c --
     86  *	Functions to manipulate libraries, archives and their members.
     87  *
     88  *	Once again, cacheing/hashing comes into play in the manipulation
     89  * of archives. The first time an archive is referenced, all of its members'
     90  * headers are read and hashed and the archive closed again. All hashed
     91  * archives are kept on a list which is searched each time an archive member
     92  * is referenced.
     93  *
     94  * The interface to this module is:
     95  *	Arch_ParseArchive   	Given an archive specification, return a list
     96  *	    	  	    	of GNode's, one for each member in the spec.
     97  *	    	  	    	FAILURE is returned if the specification is
     98  *	    	  	    	invalid for some reason.
     99  *
    100  *	Arch_Touch	    	Alter the modification time of the archive
    101  *	    	  	    	member described by the given node to be
    102  *	    	  	    	the current time.
    103  *
    104  *	Arch_TouchLib	    	Update the modification time of the library
    105  *	    	  	    	described by the given node. This is special
    106  *	    	  	    	because it also updates the modification time
    107  *	    	  	    	of the library's table of contents.
    108  *
    109  *	Arch_MTime	    	Find the modification time of a member of
    110  *	    	  	    	an archive *in the archive*. The time is also
    111  *	    	  	    	placed in the member's GNode. Returns the
    112  *	    	  	    	modification time.
    113  *
    114  *	Arch_MemTime	    	Find the modification time of a member of
    115  *	    	  	    	an archive. Called when the member doesn't
    116  *	    	  	    	already exist. Looks in the archive for the
    117  *	    	  	    	modification time. Returns the modification
    118  *	    	  	    	time.
    119  *
    120  *	Arch_FindLib	    	Search for a library along a path. The
    121  *	    	  	    	library name in the GNode should be in
    122  *	    	  	    	-l<name> format.
    123  *
    124  *	Arch_LibOODate	    	Special function to decide if a library node
    125  *	    	  	    	is out-of-date.
    126  *
    127  *	Arch_Init 	    	Initialize this module.
    128  *
    129  *	Arch_End 	    	Cleanup this module.
    130  */
    131 
    132 #include    <sys/types.h>
    133 #include    <sys/stat.h>
    134 #include    <sys/time.h>
    135 #include    <sys/param.h>
    136 
    137 #include    <ar.h>
    138 #include    <ctype.h>
    139 #include    <stdio.h>
    140 #include    <stdlib.h>
    141 #include    <utime.h>
    142 
    143 #include    "make.h"
    144 #include    "hash.h"
    145 #include    "dir.h"
    146 #include    "config.h"
    147 
    148 #ifdef TARGET_MACHINE
    149 #undef MAKE_MACHINE
    150 #define MAKE_MACHINE TARGET_MACHINE
    151 #endif
    152 #ifdef TARGET_MACHINE_ARCH
    153 #undef MAKE_MACHINE_ARCH
    154 #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
    155 #endif
    156 
    157 static Lst	  archives;   /* Lst of archives we've already examined */
    158 
    159 typedef struct Arch {
    160     char	  *name;      /* Name of archive */
    161     Hash_Table	  members;    /* All the members of the archive described
    162 			       * by <name, struct ar_hdr *> key/value pairs */
    163     char	  *fnametab;  /* Extended name table strings */
    164     size_t	  fnamesize;  /* Size of the string table */
    165 } Arch;
    166 
    167 static int ArchFindArchive(const void *, const void *);
    168 #ifdef CLEANUP
    169 static void ArchFree(void *);
    170 #endif
    171 static struct ar_hdr *ArchStatMember(const char *, const char *, Boolean);
    172 static FILE *ArchFindMember(const char *, const char *,
    173 			    struct ar_hdr *, const char *);
    174 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
    175 #define SVR4ARCHIVES
    176 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
    177 #endif
    178 
    179 #ifdef CLEANUP
    180 /*-
    181  *-----------------------------------------------------------------------
    182  * ArchFree --
    183  *	Free memory used by an archive
    184  *
    185  * Results:
    186  *	None.
    187  *
    188  * Side Effects:
    189  *	None.
    190  *
    191  *-----------------------------------------------------------------------
    192  */
    193 static void
    194 ArchFree(void *ap)
    195 {
    196     Arch *a = (Arch *)ap;
    197     Hash_Search	  search;
    198     Hash_Entry	  *entry;
    199 
    200     /* Free memory from hash entries */
    201     for (entry = Hash_EnumFirst(&a->members, &search);
    202 	 entry != NULL;
    203 	 entry = Hash_EnumNext(&search))
    204 	free(Hash_GetValue(entry));
    205 
    206     free(a->name);
    207     free(a->fnametab);
    208     Hash_DeleteTable(&a->members);
    209     free(a);
    210 }
    211 #endif
    212 
    213 
    214 
    215 /*-
    216  *-----------------------------------------------------------------------
    217  * Arch_ParseArchive --
    218  *	Parse the archive specification in the given line and find/create
    219  *	the nodes for the specified archive members, placing their nodes
    220  *	on the given list.
    221  *
    222  * Input:
    223  *	linePtr		Pointer to start of specification
    224  *	nodeLst		Lst on which to place the nodes
    225  *	ctxt		Context in which to expand variables
    226  *
    227  * Results:
    228  *	SUCCESS if it was a valid specification. The linePtr is updated
    229  *	to point to the first non-space after the archive spec. The
    230  *	nodes for the members are placed on the given list.
    231  *
    232  * Side Effects:
    233  *	Some nodes may be created. The given list is extended.
    234  *
    235  *-----------------------------------------------------------------------
    236  */
    237 ReturnStatus
    238 Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
    239 {
    240     char	    *cp;	    /* Pointer into line */
    241     GNode	    *gn;     	    /* New node */
    242     char	    *libName;  	    /* Library-part of specification */
    243     char	    *memName;  	    /* Member-part of specification */
    244     char	    saveChar;  	    /* Ending delimiter of member-name */
    245     Boolean 	    subLibName;	    /* TRUE if libName should have/had
    246 				     * variable substitution performed on it */
    247 
    248     libName = *linePtr;
    249 
    250     subLibName = FALSE;
    251 
    252     for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
    253 	if (*cp == '$') {
    254 	    /*
    255 	     * Variable spec, so call the Var module to parse the puppy
    256 	     * so we can safely advance beyond it...
    257 	     */
    258 	    int 	length;
    259 	    void	*freeIt;
    260 	    const char	*result;
    261 
    262 	    result = Var_Parse(cp, ctxt, VARE_UNDEFERR|VARE_WANTRES,
    263 			       &length, &freeIt);
    264 	    free(freeIt);
    265 
    266 	    if (result == var_Error) {
    267 		return FAILURE;
    268 	    } else {
    269 		subLibName = TRUE;
    270 	    }
    271 
    272 	    cp += length-1;
    273 	}
    274     }
    275 
    276     *cp++ = '\0';
    277     if (subLibName) {
    278 	libName = Var_Subst(libName, ctxt, VARE_UNDEFERR|VARE_WANTRES);
    279     }
    280 
    281 
    282     for (;;) {
    283 	/*
    284 	 * First skip to the start of the member's name, mark that
    285 	 * place and skip to the end of it (either white-space or
    286 	 * a close paren).
    287 	 */
    288 	Boolean	doSubst = FALSE; /* TRUE if need to substitute in memName */
    289 
    290 	while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) {
    291 	    cp++;
    292 	}
    293 	memName = cp;
    294 	while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) {
    295 	    if (*cp == '$') {
    296 		/*
    297 		 * Variable spec, so call the Var module to parse the puppy
    298 		 * so we can safely advance beyond it...
    299 		 */
    300 		int 	length;
    301 		void	*freeIt;
    302 		const char *result;
    303 
    304 		result = Var_Parse(cp, ctxt, VARE_UNDEFERR|VARE_WANTRES,
    305 				   &length, &freeIt);
    306 		free(freeIt);
    307 
    308 		if (result == var_Error) {
    309 		    return FAILURE;
    310 		} else {
    311 		    doSubst = TRUE;
    312 		}
    313 
    314 		cp += length;
    315 	    } else {
    316 		cp++;
    317 	    }
    318 	}
    319 
    320 	/*
    321 	 * If the specification ends without a closing parenthesis,
    322 	 * chances are there's something wrong (like a missing backslash),
    323 	 * so it's better to return failure than allow such things to happen
    324 	 */
    325 	if (*cp == '\0') {
    326 	    printf("No closing parenthesis in archive specification\n");
    327 	    return FAILURE;
    328 	}
    329 
    330 	/*
    331 	 * If we didn't move anywhere, we must be done
    332 	 */
    333 	if (cp == memName) {
    334 	    break;
    335 	}
    336 
    337 	saveChar = *cp;
    338 	*cp = '\0';
    339 
    340 	/*
    341 	 * XXX: This should be taken care of intelligently by
    342 	 * SuffExpandChildren, both for the archive and the member portions.
    343 	 */
    344 	/*
    345 	 * If member contains variables, try and substitute for them.
    346 	 * This will slow down archive specs with dynamic sources, of course,
    347 	 * since we'll be (non-)substituting them three times, but them's
    348 	 * the breaks -- we need to do this since SuffExpandChildren calls
    349 	 * us, otherwise we could assume the thing would be taken care of
    350 	 * later.
    351 	 */
    352 	if (doSubst) {
    353 	    char    *buf;
    354 	    char    *sacrifice;
    355 	    char    *oldMemName = memName;
    356 
    357 	    memName = Var_Subst(memName, ctxt, VARE_UNDEFERR | VARE_WANTRES);
    358 
    359 	    /*
    360 	     * Now form an archive spec and recurse to deal with nested
    361 	     * variables and multi-word variable values.... The results
    362 	     * are just placed at the end of the nodeLst we're returning.
    363 	     */
    364 	    buf = sacrifice = str_concat4(libName, "(", memName, ")");
    365 
    366 	    if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
    367 		/*
    368 		 * Must contain dynamic sources, so we can't deal with it now.
    369 		 * Just create an ARCHV node for the thing and let
    370 		 * SuffExpandChildren handle it...
    371 		 */
    372 		gn = Targ_FindNode(buf, TARG_CREATE);
    373 
    374 		if (gn == NULL) {
    375 		    free(buf);
    376 		    return FAILURE;
    377 		} else {
    378 		    gn->type |= OP_ARCHV;
    379 		    Lst_AppendS(nodeLst, gn);
    380 		}
    381 	    } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
    382 		/*
    383 		 * Error in nested call -- free buffer and return FAILURE
    384 		 * ourselves.
    385 		 */
    386 		free(buf);
    387 		return FAILURE;
    388 	    }
    389 	    /*
    390 	     * Free buffer and continue with our work.
    391 	     */
    392 	    free(buf);
    393 	} else if (Dir_HasWildcards(memName)) {
    394 	    Lst	  members = Lst_Init();
    395 	    Buffer nameBuf;
    396 
    397 	    Buf_Init(&nameBuf, 0);
    398 	    Dir_Expand(memName, dirSearchPath, members);
    399 	    while (!Lst_IsEmpty(members)) {
    400 		char *member = Lst_DequeueS(members);
    401 
    402 		Buf_Empty(&nameBuf);
    403 		Buf_AddStr(&nameBuf, libName);
    404 		Buf_AddStr(&nameBuf, "(");
    405 		Buf_AddStr(&nameBuf, member);
    406 		Buf_AddStr(&nameBuf, ")");
    407 		free(member);
    408 
    409 		gn = Targ_FindNode(Buf_GetAll(&nameBuf, NULL), TARG_CREATE);
    410 		if (gn == NULL) {
    411 		    Buf_Destroy(&nameBuf, TRUE);
    412 		    return FAILURE;
    413 		} else {
    414 		    /*
    415 		     * We've found the node, but have to make sure the rest of
    416 		     * the world knows it's an archive member, without having
    417 		     * to constantly check for parentheses, so we type the
    418 		     * thing with the OP_ARCHV bit before we place it on the
    419 		     * end of the provided list.
    420 		     */
    421 		    gn->type |= OP_ARCHV;
    422 		    Lst_AppendS(nodeLst, gn);
    423 		}
    424 	    }
    425 	    Lst_Destroy(members, NULL);
    426 	    Buf_Destroy(&nameBuf, TRUE);
    427 	} else {
    428 	    Buffer nameBuf;
    429 
    430 	    Buf_Init(&nameBuf, 0);
    431 	    Buf_AddStr(&nameBuf, libName);
    432 	    Buf_AddStr(&nameBuf, "(");
    433 	    Buf_AddStr(&nameBuf, memName);
    434 	    Buf_AddStr(&nameBuf, ")");
    435 
    436 	    gn = Targ_FindNode(Buf_GetAll(&nameBuf, NULL), TARG_CREATE);
    437 	    Buf_Destroy(&nameBuf, TRUE);
    438 	    if (gn == NULL) {
    439 		return FAILURE;
    440 	    } else {
    441 		/*
    442 		 * We've found the node, but have to make sure the rest of the
    443 		 * world knows it's an archive member, without having to
    444 		 * constantly check for parentheses, so we type the thing with
    445 		 * the OP_ARCHV bit before we place it on the end of the
    446 		 * provided list.
    447 		 */
    448 		gn->type |= OP_ARCHV;
    449 		Lst_AppendS(nodeLst, gn);
    450 	    }
    451 	}
    452 	if (doSubst) {
    453 	    free(memName);
    454 	}
    455 
    456 	*cp = saveChar;
    457     }
    458 
    459     /*
    460      * If substituted libName, free it now, since we need it no longer.
    461      */
    462     if (subLibName) {
    463 	free(libName);
    464     }
    465 
    466     /*
    467      * We promised the pointer would be set up at the next non-space, so
    468      * we must advance cp there before setting *linePtr... (note that on
    469      * entrance to the loop, cp is guaranteed to point at a ')')
    470      */
    471     do {
    472 	cp++;
    473     } while (*cp != '\0' && isspace ((unsigned char)*cp));
    474 
    475     *linePtr = cp;
    476     return SUCCESS;
    477 }
    478 
    479 /*-
    480  *-----------------------------------------------------------------------
    481  * ArchFindArchive --
    482  *	See if the given archive is the one we are looking for. Called
    483  *	From ArchStatMember and ArchFindMember via Lst_Find.
    484  *
    485  * Input:
    486  *	ar		Current list element
    487  *	archName	Name we want
    488  *
    489  * Results:
    490  *	0 if it is, non-zero if it isn't.
    491  *
    492  * Side Effects:
    493  *	None.
    494  *
    495  *-----------------------------------------------------------------------
    496  */
    497 static int
    498 ArchFindArchive(const void *ar, const void *archName)
    499 {
    500     return strcmp(archName, ((const Arch *)ar)->name);
    501 }
    502 
    503 /*-
    504  *-----------------------------------------------------------------------
    505  * ArchStatMember --
    506  *	Locate a member of an archive, given the path of the archive and
    507  *	the path of the desired member.
    508  *
    509  * Input:
    510  *	archive		Path to the archive
    511  *	member		Name of member. If it is a path, only the last
    512  *			component is used.
    513  *	hash		TRUE if archive should be hashed if not already so.
    514  *
    515  * Results:
    516  *	A pointer to the current struct ar_hdr structure for the member. Note
    517  *	That no position is returned, so this is not useful for touching
    518  *	archive members. This is mostly because we have no assurances that
    519  *	The archive will remain constant after we read all the headers, so
    520  *	there's not much point in remembering the position...
    521  *
    522  * Side Effects:
    523  *
    524  *-----------------------------------------------------------------------
    525  */
    526 static struct ar_hdr *
    527 ArchStatMember(const char *archive, const char *member, Boolean hash)
    528 {
    529 #define AR_MAX_NAME_LEN	    (sizeof(arh.ar_name)-1)
    530     FILE *	  arch;	      /* Stream to archive */
    531     size_t	  size;       /* Size of archive member */
    532     char	  magic[SARMAG];
    533     LstNode	  ln;	      /* Lst member containing archive descriptor */
    534     Arch	  *ar;	      /* Archive descriptor */
    535     Hash_Entry	  *he;	      /* Entry containing member's description */
    536     struct ar_hdr arh;        /* archive-member header for reading archive */
    537     char	  memName[MAXPATHLEN+1];
    538 			    /* Current member name while hashing. */
    539 
    540     /*
    541      * Because of space constraints and similar things, files are archived
    542      * using their final path components, not the entire thing, so we need
    543      * to point 'member' to the final component, if there is one, to make
    544      * the comparisons easier...
    545      */
    546     const char *base = strrchr(member, '/');
    547     if (base != NULL) {
    548 	member = base + 1;
    549     }
    550 
    551     ln = Lst_Find(archives, ArchFindArchive, archive);
    552     if (ln != NULL) {
    553 	ar = Lst_DatumS(ln);
    554 
    555 	he = Hash_FindEntry(&ar->members, member);
    556 
    557 	if (he != NULL) {
    558 	    return (struct ar_hdr *)Hash_GetValue(he);
    559 	} else {
    560 	    /* Try truncated name */
    561 	    char copy[AR_MAX_NAME_LEN+1];
    562 	    size_t len = strlen(member);
    563 
    564 	    if (len > AR_MAX_NAME_LEN) {
    565 		len = AR_MAX_NAME_LEN;
    566 		snprintf(copy, sizeof copy, "%s", member);
    567 	    }
    568 	    if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
    569 		return (struct ar_hdr *)Hash_GetValue(he);
    570 	    return NULL;
    571 	}
    572     }
    573 
    574     if (!hash) {
    575 	/*
    576 	 * Caller doesn't want the thing hashed, just use ArchFindMember
    577 	 * to read the header for the member out and close down the stream
    578 	 * again. Since the archive is not to be hashed, we assume there's
    579 	 * no need to allocate extra room for the header we're returning,
    580 	 * so just declare it static.
    581 	 */
    582 	 static struct ar_hdr	sarh;
    583 
    584 	 arch = ArchFindMember(archive, member, &sarh, "r");
    585 
    586 	 if (arch == NULL) {
    587 	    return NULL;
    588 	} else {
    589 	    fclose(arch);
    590 	    return &sarh;
    591 	}
    592     }
    593 
    594     /*
    595      * We don't have this archive on the list yet, so we want to find out
    596      * everything that's in it and cache it so we can get at it quickly.
    597      */
    598     arch = fopen(archive, "r");
    599     if (arch == NULL) {
    600 	return NULL;
    601     }
    602 
    603     /*
    604      * We use the ARMAG string to make sure this is an archive we
    605      * can handle...
    606      */
    607     if ((fread(magic, SARMAG, 1, arch) != 1) ||
    608 	(strncmp(magic, ARMAG, SARMAG) != 0)) {
    609 	    fclose(arch);
    610 	    return NULL;
    611     }
    612 
    613     ar = bmake_malloc(sizeof(Arch));
    614     ar->name = bmake_strdup(archive);
    615     ar->fnametab = NULL;
    616     ar->fnamesize = 0;
    617     Hash_InitTable(&ar->members, -1);
    618     memName[AR_MAX_NAME_LEN] = '\0';
    619 
    620     while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
    621 	if (strncmp( arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
    622 	    /*
    623 	     * The header is bogus, so the archive is bad
    624 	     * and there's no way we can recover...
    625 	     */
    626 	    goto badarch;
    627 	} else {
    628 	    char *nameend;
    629 
    630 	    /*
    631 	     * We need to advance the stream's pointer to the start of the
    632 	     * next header. Files are padded with newlines to an even-byte
    633 	     * boundary, so we need to extract the size of the file from the
    634 	     * 'size' field of the header and round it up during the seek.
    635 	     */
    636 	    arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
    637 	    size = (size_t)strtol(arh.ar_size, NULL, 10);
    638 
    639 	    memcpy(memName, arh.ar_name, sizeof(arh.ar_name));
    640 	    nameend = memName + AR_MAX_NAME_LEN;
    641 	    while (*nameend == ' ') {
    642 		nameend--;
    643 	    }
    644 	    nameend[1] = '\0';
    645 
    646 #ifdef SVR4ARCHIVES
    647 	    /*
    648 	     * svr4 names are slash terminated. Also svr4 extended AR format.
    649 	     */
    650 	    if (memName[0] == '/') {
    651 		/*
    652 		 * svr4 magic mode; handle it
    653 		 */
    654 		switch (ArchSVR4Entry(ar, memName, size, arch)) {
    655 		case -1:  /* Invalid data */
    656 		    goto badarch;
    657 		case 0:	  /* List of files entry */
    658 		    continue;
    659 		default:  /* Got the entry */
    660 		    break;
    661 		}
    662 	    }
    663 	    else {
    664 		if (nameend[0] == '/')
    665 		    nameend[0] = '\0';
    666 	    }
    667 #endif
    668 
    669 #ifdef AR_EFMT1
    670 	    /*
    671 	     * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    672 	     * first <namelen> bytes of the file
    673 	     */
    674 	    if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
    675 		isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) {
    676 
    677 		int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
    678 
    679 		if ((unsigned int)elen > MAXPATHLEN)
    680 			goto badarch;
    681 		if (fread(memName, (size_t)elen, 1, arch) != 1)
    682 			goto badarch;
    683 		memName[elen] = '\0';
    684 		if (fseek(arch, -elen, SEEK_CUR) != 0)
    685 			goto badarch;
    686 		if (DEBUG(ARCH) || DEBUG(MAKE)) {
    687 		    fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName);
    688 		}
    689 	    }
    690 #endif
    691 
    692 	    he = Hash_CreateEntry(&ar->members, memName, NULL);
    693 	    Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
    694 	    memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
    695 	}
    696 	if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
    697 	    goto badarch;
    698     }
    699 
    700     fclose(arch);
    701 
    702     Lst_AppendS(archives, ar);
    703 
    704     /*
    705      * Now that the archive has been read and cached, we can look into
    706      * the hash table to find the desired member's header.
    707      */
    708     he = Hash_FindEntry(&ar->members, member);
    709 
    710     if (he != NULL) {
    711 	return (struct ar_hdr *)Hash_GetValue(he);
    712     } else {
    713 	return NULL;
    714     }
    715 
    716 badarch:
    717     fclose(arch);
    718     Hash_DeleteTable(&ar->members);
    719     free(ar->fnametab);
    720     free(ar);
    721     return NULL;
    722 }
    723 
    724 #ifdef SVR4ARCHIVES
    725 /*-
    726  *-----------------------------------------------------------------------
    727  * ArchSVR4Entry --
    728  *	Parse an SVR4 style entry that begins with a slash.
    729  *	If it is "//", then load the table of filenames
    730  *	If it is "/<offset>", then try to substitute the long file name
    731  *	from offset of a table previously read.
    732  *
    733  * Results:
    734  *	-1: Bad data in archive
    735  *	 0: A table was loaded from the file
    736  *	 1: Name was successfully substituted from table
    737  *	 2: Name was not successfully substituted from table
    738  *
    739  * Side Effects:
    740  *	If a table is read, the file pointer is moved to the next archive
    741  *	member
    742  *
    743  *-----------------------------------------------------------------------
    744  */
    745 static int
    746 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
    747 {
    748 #define ARLONGNAMES1 "//"
    749 #define ARLONGNAMES2 "/ARFILENAMES"
    750     size_t entry;
    751     char *ptr, *eptr;
    752 
    753     if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
    754 	strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
    755 
    756 	if (ar->fnametab != NULL) {
    757 	    if (DEBUG(ARCH)) {
    758 		fprintf(debug_file, "Attempted to redefine an SVR4 name table\n");
    759 	    }
    760 	    return -1;
    761 	}
    762 
    763 	/*
    764 	 * This is a table of archive names, so we build one for
    765 	 * ourselves
    766 	 */
    767 	ar->fnametab = bmake_malloc(size);
    768 	ar->fnamesize = size;
    769 
    770 	if (fread(ar->fnametab, size, 1, arch) != 1) {
    771 	    if (DEBUG(ARCH)) {
    772 		fprintf(debug_file, "Reading an SVR4 name table failed\n");
    773 	    }
    774 	    return -1;
    775 	}
    776 	eptr = ar->fnametab + size;
    777 	for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
    778 	    switch (*ptr) {
    779 	    case '/':
    780 		entry++;
    781 		*ptr = '\0';
    782 		break;
    783 
    784 	    case '\n':
    785 		break;
    786 
    787 	    default:
    788 		break;
    789 	    }
    790 	if (DEBUG(ARCH)) {
    791 	    fprintf(debug_file, "Found svr4 archive name table with %lu entries\n",
    792 		    (unsigned long)entry);
    793 	}
    794 	return 0;
    795     }
    796 
    797     if (name[1] == ' ' || name[1] == '\0')
    798 	return 2;
    799 
    800     entry = (size_t)strtol(&name[1], &eptr, 0);
    801     if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
    802 	if (DEBUG(ARCH)) {
    803 	    fprintf(debug_file, "Could not parse SVR4 name %s\n", name);
    804 	}
    805 	return 2;
    806     }
    807     if (entry >= ar->fnamesize) {
    808 	if (DEBUG(ARCH)) {
    809 	    fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n",
    810 		   name, (unsigned long)ar->fnamesize);
    811 	}
    812 	return 2;
    813     }
    814 
    815     if (DEBUG(ARCH)) {
    816 	fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
    817     }
    818 
    819     snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
    820     return 1;
    821 }
    822 #endif
    823 
    824 
    825 /*-
    826  *-----------------------------------------------------------------------
    827  * ArchFindMember --
    828  *	Locate a member of an archive, given the path of the archive and
    829  *	the path of the desired member. If the archive is to be modified,
    830  *	the mode should be "r+", if not, it should be "r".
    831  *
    832  * Input:
    833  *	archive		Path to the archive
    834  *	member		Name of member. If it is a path, only the last
    835  *			component is used.
    836  *	arhPtr		Pointer to header structure to be filled in
    837  *	mode		The mode for opening the stream
    838  *
    839  * Results:
    840  *	An FILE *, opened for reading and writing, positioned at the
    841  *	start of the member's struct ar_hdr, or NULL if the member was
    842  *	nonexistent. The current struct ar_hdr for member.
    843  *
    844  * Side Effects:
    845  *	The passed struct ar_hdr structure is filled in.
    846  *
    847  *-----------------------------------------------------------------------
    848  */
    849 static FILE *
    850 ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr,
    851     const char *mode)
    852 {
    853     FILE *	  arch;	      /* Stream to archive */
    854     int		  size;       /* Size of archive member */
    855     char	  magic[SARMAG];
    856     size_t	  len, tlen;
    857     const char *  base;
    858 
    859     arch = fopen(archive, mode);
    860     if (arch == NULL) {
    861 	return NULL;
    862     }
    863 
    864     /*
    865      * We use the ARMAG string to make sure this is an archive we
    866      * can handle...
    867      */
    868     if ((fread(magic, SARMAG, 1, arch) != 1) ||
    869 	(strncmp(magic, ARMAG, SARMAG) != 0)) {
    870 	    fclose(arch);
    871 	    return NULL;
    872     }
    873 
    874     /*
    875      * Because of space constraints and similar things, files are archived
    876      * using their final path components, not the entire thing, so we need
    877      * to point 'member' to the final component, if there is one, to make
    878      * the comparisons easier...
    879      */
    880     base = strrchr(member, '/');
    881     if (base != NULL) {
    882 	member = base + 1;
    883     }
    884     len = tlen = strlen(member);
    885     if (len > sizeof(arhPtr->ar_name)) {
    886 	tlen = sizeof(arhPtr->ar_name);
    887     }
    888 
    889     while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
    890 	if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
    891 	     /*
    892 	      * The header is bogus, so the archive is bad
    893 	      * and there's no way we can recover...
    894 	      */
    895 	     fclose(arch);
    896 	     return NULL;
    897 	} else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
    898 	    /*
    899 	     * If the member's name doesn't take up the entire 'name' field,
    900 	     * we have to be careful of matching prefixes. Names are space-
    901 	     * padded to the right, so if the character in 'name' at the end
    902 	     * of the matched string is anything but a space, this isn't the
    903 	     * member we sought.
    904 	     */
    905 	    if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
    906 		goto skip;
    907 	    } else {
    908 		/*
    909 		 * To make life easier, we reposition the file at the start
    910 		 * of the header we just read before we return the stream.
    911 		 * In a more general situation, it might be better to leave
    912 		 * the file at the actual member, rather than its header, but
    913 		 * not here...
    914 		 */
    915 		if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) != 0) {
    916 		    fclose(arch);
    917 		    return NULL;
    918 		}
    919 		return arch;
    920 	    }
    921 	} else
    922 #ifdef AR_EFMT1
    923 		/*
    924 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    925 		 * first <namelen> bytes of the file
    926 		 */
    927 	    if (strncmp(arhPtr->ar_name, AR_EFMT1,
    928 					sizeof(AR_EFMT1) - 1) == 0 &&
    929 		isdigit((unsigned char)arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
    930 
    931 		unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
    932 		char ename[MAXPATHLEN + 1];
    933 
    934 		if (elen > MAXPATHLEN) {
    935 			fclose(arch);
    936 			return NULL;
    937 		}
    938 		if (fread(ename, elen, 1, arch) != 1) {
    939 			fclose(arch);
    940 			return NULL;
    941 		}
    942 		ename[elen] = '\0';
    943 		if (DEBUG(ARCH) || DEBUG(MAKE)) {
    944 		    fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename);
    945 		}
    946 		if (strncmp(ename, member, len) == 0) {
    947 			/* Found as extended name */
    948 			if (fseek(arch, -sizeof(struct ar_hdr) - elen,
    949 				SEEK_CUR) != 0) {
    950 			    fclose(arch);
    951 			    return NULL;
    952 			}
    953 			return arch;
    954 		}
    955 		if (fseek(arch, -elen, SEEK_CUR) != 0) {
    956 		    fclose(arch);
    957 		    return NULL;
    958 		}
    959 		goto skip;
    960 	} else
    961 #endif
    962 	{
    963 skip:
    964 	    /*
    965 	     * This isn't the member we're after, so we need to advance the
    966 	     * stream's pointer to the start of the next header. Files are
    967 	     * padded with newlines to an even-byte boundary, so we need to
    968 	     * extract the size of the file from the 'size' field of the
    969 	     * header and round it up during the seek.
    970 	     */
    971 	    arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
    972 	    size = (int)strtol(arhPtr->ar_size, NULL, 10);
    973 	    if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
    974 		fclose(arch);
    975 		return NULL;
    976 	    }
    977 	}
    978     }
    979 
    980     /*
    981      * We've looked everywhere, but the member is not to be found. Close the
    982      * archive and return NULL -- an error.
    983      */
    984     fclose(arch);
    985     return NULL;
    986 }
    987 
    988 /*-
    989  *-----------------------------------------------------------------------
    990  * Arch_Touch --
    991  *	Touch a member of an archive.
    992  *
    993  * Input:
    994  *	gn		Node of member to touch
    995  *
    996  * Results:
    997  *	The 'time' field of the member's header is updated.
    998  *
    999  * Side Effects:
   1000  *	The modification time of the entire archive is also changed.
   1001  *	For a library, this could necessitate the re-ranlib'ing of the
   1002  *	whole thing.
   1003  *
   1004  *-----------------------------------------------------------------------
   1005  */
   1006 void
   1007 Arch_Touch(GNode *gn)
   1008 {
   1009     FILE *	  arch;	  /* Stream open to archive, positioned properly */
   1010     struct ar_hdr arh;	  /* Current header describing member */
   1011     char *p1, *p2;
   1012 
   1013     arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
   1014 			  Var_Value(MEMBER, gn, &p2),
   1015 			  &arh, "r+");
   1016 
   1017     bmake_free(p1);
   1018     bmake_free(p2);
   1019 
   1020     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
   1021 
   1022     if (arch != NULL) {
   1023 	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
   1024 	fclose(arch);
   1025     }
   1026 }
   1027 
   1028 /*-
   1029  *-----------------------------------------------------------------------
   1030  * Arch_TouchLib --
   1031  *	Given a node which represents a library, touch the thing, making
   1032  *	sure that the table of contents also is touched.
   1033  *
   1034  * Input:
   1035  *	gn		The node of the library to touch
   1036  *
   1037  * Results:
   1038  *	None.
   1039  *
   1040  * Side Effects:
   1041  *	Both the modification time of the library and of the RANLIBMAG
   1042  *	member are set to 'now'.
   1043  *
   1044  *-----------------------------------------------------------------------
   1045  */
   1046 void
   1047 Arch_TouchLib(GNode *gn)
   1048 {
   1049 #ifdef RANLIBMAG
   1050     FILE *	    arch;	/* Stream open to archive */
   1051     struct ar_hdr   arh;      	/* Header describing table of contents */
   1052     struct utimbuf  times;	/* Times for utime() call */
   1053 
   1054     arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
   1055     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
   1056 
   1057     if (arch != NULL) {
   1058 	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
   1059 	fclose(arch);
   1060 
   1061 	times.actime = times.modtime = now;
   1062 	utime(gn->path, &times);
   1063     }
   1064 #else
   1065     (void)gn;
   1066 #endif
   1067 }
   1068 
   1069 /*-
   1070  *-----------------------------------------------------------------------
   1071  * Arch_MTime --
   1072  *	Return the modification time of a member of an archive.
   1073  *
   1074  * Input:
   1075  *	gn		Node describing archive member
   1076  *
   1077  * Results:
   1078  *	The modification time(seconds).
   1079  *
   1080  * Side Effects:
   1081  *	The mtime field of the given node is filled in with the value
   1082  *	returned by the function.
   1083  *
   1084  *-----------------------------------------------------------------------
   1085  */
   1086 time_t
   1087 Arch_MTime(GNode *gn)
   1088 {
   1089     struct ar_hdr *arhPtr;    /* Header of desired member */
   1090     time_t	  modTime;    /* Modification time as an integer */
   1091     char *p1, *p2;
   1092 
   1093     arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
   1094 			     Var_Value(MEMBER, gn, &p2),
   1095 			     TRUE);
   1096 
   1097     bmake_free(p1);
   1098     bmake_free(p2);
   1099 
   1100     if (arhPtr != NULL) {
   1101 	modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
   1102     } else {
   1103 	modTime = 0;
   1104     }
   1105 
   1106     gn->mtime = modTime;
   1107     return modTime;
   1108 }
   1109 
   1110 /*-
   1111  *-----------------------------------------------------------------------
   1112  * Arch_MemMTime --
   1113  *	Given a non-existent archive member's node, get its modification
   1114  *	time from its archived form, if it exists.
   1115  *
   1116  * Results:
   1117  *	The modification time.
   1118  *
   1119  * Side Effects:
   1120  *	The mtime field is filled in.
   1121  *
   1122  *-----------------------------------------------------------------------
   1123  */
   1124 time_t
   1125 Arch_MemMTime(GNode *gn)
   1126 {
   1127     LstNode 	  ln;
   1128     GNode   	  *pgn;
   1129 
   1130     Lst_OpenS(gn->parents);
   1131     while ((ln = Lst_NextS(gn->parents)) != NULL) {
   1132 	pgn = Lst_DatumS(ln);
   1133 
   1134 	if (pgn->type & OP_ARCHV) {
   1135 	    /*
   1136 	     * If the parent is an archive specification and is being made
   1137 	     * and its member's name matches the name of the node we were
   1138 	     * given, record the modification time of the parent in the
   1139 	     * child. We keep searching its parents in case some other
   1140 	     * parent requires this child to exist...
   1141 	     */
   1142 	    const char *nameStart = strchr(pgn->name, '(') + 1;
   1143 	    const char *nameEnd = strchr(nameStart, ')');
   1144 	    size_t nameLen = (size_t)(nameEnd - nameStart);
   1145 
   1146 	    if ((pgn->flags & REMAKE) &&
   1147 		strncmp(nameStart, gn->name, nameLen) == 0) {
   1148 		gn->mtime = Arch_MTime(pgn);
   1149 	    }
   1150 	} else if (pgn->flags & REMAKE) {
   1151 	    /*
   1152 	     * Something which isn't a library depends on the existence of
   1153 	     * this target, so it needs to exist.
   1154 	     */
   1155 	    gn->mtime = 0;
   1156 	    break;
   1157 	}
   1158     }
   1159 
   1160     Lst_CloseS(gn->parents);
   1161 
   1162     return gn->mtime;
   1163 }
   1164 
   1165 /*-
   1166  *-----------------------------------------------------------------------
   1167  * Arch_FindLib --
   1168  *	Search for a library along the given search path.
   1169  *
   1170  * Input:
   1171  *	gn		Node of library to find
   1172  *	path		Search path
   1173  *
   1174  * Results:
   1175  *	None.
   1176  *
   1177  * Side Effects:
   1178  *	The node's 'path' field is set to the found path (including the
   1179  *	actual file name, not -l...). If the system can handle the -L
   1180  *	flag when linking (or we cannot find the library), we assume that
   1181  *	the user has placed the .LIBRARIES variable in the final linking
   1182  *	command (or the linker will know where to find it) and set the
   1183  *	TARGET variable for this node to be the node's name. Otherwise,
   1184  *	we set the TARGET variable to be the full path of the library,
   1185  *	as returned by Dir_FindFile.
   1186  *
   1187  *-----------------------------------------------------------------------
   1188  */
   1189 void
   1190 Arch_FindLib(GNode *gn, Lst path)
   1191 {
   1192     char	    *libName;   /* file name for archive */
   1193     size_t	     sz = strlen(gn->name) + 6 - 2;
   1194 
   1195     libName = bmake_malloc(sz);
   1196     snprintf(libName, sz, "lib%s.a", &gn->name[2]);
   1197 
   1198     gn->path = Dir_FindFile(libName, path);
   1199 
   1200     free(libName);
   1201 
   1202 #ifdef LIBRARIES
   1203     Var_Set(TARGET, gn->name, gn);
   1204 #else
   1205     Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
   1206 #endif /* LIBRARIES */
   1207 }
   1208 
   1209 /*-
   1210  *-----------------------------------------------------------------------
   1211  * Arch_LibOODate --
   1212  *	Decide if a node with the OP_LIB attribute is out-of-date. Called
   1213  *	from Make_OODate to make its life easier.
   1214  *
   1215  *	There are several ways for a library to be out-of-date that are
   1216  *	not available to ordinary files. In addition, there are ways
   1217  *	that are open to regular files that are not available to
   1218  *	libraries. A library that is only used as a source is never
   1219  *	considered out-of-date by itself. This does not preclude the
   1220  *	library's modification time from making its parent be out-of-date.
   1221  *	A library will be considered out-of-date for any of these reasons,
   1222  *	given that it is a target on a dependency line somewhere:
   1223  *	    Its modification time is less than that of one of its
   1224  *	    	  sources (gn->mtime < gn->cmgn->mtime).
   1225  *	    Its modification time is greater than the time at which the
   1226  *	    	  make began (i.e. it's been modified in the course
   1227  *	    	  of the make, probably by archiving).
   1228  *	    The modification time of one of its sources is greater than
   1229  *		  the one of its RANLIBMAG member (i.e. its table of contents
   1230  *	    	  is out-of-date). We don't compare of the archive time
   1231  *		  vs. TOC time because they can be too close. In my
   1232  *		  opinion we should not bother with the TOC at all since
   1233  *		  this is used by 'ar' rules that affect the data contents
   1234  *		  of the archive, not by ranlib rules, which affect the
   1235  *		  TOC.
   1236  *
   1237  * Input:
   1238  *	gn		The library's graph node
   1239  *
   1240  * Results:
   1241  *	TRUE if the library is out-of-date. FALSE otherwise.
   1242  *
   1243  * Side Effects:
   1244  *	The library will be hashed if it hasn't been already.
   1245  *
   1246  *-----------------------------------------------------------------------
   1247  */
   1248 Boolean
   1249 Arch_LibOODate(GNode *gn)
   1250 {
   1251     Boolean 	  oodate;
   1252 
   1253     if (gn->type & OP_PHONY) {
   1254 	oodate = TRUE;
   1255     } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
   1256 	oodate = FALSE;
   1257     } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
   1258 	       (gn->mtime > now) ||
   1259 	       (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
   1260 	oodate = TRUE;
   1261     } else {
   1262 #ifdef RANLIBMAG
   1263 	struct ar_hdr  	*arhPtr;    /* Header for __.SYMDEF */
   1264 	int 	  	modTimeTOC; /* The table-of-contents's mod time */
   1265 
   1266 	arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
   1267 
   1268 	if (arhPtr != NULL) {
   1269 	    modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
   1270 
   1271 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
   1272 		fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
   1273 	    }
   1274 	    oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
   1275 	} else {
   1276 	    /*
   1277 	     * A library w/o a table of contents is out-of-date
   1278 	     */
   1279 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
   1280 		fprintf(debug_file, "No t.o.c....");
   1281 	    }
   1282 	    oodate = TRUE;
   1283 	}
   1284 #else
   1285 	oodate = FALSE;
   1286 #endif
   1287     }
   1288     return oodate;
   1289 }
   1290 
   1291 /*-
   1292  *-----------------------------------------------------------------------
   1293  * Arch_Init --
   1294  *	Initialize things for this module.
   1295  *
   1296  * Results:
   1297  *	None.
   1298  *
   1299  * Side Effects:
   1300  *	The 'archives' list is initialized.
   1301  *
   1302  *-----------------------------------------------------------------------
   1303  */
   1304 void
   1305 Arch_Init(void)
   1306 {
   1307     archives = Lst_Init();
   1308 }
   1309 
   1310 
   1311 
   1312 /*-
   1313  *-----------------------------------------------------------------------
   1314  * Arch_End --
   1315  *	Cleanup things for this module.
   1316  *
   1317  * Results:
   1318  *	None.
   1319  *
   1320  * Side Effects:
   1321  *	The 'archives' list is freed
   1322  *
   1323  *-----------------------------------------------------------------------
   1324  */
   1325 void
   1326 Arch_End(void)
   1327 {
   1328 #ifdef CLEANUP
   1329     Lst_Destroy(archives, ArchFree);
   1330 #endif
   1331 }
   1332 
   1333 /*-
   1334  *-----------------------------------------------------------------------
   1335  * Arch_IsLib --
   1336  *	Check if the node is a library
   1337  *
   1338  * Results:
   1339  *	True or False.
   1340  *
   1341  * Side Effects:
   1342  *	None.
   1343  *
   1344  *-----------------------------------------------------------------------
   1345  */
   1346 int
   1347 Arch_IsLib(GNode *gn)
   1348 {
   1349     static const char armag[] = "!<arch>\n";
   1350     char buf[sizeof(armag)-1];
   1351     int fd;
   1352 
   1353     if ((fd = open(gn->path, O_RDONLY)) == -1)
   1354 	return FALSE;
   1355 
   1356     if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
   1357 	(void)close(fd);
   1358 	return FALSE;
   1359     }
   1360 
   1361     (void)close(fd);
   1362 
   1363     return memcmp(buf, armag, sizeof(buf)) == 0;
   1364 }
   1365