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