Home | History | Annotate | Line # | Download | only in make
arch.c revision 1.73
      1 /*	$NetBSD: arch.c,v 1.73 2020/07/03 08:02:55 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.73 2020/07/03 08:02:55 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.73 2020/07/03 08:02:55 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(char *, char *, Boolean);
    172 static FILE *ArchFindMember(char *, char *, struct ar_hdr *, const char *);
    173 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
    174 #define SVR4ARCHIVES
    175 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
    176 #endif
    177 
    178 #ifdef CLEANUP
    179 /*-
    180  *-----------------------------------------------------------------------
    181  * ArchFree --
    182  *	Free memory used by an archive
    183  *
    184  * Results:
    185  *	None.
    186  *
    187  * Side Effects:
    188  *	None.
    189  *
    190  *-----------------------------------------------------------------------
    191  */
    192 static void
    193 ArchFree(void *ap)
    194 {
    195     Arch *a = (Arch *)ap;
    196     Hash_Search	  search;
    197     Hash_Entry	  *entry;
    198 
    199     /* Free memory from hash entries */
    200     for (entry = Hash_EnumFirst(&a->members, &search);
    201 	 entry != NULL;
    202 	 entry = Hash_EnumNext(&search))
    203 	free(Hash_GetValue(entry));
    204 
    205     free(a->name);
    206     free(a->fnametab);
    207     Hash_DeleteTable(&a->members);
    208     free(a);
    209 }
    210 #endif
    211 
    212 
    213 
    214 /*-
    215  *-----------------------------------------------------------------------
    216  * Arch_ParseArchive --
    217  *	Parse the archive specification in the given line and find/create
    218  *	the nodes for the specified archive members, placing their nodes
    219  *	on the given list.
    220  *
    221  * Input:
    222  *	linePtr		Pointer to start of specification
    223  *	nodeLst		Lst on which to place the nodes
    224  *	ctxt		Context in which to expand variables
    225  *
    226  * Results:
    227  *	SUCCESS if it was a valid specification. The linePtr is updated
    228  *	to point to the first non-space after the archive spec. The
    229  *	nodes for the members are placed on the given list.
    230  *
    231  * Side Effects:
    232  *	Some nodes may be created. The given list is extended.
    233  *
    234  *-----------------------------------------------------------------------
    235  */
    236 ReturnStatus
    237 Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
    238 {
    239     char	    *cp;	    /* Pointer into line */
    240     GNode	    *gn;     	    /* New node */
    241     char	    *libName;  	    /* Library-part of specification */
    242     char	    *memName;  	    /* Member-part of specification */
    243     char	    *nameBuf;	    /* temporary place for node name */
    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 	    char	*result;
    261 
    262 	    result = Var_Parse(cp, ctxt, VARF_UNDEFERR|VARF_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(NULL, libName, ctxt, VARF_UNDEFERR|VARF_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 		char	*result;
    303 
    304 		result = Var_Parse(cp, ctxt, VARF_UNDEFERR|VARF_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 	    size_t   sz;
    357 
    358 	    memName = Var_Subst(NULL, memName, ctxt,
    359 				VARF_UNDEFERR|VARF_WANTRES);
    360 
    361 	    /*
    362 	     * Now form an archive spec and recurse to deal with nested
    363 	     * variables and multi-word variable values.... The results
    364 	     * are just placed at the end of the nodeLst we're returning.
    365 	     */
    366 	    sz = strlen(memName)+strlen(libName)+3;
    367 	    buf = sacrifice = bmake_malloc(sz);
    368 
    369 	    snprintf(buf, sz, "%s(%s)", libName, memName);
    370 
    371 	    if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
    372 		/*
    373 		 * Must contain dynamic sources, so we can't deal with it now.
    374 		 * Just create an ARCHV node for the thing and let
    375 		 * SuffExpandChildren handle it...
    376 		 */
    377 		gn = Targ_FindNode(buf, TARG_CREATE);
    378 
    379 		if (gn == NULL) {
    380 		    free(buf);
    381 		    return FAILURE;
    382 		} else {
    383 		    gn->type |= OP_ARCHV;
    384 		    (void)Lst_AtEnd(nodeLst, gn);
    385 		}
    386 	    } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
    387 		/*
    388 		 * Error in nested call -- free buffer and return FAILURE
    389 		 * ourselves.
    390 		 */
    391 		free(buf);
    392 		return FAILURE;
    393 	    }
    394 	    /*
    395 	     * Free buffer and continue with our work.
    396 	     */
    397 	    free(buf);
    398 	} else if (Dir_HasWildcards(memName)) {
    399 	    Lst	  members = Lst_Init(FALSE);
    400 	    char  *member;
    401 	    size_t sz = MAXPATHLEN, nsz;
    402 	    nameBuf = bmake_malloc(sz);
    403 
    404 	    Dir_Expand(memName, dirSearchPath, members);
    405 	    while (!Lst_IsEmpty(members)) {
    406 		member = (char *)Lst_DeQueue(members);
    407 		nsz = strlen(libName) + strlen(member) + 3;
    408 		if (sz > nsz)
    409 		    nameBuf = bmake_realloc(nameBuf, sz = nsz * 2);
    410 
    411 		snprintf(nameBuf, sz, "%s(%s)", libName, member);
    412 		free(member);
    413 		gn = Targ_FindNode(nameBuf, TARG_CREATE);
    414 		if (gn == NULL) {
    415 		    free(nameBuf);
    416 		    return FAILURE;
    417 		} else {
    418 		    /*
    419 		     * We've found the node, but have to make sure the rest of
    420 		     * the world knows it's an archive member, without having
    421 		     * to constantly check for parentheses, so we type the
    422 		     * thing with the OP_ARCHV bit before we place it on the
    423 		     * end of the provided list.
    424 		     */
    425 		    gn->type |= OP_ARCHV;
    426 		    (void)Lst_AtEnd(nodeLst, gn);
    427 		}
    428 	    }
    429 	    Lst_Destroy(members, NULL);
    430 	    free(nameBuf);
    431 	} else {
    432 	    size_t	sz = strlen(libName) + strlen(memName) + 3;
    433 	    nameBuf = bmake_malloc(sz);
    434 	    snprintf(nameBuf, sz, "%s(%s)", libName, memName);
    435 	    gn = Targ_FindNode(nameBuf, TARG_CREATE);
    436 	    free(nameBuf);
    437 	    if (gn == NULL) {
    438 		return FAILURE;
    439 	    } else {
    440 		/*
    441 		 * We've found the node, but have to make sure the rest of the
    442 		 * world knows it's an archive member, without having to
    443 		 * constantly check for parentheses, so we type the thing with
    444 		 * the OP_ARCHV bit before we place it on the end of the
    445 		 * provided list.
    446 		 */
    447 		gn->type |= OP_ARCHV;
    448 		(void)Lst_AtEnd(nodeLst, gn);
    449 	    }
    450 	}
    451 	if (doSubst) {
    452 	    free(memName);
    453 	}
    454 
    455 	*cp = saveChar;
    456     }
    457 
    458     /*
    459      * If substituted libName, free it now, since we need it no longer.
    460      */
    461     if (subLibName) {
    462 	free(libName);
    463     }
    464 
    465     /*
    466      * We promised the pointer would be set up at the next non-space, so
    467      * we must advance cp there before setting *linePtr... (note that on
    468      * entrance to the loop, cp is guaranteed to point at a ')')
    469      */
    470     do {
    471 	cp++;
    472     } while (*cp != '\0' && isspace ((unsigned char)*cp));
    473 
    474     *linePtr = cp;
    475     return SUCCESS;
    476 }
    477 
    478 /*-
    479  *-----------------------------------------------------------------------
    480  * ArchFindArchive --
    481  *	See if the given archive is the one we are looking for. Called
    482  *	From ArchStatMember and ArchFindMember via Lst_Find.
    483  *
    484  * Input:
    485  *	ar		Current list element
    486  *	archName	Name we want
    487  *
    488  * Results:
    489  *	0 if it is, non-zero if it isn't.
    490  *
    491  * Side Effects:
    492  *	None.
    493  *
    494  *-----------------------------------------------------------------------
    495  */
    496 static int
    497 ArchFindArchive(const void *ar, const void *archName)
    498 {
    499     return strcmp(archName, ((const Arch *)ar)->name);
    500 }
    501 
    502 /*-
    503  *-----------------------------------------------------------------------
    504  * ArchStatMember --
    505  *	Locate a member of an archive, given the path of the archive and
    506  *	the path of the desired member.
    507  *
    508  * Input:
    509  *	archive		Path to the archive
    510  *	member		Name of member. If it is a path, only the last
    511  *			component is used.
    512  *	hash		TRUE if archive should be hashed if not already so.
    513  *
    514  * Results:
    515  *	A pointer to the current struct ar_hdr structure for the member. Note
    516  *	That no position is returned, so this is not useful for touching
    517  *	archive members. This is mostly because we have no assurances that
    518  *	The archive will remain constant after we read all the headers, so
    519  *	there's not much point in remembering the position...
    520  *
    521  * Side Effects:
    522  *
    523  *-----------------------------------------------------------------------
    524  */
    525 static struct ar_hdr *
    526 ArchStatMember(char *archive, char *member, Boolean hash)
    527 {
    528 #define AR_MAX_NAME_LEN	    (sizeof(arh.ar_name)-1)
    529     FILE *	  arch;	      /* Stream to archive */
    530     int		  size;       /* Size of archive member */
    531     char	  *cp;	      /* Useful character pointer */
    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     cp = strrchr(member, '/');
    547     if (cp != NULL) {
    548 	member = cp + 1;
    549     }
    550 
    551     ln = Lst_Find(archives, archive, ArchFindArchive);
    552     if (ln != NULL) {
    553 	ar = (Arch *)Lst_Datum(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 		strncpy(copy, member, AR_MAX_NAME_LEN);
    567 		copy[AR_MAX_NAME_LEN] = '\0';
    568 	    }
    569 	    if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
    570 		return (struct ar_hdr *)Hash_GetValue(he);
    571 	    return NULL;
    572 	}
    573     }
    574 
    575     if (!hash) {
    576 	/*
    577 	 * Caller doesn't want the thing hashed, just use ArchFindMember
    578 	 * to read the header for the member out and close down the stream
    579 	 * again. Since the archive is not to be hashed, we assume there's
    580 	 * no need to allocate extra room for the header we're returning,
    581 	 * so just declare it static.
    582 	 */
    583 	 static struct ar_hdr	sarh;
    584 
    585 	 arch = ArchFindMember(archive, member, &sarh, "r");
    586 
    587 	 if (arch == NULL) {
    588 	    return NULL;
    589 	} else {
    590 	    fclose(arch);
    591 	    return &sarh;
    592 	}
    593     }
    594 
    595     /*
    596      * We don't have this archive on the list yet, so we want to find out
    597      * everything that's in it and cache it so we can get at it quickly.
    598      */
    599     arch = fopen(archive, "r");
    600     if (arch == NULL) {
    601 	return NULL;
    602     }
    603 
    604     /*
    605      * We use the ARMAG string to make sure this is an archive we
    606      * can handle...
    607      */
    608     if ((fread(magic, SARMAG, 1, arch) != 1) ||
    609     	(strncmp(magic, ARMAG, SARMAG) != 0)) {
    610 	    fclose(arch);
    611 	    return NULL;
    612     }
    613 
    614     ar = bmake_malloc(sizeof(Arch));
    615     ar->name = bmake_strdup(archive);
    616     ar->fnametab = NULL;
    617     ar->fnamesize = 0;
    618     Hash_InitTable(&ar->members, -1);
    619     memName[AR_MAX_NAME_LEN] = '\0';
    620 
    621     while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
    622 	if (strncmp( arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
    623 	    /*
    624 	     * The header is bogus, so the archive is bad
    625 	     * and there's no way we can recover...
    626 	     */
    627 	    goto badarch;
    628 	} else {
    629 	    /*
    630 	     * We need to advance the stream's pointer to the start of the
    631 	     * next header. Files are padded with newlines to an even-byte
    632 	     * boundary, so we need to extract the size of the file from the
    633 	     * 'size' field of the header and round it up during the seek.
    634 	     */
    635 	    arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
    636 	    size = (int)strtol(arh.ar_size, NULL, 10);
    637 
    638 	    memcpy(memName, arh.ar_name, sizeof(arh.ar_name));
    639 	    for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
    640 		continue;
    641 	    }
    642 	    cp[1] = '\0';
    643 
    644 #ifdef SVR4ARCHIVES
    645 	    /*
    646 	     * svr4 names are slash terminated. Also svr4 extended AR format.
    647 	     */
    648 	    if (memName[0] == '/') {
    649 		/*
    650 		 * svr4 magic mode; handle it
    651 		 */
    652 		switch (ArchSVR4Entry(ar, memName, size, arch)) {
    653 		case -1:  /* Invalid data */
    654 		    goto badarch;
    655 		case 0:	  /* List of files entry */
    656 		    continue;
    657 		default:  /* Got the entry */
    658 		    break;
    659 		}
    660 	    }
    661 	    else {
    662 		if (cp[0] == '/')
    663 		    cp[0] = '\0';
    664 	    }
    665 #endif
    666 
    667 #ifdef AR_EFMT1
    668 	    /*
    669 	     * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    670 	     * first <namelen> bytes of the file
    671 	     */
    672 	    if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
    673 		isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) {
    674 
    675 		unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
    676 
    677 		if (elen > MAXPATHLEN)
    678 			goto badarch;
    679 		if (fread(memName, elen, 1, arch) != 1)
    680 			goto badarch;
    681 		memName[elen] = '\0';
    682 		if (fseek(arch, -elen, SEEK_CUR) != 0)
    683 			goto badarch;
    684 		if (DEBUG(ARCH) || DEBUG(MAKE)) {
    685 		    fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName);
    686 		}
    687 	    }
    688 #endif
    689 
    690 	    he = Hash_CreateEntry(&ar->members, memName, NULL);
    691 	    Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
    692 	    memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
    693 	}
    694 	if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
    695 	    goto badarch;
    696     }
    697 
    698     fclose(arch);
    699 
    700     (void)Lst_AtEnd(archives, ar);
    701 
    702     /*
    703      * Now that the archive has been read and cached, we can look into
    704      * the hash table to find the desired member's header.
    705      */
    706     he = Hash_FindEntry(&ar->members, member);
    707 
    708     if (he != NULL) {
    709 	return (struct ar_hdr *)Hash_GetValue(he);
    710     } else {
    711 	return NULL;
    712     }
    713 
    714 badarch:
    715     fclose(arch);
    716     Hash_DeleteTable(&ar->members);
    717     free(ar->fnametab);
    718     free(ar);
    719     return NULL;
    720 }
    721 
    722 #ifdef SVR4ARCHIVES
    723 /*-
    724  *-----------------------------------------------------------------------
    725  * ArchSVR4Entry --
    726  *	Parse an SVR4 style entry that begins with a slash.
    727  *	If it is "//", then load the table of filenames
    728  *	If it is "/<offset>", then try to substitute the long file name
    729  *	from offset of a table previously read.
    730  *
    731  * Results:
    732  *	-1: Bad data in archive
    733  *	 0: A table was loaded from the file
    734  *	 1: Name was successfully substituted from table
    735  *	 2: Name was not successfully substituted from table
    736  *
    737  * Side Effects:
    738  *	If a table is read, the file pointer is moved to the next archive
    739  *	member
    740  *
    741  *-----------------------------------------------------------------------
    742  */
    743 static int
    744 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
    745 {
    746 #define ARLONGNAMES1 "//"
    747 #define ARLONGNAMES2 "/ARFILENAMES"
    748     size_t entry;
    749     char *ptr, *eptr;
    750 
    751     if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
    752 	strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
    753 
    754 	if (ar->fnametab != NULL) {
    755 	    if (DEBUG(ARCH)) {
    756 		fprintf(debug_file, "Attempted to redefine an SVR4 name table\n");
    757 	    }
    758 	    return -1;
    759 	}
    760 
    761 	/*
    762 	 * This is a table of archive names, so we build one for
    763 	 * ourselves
    764 	 */
    765 	ar->fnametab = bmake_malloc(size);
    766 	ar->fnamesize = size;
    767 
    768 	if (fread(ar->fnametab, size, 1, arch) != 1) {
    769 	    if (DEBUG(ARCH)) {
    770 		fprintf(debug_file, "Reading an SVR4 name table failed\n");
    771 	    }
    772 	    return -1;
    773 	}
    774 	eptr = ar->fnametab + size;
    775 	for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
    776 	    switch (*ptr) {
    777 	    case '/':
    778 		entry++;
    779 		*ptr = '\0';
    780 		break;
    781 
    782 	    case '\n':
    783 		break;
    784 
    785 	    default:
    786 		break;
    787 	    }
    788 	if (DEBUG(ARCH)) {
    789 	    fprintf(debug_file, "Found svr4 archive name table with %lu entries\n",
    790 	            (unsigned long)entry);
    791 	}
    792 	return 0;
    793     }
    794 
    795     if (name[1] == ' ' || name[1] == '\0')
    796 	return 2;
    797 
    798     entry = (size_t)strtol(&name[1], &eptr, 0);
    799     if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
    800 	if (DEBUG(ARCH)) {
    801 	    fprintf(debug_file, "Could not parse SVR4 name %s\n", name);
    802 	}
    803 	return 2;
    804     }
    805     if (entry >= ar->fnamesize) {
    806 	if (DEBUG(ARCH)) {
    807 	    fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n",
    808 		   name, (unsigned long)ar->fnamesize);
    809 	}
    810 	return 2;
    811     }
    812 
    813     if (DEBUG(ARCH)) {
    814 	fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
    815     }
    816 
    817     (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
    818     name[MAXPATHLEN] = '\0';
    819     return 1;
    820 }
    821 #endif
    822 
    823 
    824 /*-
    825  *-----------------------------------------------------------------------
    826  * ArchFindMember --
    827  *	Locate a member of an archive, given the path of the archive and
    828  *	the path of the desired member. If the archive is to be modified,
    829  *	the mode should be "r+", if not, it should be "r".
    830  *
    831  * Input:
    832  *	archive		Path to the archive
    833  *	member		Name of member. If it is a path, only the last
    834  *			component is used.
    835  *	arhPtr		Pointer to header structure to be filled in
    836  *	mode		The mode for opening the stream
    837  *
    838  * Results:
    839  *	An FILE *, opened for reading and writing, positioned at the
    840  *	start of the member's struct ar_hdr, or NULL if the member was
    841  *	nonexistent. The current struct ar_hdr for member.
    842  *
    843  * Side Effects:
    844  *	The passed struct ar_hdr structure is filled in.
    845  *
    846  *-----------------------------------------------------------------------
    847  */
    848 static FILE *
    849 ArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr,
    850     const char *mode)
    851 {
    852     FILE *	  arch;	      /* Stream to archive */
    853     int		  size;       /* Size of archive member */
    854     char	  *cp;	      /* Useful character pointer */
    855     char	  magic[SARMAG];
    856     size_t	  len, tlen;
    857 
    858     arch = fopen(archive, mode);
    859     if (arch == NULL) {
    860 	return NULL;
    861     }
    862 
    863     /*
    864      * We use the ARMAG string to make sure this is an archive we
    865      * can handle...
    866      */
    867     if ((fread(magic, SARMAG, 1, arch) != 1) ||
    868     	(strncmp(magic, ARMAG, SARMAG) != 0)) {
    869 	    fclose(arch);
    870 	    return NULL;
    871     }
    872 
    873     /*
    874      * Because of space constraints and similar things, files are archived
    875      * using their final path components, not the entire thing, so we need
    876      * to point 'member' to the final component, if there is one, to make
    877      * the comparisons easier...
    878      */
    879     cp = strrchr(member, '/');
    880     if (cp != NULL) {
    881 	member = cp + 1;
    882     }
    883     len = tlen = strlen(member);
    884     if (len > sizeof(arhPtr->ar_name)) {
    885 	tlen = sizeof(arhPtr->ar_name);
    886     }
    887 
    888     while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
    889 	if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
    890 	     /*
    891 	      * The header is bogus, so the archive is bad
    892 	      * and there's no way we can recover...
    893 	      */
    894 	     fclose(arch);
    895 	     return NULL;
    896 	} else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
    897 	    /*
    898 	     * If the member's name doesn't take up the entire 'name' field,
    899 	     * we have to be careful of matching prefixes. Names are space-
    900 	     * padded to the right, so if the character in 'name' at the end
    901 	     * of the matched string is anything but a space, this isn't the
    902 	     * member we sought.
    903 	     */
    904 	    if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
    905 		goto skip;
    906 	    } else {
    907 		/*
    908 		 * To make life easier, we reposition the file at the start
    909 		 * of the header we just read before we return the stream.
    910 		 * In a more general situation, it might be better to leave
    911 		 * the file at the actual member, rather than its header, but
    912 		 * not here...
    913 		 */
    914 		if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) != 0) {
    915 		    fclose(arch);
    916 		    return NULL;
    917 		}
    918 		return arch;
    919 	    }
    920 	} else
    921 #ifdef AR_EFMT1
    922 		/*
    923 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    924 		 * first <namelen> bytes of the file
    925 		 */
    926 	    if (strncmp(arhPtr->ar_name, AR_EFMT1,
    927 					sizeof(AR_EFMT1) - 1) == 0 &&
    928 		isdigit((unsigned char)arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
    929 
    930 		unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
    931 		char ename[MAXPATHLEN + 1];
    932 
    933 		if (elen > MAXPATHLEN) {
    934 			fclose(arch);
    935 			return NULL;
    936 		}
    937 		if (fread(ename, elen, 1, arch) != 1) {
    938 			fclose(arch);
    939 			return NULL;
    940 		}
    941 		ename[elen] = '\0';
    942 		if (DEBUG(ARCH) || DEBUG(MAKE)) {
    943 		    fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename);
    944 		}
    945 		if (strncmp(ename, member, len) == 0) {
    946 			/* Found as extended name */
    947 			if (fseek(arch, -sizeof(struct ar_hdr) - elen,
    948 				SEEK_CUR) != 0) {
    949 			    fclose(arch);
    950 			    return NULL;
    951 			}
    952 			return arch;
    953 		}
    954 		if (fseek(arch, -elen, SEEK_CUR) != 0) {
    955 		    fclose(arch);
    956 		    return NULL;
    957 		}
    958 		goto skip;
    959 	} else
    960 #endif
    961 	{
    962 skip:
    963 	    /*
    964 	     * This isn't the member we're after, so we need to advance the
    965 	     * stream's pointer to the start of the next header. Files are
    966 	     * padded with newlines to an even-byte boundary, so we need to
    967 	     * extract the size of the file from the 'size' field of the
    968 	     * header and round it up during the seek.
    969 	     */
    970 	    arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
    971 	    size = (int)strtol(arhPtr->ar_size, NULL, 10);
    972 	    if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
    973 		fclose(arch);
    974 		return NULL;
    975 	    }
    976 	}
    977     }
    978 
    979     /*
    980      * We've looked everywhere, but the member is not to be found. Close the
    981      * archive and return NULL -- an error.
    982      */
    983     fclose(arch);
    984     return NULL;
    985 }
    986 
    987 /*-
    988  *-----------------------------------------------------------------------
    989  * Arch_Touch --
    990  *	Touch a member of an archive.
    991  *
    992  * Input:
    993  *	gn		Node of member to touch
    994  *
    995  * Results:
    996  *	The 'time' field of the member's header is updated.
    997  *
    998  * Side Effects:
    999  *	The modification time of the entire archive is also changed.
   1000  *	For a library, this could necessitate the re-ranlib'ing of the
   1001  *	whole thing.
   1002  *
   1003  *-----------------------------------------------------------------------
   1004  */
   1005 void
   1006 Arch_Touch(GNode *gn)
   1007 {
   1008     FILE *	  arch;	  /* Stream open to archive, positioned properly */
   1009     struct ar_hdr arh;	  /* Current header describing member */
   1010     char *p1, *p2;
   1011 
   1012     arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
   1013 			  Var_Value(MEMBER, gn, &p2),
   1014 			  &arh, "r+");
   1015 
   1016     free(p1);
   1017     free(p2);
   1018 
   1019     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
   1020 
   1021     if (arch != NULL) {
   1022 	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
   1023 	fclose(arch);
   1024     }
   1025 }
   1026 
   1027 /*-
   1028  *-----------------------------------------------------------------------
   1029  * Arch_TouchLib --
   1030  *	Given a node which represents a library, touch the thing, making
   1031  *	sure that the table of contents also is touched.
   1032  *
   1033  * Input:
   1034  *	gn		The node of the library to touch
   1035  *
   1036  * Results:
   1037  *	None.
   1038  *
   1039  * Side Effects:
   1040  *	Both the modification time of the library and of the RANLIBMAG
   1041  *	member are set to 'now'.
   1042  *
   1043  *-----------------------------------------------------------------------
   1044  */
   1045 void
   1046 #if !defined(RANLIBMAG)
   1047 Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
   1048 #else
   1049 Arch_TouchLib(GNode *gn)
   1050 #endif
   1051 {
   1052 #ifdef RANLIBMAG
   1053     FILE *	    arch;	/* Stream open to archive */
   1054     struct ar_hdr   arh;      	/* Header describing table of contents */
   1055     struct utimbuf  times;	/* Times for utime() call */
   1056 
   1057     arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+");
   1058     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
   1059 
   1060     if (arch != NULL) {
   1061 	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
   1062 	fclose(arch);
   1063 
   1064 	times.actime = times.modtime = now;
   1065 	utime(gn->path, &times);
   1066     }
   1067 #endif
   1068 }
   1069 
   1070 /*-
   1071  *-----------------------------------------------------------------------
   1072  * Arch_MTime --
   1073  *	Return the modification time of a member of an archive.
   1074  *
   1075  * Input:
   1076  *	gn		Node describing archive member
   1077  *
   1078  * Results:
   1079  *	The modification time(seconds).
   1080  *
   1081  * Side Effects:
   1082  *	The mtime field of the given node is filled in with the value
   1083  *	returned by the function.
   1084  *
   1085  *-----------------------------------------------------------------------
   1086  */
   1087 time_t
   1088 Arch_MTime(GNode *gn)
   1089 {
   1090     struct ar_hdr *arhPtr;    /* Header of desired member */
   1091     time_t	  modTime;    /* Modification time as an integer */
   1092     char *p1, *p2;
   1093 
   1094     arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
   1095 			     Var_Value(MEMBER, gn, &p2),
   1096 			     TRUE);
   1097 
   1098     free(p1);
   1099     free(p2);
   1100 
   1101     if (arhPtr != NULL) {
   1102 	modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
   1103     } else {
   1104 	modTime = 0;
   1105     }
   1106 
   1107     gn->mtime = modTime;
   1108     return modTime;
   1109 }
   1110 
   1111 /*-
   1112  *-----------------------------------------------------------------------
   1113  * Arch_MemMTime --
   1114  *	Given a non-existent archive member's node, get its modification
   1115  *	time from its archived form, if it exists.
   1116  *
   1117  * Results:
   1118  *	The modification time.
   1119  *
   1120  * Side Effects:
   1121  *	The mtime field is filled in.
   1122  *
   1123  *-----------------------------------------------------------------------
   1124  */
   1125 time_t
   1126 Arch_MemMTime(GNode *gn)
   1127 {
   1128     LstNode 	  ln;
   1129     GNode   	  *pgn;
   1130     char    	  *nameStart,
   1131 		  *nameEnd;
   1132 
   1133     if (Lst_Open(gn->parents) != SUCCESS) {
   1134 	gn->mtime = 0;
   1135 	return 0;
   1136     }
   1137     while ((ln = Lst_Next(gn->parents)) != NULL) {
   1138 	pgn = (GNode *)Lst_Datum(ln);
   1139 
   1140 	if (pgn->type & OP_ARCHV) {
   1141 	    /*
   1142 	     * If the parent is an archive specification and is being made
   1143 	     * and its member's name matches the name of the node we were
   1144 	     * given, record the modification time of the parent in the
   1145 	     * child. We keep searching its parents in case some other
   1146 	     * parent requires this child to exist...
   1147 	     */
   1148 	    nameStart = strchr(pgn->name, '(') + 1;
   1149 	    nameEnd = strchr(nameStart, ')');
   1150 
   1151 	    if ((pgn->flags & REMAKE) &&
   1152 		strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
   1153 				     gn->mtime = Arch_MTime(pgn);
   1154 	    }
   1155 	} else if (pgn->flags & REMAKE) {
   1156 	    /*
   1157 	     * Something which isn't a library depends on the existence of
   1158 	     * this target, so it needs to exist.
   1159 	     */
   1160 	    gn->mtime = 0;
   1161 	    break;
   1162 	}
   1163     }
   1164 
   1165     Lst_Close(gn->parents);
   1166 
   1167     return gn->mtime;
   1168 }
   1169 
   1170 /*-
   1171  *-----------------------------------------------------------------------
   1172  * Arch_FindLib --
   1173  *	Search for a library along the given search path.
   1174  *
   1175  * Input:
   1176  *	gn		Node of library to find
   1177  *	path		Search path
   1178  *
   1179  * Results:
   1180  *	None.
   1181  *
   1182  * Side Effects:
   1183  *	The node's 'path' field is set to the found path (including the
   1184  *	actual file name, not -l...). If the system can handle the -L
   1185  *	flag when linking (or we cannot find the library), we assume that
   1186  *	the user has placed the .LIBRARIES variable in the final linking
   1187  *	command (or the linker will know where to find it) and set the
   1188  *	TARGET variable for this node to be the node's name. Otherwise,
   1189  *	we set the TARGET variable to be the full path of the library,
   1190  *	as returned by Dir_FindFile.
   1191  *
   1192  *-----------------------------------------------------------------------
   1193  */
   1194 void
   1195 Arch_FindLib(GNode *gn, Lst path)
   1196 {
   1197     char	    *libName;   /* file name for archive */
   1198     size_t	     sz = strlen(gn->name) + 6 - 2;
   1199 
   1200     libName = bmake_malloc(sz);
   1201     snprintf(libName, sz, "lib%s.a", &gn->name[2]);
   1202 
   1203     gn->path = Dir_FindFile(libName, path);
   1204 
   1205     free(libName);
   1206 
   1207 #ifdef LIBRARIES
   1208     Var_Set(TARGET, gn->name, gn);
   1209 #else
   1210     Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
   1211 #endif /* LIBRARIES */
   1212 }
   1213 
   1214 /*-
   1215  *-----------------------------------------------------------------------
   1216  * Arch_LibOODate --
   1217  *	Decide if a node with the OP_LIB attribute is out-of-date. Called
   1218  *	from Make_OODate to make its life easier.
   1219  *
   1220  *	There are several ways for a library to be out-of-date that are
   1221  *	not available to ordinary files. In addition, there are ways
   1222  *	that are open to regular files that are not available to
   1223  *	libraries. A library that is only used as a source is never
   1224  *	considered out-of-date by itself. This does not preclude the
   1225  *	library's modification time from making its parent be out-of-date.
   1226  *	A library will be considered out-of-date for any of these reasons,
   1227  *	given that it is a target on a dependency line somewhere:
   1228  *	    Its modification time is less than that of one of its
   1229  *	    	  sources (gn->mtime < gn->cmgn->mtime).
   1230  *	    Its modification time is greater than the time at which the
   1231  *	    	  make began (i.e. it's been modified in the course
   1232  *	    	  of the make, probably by archiving).
   1233  *	    The modification time of one of its sources is greater than
   1234  *		  the one of its RANLIBMAG member (i.e. its table of contents
   1235  *	    	  is out-of-date). We don't compare of the archive time
   1236  *		  vs. TOC time because they can be too close. In my
   1237  *		  opinion we should not bother with the TOC at all since
   1238  *		  this is used by 'ar' rules that affect the data contents
   1239  *		  of the archive, not by ranlib rules, which affect the
   1240  *		  TOC.
   1241  *
   1242  * Input:
   1243  *	gn		The library's graph node
   1244  *
   1245  * Results:
   1246  *	TRUE if the library is out-of-date. FALSE otherwise.
   1247  *
   1248  * Side Effects:
   1249  *	The library will be hashed if it hasn't been already.
   1250  *
   1251  *-----------------------------------------------------------------------
   1252  */
   1253 Boolean
   1254 Arch_LibOODate(GNode *gn)
   1255 {
   1256     Boolean 	  oodate;
   1257 
   1258     if (gn->type & OP_PHONY) {
   1259 	oodate = TRUE;
   1260     } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
   1261 	oodate = FALSE;
   1262     } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
   1263 	       (gn->mtime > now) ||
   1264 	       (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
   1265 	oodate = TRUE;
   1266     } else {
   1267 #ifdef RANLIBMAG
   1268 	struct ar_hdr  	*arhPtr;    /* Header for __.SYMDEF */
   1269 	int 	  	modTimeTOC; /* The table-of-contents's mod time */
   1270 
   1271 	arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE);
   1272 
   1273 	if (arhPtr != NULL) {
   1274 	    modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
   1275 
   1276 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
   1277 		fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
   1278 	    }
   1279 	    oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
   1280 	} else {
   1281 	    /*
   1282 	     * A library w/o a table of contents is out-of-date
   1283 	     */
   1284 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
   1285 		fprintf(debug_file, "No t.o.c....");
   1286 	    }
   1287 	    oodate = TRUE;
   1288 	}
   1289 #else
   1290 	oodate = FALSE;
   1291 #endif
   1292     }
   1293     return oodate;
   1294 }
   1295 
   1296 /*-
   1297  *-----------------------------------------------------------------------
   1298  * Arch_Init --
   1299  *	Initialize things for this module.
   1300  *
   1301  * Results:
   1302  *	None.
   1303  *
   1304  * Side Effects:
   1305  *	The 'archives' list is initialized.
   1306  *
   1307  *-----------------------------------------------------------------------
   1308  */
   1309 void
   1310 Arch_Init(void)
   1311 {
   1312     archives = Lst_Init(FALSE);
   1313 }
   1314 
   1315 
   1316 
   1317 /*-
   1318  *-----------------------------------------------------------------------
   1319  * Arch_End --
   1320  *	Cleanup things for this module.
   1321  *
   1322  * Results:
   1323  *	None.
   1324  *
   1325  * Side Effects:
   1326  *	The 'archives' list is freed
   1327  *
   1328  *-----------------------------------------------------------------------
   1329  */
   1330 void
   1331 Arch_End(void)
   1332 {
   1333 #ifdef CLEANUP
   1334     Lst_Destroy(archives, ArchFree);
   1335 #endif
   1336 }
   1337 
   1338 /*-
   1339  *-----------------------------------------------------------------------
   1340  * Arch_IsLib --
   1341  *	Check if the node is a library
   1342  *
   1343  * Results:
   1344  *	True or False.
   1345  *
   1346  * Side Effects:
   1347  *	None.
   1348  *
   1349  *-----------------------------------------------------------------------
   1350  */
   1351 int
   1352 Arch_IsLib(GNode *gn)
   1353 {
   1354     static const char armag[] = "!<arch>\n";
   1355     char buf[sizeof(armag)-1];
   1356     int fd;
   1357 
   1358     if ((fd = open(gn->path, O_RDONLY)) == -1)
   1359 	return FALSE;
   1360 
   1361     if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
   1362 	(void)close(fd);
   1363 	return FALSE;
   1364     }
   1365 
   1366     (void)close(fd);
   1367 
   1368     return memcmp(buf, armag, sizeof(buf)) == 0;
   1369 }
   1370