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