Home | History | Annotate | Line # | Download | only in make
arch.c revision 1.216
      1  1.216    rillig /*	$NetBSD: arch.c,v 1.216 2024/04/27 17:33:46 rillig Exp $	*/
      2    1.9  christos 
      3    1.1       cgd /*
      4   1.17  christos  * Copyright (c) 1988, 1989, 1990, 1993
      5   1.17  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.39       agc  *
      7   1.39       agc  * This code is derived from software contributed to Berkeley by
      8   1.39       agc  * Adam de Boor.
      9   1.39       agc  *
     10   1.39       agc  * Redistribution and use in source and binary forms, with or without
     11   1.39       agc  * modification, are permitted provided that the following conditions
     12   1.39       agc  * are met:
     13   1.39       agc  * 1. Redistributions of source code must retain the above copyright
     14   1.39       agc  *    notice, this list of conditions and the following disclaimer.
     15   1.39       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.39       agc  *    notice, this list of conditions and the following disclaimer in the
     17   1.39       agc  *    documentation and/or other materials provided with the distribution.
     18   1.39       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.39       agc  *    may be used to endorse or promote products derived from this software
     20   1.39       agc  *    without specific prior written permission.
     21   1.39       agc  *
     22   1.39       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.39       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.39       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.39       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.39       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.39       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.39       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.39       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.39       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.39       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.39       agc  * SUCH DAMAGE.
     33   1.39       agc  */
     34   1.39       agc 
     35   1.39       agc /*
     36    1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     37    1.1       cgd  * All rights reserved.
     38    1.1       cgd  *
     39    1.1       cgd  * This code is derived from software contributed to Berkeley by
     40    1.1       cgd  * Adam de Boor.
     41    1.1       cgd  *
     42    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     43    1.1       cgd  * modification, are permitted provided that the following conditions
     44    1.1       cgd  * are met:
     45    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     46    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     47    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     49    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     50    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     51    1.1       cgd  *    must display the following acknowledgement:
     52    1.1       cgd  *	This product includes software developed by the University of
     53    1.1       cgd  *	California, Berkeley and its contributors.
     54    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     55    1.1       cgd  *    may be used to endorse or promote products derived from this software
     56    1.1       cgd  *    without specific prior written permission.
     57    1.1       cgd  *
     58    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68    1.1       cgd  * SUCH DAMAGE.
     69    1.1       cgd  */
     70    1.1       cgd 
     71  1.192    rillig /*
     72  1.192    rillig  * Manipulate libraries, archives and their members.
     73    1.1       cgd  *
     74  1.157    rillig  * The first time an archive is referenced, all of its members' headers are
     75  1.176    rillig  * read and cached and the archive closed again.  All cached archives are kept
     76  1.157    rillig  * on a list which is searched each time an archive member is referenced.
     77    1.1       cgd  *
     78    1.1       cgd  * The interface to this module is:
     79  1.157    rillig  *
     80  1.157    rillig  *	Arch_Init	Initialize this module.
     81  1.157    rillig  *
     82  1.157    rillig  *	Arch_End	Clean up this module.
     83  1.157    rillig  *
     84  1.124    rillig  *	Arch_ParseArchive
     85  1.157    rillig  *			Parse an archive specification such as
     86  1.157    rillig  *			"archive.a(member1 member2)".
     87  1.124    rillig  *
     88  1.124    rillig  *	Arch_Touch	Alter the modification time of the archive
     89  1.124    rillig  *			member described by the given node to be
     90  1.157    rillig  *			the time when make was started.
     91  1.124    rillig  *
     92  1.124    rillig  *	Arch_TouchLib	Update the modification time of the library
     93  1.124    rillig  *			described by the given node. This is special
     94  1.124    rillig  *			because it also updates the modification time
     95  1.124    rillig  *			of the library's table of contents.
     96  1.124    rillig  *
     97  1.174    rillig  *	Arch_UpdateMTime
     98  1.174    rillig  *			Find the modification time of a member of
     99  1.174    rillig  *			an archive *in the archive* and place it in the
    100  1.174    rillig  *			member's GNode.
    101  1.124    rillig  *
    102  1.173    rillig  *	Arch_UpdateMemberMTime
    103  1.157    rillig  *			Find the modification time of a member of
    104  1.124    rillig  *			an archive. Called when the member doesn't
    105  1.124    rillig  *			already exist. Looks in the archive for the
    106  1.124    rillig  *			modification time. Returns the modification
    107  1.124    rillig  *			time.
    108  1.124    rillig  *
    109  1.124    rillig  *	Arch_FindLib	Search for a library along a path. The
    110  1.124    rillig  *			library name in the GNode should be in
    111  1.124    rillig  *			-l<name> format.
    112    1.1       cgd  *
    113  1.157    rillig  *	Arch_LibOODate	Decide if a library node is out-of-date.
    114    1.1       cgd  */
    115    1.1       cgd 
    116  1.151    rillig #include <sys/types.h>
    117  1.151    rillig #include <sys/stat.h>
    118  1.151    rillig #include <sys/time.h>
    119  1.151    rillig #include <sys/param.h>
    120  1.151    rillig 
    121  1.151    rillig #include <ar.h>
    122  1.151    rillig #include <utime.h>
    123  1.151    rillig 
    124  1.151    rillig #include "make.h"
    125  1.151    rillig #include "dir.h"
    126  1.151    rillig #include "config.h"
    127   1.27        tv 
    128  1.114    rillig /*	"@(#)arch.c	8.2 (Berkeley) 1/2/94"	*/
    129  1.216    rillig MAKE_RCSID("$NetBSD: arch.c,v 1.216 2024/04/27 17:33:46 rillig Exp $");
    130    1.2     glass 
    131  1.117    rillig typedef struct List ArchList;
    132  1.117    rillig typedef struct ListNode ArchListNode;
    133  1.117    rillig 
    134  1.180    rillig static ArchList archives;	/* The archives we've already examined */
    135    1.1       cgd 
    136    1.1       cgd typedef struct Arch {
    137  1.178    rillig 	char *name;		/* Name of archive */
    138  1.178    rillig 	HashTable members;	/* All the members of the archive described
    139  1.132    rillig 				 * by <name, struct ar_hdr *> key/value pairs */
    140  1.178    rillig 	char *fnametab;		/* Extended name table strings */
    141  1.178    rillig 	size_t fnamesize;	/* Size of the string table */
    142    1.1       cgd } Arch;
    143    1.1       cgd 
    144   1.76    rillig static FILE *ArchFindMember(const char *, const char *,
    145   1.76    rillig 			    struct ar_hdr *, const char *);
    146   1.23  christos #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
    147   1.15  christos #define SVR4ARCHIVES
    148   1.35       wiz static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
    149   1.15  christos #endif
    150    1.1       cgd 
    151  1.200    rillig 
    152   1.31   mycroft #ifdef CLEANUP
    153    1.7       jtc static void
    154  1.216    rillig ArchFree(Arch *a)
    155    1.7       jtc {
    156  1.178    rillig 	HashIter hi;
    157   1.17  christos 
    158  1.178    rillig 	/* Free memory from hash entries */
    159  1.178    rillig 	HashIter_Init(&hi, &a->members);
    160  1.178    rillig 	while (HashIter_Next(&hi) != NULL)
    161  1.178    rillig 		free(hi.entry->value);
    162  1.178    rillig 
    163  1.178    rillig 	free(a->name);
    164  1.178    rillig 	free(a->fnametab);
    165  1.178    rillig 	HashTable_Done(&a->members);
    166  1.178    rillig 	free(a);
    167    1.7       jtc }
    168   1.31   mycroft #endif
    169   1.17  christos 
    170  1.200    rillig /* Return "archive(member)". */
    171  1.200    rillig static char *
    172  1.200    rillig FullName(const char *archive, const char *member)
    173  1.200    rillig {
    174  1.200    rillig 	size_t len1 = strlen(archive);
    175  1.200    rillig 	size_t len3 = strlen(member);
    176  1.200    rillig 	char *result = bmake_malloc(len1 + 1 + len3 + 1 + 1);
    177  1.200    rillig 	memcpy(result, archive, len1);
    178  1.200    rillig 	memcpy(result + len1, "(", 1);
    179  1.200    rillig 	memcpy(result + len1 + 1, member, len3);
    180  1.200    rillig 	memcpy(result + len1 + 1 + len3, ")", 1 + 1);
    181  1.200    rillig 	return result;
    182  1.200    rillig }
    183    1.7       jtc 
    184  1.157    rillig /*
    185  1.157    rillig  * Parse an archive specification such as "archive.a(member1 member2.${EXT})",
    186  1.183    rillig  * adding nodes for the expanded members to gns.  Nodes are created as
    187  1.157    rillig  * necessary.
    188    1.1       cgd  *
    189   1.35       wiz  * Input:
    190  1.157    rillig  *	pp		The start of the specification.
    191  1.183    rillig  *	gns		The list on which to place the nodes.
    192  1.196    rillig  *	scope		The scope in which to expand variables.
    193  1.157    rillig  *
    194  1.157    rillig  * Output:
    195  1.199    rillig  *	return		True if it was a valid specification.
    196  1.157    rillig  *	*pp		Points to the first non-space after the archive spec.
    197    1.1       cgd  */
    198  1.199    rillig bool
    199  1.196    rillig Arch_ParseArchive(char **pp, GNodeList *gns, GNode *scope)
    200    1.1       cgd {
    201  1.207    rillig 	char *spec;		/* For modifying some bytes of *pp */
    202  1.206    rillig 	const char *cp;		/* Pointer into line */
    203  1.178    rillig 	GNode *gn;		/* New node */
    204  1.207    rillig 	FStr lib;		/* Library-part of specification */
    205  1.205    rillig 	FStr mem;		/* Member-part of specification */
    206  1.178    rillig 	char saveChar;		/* Ending delimiter of member-name */
    207  1.214    rillig 	bool expandLib;		/* Whether the parsed lib contains
    208  1.209    rillig 				 * expressions that need to be expanded */
    209    1.1       cgd 
    210  1.207    rillig 	spec = *pp;
    211  1.207    rillig 	lib = FStr_InitRefer(spec);
    212  1.207    rillig 	expandLib = false;
    213    1.1       cgd 
    214  1.207    rillig 	for (cp = lib.str; *cp != '(' && *cp != '\0';) {
    215  1.178    rillig 		if (*cp == '$') {
    216  1.214    rillig 			/* Expand nested expressions. */
    217  1.178    rillig 			/* XXX: This code can probably be shortened. */
    218  1.178    rillig 			const char *nested_p = cp;
    219  1.190    rillig 			FStr result;
    220  1.199    rillig 			bool isError;
    221  1.178    rillig 
    222  1.178    rillig 			/* XXX: is expanded twice: once here and once below */
    223  1.213    rillig 			result = Var_Parse(&nested_p, scope, VARE_UNDEFERR);
    224  1.178    rillig 			/* TODO: handle errors */
    225  1.190    rillig 			isError = result.str == var_Error;
    226  1.190    rillig 			FStr_Done(&result);
    227  1.178    rillig 			if (isError)
    228  1.199    rillig 				return false;
    229  1.178    rillig 
    230  1.207    rillig 			expandLib = true;
    231  1.178    rillig 			cp += nested_p - cp;
    232  1.178    rillig 		} else
    233  1.178    rillig 			cp++;
    234  1.178    rillig 	}
    235   1.17  christos 
    236  1.206    rillig 	spec[cp++ - spec] = '\0';
    237  1.210    rillig 	if (expandLib)
    238  1.210    rillig 		Var_Expand(&lib, scope, VARE_UNDEFERR);
    239    1.1       cgd 
    240  1.178    rillig 	for (;;) {
    241    1.1       cgd 		/*
    242  1.178    rillig 		 * First skip to the start of the member's name, mark that
    243  1.178    rillig 		 * place and skip to the end of it (either white-space or
    244  1.178    rillig 		 * a close paren).
    245    1.1       cgd 		 */
    246  1.199    rillig 		bool doSubst = false;
    247    1.1       cgd 
    248  1.206    rillig 		cpp_skip_whitespace(&cp);
    249   1.67  christos 
    250  1.205    rillig 		mem = FStr_InitRefer(cp);
    251  1.178    rillig 		while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
    252  1.178    rillig 			if (*cp == '$') {
    253  1.214    rillig 				/* Expand nested expressions. */
    254  1.209    rillig 				/*
    255  1.209    rillig 				 * XXX: This code can probably be shortened.
    256  1.209    rillig 				 */
    257  1.190    rillig 				FStr result;
    258  1.199    rillig 				bool isError;
    259  1.178    rillig 				const char *nested_p = cp;
    260  1.178    rillig 
    261  1.213    rillig 				result = Var_Parse(&nested_p, scope,
    262  1.213    rillig 				    VARE_UNDEFERR);
    263  1.178    rillig 				/* TODO: handle errors */
    264  1.190    rillig 				isError = result.str == var_Error;
    265  1.190    rillig 				FStr_Done(&result);
    266  1.178    rillig 
    267  1.178    rillig 				if (isError)
    268  1.199    rillig 					return false;
    269  1.178    rillig 
    270  1.199    rillig 				doSubst = true;
    271  1.178    rillig 				cp += nested_p - cp;
    272  1.178    rillig 			} else {
    273  1.178    rillig 				cp++;
    274  1.178    rillig 			}
    275  1.178    rillig 		}
    276    1.1       cgd 
    277  1.178    rillig 		/*
    278  1.178    rillig 		 * If the specification ends without a closing parenthesis,
    279  1.178    rillig 		 * chances are there's something wrong (like a missing
    280  1.178    rillig 		 * backslash), so it's better to return failure than allow
    281  1.178    rillig 		 * such things to happen
    282  1.178    rillig 		 */
    283  1.178    rillig 		if (*cp == '\0') {
    284  1.178    rillig 			Parse_Error(PARSE_FATAL,
    285  1.208    rillig 			    "No closing parenthesis "
    286  1.208    rillig 			    "in archive specification");
    287  1.199    rillig 			return false;
    288  1.178    rillig 		}
    289    1.1       cgd 
    290  1.178    rillig 		/*
    291  1.178    rillig 		 * If we didn't move anywhere, we must be done
    292  1.178    rillig 		 */
    293  1.205    rillig 		if (cp == mem.str)
    294  1.178    rillig 			break;
    295    1.1       cgd 
    296  1.178    rillig 		saveChar = *cp;
    297  1.206    rillig 		spec[cp - spec] = '\0';
    298    1.1       cgd 
    299    1.1       cgd 		/*
    300  1.178    rillig 		 * XXX: This should be taken care of intelligently by
    301  1.178    rillig 		 * SuffExpandChildren, both for the archive and the member
    302  1.178    rillig 		 * portions.
    303  1.178    rillig 		 */
    304  1.178    rillig 		/*
    305  1.178    rillig 		 * If member contains variables, try and substitute for them.
    306  1.212    rillig 		 * This slows down archive specs with dynamic sources, since
    307  1.212    rillig 		 * they are (non-)substituted three times, but we need to do
    308  1.212    rillig 		 * this since SuffExpandChildren calls us, otherwise we could
    309  1.212    rillig 		 * assume the substitutions would be taken care of later.
    310    1.1       cgd 		 */
    311  1.178    rillig 		if (doSubst) {
    312  1.184    rillig 			char *fullName;
    313  1.210    rillig 			char *p;
    314  1.205    rillig 			const char *unexpandedMem = mem.str;
    315  1.178    rillig 
    316  1.210    rillig 			Var_Expand(&mem, scope, VARE_UNDEFERR);
    317  1.178    rillig 
    318  1.178    rillig 			/*
    319  1.178    rillig 			 * Now form an archive spec and recurse to deal with
    320  1.178    rillig 			 * nested variables and multi-word variable values.
    321  1.178    rillig 			 */
    322  1.207    rillig 			fullName = FullName(lib.str, mem.str);
    323  1.184    rillig 			p = fullName;
    324  1.178    rillig 
    325  1.205    rillig 			if (strcmp(mem.str, unexpandedMem) == 0) {
    326  1.178    rillig 				/*
    327  1.178    rillig 				 * Must contain dynamic sources, so we can't
    328  1.178    rillig 				 * deal with it now. Just create an ARCHV node
    329  1.178    rillig 				 * for the thing and let SuffExpandChildren
    330  1.178    rillig 				 * handle it.
    331  1.178    rillig 				 */
    332  1.184    rillig 				gn = Targ_GetNode(fullName);
    333  1.178    rillig 				gn->type |= OP_ARCHV;
    334  1.183    rillig 				Lst_Append(gns, gn);
    335  1.178    rillig 
    336  1.196    rillig 			} else if (!Arch_ParseArchive(&p, gns, scope)) {
    337  1.178    rillig 				/* Error in nested call. */
    338  1.184    rillig 				free(fullName);
    339  1.184    rillig 				/* XXX: does unexpandedMemName leak? */
    340  1.199    rillig 				return false;
    341  1.178    rillig 			}
    342  1.184    rillig 			free(fullName);
    343  1.184    rillig 			/* XXX: does unexpandedMemName leak? */
    344  1.178    rillig 
    345  1.205    rillig 		} else if (Dir_HasWildcards(mem.str)) {
    346  1.181    rillig 			StringList members = LST_INIT;
    347  1.205    rillig 			SearchPath_Expand(&dirSearchPath, mem.str, &members);
    348  1.178    rillig 
    349  1.181    rillig 			while (!Lst_IsEmpty(&members)) {
    350  1.181    rillig 				char *member = Lst_Dequeue(&members);
    351  1.207    rillig 				char *fullname = FullName(lib.str, member);
    352  1.178    rillig 				free(member);
    353  1.178    rillig 
    354  1.178    rillig 				gn = Targ_GetNode(fullname);
    355  1.178    rillig 				free(fullname);
    356  1.178    rillig 
    357  1.178    rillig 				gn->type |= OP_ARCHV;
    358  1.183    rillig 				Lst_Append(gns, gn);
    359  1.178    rillig 			}
    360  1.181    rillig 			Lst_Done(&members);
    361  1.178    rillig 
    362  1.178    rillig 		} else {
    363  1.207    rillig 			char *fullname = FullName(lib.str, mem.str);
    364  1.178    rillig 			gn = Targ_GetNode(fullname);
    365  1.178    rillig 			free(fullname);
    366  1.178    rillig 
    367  1.178    rillig 			/*
    368  1.178    rillig 			 * We've found the node, but have to make sure the
    369  1.178    rillig 			 * rest of the world knows it's an archive member,
    370  1.178    rillig 			 * without having to constantly check for parentheses,
    371  1.178    rillig 			 * so we type the thing with the OP_ARCHV bit before
    372  1.178    rillig 			 * we place it on the end of the provided list.
    373  1.178    rillig 			 */
    374  1.178    rillig 			gn->type |= OP_ARCHV;
    375  1.183    rillig 			Lst_Append(gns, gn);
    376  1.178    rillig 		}
    377  1.205    rillig 		FStr_Done(&mem);
    378  1.141    rillig 
    379  1.206    rillig 		spec[cp - spec] = saveChar;
    380    1.1       cgd 	}
    381   1.17  christos 
    382  1.207    rillig 	FStr_Done(&lib);
    383    1.1       cgd 
    384  1.178    rillig 	cp++;			/* skip the ')' */
    385  1.178    rillig 	/* We promised that pp would be set up at the next non-space. */
    386  1.206    rillig 	cpp_skip_whitespace(&cp);
    387  1.206    rillig 	*pp += cp - *pp;
    388  1.199    rillig 	return true;
    389    1.1       cgd }
    390    1.1       cgd 
    391  1.192    rillig /*
    392  1.192    rillig  * Locate a member of an archive, given the path of the archive and the path
    393  1.143    rillig  * of the desired member.
    394    1.1       cgd  *
    395   1.35       wiz  * Input:
    396   1.35       wiz  *	archive		Path to the archive
    397  1.143    rillig  *	member		Name of member; only its basename is used.
    398  1.199    rillig  *	addToCache	True if archive should be cached if not already so.
    399   1.35       wiz  *
    400    1.1       cgd  * Results:
    401  1.164    rillig  *	The ar_hdr for the member, or NULL.
    402  1.170    rillig  *
    403  1.170    rillig  * See ArchFindMember for an almost identical copy of this code.
    404    1.1       cgd  */
    405    1.1       cgd static struct ar_hdr *
    406  1.199    rillig ArchStatMember(const char *archive, const char *member, bool addToCache)
    407    1.1       cgd {
    408  1.154    rillig #define AR_MAX_NAME_LEN (sizeof arh.ar_name - 1)
    409  1.178    rillig 	FILE *arch;
    410  1.178    rillig 	size_t size;		/* Size of archive member */
    411  1.178    rillig 	char magic[SARMAG];
    412  1.178    rillig 	ArchListNode *ln;
    413  1.178    rillig 	Arch *ar;		/* Archive descriptor */
    414  1.178    rillig 	struct ar_hdr arh;	/* archive-member header for reading archive */
    415  1.178    rillig 	char memName[MAXPATHLEN + 1];
    416  1.178    rillig 	/* Current member name while hashing. */
    417    1.1       cgd 
    418    1.1       cgd 	/*
    419  1.178    rillig 	 * Because of space constraints and similar things, files are archived
    420  1.178    rillig 	 * using their basename, not the entire path.
    421    1.1       cgd 	 */
    422  1.188    rillig 	member = str_basename(member);
    423  1.178    rillig 
    424  1.180    rillig 	for (ln = archives.first; ln != NULL; ln = ln->next) {
    425  1.178    rillig 		const Arch *a = ln->datum;
    426  1.178    rillig 		if (strcmp(a->name, archive) == 0)
    427  1.178    rillig 			break;
    428  1.178    rillig 	}
    429    1.1       cgd 
    430  1.178    rillig 	if (ln != NULL) {
    431  1.178    rillig 		struct ar_hdr *hdr;
    432    1.1       cgd 
    433  1.178    rillig 		ar = ln->datum;
    434  1.178    rillig 		hdr = HashTable_FindValue(&ar->members, member);
    435  1.178    rillig 		if (hdr != NULL)
    436  1.178    rillig 			return hdr;
    437  1.178    rillig 
    438  1.178    rillig 		{
    439  1.178    rillig 			/* Try truncated name */
    440  1.178    rillig 			char copy[AR_MAX_NAME_LEN + 1];
    441  1.178    rillig 			size_t len = strlen(member);
    442  1.178    rillig 
    443  1.178    rillig 			if (len > AR_MAX_NAME_LEN) {
    444  1.178    rillig 				snprintf(copy, sizeof copy, "%s", member);
    445  1.178    rillig 				hdr = HashTable_FindValue(&ar->members, copy);
    446  1.178    rillig 			}
    447  1.178    rillig 			return hdr;
    448  1.178    rillig 		}
    449  1.178    rillig 	}
    450    1.1       cgd 
    451  1.178    rillig 	if (!addToCache) {
    452  1.178    rillig 		/*
    453  1.178    rillig 		 * Caller doesn't want the thing cached, just use
    454  1.178    rillig 		 * ArchFindMember to read the header for the member out and
    455  1.178    rillig 		 * close down the stream again. Since the archive is not to be
    456  1.178    rillig 		 * cached, we assume there's no need to allocate extra room
    457  1.178    rillig 		 * for the header we're returning, so just declare it static.
    458  1.178    rillig 		 */
    459  1.178    rillig 		static struct ar_hdr sarh;
    460   1.17  christos 
    461  1.178    rillig 		arch = ArchFindMember(archive, member, &sarh, "r");
    462  1.178    rillig 		if (arch == NULL)
    463  1.178    rillig 			return NULL;
    464    1.1       cgd 
    465  1.178    rillig 		fclose(arch);
    466  1.178    rillig 		return &sarh;
    467  1.178    rillig 	}
    468   1.81    rillig 
    469  1.167    rillig 	/*
    470  1.178    rillig 	 * We don't have this archive on the list yet, so we want to find out
    471  1.178    rillig 	 * everything that's in it and cache it so we can get at it quickly.
    472  1.167    rillig 	 */
    473  1.178    rillig 	arch = fopen(archive, "r");
    474  1.178    rillig 	if (arch == NULL)
    475  1.178    rillig 		return NULL;
    476    1.1       cgd 
    477  1.167    rillig 	/*
    478  1.178    rillig 	 * We use the ARMAG string to make sure this is an archive we
    479  1.178    rillig 	 * can handle...
    480  1.167    rillig 	 */
    481  1.178    rillig 	if (fread(magic, SARMAG, 1, arch) != 1 ||
    482  1.178    rillig 	    strncmp(magic, ARMAG, SARMAG) != 0) {
    483  1.178    rillig 		(void)fclose(arch);
    484  1.178    rillig 		return NULL;
    485  1.167    rillig 	}
    486  1.178    rillig 
    487  1.178    rillig 	ar = bmake_malloc(sizeof *ar);
    488  1.178    rillig 	ar->name = bmake_strdup(archive);
    489  1.178    rillig 	ar->fnametab = NULL;
    490  1.178    rillig 	ar->fnamesize = 0;
    491  1.178    rillig 	HashTable_Init(&ar->members);
    492  1.178    rillig 	memName[AR_MAX_NAME_LEN] = '\0';
    493  1.178    rillig 
    494  1.178    rillig 	while (fread(&arh, sizeof arh, 1, arch) == 1) {
    495  1.178    rillig 		char *nameend;
    496  1.178    rillig 
    497  1.178    rillig 		/* If the header is bogus, there's no way we can recover. */
    498  1.178    rillig 		if (strncmp(arh.ar_fmag, ARFMAG, sizeof arh.ar_fmag) != 0)
    499  1.178    rillig 			goto badarch;
    500  1.178    rillig 
    501  1.178    rillig 		/*
    502  1.178    rillig 		 * We need to advance the stream's pointer to the start of the
    503  1.178    rillig 		 * next header. Files are padded with newlines to an even-byte
    504  1.178    rillig 		 * boundary, so we need to extract the size of the file from
    505  1.178    rillig 		 * the 'size' field of the header and round it up during the
    506  1.178    rillig 		 * seek.
    507  1.178    rillig 		 */
    508  1.178    rillig 		arh.ar_size[sizeof arh.ar_size - 1] = '\0';
    509  1.178    rillig 		size = (size_t)strtol(arh.ar_size, NULL, 10);
    510  1.178    rillig 
    511  1.178    rillig 		memcpy(memName, arh.ar_name, sizeof arh.ar_name);
    512  1.178    rillig 		nameend = memName + AR_MAX_NAME_LEN;
    513  1.178    rillig 		while (nameend > memName && *nameend == ' ')
    514  1.178    rillig 			nameend--;
    515  1.178    rillig 		nameend[1] = '\0';
    516  1.178    rillig 
    517  1.178    rillig #ifdef SVR4ARCHIVES
    518  1.178    rillig 		/*
    519  1.178    rillig 		 * svr4 names are slash-terminated.
    520  1.178    rillig 		 * Also svr4 extended the AR format.
    521  1.178    rillig 		 */
    522  1.178    rillig 		if (memName[0] == '/') {
    523  1.178    rillig 			/* svr4 magic mode; handle it */
    524  1.178    rillig 			switch (ArchSVR4Entry(ar, memName, size, arch)) {
    525  1.178    rillig 			case -1:	/* Invalid data */
    526  1.178    rillig 				goto badarch;
    527  1.178    rillig 			case 0:		/* List of files entry */
    528  1.178    rillig 				continue;
    529  1.178    rillig 			default:	/* Got the entry */
    530  1.178    rillig 				break;
    531  1.178    rillig 			}
    532  1.178    rillig 		} else {
    533  1.178    rillig 			if (nameend[0] == '/')
    534  1.178    rillig 				nameend[0] = '\0';
    535  1.178    rillig 		}
    536   1.12  christos #endif
    537   1.12  christos 
    538    1.6        pk #ifdef AR_EFMT1
    539  1.178    rillig 		/*
    540  1.178    rillig 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    541  1.178    rillig 		 * first <namelen> bytes of the file
    542  1.178    rillig 		 */
    543  1.178    rillig 		if (strncmp(memName, AR_EFMT1, sizeof AR_EFMT1 - 1) == 0 &&
    544  1.178    rillig 		    ch_isdigit(memName[sizeof AR_EFMT1 - 1])) {
    545    1.6        pk 
    546  1.211    rillig 			size_t elen = (size_t)atoi(
    547  1.211    rillig 			    memName + sizeof AR_EFMT1 - 1);
    548    1.6        pk 
    549  1.203    rillig 			if (elen > MAXPATHLEN)
    550  1.178    rillig 				goto badarch;
    551  1.203    rillig 			if (fread(memName, elen, 1, arch) != 1)
    552  1.178    rillig 				goto badarch;
    553  1.178    rillig 			memName[elen] = '\0';
    554  1.203    rillig 			if (fseek(arch, -(long)elen, SEEK_CUR) != 0)
    555  1.178    rillig 				goto badarch;
    556  1.178    rillig 			if (DEBUG(ARCH) || DEBUG(MAKE))
    557  1.178    rillig 				debug_printf(
    558  1.178    rillig 				    "ArchStatMember: "
    559  1.178    rillig 				    "Extended format entry for %s\n",
    560  1.178    rillig 				    memName);
    561  1.178    rillig 		}
    562    1.6        pk #endif
    563    1.6        pk 
    564  1.178    rillig 		{
    565  1.178    rillig 			struct ar_hdr *cached_hdr = bmake_malloc(
    566  1.178    rillig 			    sizeof *cached_hdr);
    567  1.178    rillig 			memcpy(cached_hdr, &arh, sizeof arh);
    568  1.178    rillig 			HashTable_Set(&ar->members, memName, cached_hdr);
    569  1.178    rillig 		}
    570  1.178    rillig 
    571  1.178    rillig 		if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
    572  1.178    rillig 			goto badarch;
    573    1.1       cgd 	}
    574  1.167    rillig 
    575  1.178    rillig 	fclose(arch);
    576  1.178    rillig 
    577  1.180    rillig 	Lst_Append(&archives, ar);
    578  1.178    rillig 
    579  1.178    rillig 	/*
    580  1.178    rillig 	 * Now that the archive has been read and cached, we can look into
    581  1.178    rillig 	 * the addToCache table to find the desired member's header.
    582  1.178    rillig 	 */
    583  1.178    rillig 	return HashTable_FindValue(&ar->members, member);
    584   1.15  christos 
    585   1.15  christos badarch:
    586  1.178    rillig 	fclose(arch);
    587  1.178    rillig 	HashTable_Done(&ar->members);
    588  1.178    rillig 	free(ar->fnametab);
    589  1.178    rillig 	free(ar);
    590  1.178    rillig 	return NULL;
    591    1.1       cgd }
    592   1.15  christos 
    593   1.15  christos #ifdef SVR4ARCHIVES
    594  1.187    rillig /*
    595  1.187    rillig  * Parse an SVR4 style entry that begins with a slash.
    596  1.187    rillig  * If it is "//", then load the table of filenames.
    597  1.187    rillig  * If it is "/<offset>", then try to substitute the long file name
    598  1.187    rillig  * from offset of a table previously read.
    599  1.187    rillig  * If a table is read, the file pointer is moved to the next archive member.
    600   1.15  christos  *
    601   1.15  christos  * Results:
    602   1.15  christos  *	-1: Bad data in archive
    603   1.15  christos  *	 0: A table was loaded from the file
    604   1.15  christos  *	 1: Name was successfully substituted from table
    605   1.15  christos  *	 2: Name was not successfully substituted from table
    606   1.15  christos  */
    607   1.15  christos static int
    608  1.161    rillig ArchSVR4Entry(Arch *ar, char *inout_name, size_t size, FILE *arch)
    609   1.15  christos {
    610   1.15  christos #define ARLONGNAMES1 "//"
    611   1.15  christos #define ARLONGNAMES2 "/ARFILENAMES"
    612  1.178    rillig 	size_t entry;
    613  1.178    rillig 	char *ptr, *eptr;
    614  1.178    rillig 
    615  1.178    rillig 	if (strncmp(inout_name, ARLONGNAMES1, sizeof ARLONGNAMES1 - 1) == 0 ||
    616  1.178    rillig 	    strncmp(inout_name, ARLONGNAMES2, sizeof ARLONGNAMES2 - 1) == 0) {
    617   1.15  christos 
    618  1.178    rillig 		if (ar->fnametab != NULL) {
    619  1.178    rillig 			DEBUG0(ARCH,
    620  1.208    rillig 			    "Attempted to redefine an SVR4 name table\n");
    621  1.178    rillig 			return -1;
    622  1.178    rillig 		}
    623   1.15  christos 
    624  1.178    rillig 		/*
    625  1.178    rillig 		 * This is a table of archive names, so we build one for
    626  1.178    rillig 		 * ourselves
    627  1.178    rillig 		 */
    628  1.178    rillig 		ar->fnametab = bmake_malloc(size);
    629  1.178    rillig 		ar->fnamesize = size;
    630  1.178    rillig 
    631  1.178    rillig 		if (fread(ar->fnametab, size, 1, arch) != 1) {
    632  1.178    rillig 			DEBUG0(ARCH, "Reading an SVR4 name table failed\n");
    633  1.178    rillig 			return -1;
    634  1.178    rillig 		}
    635  1.178    rillig 		eptr = ar->fnametab + size;
    636  1.178    rillig 		for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
    637  1.178    rillig 			if (*ptr == '/') {
    638  1.178    rillig 				entry++;
    639  1.178    rillig 				*ptr = '\0';
    640  1.178    rillig 			}
    641  1.208    rillig 		DEBUG1(ARCH,
    642  1.208    rillig 		    "Found svr4 archive name table with %lu entries\n",
    643  1.208    rillig 		    (unsigned long)entry);
    644  1.178    rillig 		return 0;
    645   1.15  christos 	}
    646   1.15  christos 
    647  1.178    rillig 	if (inout_name[1] == ' ' || inout_name[1] == '\0')
    648  1.178    rillig 		return 2;
    649   1.15  christos 
    650  1.178    rillig 	entry = (size_t)strtol(&inout_name[1], &eptr, 0);
    651  1.178    rillig 	if ((*eptr != ' ' && *eptr != '\0') || eptr == &inout_name[1]) {
    652  1.178    rillig 		DEBUG1(ARCH, "Could not parse SVR4 name %s\n", inout_name);
    653  1.178    rillig 		return 2;
    654  1.178    rillig 	}
    655  1.178    rillig 	if (entry >= ar->fnamesize) {
    656  1.178    rillig 		DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n",
    657  1.208    rillig 		    inout_name, (unsigned long)ar->fnamesize);
    658  1.178    rillig 		return 2;
    659   1.15  christos 	}
    660   1.15  christos 
    661  1.178    rillig 	DEBUG2(ARCH, "Replaced %s with %s\n", inout_name, &ar->fnametab[entry]);
    662   1.15  christos 
    663  1.178    rillig 	snprintf(inout_name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
    664  1.178    rillig 	return 1;
    665   1.15  christos }
    666   1.15  christos #endif
    667   1.15  christos 
    668    1.1       cgd 
    669  1.199    rillig static bool
    670  1.158    rillig ArchiveMember_HasName(const struct ar_hdr *hdr,
    671  1.158    rillig 		      const char *name, size_t namelen)
    672  1.158    rillig {
    673  1.178    rillig 	const size_t ar_name_len = sizeof hdr->ar_name;
    674  1.178    rillig 	const char *ar_name = hdr->ar_name;
    675  1.158    rillig 
    676  1.178    rillig 	if (strncmp(ar_name, name, namelen) != 0)
    677  1.199    rillig 		return false;
    678  1.158    rillig 
    679  1.178    rillig 	if (namelen >= ar_name_len)
    680  1.178    rillig 		return namelen == ar_name_len;
    681  1.158    rillig 
    682  1.178    rillig 	/* hdr->ar_name is space-padded to the right. */
    683  1.178    rillig 	if (ar_name[namelen] == ' ')
    684  1.199    rillig 		return true;
    685  1.178    rillig 
    686  1.209    rillig 	/*
    687  1.209    rillig 	 * In archives created by GNU binutils 2.27, the member names end
    688  1.209    rillig 	 * with a slash.
    689  1.209    rillig 	 */
    690  1.178    rillig 	if (ar_name[namelen] == '/' &&
    691  1.178    rillig 	    (namelen == ar_name_len || ar_name[namelen + 1] == ' '))
    692  1.199    rillig 		return true;
    693  1.159    rillig 
    694  1.199    rillig 	return false;
    695  1.158    rillig }
    696  1.158    rillig 
    697  1.192    rillig /*
    698  1.192    rillig  * Locate a member of an archive, given the path of the archive and the path
    699  1.157    rillig  * of the desired member.
    700    1.1       cgd  *
    701   1.35       wiz  * Input:
    702   1.35       wiz  *	archive		Path to the archive
    703   1.35       wiz  *	member		Name of member. If it is a path, only the last
    704   1.35       wiz  *			component is used.
    705  1.153    rillig  *	out_arh		Archive header to be filled in
    706  1.157    rillig  *	mode		"r" for read-only access, "r+" for read-write access
    707   1.35       wiz  *
    708  1.157    rillig  * Output:
    709  1.157    rillig  *	return		The archive file, positioned at the start of the
    710  1.157    rillig  *			member's struct ar_hdr, or NULL if the member doesn't
    711  1.157    rillig  *			exist.
    712  1.157    rillig  *	*out_arh	The current struct ar_hdr for member.
    713  1.170    rillig  *
    714  1.170    rillig  * See ArchStatMember for an almost identical copy of this code.
    715    1.1       cgd  */
    716    1.1       cgd static FILE *
    717  1.153    rillig ArchFindMember(const char *archive, const char *member, struct ar_hdr *out_arh,
    718  1.132    rillig 	       const char *mode)
    719    1.1       cgd {
    720  1.178    rillig 	FILE *arch;		/* Stream to archive */
    721  1.178    rillig 	int size;		/* Size of archive member */
    722  1.178    rillig 	char magic[SARMAG];
    723  1.185    rillig 	size_t len;
    724    1.1       cgd 
    725  1.178    rillig 	arch = fopen(archive, mode);
    726  1.178    rillig 	if (arch == NULL)
    727  1.178    rillig 		return NULL;
    728   1.17  christos 
    729  1.178    rillig 	/*
    730  1.178    rillig 	 * We use the ARMAG string to make sure this is an archive we
    731  1.178    rillig 	 * can handle...
    732  1.178    rillig 	 */
    733  1.178    rillig 	if (fread(magic, SARMAG, 1, arch) != 1 ||
    734  1.178    rillig 	    strncmp(magic, ARMAG, SARMAG) != 0) {
    735  1.145    rillig 		fclose(arch);
    736  1.145    rillig 		return NULL;
    737  1.145    rillig 	}
    738  1.145    rillig 
    739  1.145    rillig 	/*
    740  1.178    rillig 	 * Because of space constraints and similar things, files are archived
    741  1.178    rillig 	 * using their basename, not the entire path.
    742  1.145    rillig 	 */
    743  1.188    rillig 	member = str_basename(member);
    744  1.178    rillig 
    745  1.185    rillig 	len = strlen(member);
    746  1.178    rillig 
    747  1.178    rillig 	while (fread(out_arh, sizeof *out_arh, 1, arch) == 1) {
    748  1.145    rillig 
    749  1.178    rillig 		if (strncmp(out_arh->ar_fmag, ARFMAG,
    750  1.178    rillig 			    sizeof out_arh->ar_fmag) != 0) {
    751  1.178    rillig 			/*
    752  1.178    rillig 			 * The header is bogus, so the archive is bad
    753  1.178    rillig 			 * and there's no way we can recover...
    754  1.178    rillig 			 */
    755  1.178    rillig 			fclose(arch);
    756  1.178    rillig 			return NULL;
    757  1.178    rillig 		}
    758  1.178    rillig 
    759  1.178    rillig 		DEBUG5(ARCH, "Reading archive %s member %.*s mtime %.*s\n",
    760  1.208    rillig 		    archive,
    761  1.208    rillig 		    (int)sizeof out_arh->ar_name, out_arh->ar_name,
    762  1.208    rillig 		    (int)sizeof out_arh->ar_date, out_arh->ar_date);
    763  1.178    rillig 
    764  1.178    rillig 		if (ArchiveMember_HasName(out_arh, member, len)) {
    765  1.178    rillig 			/*
    766  1.178    rillig 			 * To make life easier for callers that want to update
    767  1.178    rillig 			 * the archive, we reposition the file at the start of
    768  1.178    rillig 			 * the header we just read before we return the
    769  1.178    rillig 			 * stream. In a more general situation, it might be
    770  1.178    rillig 			 * better to leave the file at the actual member,
    771  1.178    rillig 			 * rather than its header, but not here.
    772  1.178    rillig 			 */
    773  1.178    rillig 			if (fseek(arch, -(long)sizeof *out_arh, SEEK_CUR) !=
    774  1.178    rillig 			    0) {
    775  1.178    rillig 				fclose(arch);
    776  1.178    rillig 				return NULL;
    777  1.178    rillig 			}
    778  1.178    rillig 			return arch;
    779  1.178    rillig 		}
    780  1.178    rillig 
    781  1.178    rillig #ifdef AR_EFMT1
    782  1.178    rillig 		/*
    783  1.178    rillig 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
    784  1.178    rillig 		 * first <namelen> bytes of the file
    785  1.178    rillig 		 */
    786  1.178    rillig 		if (strncmp(out_arh->ar_name, AR_EFMT1, sizeof AR_EFMT1 - 1) ==
    787  1.178    rillig 		    0 &&
    788  1.178    rillig 		    (ch_isdigit(out_arh->ar_name[sizeof AR_EFMT1 - 1]))) {
    789  1.211    rillig 			size_t elen = (size_t)atoi(
    790  1.203    rillig 			    &out_arh->ar_name[sizeof AR_EFMT1 - 1]);
    791  1.178    rillig 			char ename[MAXPATHLEN + 1];
    792  1.178    rillig 
    793  1.203    rillig 			if (elen > MAXPATHLEN) {
    794  1.178    rillig 				fclose(arch);
    795  1.178    rillig 				return NULL;
    796  1.178    rillig 			}
    797  1.203    rillig 			if (fread(ename, elen, 1, arch) != 1) {
    798  1.178    rillig 				fclose(arch);
    799  1.178    rillig 				return NULL;
    800  1.178    rillig 			}
    801  1.178    rillig 			ename[elen] = '\0';
    802  1.178    rillig 			if (DEBUG(ARCH) || DEBUG(MAKE))
    803  1.178    rillig 				debug_printf(
    804  1.178    rillig 				    "ArchFindMember: "
    805  1.178    rillig 				    "Extended format entry for %s\n",
    806  1.178    rillig 				    ename);
    807  1.178    rillig 			if (strncmp(ename, member, len) == 0) {
    808  1.178    rillig 				/* Found as extended name */
    809  1.178    rillig 				if (fseek(arch,
    810  1.203    rillig 				    -(long)(sizeof(struct ar_hdr) - elen),
    811  1.203    rillig 				    SEEK_CUR) != 0) {
    812  1.178    rillig 					fclose(arch);
    813  1.178    rillig 					return NULL;
    814  1.178    rillig 				}
    815  1.178    rillig 				return arch;
    816  1.178    rillig 			}
    817  1.203    rillig 			if (fseek(arch, -(long)elen, SEEK_CUR) != 0) {
    818  1.178    rillig 				fclose(arch);
    819  1.178    rillig 				return NULL;
    820  1.178    rillig 			}
    821   1.70  riastrad 		}
    822  1.145    rillig #endif
    823  1.145    rillig 
    824  1.178    rillig 		/*
    825  1.178    rillig 		 * This isn't the member we're after, so we need to advance the
    826  1.178    rillig 		 * stream's pointer to the start of the next header. Files are
    827  1.178    rillig 		 * padded with newlines to an even-byte boundary, so we need to
    828  1.178    rillig 		 * extract the size of the file from the 'size' field of the
    829  1.178    rillig 		 * header and round it up during the seek.
    830  1.178    rillig 		 */
    831  1.178    rillig 		out_arh->ar_size[sizeof out_arh->ar_size - 1] = '\0';
    832  1.178    rillig 		size = (int)strtol(out_arh->ar_size, NULL, 10);
    833  1.203    rillig 		if (fseek(arch, (size + 1) & ~1L, SEEK_CUR) != 0) {
    834  1.178    rillig 			fclose(arch);
    835  1.178    rillig 			return NULL;
    836  1.178    rillig 		}
    837  1.145    rillig 	}
    838    1.1       cgd 
    839  1.178    rillig 	fclose(arch);
    840  1.178    rillig 	return NULL;
    841    1.1       cgd }
    842    1.1       cgd 
    843  1.192    rillig /*
    844  1.192    rillig  * Touch a member of an archive, on disk.
    845  1.157    rillig  * The GNode's modification time is left as-is.
    846  1.157    rillig  *
    847  1.157    rillig  * The st_mtime of the entire archive is also changed.
    848  1.157    rillig  * For a library, it may be required to run ranlib after this.
    849    1.1       cgd  *
    850   1.35       wiz  * Input:
    851   1.35       wiz  *	gn		Node of member to touch
    852   1.35       wiz  *
    853    1.1       cgd  * Results:
    854    1.1       cgd  *	The 'time' field of the member's header is updated.
    855    1.1       cgd  */
    856    1.1       cgd void
    857   1.35       wiz Arch_Touch(GNode *gn)
    858    1.1       cgd {
    859  1.178    rillig 	FILE *f;
    860  1.178    rillig 	struct ar_hdr arh;
    861    1.1       cgd 
    862  1.178    rillig 	f = ArchFindMember(GNode_VarArchive(gn), GNode_VarMember(gn), &arh,
    863  1.208    rillig 	    "r+");
    864  1.178    rillig 	if (f == NULL)
    865  1.178    rillig 		return;
    866  1.178    rillig 
    867  1.178    rillig 	snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
    868  1.178    rillig 	(void)fwrite(&arh, sizeof arh, 1, f);
    869  1.178    rillig 	fclose(f);		/* TODO: handle errors */
    870    1.1       cgd }
    871    1.1       cgd 
    872  1.192    rillig /*
    873  1.192    rillig  * Given a node which represents a library, touch the thing, making sure that
    874  1.159    rillig  * the table of contents is also touched.
    875  1.102    rillig  *
    876  1.102    rillig  * Both the modification time of the library and of the RANLIBMAG member are
    877  1.192    rillig  * set to 'now'.
    878  1.192    rillig  */
    879  1.193    rillig /*ARGSUSED*/
    880    1.1       cgd void
    881  1.159    rillig Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
    882    1.1       cgd {
    883   1.12  christos #ifdef RANLIBMAG
    884  1.178    rillig 	FILE *f;
    885  1.178    rillig 	struct ar_hdr arh;	/* Header describing table of contents */
    886  1.178    rillig 	struct utimbuf times;
    887  1.178    rillig 
    888  1.178    rillig 	f = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
    889  1.178    rillig 	if (f == NULL)
    890  1.178    rillig 		return;
    891  1.178    rillig 
    892  1.178    rillig 	snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
    893  1.178    rillig 	(void)fwrite(&arh, sizeof arh, 1, f);
    894  1.178    rillig 	fclose(f);		/* TODO: handle errors */
    895    1.1       cgd 
    896  1.178    rillig 	times.actime = times.modtime = now;
    897  1.178    rillig 	utime(gn->path, &times);	/* TODO: handle errors */
    898   1.12  christos #endif
    899    1.1       cgd }
    900    1.1       cgd 
    901  1.192    rillig /*
    902  1.192    rillig  * Update the mtime of the GNode with the mtime from the archive member on
    903  1.192    rillig  * disk (or in the cache).
    904  1.192    rillig  */
    905  1.174    rillig void
    906  1.174    rillig Arch_UpdateMTime(GNode *gn)
    907    1.1       cgd {
    908  1.178    rillig 	struct ar_hdr *arh;
    909    1.7       jtc 
    910  1.199    rillig 	arh = ArchStatMember(GNode_VarArchive(gn), GNode_VarMember(gn), true);
    911  1.178    rillig 	if (arh != NULL)
    912  1.178    rillig 		gn->mtime = (time_t)strtol(arh->ar_date, NULL, 10);
    913  1.178    rillig 	else
    914  1.178    rillig 		gn->mtime = 0;
    915    1.1       cgd }
    916    1.1       cgd 
    917  1.192    rillig /*
    918  1.192    rillig  * Given a nonexistent archive member's node, update gn->mtime from its
    919  1.192    rillig  * archived form, if it exists.
    920  1.192    rillig  */
    921  1.173    rillig void
    922  1.173    rillig Arch_UpdateMemberMTime(GNode *gn)
    923    1.1       cgd {
    924  1.178    rillig 	GNodeListNode *ln;
    925    1.1       cgd 
    926  1.179    rillig 	for (ln = gn->parents.first; ln != NULL; ln = ln->next) {
    927  1.178    rillig 		GNode *pgn = ln->datum;
    928    1.1       cgd 
    929  1.178    rillig 		if (pgn->type & OP_ARCHV) {
    930  1.178    rillig 			/*
    931  1.178    rillig 			 * If the parent is an archive specification and is
    932  1.178    rillig 			 * being made and its member's name matches the name
    933  1.178    rillig 			 * of the node we were given, record the modification
    934  1.178    rillig 			 * time of the parent in the child. We keep searching
    935  1.178    rillig 			 * its parents in case some other parent requires this
    936  1.178    rillig 			 * child to exist.
    937  1.178    rillig 			 */
    938  1.178    rillig 			const char *nameStart = strchr(pgn->name, '(') + 1;
    939  1.178    rillig 			const char *nameEnd = strchr(nameStart, ')');
    940  1.178    rillig 			size_t nameLen = (size_t)(nameEnd - nameStart);
    941  1.178    rillig 
    942  1.204    rillig 			if (pgn->flags.remake &&
    943  1.178    rillig 			    strncmp(nameStart, gn->name, nameLen) == 0) {
    944  1.178    rillig 				Arch_UpdateMTime(pgn);
    945  1.178    rillig 				gn->mtime = pgn->mtime;
    946  1.178    rillig 			}
    947  1.204    rillig 		} else if (pgn->flags.remake) {
    948  1.178    rillig 			/*
    949  1.178    rillig 			 * Something which isn't a library depends on the
    950  1.178    rillig 			 * existence of this target, so it needs to exist.
    951  1.178    rillig 			 */
    952  1.178    rillig 			gn->mtime = 0;
    953  1.178    rillig 			break;
    954  1.178    rillig 		}
    955    1.1       cgd 	}
    956    1.1       cgd }
    957    1.1       cgd 
    958  1.192    rillig /*
    959  1.192    rillig  * Search for a library along the given search path.
    960  1.102    rillig  *
    961  1.102    rillig  * The node's 'path' field is set to the found path (including the
    962  1.102    rillig  * actual file name, not -l...). If the system can handle the -L
    963  1.102    rillig  * flag when linking (or we cannot find the library), we assume that
    964  1.102    rillig  * the user has placed the .LIBS variable in the final linking
    965  1.102    rillig  * command (or the linker will know where to find it) and set the
    966  1.102    rillig  * TARGET variable for this node to be the node's name. Otherwise,
    967  1.102    rillig  * we set the TARGET variable to be the full path of the library,
    968  1.102    rillig  * as returned by Dir_FindFile.
    969    1.1       cgd  *
    970   1.35       wiz  * Input:
    971   1.35       wiz  *	gn		Node of library to find
    972    1.1       cgd  */
    973    1.1       cgd void
    974  1.117    rillig Arch_FindLib(GNode *gn, SearchPath *path)
    975    1.1       cgd {
    976  1.178    rillig 	char *libName = str_concat3("lib", gn->name + 2, ".a");
    977  1.178    rillig 	gn->path = Dir_FindFile(libName, path);
    978  1.178    rillig 	free(libName);
    979    1.1       cgd 
    980  1.197    rillig 	Var_Set(gn, TARGET, gn->name);
    981    1.1       cgd }
    982    1.1       cgd 
    983  1.202    rillig /* ARGSUSED */
    984  1.201    rillig static bool
    985  1.201    rillig RanlibOODate(const GNode *gn MAKE_ATTR_UNUSED)
    986  1.201    rillig {
    987  1.201    rillig #ifdef RANLIBMAG
    988  1.201    rillig 	struct ar_hdr *arh;	/* Header for __.SYMDEF */
    989  1.201    rillig 	int tocModTime;		/* The table-of-contents' mod time */
    990  1.201    rillig 
    991  1.201    rillig 	arh = ArchStatMember(gn->path, RANLIBMAG, false);
    992  1.201    rillig 
    993  1.201    rillig 	if (arh == NULL) {
    994  1.201    rillig 		/* A library without a table of contents is out-of-date. */
    995  1.201    rillig 		if (DEBUG(ARCH) || DEBUG(MAKE))
    996  1.201    rillig 			debug_printf("no toc...");
    997  1.201    rillig 		return true;
    998  1.201    rillig 	}
    999  1.201    rillig 
   1000  1.201    rillig 	tocModTime = (int)strtol(arh->ar_date, NULL, 10);
   1001  1.201    rillig 
   1002  1.201    rillig 	if (DEBUG(ARCH) || DEBUG(MAKE))
   1003  1.201    rillig 		debug_printf("%s modified %s...",
   1004  1.201    rillig 		    RANLIBMAG, Targ_FmtTime(tocModTime));
   1005  1.201    rillig 	return gn->youngestChild == NULL ||
   1006  1.201    rillig 	       gn->youngestChild->mtime > tocModTime;
   1007  1.201    rillig #else
   1008  1.201    rillig 	return false;
   1009  1.201    rillig #endif
   1010  1.201    rillig }
   1011  1.201    rillig 
   1012  1.192    rillig /*
   1013  1.192    rillig  * Decide if a node with the OP_LIB attribute is out-of-date. Called from
   1014  1.172    rillig  * GNode_IsOODate to make its life easier.
   1015  1.157    rillig  * The library is cached if it hasn't been already.
   1016  1.102    rillig  *
   1017  1.102    rillig  * There are several ways for a library to be out-of-date that are
   1018  1.102    rillig  * not available to ordinary files. In addition, there are ways
   1019  1.102    rillig  * that are open to regular files that are not available to
   1020  1.171    rillig  * libraries.
   1021  1.171    rillig  *
   1022  1.171    rillig  * A library that is only used as a source is never
   1023  1.102    rillig  * considered out-of-date by itself. This does not preclude the
   1024  1.102    rillig  * library's modification time from making its parent be out-of-date.
   1025  1.102    rillig  * A library will be considered out-of-date for any of these reasons,
   1026  1.102    rillig  * given that it is a target on a dependency line somewhere:
   1027  1.102    rillig  *
   1028  1.102    rillig  *	Its modification time is less than that of one of its sources
   1029  1.140    rillig  *	(gn->mtime < gn->youngestChild->mtime).
   1030  1.102    rillig  *
   1031  1.102    rillig  *	Its modification time is greater than the time at which the make
   1032  1.102    rillig  *	began (i.e. it's been modified in the course of the make, probably
   1033  1.102    rillig  *	by archiving).
   1034  1.102    rillig  *
   1035  1.102    rillig  *	The modification time of one of its sources is greater than the one
   1036  1.102    rillig  *	of its RANLIBMAG member (i.e. its table of contents is out-of-date).
   1037  1.171    rillig  *	We don't compare the archive time vs. TOC time because they can be
   1038  1.102    rillig  *	too close. In my opinion we should not bother with the TOC at all
   1039  1.102    rillig  *	since this is used by 'ar' rules that affect the data contents of the
   1040  1.102    rillig  *	archive, not by ranlib rules, which affect the TOC.
   1041    1.1       cgd  */
   1042  1.199    rillig bool
   1043   1.35       wiz Arch_LibOODate(GNode *gn)
   1044    1.1       cgd {
   1045   1.17  christos 
   1046  1.178    rillig 	if (gn->type & OP_PHONY) {
   1047  1.201    rillig 		return true;
   1048  1.179    rillig 	} else if (!GNode_IsTarget(gn) && Lst_IsEmpty(&gn->children)) {
   1049  1.201    rillig 		return false;
   1050  1.179    rillig 	} else if ((!Lst_IsEmpty(&gn->children) && gn->youngestChild == NULL) ||
   1051  1.178    rillig 		   (gn->mtime > now) ||
   1052  1.178    rillig 		   (gn->youngestChild != NULL &&
   1053  1.178    rillig 		    gn->mtime < gn->youngestChild->mtime)) {
   1054  1.201    rillig 		return true;
   1055  1.178    rillig 	} else {
   1056  1.201    rillig 		return RanlibOODate(gn);
   1057  1.178    rillig 	}
   1058    1.1       cgd }
   1059    1.1       cgd 
   1060  1.133    rillig /* Initialize the archives module. */
   1061    1.1       cgd void
   1062   1.35       wiz Arch_Init(void)
   1063    1.1       cgd {
   1064  1.180    rillig 	Lst_Init(&archives);
   1065    1.7       jtc }
   1066    1.7       jtc 
   1067  1.133    rillig /* Clean up the archives module. */
   1068    1.7       jtc void
   1069   1.35       wiz Arch_End(void)
   1070    1.7       jtc {
   1071   1.31   mycroft #ifdef CLEANUP
   1072  1.216    rillig 	ArchListNode *ln;
   1073  1.216    rillig 
   1074  1.216    rillig 	for (ln = archives.first; ln != NULL; ln = ln->next)
   1075  1.216    rillig 		ArchFree(ln->datum);
   1076  1.216    rillig 	Lst_Done(&archives);
   1077   1.31   mycroft #endif
   1078   1.17  christos }
   1079   1.17  christos 
   1080  1.199    rillig bool
   1081   1.35       wiz Arch_IsLib(GNode *gn)
   1082   1.17  christos {
   1083  1.178    rillig 	static const char armag[] = "!<arch>\n";
   1084  1.178    rillig 	char buf[sizeof armag - 1];
   1085  1.178    rillig 	int fd;
   1086   1.17  christos 
   1087  1.178    rillig 	if ((fd = open(gn->path, O_RDONLY)) == -1)
   1088  1.199    rillig 		return false;
   1089  1.178    rillig 
   1090  1.178    rillig 	if (read(fd, buf, sizeof buf) != sizeof buf) {
   1091  1.178    rillig 		(void)close(fd);
   1092  1.199    rillig 		return false;
   1093  1.178    rillig 	}
   1094   1.17  christos 
   1095   1.45  christos 	(void)close(fd);
   1096   1.17  christos 
   1097  1.178    rillig 	return memcmp(buf, armag, sizeof buf) == 0;
   1098    1.1       cgd }
   1099