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