Home | History | Annotate | Line # | Download | only in make
arch.c revision 1.135
      1 /*	$NetBSD: arch.c,v 1.135 2020/10/18 12:36:43 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 /*-
     72  * arch.c --
     73  *	Functions to manipulate libraries, archives and their members.
     74  *
     75  *	Once again, cacheing/hashing comes into play in the manipulation
     76  * of archives. The first time an archive is referenced, all of its members'
     77  * headers are read and hashed and the archive closed again. All hashed
     78  * archives are kept on a list which is searched each time an archive member
     79  * is referenced.
     80  *
     81  * The interface to this module is:
     82  *	Arch_ParseArchive
     83  *			Given an archive specification, return a list
     84  *			of GNode's, one for each member in the spec.
     85  *			FALSE is returned if the specification is
     86  *			invalid for some reason.
     87  *
     88  *	Arch_Touch	Alter the modification time of the archive
     89  *			member described by the given node to be
     90  *			the current time.
     91  *
     92  *	Arch_TouchLib	Update the modification time of the library
     93  *			described by the given node. This is special
     94  *			because it also updates the modification time
     95  *			of the library's table of contents.
     96  *
     97  *	Arch_MTime	Find the modification time of a member of
     98  *			an archive *in the archive*. The time is also
     99  *			placed in the member's GNode. Returns the
    100  *			modification time.
    101  *
    102  *	Arch_MemTime	Find the modification time of a member of
    103  *			an archive. Called when the member doesn't
    104  *			already exist. Looks in the archive for the
    105  *			modification time. Returns the modification
    106  *			time.
    107  *
    108  *	Arch_FindLib	Search for a library along a path. The
    109  *			library name in the GNode should be in
    110  *			-l<name> format.
    111  *
    112  *	Arch_LibOODate	Special function to decide if a library node
    113  *			is out-of-date.
    114  *
    115  *	Arch_Init	Initialize this module.
    116  *
    117  *	Arch_End	Clean up this module.
    118  */
    119 
    120 #include    <sys/types.h>
    121 #include    <sys/stat.h>
    122 #include    <sys/time.h>
    123 #include    <sys/param.h>
    124 
    125 #include    <ar.h>
    126 #include    <utime.h>
    127 
    128 #include    "make.h"
    129 #include    "dir.h"
    130 #include    "config.h"
    131 
    132 /*	"@(#)arch.c	8.2 (Berkeley) 1/2/94"	*/
    133 MAKE_RCSID("$NetBSD: arch.c,v 1.135 2020/10/18 12:36:43 rillig Exp $");
    134 
    135 #ifdef TARGET_MACHINE
    136 #undef MAKE_MACHINE
    137 #define MAKE_MACHINE TARGET_MACHINE
    138 #endif
    139 #ifdef TARGET_MACHINE_ARCH
    140 #undef MAKE_MACHINE_ARCH
    141 #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
    142 #endif
    143 
    144 typedef struct List ArchList;
    145 typedef struct ListNode ArchListNode;
    146 
    147 static ArchList *archives;	/* The archives we've already examined */
    148 
    149 typedef struct Arch {
    150     char *name;			/* Name of archive */
    151     HashTable members;		/* All the members of the archive described
    152 				 * by <name, struct ar_hdr *> key/value pairs */
    153     char *fnametab;		/* Extended name table strings */
    154     size_t fnamesize;		/* Size of the string table */
    155 } Arch;
    156 
    157 static FILE *ArchFindMember(const char *, const char *,
    158 			    struct ar_hdr *, const char *);
    159 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
    160 #define SVR4ARCHIVES
    161 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
    162 #endif
    163 
    164 #ifdef CLEANUP
    165 static void
    166 ArchFree(void *ap)
    167 {
    168     Arch *a = (Arch *)ap;
    169     HashIter hi;
    170     HashEntry *he;
    171 
    172     /* Free memory from hash entries */
    173     HashIter_Init(&hi, &a->members);
    174     while ((he = HashIter_Next(&hi)) != NULL)
    175 	free(Hash_GetValue(he));
    176 
    177     free(a->name);
    178     free(a->fnametab);
    179     Hash_DeleteTable(&a->members);
    180     free(a);
    181 }
    182 #endif
    183 
    184 
    185 /*-
    186  *-----------------------------------------------------------------------
    187  * Arch_ParseArchive --
    188  *	Parse the archive specification in the given line and find/create
    189  *	the nodes for the specified archive members, placing their nodes
    190  *	on the given list.
    191  *
    192  * Input:
    193  *	linePtr		Pointer to start of specification
    194  *	nodeLst		Lst on which to place the nodes
    195  *	ctxt		Context in which to expand variables
    196  *
    197  * Results:
    198  *	TRUE if it was a valid specification. The linePtr is updated
    199  *	to point to the first non-space after the archive spec. The
    200  *	nodes for the members are placed on the given list.
    201  *-----------------------------------------------------------------------
    202  */
    203 Boolean
    204 Arch_ParseArchive(char **linePtr, GNodeList *nodeLst, GNode *ctxt)
    205 {
    206     char *cp;			/* Pointer into line */
    207     GNode *gn;			/* New node */
    208     char *libName;		/* Library-part of specification */
    209     char *memName;		/* Member-part of specification */
    210     char saveChar;		/* Ending delimiter of member-name */
    211     Boolean subLibName;		/* TRUE if libName should have/had
    212 				 * variable substitution performed on it */
    213 
    214     libName = *linePtr;
    215 
    216     subLibName = FALSE;
    217 
    218     for (cp = libName; *cp != '(' && *cp != '\0';) {
    219 	if (*cp == '$') {
    220 	    /*
    221 	     * Variable spec, so call the Var module to parse the puppy
    222 	     * so we can safely advance beyond it...
    223 	     */
    224 	    const char *nested_p = cp;
    225 	    void *result_freeIt;
    226 	    const char *result;
    227 	    Boolean isError;
    228 
    229 	    (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES,
    230 			    &result, &result_freeIt);
    231 	    /* TODO: handle errors */
    232 	    isError = result == var_Error;
    233 	    free(result_freeIt);
    234 	    if (isError)
    235 		return FALSE;
    236 
    237 	    subLibName = TRUE;
    238 	    cp += nested_p - cp;
    239 	} else
    240 	    cp++;
    241     }
    242 
    243     *cp++ = '\0';
    244     if (subLibName) {
    245 	(void)Var_Subst(libName, ctxt, VARE_UNDEFERR|VARE_WANTRES, &libName);
    246 	/* TODO: handle errors */
    247     }
    248 
    249 
    250     for (;;) {
    251 	/*
    252 	 * First skip to the start of the member's name, mark that
    253 	 * place and skip to the end of it (either white-space or
    254 	 * a close paren).
    255 	 */
    256 	Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
    257 
    258 	while (*cp != '\0' && *cp != ')' && ch_isspace(*cp)) {
    259 	    cp++;
    260 	}
    261 	memName = cp;
    262 	while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
    263 	    if (*cp == '$') {
    264 		/*
    265 		 * Variable spec, so call the Var module to parse the puppy
    266 		 * so we can safely advance beyond it...
    267 		 */
    268 		void *freeIt;
    269 		const char *result;
    270 		Boolean isError;
    271 		const char *nested_p = cp;
    272 
    273 		(void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES,
    274 				&result, &freeIt);
    275 		/* TODO: handle errors */
    276 		isError = result == var_Error;
    277 		free(freeIt);
    278 
    279 		if (isError)
    280 		    return FALSE;
    281 
    282 		doSubst = TRUE;
    283 		cp += nested_p - cp;
    284 	    } else {
    285 		cp++;
    286 	    }
    287 	}
    288 
    289 	/*
    290 	 * If the specification ends without a closing parenthesis,
    291 	 * chances are there's something wrong (like a missing backslash),
    292 	 * so it's better to return failure than allow such things to happen
    293 	 */
    294 	if (*cp == '\0') {
    295 	    printf("No closing parenthesis in archive specification\n");
    296 	    return FALSE;
    297 	}
    298 
    299 	/*
    300 	 * If we didn't move anywhere, we must be done
    301 	 */
    302 	if (cp == memName) {
    303 	    break;
    304 	}
    305 
    306 	saveChar = *cp;
    307 	*cp = '\0';
    308 
    309 	/*
    310 	 * XXX: This should be taken care of intelligently by
    311 	 * SuffExpandChildren, both for the archive and the member portions.
    312 	 */
    313 	/*
    314 	 * If member contains variables, try and substitute for them.
    315 	 * This will slow down archive specs with dynamic sources, of course,
    316 	 * since we'll be (non-)substituting them three times, but them's
    317 	 * the breaks -- we need to do this since SuffExpandChildren calls
    318 	 * us, otherwise we could assume the thing would be taken care of
    319 	 * later.
    320 	 */
    321 	if (doSubst) {
    322 	    char *buf;
    323 	    char *sacrifice;
    324 	    char *oldMemName = memName;
    325 
    326 	    (void)Var_Subst(memName, ctxt, VARE_UNDEFERR|VARE_WANTRES,
    327 			    &memName);
    328 	    /* TODO: handle errors */
    329 
    330 	    /*
    331 	     * Now form an archive spec and recurse to deal with nested
    332 	     * variables and multi-word variable values.... The results
    333 	     * are just placed at the end of the nodeLst we're returning.
    334 	     */
    335 	    buf = sacrifice = str_concat4(libName, "(", memName, ")");
    336 
    337 	    if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
    338 		/*
    339 		 * Must contain dynamic sources, so we can't deal with it now.
    340 		 * Just create an ARCHV node for the thing and let
    341 		 * SuffExpandChildren handle it...
    342 		 */
    343 		gn = Targ_GetNode(buf);
    344 		gn->type |= OP_ARCHV;
    345 		Lst_Append(nodeLst, gn);
    346 
    347 	    } else if (!Arch_ParseArchive(&sacrifice, nodeLst, ctxt)) {
    348 		/*
    349 		 * Error in nested call -- free buffer and return FALSE
    350 		 * ourselves.
    351 		 */
    352 		free(buf);
    353 		return FALSE;
    354 	    }
    355 	    /*
    356 	     * Free buffer and continue with our work.
    357 	     */
    358 	    free(buf);
    359 	} else if (Dir_HasWildcards(memName)) {
    360 	    StringList *members = Lst_Init();
    361 	    Dir_Expand(memName, dirSearchPath, members);
    362 
    363 	    while (!Lst_IsEmpty(members)) {
    364 		char *member = Lst_Dequeue(members);
    365 		char *fullname = str_concat4(libName, "(", member, ")");
    366 		free(member);
    367 
    368 		gn = Targ_GetNode(fullname);
    369 		free(fullname);
    370 
    371 		/*
    372 		 * We've found the node, but have to make sure the rest of
    373 		 * the world knows it's an archive member, without having
    374 		 * to constantly check for parentheses, so we type the
    375 		 * thing with the OP_ARCHV bit before we place it on the
    376 		 * end of the provided list.
    377 		 */
    378 		gn->type |= OP_ARCHV;
    379 		Lst_Append(nodeLst, gn);
    380 	    }
    381 	    Lst_Free(members);
    382 	} else {
    383 	    char *fullname = str_concat4(libName, "(", memName, ")");
    384 	    gn = Targ_GetNode(fullname);
    385 	    free(fullname);
    386 
    387 	    /*
    388 	     * We've found the node, but have to make sure the rest of the
    389 	     * world knows it's an archive member, without having to
    390 	     * constantly check for parentheses, so we type the thing with
    391 	     * the OP_ARCHV bit before we place it on the end of the
    392 	     * provided list.
    393 	     */
    394 	    gn->type |= OP_ARCHV;
    395 	    Lst_Append(nodeLst, gn);
    396 	}
    397 	if (doSubst) {
    398 	    free(memName);
    399 	}
    400 
    401 	*cp = saveChar;
    402     }
    403 
    404     /*
    405      * If substituted libName, free it now, since we need it no longer.
    406      */
    407     if (subLibName) {
    408 	free(libName);
    409     }
    410 
    411     cp++;			/* skip the ')' */
    412     /* We promised that linePtr would be set up at the next non-space. */
    413     pp_skip_whitespace(&cp);
    414     *linePtr = cp;
    415     return TRUE;
    416 }
    417 
    418 /*-
    419  *-----------------------------------------------------------------------
    420  * ArchStatMember --
    421  *	Locate a member of an archive, given the path of the archive and
    422  *	the path of the desired member.
    423  *
    424  * Input:
    425  *	archive		Path to the archive
    426  *	member		Name of member. If it is a path, only the last
    427  *			component is used.
    428  *	hash		TRUE if archive should be hashed if not already so.
    429  *
    430  * Results:
    431  *	A pointer to the current struct ar_hdr structure for the member. Note
    432  *	That no position is returned, so this is not useful for touching
    433  *	archive members. This is mostly because we have no assurances that
    434  *	The archive will remain constant after we read all the headers, so
    435  *	there's not much point in remembering the position...
    436  *-----------------------------------------------------------------------
    437  */
    438 static struct ar_hdr *
    439 ArchStatMember(const char *archive, const char *member, Boolean hash)
    440 {
    441 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name) - 1)
    442     FILE *arch;			/* Stream to archive */
    443     size_t size;		/* Size of archive member */
    444     char magic[SARMAG];
    445     ArchListNode *ln;
    446     Arch *ar;			/* Archive descriptor */
    447     struct ar_hdr arh;		/* archive-member header for reading archive */
    448     char memName[MAXPATHLEN + 1];
    449 				/* Current member name while hashing. */
    450 
    451     /*
    452      * Because of space constraints and similar things, files are archived
    453      * using their final path components, not the entire thing, so we need
    454      * to point 'member' to the final component, if there is one, to make
    455      * the comparisons easier...
    456      */
    457     const char *base = strrchr(member, '/');
    458     if (base != NULL) {
    459 	member = base + 1;
    460     }
    461 
    462     for (ln = archives->first; ln != NULL; ln = ln->next) {
    463 	const Arch *archPtr = ln->datum;
    464 	if (strcmp(archPtr->name, archive) == 0)
    465 	    break;
    466     }
    467 
    468     if (ln != NULL) {
    469 	struct ar_hdr *hdr;
    470 
    471 	ar = LstNode_Datum(ln);
    472 	hdr = Hash_FindValue(&ar->members, member);
    473 	if (hdr != NULL)
    474 	    return hdr;
    475 
    476 	{
    477 	    /* Try truncated name */
    478 	    char copy[AR_MAX_NAME_LEN + 1];
    479 	    size_t len = strlen(member);
    480 
    481 	    if (len > AR_MAX_NAME_LEN) {
    482 		len = AR_MAX_NAME_LEN;
    483 		snprintf(copy, sizeof copy, "%s", member);
    484 	    }
    485 	    hdr = Hash_FindValue(&ar->members, copy);
    486 	    return hdr;
    487 	}
    488     }
    489 
    490     if (!hash) {
    491 	/*
    492 	 * Caller doesn't want the thing hashed, just use ArchFindMember
    493 	 * to read the header for the member out and close down the stream
    494 	 * again. Since the archive is not to be hashed, we assume there's
    495 	 * no need to allocate extra room for the header we're returning,
    496 	 * so just declare it static.
    497 	 */
    498 	static struct ar_hdr sarh;
    499 
    500 	arch = ArchFindMember(archive, member, &sarh, "r");
    501 
    502 	if (arch == NULL) {
    503 	    return NULL;
    504 	} else {
    505 	    fclose(arch);
    506 	    return &sarh;
    507 	}
    508     }
    509 
    510     /*
    511      * We don't have this archive on the list yet, so we want to find out
    512      * everything that's in it and cache it so we can get at it quickly.
    513      */
    514     arch = fopen(archive, "r");
    515     if (arch == NULL) {
    516 	return NULL;
    517     }
    518 
    519     /*
    520      * We use the ARMAG string to make sure this is an archive we
    521      * can handle...
    522      */
    523     if ((fread(magic, SARMAG, 1, arch) != 1) ||
    524 	(strncmp(magic, ARMAG, SARMAG) != 0)) {
    525 	fclose(arch);
    526 	return NULL;
    527     }
    528 
    529     ar = bmake_malloc(sizeof(Arch));
    530     ar->name = bmake_strdup(archive);
    531     ar->fnametab = NULL;
    532     ar->fnamesize = 0;
    533     Hash_InitTable(&ar->members);
    534     memName[AR_MAX_NAME_LEN] = '\0';
    535 
    536     while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
    537 	if (strncmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
    538 	    /*
    539 	     * The header is bogus, so the archive is bad
    540 	     * and there's no way we can recover...
    541 	     */
    542 	    goto badarch;
    543 	} else {
    544 	    char *nameend;
    545 
    546 	    /*
    547 	     * We need to advance the stream's pointer to the start of the
    548 	     * next header. Files are padded with newlines to an even-byte
    549 	     * boundary, so we need to extract the size of the file from the
    550 	     * 'size' field of the header and round it up during the seek.
    551 	     */
    552 	    arh.ar_size[sizeof(arh.ar_size) - 1] = '\0';
    553 	    size = (size_t)strtol(arh.ar_size, NULL, 10);
    554 
    555 	    memcpy(memName, arh.ar_name, sizeof(arh.ar_name));
    556 	    nameend = memName + AR_MAX_NAME_LEN;
    557 	    while (*nameend == ' ') {
    558 		nameend--;
    559 	    }
    560 	    nameend[1] = '\0';
    561 
    562 #ifdef SVR4ARCHIVES
    563 	    /*
    564 	     * svr4 names are slash terminated. Also svr4 extended AR format.
    565 	     */
    566 	    if (memName[0] == '/') {
    567 		/*
    568 		 * svr4 magic mode; handle it
    569 		 */
    570 		switch (ArchSVR4Entry(ar, memName, size, arch)) {
    571 		case -1:	/* Invalid data */
    572 		    goto badarch;
    573 		case 0:		/* List of files entry */
    574 		    continue;
    575 		default:	/* Got the entry */
    576 		    break;
    577 		}
    578 	    } else {
    579 		if (nameend[0] == '/')
    580 		    nameend[0] = '\0';
    581 	    }
    582 #endif
    583 
    584 #ifdef AR_EFMT1
    585 	    /*
    586 	     * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    587 	     * first <namelen> bytes of the file
    588 	     */
    589 	    if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
    590 		ch_isdigit(memName[sizeof(AR_EFMT1) - 1])) {
    591 
    592 		int elen = atoi(&memName[sizeof(AR_EFMT1) - 1]);
    593 
    594 		if ((unsigned int)elen > MAXPATHLEN)
    595 		    goto badarch;
    596 		if (fread(memName, (size_t)elen, 1, arch) != 1)
    597 		    goto badarch;
    598 		memName[elen] = '\0';
    599 		if (fseek(arch, -elen, SEEK_CUR) != 0)
    600 		    goto badarch;
    601 		if (DEBUG(ARCH) || DEBUG(MAKE)) {
    602 		    debug_printf("ArchStat: Extended format entry for %s\n",
    603 				 memName);
    604 		}
    605 	    }
    606 #endif
    607 
    608 	    {
    609 		HashEntry *he;
    610 		he = Hash_CreateEntry(&ar->members, memName, NULL);
    611 		Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
    612 		memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
    613 	    }
    614 	}
    615 	if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
    616 	    goto badarch;
    617     }
    618 
    619     fclose(arch);
    620 
    621     Lst_Append(archives, ar);
    622 
    623     /*
    624      * Now that the archive has been read and cached, we can look into
    625      * the hash table to find the desired member's header.
    626      */
    627     return Hash_FindValue(&ar->members, member);
    628 
    629 badarch:
    630     fclose(arch);
    631     Hash_DeleteTable(&ar->members);
    632     free(ar->fnametab);
    633     free(ar);
    634     return NULL;
    635 }
    636 
    637 #ifdef SVR4ARCHIVES
    638 /*-
    639  *-----------------------------------------------------------------------
    640  * ArchSVR4Entry --
    641  *	Parse an SVR4 style entry that begins with a slash.
    642  *	If it is "//", then load the table of filenames
    643  *	If it is "/<offset>", then try to substitute the long file name
    644  *	from offset of a table previously read.
    645  *	If a table is read, the file pointer is moved to the next archive
    646  *	member.
    647  *
    648  * Results:
    649  *	-1: Bad data in archive
    650  *	 0: A table was loaded from the file
    651  *	 1: Name was successfully substituted from table
    652  *	 2: Name was not successfully substituted from table
    653  *-----------------------------------------------------------------------
    654  */
    655 static int
    656 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
    657 {
    658 #define ARLONGNAMES1 "//"
    659 #define ARLONGNAMES2 "/ARFILENAMES"
    660     size_t entry;
    661     char *ptr, *eptr;
    662 
    663     if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
    664 	strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
    665 
    666 	if (ar->fnametab != NULL) {
    667 	    DEBUG0(ARCH, "Attempted to redefine an SVR4 name table\n");
    668 	    return -1;
    669 	}
    670 
    671 	/*
    672 	 * This is a table of archive names, so we build one for
    673 	 * ourselves
    674 	 */
    675 	ar->fnametab = bmake_malloc(size);
    676 	ar->fnamesize = size;
    677 
    678 	if (fread(ar->fnametab, size, 1, arch) != 1) {
    679 	    DEBUG0(ARCH, "Reading an SVR4 name table failed\n");
    680 	    return -1;
    681 	}
    682 	eptr = ar->fnametab + size;
    683 	for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
    684 	    if (*ptr == '/') {
    685 		entry++;
    686 		*ptr = '\0';
    687 	    }
    688 	DEBUG1(ARCH, "Found svr4 archive name table with %lu entries\n",
    689 	       (unsigned long)entry);
    690 	return 0;
    691     }
    692 
    693     if (name[1] == ' ' || name[1] == '\0')
    694 	return 2;
    695 
    696     entry = (size_t)strtol(&name[1], &eptr, 0);
    697     if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
    698 	DEBUG1(ARCH, "Could not parse SVR4 name %s\n", name);
    699 	return 2;
    700     }
    701     if (entry >= ar->fnamesize) {
    702 	DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n",
    703 	       name, (unsigned long)ar->fnamesize);
    704 	return 2;
    705     }
    706 
    707     DEBUG2(ARCH, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
    708 
    709     snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
    710     return 1;
    711 }
    712 #endif
    713 
    714 
    715 /*-
    716  *-----------------------------------------------------------------------
    717  * ArchFindMember --
    718  *	Locate a member of an archive, given the path of the archive and
    719  *	the path of the desired member. If the archive is to be modified,
    720  *	the mode should be "r+", if not, it should be "r".
    721  *	The passed struct ar_hdr structure is filled in.
    722  *
    723  * Input:
    724  *	archive		Path to the archive
    725  *	member		Name of member. If it is a path, only the last
    726  *			component is used.
    727  *	arhPtr		Pointer to header structure to be filled in
    728  *	mode		The mode for opening the stream
    729  *
    730  * Results:
    731  *	An FILE *, opened for reading and writing, positioned at the
    732  *	start of the member's struct ar_hdr, or NULL if the member was
    733  *	nonexistent. The current struct ar_hdr for member.
    734  *-----------------------------------------------------------------------
    735  */
    736 static FILE *
    737 ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr,
    738 	       const char *mode)
    739 {
    740     FILE *arch;			/* Stream to archive */
    741     int size;			/* Size of archive member */
    742     char magic[SARMAG];
    743     size_t len, tlen;
    744     const char *base;
    745 
    746     arch = fopen(archive, mode);
    747     if (arch == NULL) {
    748 	return NULL;
    749     }
    750 
    751     /*
    752      * We use the ARMAG string to make sure this is an archive we
    753      * can handle...
    754      */
    755     if ((fread(magic, SARMAG, 1, arch) != 1) ||
    756 	(strncmp(magic, ARMAG, SARMAG) != 0)) {
    757 	fclose(arch);
    758 	return NULL;
    759     }
    760 
    761     /*
    762      * Because of space constraints and similar things, files are archived
    763      * using their final path components, not the entire thing, so we need
    764      * to point 'member' to the final component, if there is one, to make
    765      * the comparisons easier...
    766      */
    767     base = strrchr(member, '/');
    768     if (base != NULL) {
    769 	member = base + 1;
    770     }
    771     len = tlen = strlen(member);
    772     if (len > sizeof(arhPtr->ar_name)) {
    773 	tlen = sizeof(arhPtr->ar_name);
    774     }
    775 
    776     while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
    777 	if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag)) != 0) {
    778 	    /*
    779 	     * The header is bogus, so the archive is bad
    780 	     * and there's no way we can recover...
    781 	     */
    782 	    fclose(arch);
    783 	    return NULL;
    784 	} else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
    785 	    /*
    786 	     * If the member's name doesn't take up the entire 'name' field,
    787 	     * we have to be careful of matching prefixes. Names are space-
    788 	     * padded to the right, so if the character in 'name' at the end
    789 	     * of the matched string is anything but a space, this isn't the
    790 	     * member we sought.
    791 	     */
    792 	    if (tlen != sizeof(arhPtr->ar_name) &&
    793 		arhPtr->ar_name[tlen] != ' ') {
    794 		goto skip;
    795 	    } else {
    796 		/*
    797 		 * To make life easier, we reposition the file at the start
    798 		 * of the header we just read before we return the stream.
    799 		 * In a more general situation, it might be better to leave
    800 		 * the file at the actual member, rather than its header, but
    801 		 * not here...
    802 		 */
    803 		if (fseek(arch, -(long)sizeof(struct ar_hdr), SEEK_CUR) != 0) {
    804 		    fclose(arch);
    805 		    return NULL;
    806 		}
    807 		return arch;
    808 	    }
    809 	} else
    810 #ifdef AR_EFMT1
    811 		/*
    812 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    813 		 * first <namelen> bytes of the file
    814 		 */
    815 	    if (strncmp(arhPtr->ar_name, AR_EFMT1,
    816 					sizeof(AR_EFMT1) - 1) == 0 &&
    817 		ch_isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
    818 
    819 		int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
    820 		char ename[MAXPATHLEN + 1];
    821 
    822 		if ((unsigned int)elen > MAXPATHLEN) {
    823 			fclose(arch);
    824 			return NULL;
    825 		}
    826 		if (fread(ename, (size_t)elen, 1, arch) != 1) {
    827 			fclose(arch);
    828 			return NULL;
    829 		}
    830 		ename[elen] = '\0';
    831 		if (DEBUG(ARCH) || DEBUG(MAKE)) {
    832 		    debug_printf("ArchFind: Extended format entry for %s\n", ename);
    833 		}
    834 		if (strncmp(ename, member, len) == 0) {
    835 			/* Found as extended name */
    836 			if (fseek(arch, -(long)sizeof(struct ar_hdr) - elen,
    837 				SEEK_CUR) != 0) {
    838 			    fclose(arch);
    839 			    return NULL;
    840 			}
    841 			return arch;
    842 		}
    843 		if (fseek(arch, -elen, SEEK_CUR) != 0) {
    844 		    fclose(arch);
    845 		    return NULL;
    846 		}
    847 		goto skip;
    848 	} else
    849 #endif
    850 	{
    851 skip:
    852 	    /*
    853 	     * This isn't the member we're after, so we need to advance the
    854 	     * stream's pointer to the start of the next header. Files are
    855 	     * padded with newlines to an even-byte boundary, so we need to
    856 	     * extract the size of the file from the 'size' field of the
    857 	     * header and round it up during the seek.
    858 	     */
    859 	    arhPtr->ar_size[sizeof(arhPtr->ar_size) - 1] = '\0';
    860 	    size = (int)strtol(arhPtr->ar_size, NULL, 10);
    861 	    if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
    862 		fclose(arch);
    863 		return NULL;
    864 	    }
    865 	}
    866     }
    867 
    868     /*
    869      * We've looked everywhere, but the member is not to be found. Close the
    870      * archive and return NULL -- an error.
    871      */
    872     fclose(arch);
    873     return NULL;
    874 }
    875 
    876 /*-
    877  *-----------------------------------------------------------------------
    878  * Arch_Touch --
    879  *	Touch a member of an archive.
    880  *	The modification time of the entire archive is also changed.
    881  *	For a library, this could necessitate the re-ranlib'ing of the
    882  *	whole thing.
    883  *
    884  * Input:
    885  *	gn		Node of member to touch
    886  *
    887  * Results:
    888  *	The 'time' field of the member's header is updated.
    889  *-----------------------------------------------------------------------
    890  */
    891 void
    892 Arch_Touch(GNode *gn)
    893 {
    894     FILE *arch;		/* Stream open to archive, positioned properly */
    895     struct ar_hdr arh;	/* Current header describing member */
    896     char *p1, *p2;
    897 
    898     arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
    899 			  Var_Value(MEMBER, gn, &p2),
    900 			  &arh, "r+");
    901 
    902     bmake_free(p1);
    903     bmake_free(p2);
    904 
    905     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)now);
    906 
    907     if (arch != NULL) {
    908 	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
    909 	fclose(arch);
    910     }
    911 }
    912 
    913 /* Given a node which represents a library, touch the thing, making sure that
    914  * the table of contents also is touched.
    915  *
    916  * Both the modification time of the library and of the RANLIBMAG member are
    917  * set to 'now'.
    918  *
    919  * Input:
    920  *	gn		The node of the library to touch
    921  */
    922 void
    923 Arch_TouchLib(GNode *gn)
    924 {
    925 #ifdef RANLIBMAG
    926     FILE *	    arch;	/* Stream open to archive */
    927     struct ar_hdr   arh;	/* Header describing table of contents */
    928     struct utimbuf  times;	/* Times for utime() call */
    929 
    930     arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
    931     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
    932 
    933     if (arch != NULL) {
    934 	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
    935 	fclose(arch);
    936 
    937 	times.actime = times.modtime = now;
    938 	utime(gn->path, &times);
    939     }
    940 #else
    941     (void)gn;
    942 #endif
    943 }
    944 
    945 /* Return the modification time of a member of an archive. The mtime field
    946  * of the given node is filled in with the value returned by the function.
    947  *
    948  * Input:
    949  *	gn		Node describing archive member
    950  */
    951 time_t
    952 Arch_MTime(GNode *gn)
    953 {
    954     struct ar_hdr *arhPtr;	/* Header of desired member */
    955     time_t modTime;		/* Modification time as an integer */
    956     char *p1, *p2;
    957 
    958     arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
    959 			    Var_Value(MEMBER, gn, &p2),
    960 			    TRUE);
    961 
    962     bmake_free(p1);
    963     bmake_free(p2);
    964 
    965     if (arhPtr != NULL) {
    966 	modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
    967     } else {
    968 	modTime = 0;
    969     }
    970 
    971     gn->mtime = modTime;
    972     return modTime;
    973 }
    974 
    975 /* Given a non-existent archive member's node, get its modification time from
    976  * its archived form, if it exists. gn->mtime is filled in as well. */
    977 time_t
    978 Arch_MemMTime(GNode *gn)
    979 {
    980     GNodeListNode *ln;
    981 
    982     for (ln = gn->parents->first; ln != NULL; ln = ln->next) {
    983 	GNode *pgn = ln->datum;
    984 
    985 	if (pgn->type & OP_ARCHV) {
    986 	    /*
    987 	     * If the parent is an archive specification and is being made
    988 	     * and its member's name matches the name of the node we were
    989 	     * given, record the modification time of the parent in the
    990 	     * child. We keep searching its parents in case some other
    991 	     * parent requires this child to exist...
    992 	     */
    993 	    const char *nameStart = strchr(pgn->name, '(') + 1;
    994 	    const char *nameEnd = strchr(nameStart, ')');
    995 	    size_t nameLen = (size_t)(nameEnd - nameStart);
    996 
    997 	    if ((pgn->flags & REMAKE) &&
    998 		strncmp(nameStart, gn->name, nameLen) == 0) {
    999 		gn->mtime = Arch_MTime(pgn);
   1000 	    }
   1001 	} else if (pgn->flags & REMAKE) {
   1002 	    /*
   1003 	     * Something which isn't a library depends on the existence of
   1004 	     * this target, so it needs to exist.
   1005 	     */
   1006 	    gn->mtime = 0;
   1007 	    break;
   1008 	}
   1009     }
   1010 
   1011     return gn->mtime;
   1012 }
   1013 
   1014 /* Search for a library along the given search path.
   1015  *
   1016  * The node's 'path' field is set to the found path (including the
   1017  * actual file name, not -l...). If the system can handle the -L
   1018  * flag when linking (or we cannot find the library), we assume that
   1019  * the user has placed the .LIBS variable in the final linking
   1020  * command (or the linker will know where to find it) and set the
   1021  * TARGET variable for this node to be the node's name. Otherwise,
   1022  * we set the TARGET variable to be the full path of the library,
   1023  * as returned by Dir_FindFile.
   1024  *
   1025  * Input:
   1026  *	gn		Node of library to find
   1027  *	path		Search path
   1028  */
   1029 void
   1030 Arch_FindLib(GNode *gn, SearchPath *path)
   1031 {
   1032     char *libName;		/* file name for archive */
   1033     size_t sz = strlen(gn->name) + 6 - 2;
   1034 
   1035     libName = bmake_malloc(sz);
   1036     snprintf(libName, sz, "lib%s.a", &gn->name[2]);
   1037 
   1038     gn->path = Dir_FindFile(libName, path);
   1039 
   1040     free(libName);
   1041 
   1042 #ifdef LIBRARIES
   1043     Var_Set(TARGET, gn->name, gn);
   1044 #else
   1045     Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
   1046 #endif
   1047 }
   1048 
   1049 /* Decide if a node with the OP_LIB attribute is out-of-date. Called from
   1050  * Make_OODate to make its life easier.
   1051  * The library will be hashed if it hasn't been already.
   1052  *
   1053  * There are several ways for a library to be out-of-date that are
   1054  * not available to ordinary files. In addition, there are ways
   1055  * that are open to regular files that are not available to
   1056  * libraries. A library that is only used as a source is never
   1057  * considered out-of-date by itself. This does not preclude the
   1058  * library's modification time from making its parent be out-of-date.
   1059  * A library will be considered out-of-date for any of these reasons,
   1060  * given that it is a target on a dependency line somewhere:
   1061  *
   1062  *	Its modification time is less than that of one of its sources
   1063  *	(gn->mtime < gn->cmgn->mtime).
   1064  *
   1065  *	Its modification time is greater than the time at which the make
   1066  *	began (i.e. it's been modified in the course of the make, probably
   1067  *	by archiving).
   1068  *
   1069  *	The modification time of one of its sources is greater than the one
   1070  *	of its RANLIBMAG member (i.e. its table of contents is out-of-date).
   1071  *	We don't compare of the archive time vs. TOC time because they can be
   1072  *	too close. In my opinion we should not bother with the TOC at all
   1073  *	since this is used by 'ar' rules that affect the data contents of the
   1074  *	archive, not by ranlib rules, which affect the TOC.
   1075  *
   1076  * Input:
   1077  *	gn		The library's graph node
   1078  *
   1079  * Results:
   1080  *	TRUE if the library is out-of-date. FALSE otherwise.
   1081  */
   1082 Boolean
   1083 Arch_LibOODate(GNode *gn)
   1084 {
   1085     Boolean oodate;
   1086 
   1087     if (gn->type & OP_PHONY) {
   1088 	oodate = TRUE;
   1089     } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
   1090 	oodate = FALSE;
   1091     } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
   1092 	       (gn->mtime > now) ||
   1093 	       (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
   1094 	oodate = TRUE;
   1095     } else {
   1096 #ifdef RANLIBMAG
   1097 	struct ar_hdr *arhPtr;	/* Header for __.SYMDEF */
   1098 	int modTimeTOC;		/* The table-of-contents's mod time */
   1099 
   1100 	arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
   1101 
   1102 	if (arhPtr != NULL) {
   1103 	    modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
   1104 
   1105 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
   1106 		debug_printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
   1107 	    }
   1108 	    oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
   1109 	} else {
   1110 	    /*
   1111 	     * A library w/o a table of contents is out-of-date
   1112 	     */
   1113 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
   1114 		debug_printf("No t.o.c....");
   1115 	    }
   1116 	    oodate = TRUE;
   1117 	}
   1118 #else
   1119 	oodate = FALSE;
   1120 #endif
   1121     }
   1122     return oodate;
   1123 }
   1124 
   1125 /* Initialize the archives module. */
   1126 void
   1127 Arch_Init(void)
   1128 {
   1129     archives = Lst_Init();
   1130 }
   1131 
   1132 /* Clean up the archives module. */
   1133 void
   1134 Arch_End(void)
   1135 {
   1136 #ifdef CLEANUP
   1137     Lst_Destroy(archives, ArchFree);
   1138 #endif
   1139 }
   1140 
   1141 Boolean
   1142 Arch_IsLib(GNode *gn)
   1143 {
   1144     static const char armag[] = "!<arch>\n";
   1145     char buf[sizeof armag - 1];
   1146     int fd;
   1147 
   1148     if ((fd = open(gn->path, O_RDONLY)) == -1)
   1149 	return FALSE;
   1150 
   1151     if (read(fd, buf, sizeof buf) != sizeof buf) {
   1152 	(void)close(fd);
   1153 	return FALSE;
   1154     }
   1155 
   1156     (void)close(fd);
   1157 
   1158     return memcmp(buf, armag, sizeof buf) == 0;
   1159 }
   1160