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