arch.c revision 321964
1/*	$NetBSD: arch.c,v 1.70 2017/04/16 20:49:09 riastradh 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
72static char rcsid[] = "$NetBSD: arch.c,v 1.70 2017/04/16 20:49:09 riastradh Exp $";
73#else
74#include <sys/cdefs.h>
75#ifndef lint
76#if 0
77static char sccsid[] = "@(#)arch.c	8.2 (Berkeley) 1/2/94";
78#else
79__RCSID("$NetBSD: arch.c,v 1.70 2017/04/16 20:49:09 riastradh 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 *	    	  	    	FAILURE 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#ifdef HAVE_CONFIG_H
133# include "config.h"
134#endif
135#include    <sys/types.h>
136#include    <sys/stat.h>
137#include    <sys/time.h>
138#include    <sys/param.h>
139#ifdef HAVE_AR_H
140#include    <ar.h>
141#else
142struct ar_hdr {
143        char ar_name[16];               /* name */
144        char ar_date[12];               /* modification time */
145        char ar_uid[6];                 /* user id */
146        char ar_gid[6];                 /* group id */
147        char ar_mode[8];                /* octal file permissions */
148        char ar_size[10];               /* size in bytes */
149#ifndef ARFMAG
150#define ARFMAG  "`\n"
151#endif
152        char ar_fmag[2];                /* consistency check */
153};
154#endif
155#if defined(HAVE_RANLIB_H) && !(defined(__ELF__) || defined(NO_RANLIB))
156#include    <ranlib.h>
157#endif
158#include    <stdio.h>
159#include    <stdlib.h>
160#ifdef HAVE_UTIME_H
161#include    <utime.h>
162#endif
163
164#include    "make.h"
165#include    "hash.h"
166#include    "dir.h"
167
168#ifdef TARGET_MACHINE
169#undef MAKE_MACHINE
170#define MAKE_MACHINE TARGET_MACHINE
171#endif
172#ifdef TARGET_MACHINE_ARCH
173#undef MAKE_MACHINE_ARCH
174#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
175#endif
176
177static Lst	  archives;   /* Lst of archives we've already examined */
178
179typedef struct Arch {
180    char	  *name;      /* Name of archive */
181    Hash_Table	  members;    /* All the members of the archive described
182			       * by <name, struct ar_hdr *> key/value pairs */
183    char	  *fnametab;  /* Extended name table strings */
184    size_t	  fnamesize;  /* Size of the string table */
185} Arch;
186
187static int ArchFindArchive(const void *, const void *);
188#ifdef CLEANUP
189static void ArchFree(void *);
190#endif
191static struct ar_hdr *ArchStatMember(char *, char *, Boolean);
192static FILE *ArchFindMember(char *, char *, struct ar_hdr *, const char *);
193#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
194#define SVR4ARCHIVES
195static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
196#endif
197
198
199#if defined(_AIX)
200# define AR_NAME _ar_name.ar_name
201# define AR_FMAG _ar_name.ar_fmag
202# define SARMAG  SAIAMAG
203# define ARMAG   AIAMAG
204# define ARFMAG  AIAFMAG
205#endif
206#ifndef  AR_NAME
207# define AR_NAME ar_name
208#endif
209#ifndef  AR_DATE
210# define AR_DATE ar_date
211#endif
212#ifndef  AR_SIZE
213# define AR_SIZE ar_size
214#endif
215#ifndef  AR_FMAG
216# define AR_FMAG ar_fmag
217#endif
218#ifndef ARMAG
219# define ARMAG	"!<arch>\n"
220#endif
221#ifndef SARMAG
222# define SARMAG	8
223#endif
224
225#define AR_MAX_NAME_LEN	    (sizeof(arh.AR_NAME)-1)
226
227#ifdef CLEANUP
228/*-
229 *-----------------------------------------------------------------------
230 * ArchFree --
231 *	Free memory used by an archive
232 *
233 * Results:
234 *	None.
235 *
236 * Side Effects:
237 *	None.
238 *
239 *-----------------------------------------------------------------------
240 */
241static void
242ArchFree(void *ap)
243{
244    Arch *a = (Arch *)ap;
245    Hash_Search	  search;
246    Hash_Entry	  *entry;
247
248    /* Free memory from hash entries */
249    for (entry = Hash_EnumFirst(&a->members, &search);
250	 entry != NULL;
251	 entry = Hash_EnumNext(&search))
252	free(Hash_GetValue(entry));
253
254    free(a->name);
255    free(a->fnametab);
256    Hash_DeleteTable(&a->members);
257    free(a);
258}
259#endif
260
261
262/*-
263 *-----------------------------------------------------------------------
264 * Arch_ParseArchive --
265 *	Parse the archive specification in the given line and find/create
266 *	the nodes for the specified archive members, placing their nodes
267 *	on the given list.
268 *
269 * Input:
270 *	linePtr		Pointer to start of specification
271 *	nodeLst		Lst on which to place the nodes
272 *	ctxt		Context in which to expand variables
273 *
274 * Results:
275 *	SUCCESS if it was a valid specification. The linePtr is updated
276 *	to point to the first non-space after the archive spec. The
277 *	nodes for the members are placed on the given list.
278 *
279 * Side Effects:
280 *	Some nodes may be created. The given list is extended.
281 *
282 *-----------------------------------------------------------------------
283 */
284ReturnStatus
285Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
286{
287    char	    *cp;	    /* Pointer into line */
288    GNode	    *gn;     	    /* New node */
289    char	    *libName;  	    /* Library-part of specification */
290    char	    *memName;  	    /* Member-part of specification */
291    char	    *nameBuf;	    /* temporary place for node name */
292    char	    saveChar;  	    /* Ending delimiter of member-name */
293    Boolean 	    subLibName;	    /* TRUE if libName should have/had
294				     * variable substitution performed on it */
295
296    libName = *linePtr;
297
298    subLibName = FALSE;
299
300    for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
301	if (*cp == '$') {
302	    /*
303	     * Variable spec, so call the Var module to parse the puppy
304	     * so we can safely advance beyond it...
305	     */
306	    int 	length;
307	    void	*freeIt;
308	    char	*result;
309
310	    result = Var_Parse(cp, ctxt, VARF_UNDEFERR|VARF_WANTRES,
311			       &length, &freeIt);
312	    free(freeIt);
313
314	    if (result == var_Error) {
315		return(FAILURE);
316	    } else {
317		subLibName = TRUE;
318	    }
319
320	    cp += length-1;
321	}
322    }
323
324    *cp++ = '\0';
325    if (subLibName) {
326	libName = Var_Subst(NULL, libName, ctxt, VARF_UNDEFERR|VARF_WANTRES);
327    }
328
329
330    for (;;) {
331	/*
332	 * First skip to the start of the member's name, mark that
333	 * place and skip to the end of it (either white-space or
334	 * a close paren).
335	 */
336	Boolean	doSubst = FALSE; /* TRUE if need to substitute in memName */
337
338	while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) {
339	    cp++;
340	}
341	memName = cp;
342	while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) {
343	    if (*cp == '$') {
344		/*
345		 * Variable spec, so call the Var module to parse the puppy
346		 * so we can safely advance beyond it...
347		 */
348		int 	length;
349		void	*freeIt;
350		char	*result;
351
352		result = Var_Parse(cp, ctxt, VARF_UNDEFERR|VARF_WANTRES,
353				   &length, &freeIt);
354		free(freeIt);
355
356		if (result == var_Error) {
357		    return(FAILURE);
358		} else {
359		    doSubst = TRUE;
360		}
361
362		cp += length;
363	    } else {
364		cp++;
365	    }
366	}
367
368	/*
369	 * If the specification ends without a closing parenthesis,
370	 * chances are there's something wrong (like a missing backslash),
371	 * so it's better to return failure than allow such things to happen
372	 */
373	if (*cp == '\0') {
374	    printf("No closing parenthesis in archive specification\n");
375	    return (FAILURE);
376	}
377
378	/*
379	 * If we didn't move anywhere, we must be done
380	 */
381	if (cp == memName) {
382	    break;
383	}
384
385	saveChar = *cp;
386	*cp = '\0';
387
388	/*
389	 * XXX: This should be taken care of intelligently by
390	 * SuffExpandChildren, both for the archive and the member portions.
391	 */
392	/*
393	 * If member contains variables, try and substitute for them.
394	 * This will slow down archive specs with dynamic sources, of course,
395	 * since we'll be (non-)substituting them three times, but them's
396	 * the breaks -- we need to do this since SuffExpandChildren calls
397	 * us, otherwise we could assume the thing would be taken care of
398	 * later.
399	 */
400	if (doSubst) {
401	    char    *buf;
402	    char    *sacrifice;
403	    char    *oldMemName = memName;
404	    size_t   sz;
405
406	    memName = Var_Subst(NULL, memName, ctxt,
407				VARF_UNDEFERR|VARF_WANTRES);
408
409	    /*
410	     * Now form an archive spec and recurse to deal with nested
411	     * variables and multi-word variable values.... The results
412	     * are just placed at the end of the nodeLst we're returning.
413	     */
414	    sz = strlen(memName)+strlen(libName)+3;
415	    buf = sacrifice = bmake_malloc(sz);
416
417	    snprintf(buf, sz, "%s(%s)", libName, memName);
418
419	    if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
420		/*
421		 * Must contain dynamic sources, so we can't deal with it now.
422		 * Just create an ARCHV node for the thing and let
423		 * SuffExpandChildren handle it...
424		 */
425		gn = Targ_FindNode(buf, TARG_CREATE);
426
427		if (gn == NULL) {
428		    free(buf);
429		    return(FAILURE);
430		} else {
431		    gn->type |= OP_ARCHV;
432		    (void)Lst_AtEnd(nodeLst, gn);
433		}
434	    } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
435		/*
436		 * Error in nested call -- free buffer and return FAILURE
437		 * ourselves.
438		 */
439		free(buf);
440		return(FAILURE);
441	    }
442	    /*
443	     * Free buffer and continue with our work.
444	     */
445	    free(buf);
446	} else if (Dir_HasWildcards(memName)) {
447	    Lst	  members = Lst_Init(FALSE);
448	    char  *member;
449	    size_t sz = MAXPATHLEN, nsz;
450	    nameBuf = bmake_malloc(sz);
451
452	    Dir_Expand(memName, dirSearchPath, members);
453	    while (!Lst_IsEmpty(members)) {
454		member = (char *)Lst_DeQueue(members);
455		nsz = strlen(libName) + strlen(member) + 3;
456		if (sz > nsz)
457		    nameBuf = bmake_realloc(nameBuf, sz = nsz * 2);
458
459		snprintf(nameBuf, sz, "%s(%s)", libName, member);
460		free(member);
461		gn = Targ_FindNode(nameBuf, TARG_CREATE);
462		if (gn == NULL) {
463		    free(nameBuf);
464		    return (FAILURE);
465		} else {
466		    /*
467		     * We've found the node, but have to make sure the rest of
468		     * the world knows it's an archive member, without having
469		     * to constantly check for parentheses, so we type the
470		     * thing with the OP_ARCHV bit before we place it on the
471		     * end of the provided list.
472		     */
473		    gn->type |= OP_ARCHV;
474		    (void)Lst_AtEnd(nodeLst, gn);
475		}
476	    }
477	    Lst_Destroy(members, NULL);
478	    free(nameBuf);
479	} else {
480	    size_t	sz = strlen(libName) + strlen(memName) + 3;
481	    nameBuf = bmake_malloc(sz);
482	    snprintf(nameBuf, sz, "%s(%s)", libName, memName);
483	    gn = Targ_FindNode(nameBuf, TARG_CREATE);
484	    free(nameBuf);
485	    if (gn == NULL) {
486		return (FAILURE);
487	    } else {
488		/*
489		 * We've found the node, but have to make sure the rest of the
490		 * world knows it's an archive member, without having to
491		 * constantly check for parentheses, so we type the thing with
492		 * the OP_ARCHV bit before we place it on the end of the
493		 * provided list.
494		 */
495		gn->type |= OP_ARCHV;
496		(void)Lst_AtEnd(nodeLst, gn);
497	    }
498	}
499	if (doSubst) {
500	    free(memName);
501	}
502
503	*cp = saveChar;
504    }
505
506    /*
507     * If substituted libName, free it now, since we need it no longer.
508     */
509    if (subLibName) {
510	free(libName);
511    }
512
513    /*
514     * We promised the pointer would be set up at the next non-space, so
515     * we must advance cp there before setting *linePtr... (note that on
516     * entrance to the loop, cp is guaranteed to point at a ')')
517     */
518    do {
519	cp++;
520    } while (*cp != '\0' && isspace ((unsigned char)*cp));
521
522    *linePtr = cp;
523    return (SUCCESS);
524}
525
526/*-
527 *-----------------------------------------------------------------------
528 * ArchFindArchive --
529 *	See if the given archive is the one we are looking for. Called
530 *	From ArchStatMember and ArchFindMember via Lst_Find.
531 *
532 * Input:
533 *	ar		Current list element
534 *	archName	Name we want
535 *
536 * Results:
537 *	0 if it is, non-zero if it isn't.
538 *
539 * Side Effects:
540 *	None.
541 *
542 *-----------------------------------------------------------------------
543 */
544static int
545ArchFindArchive(const void *ar, const void *archName)
546{
547    return (strcmp(archName, ((const Arch *)ar)->name));
548}
549
550/*-
551 *-----------------------------------------------------------------------
552 * ArchStatMember --
553 *	Locate a member of an archive, given the path of the archive and
554 *	the path of the desired member.
555 *
556 * Input:
557 *	archive		Path to the archive
558 *	member		Name of member. If it is a path, only the last
559 *			component is used.
560 *	hash		TRUE if archive should be hashed if not already so.
561 *
562 * Results:
563 *	A pointer to the current struct ar_hdr structure for the member. Note
564 *	That no position is returned, so this is not useful for touching
565 *	archive members. This is mostly because we have no assurances that
566 *	The archive will remain constant after we read all the headers, so
567 *	there's not much point in remembering the position...
568 *
569 * Side Effects:
570 *
571 *-----------------------------------------------------------------------
572 */
573static struct ar_hdr *
574ArchStatMember(char *archive, char *member, Boolean hash)
575{
576    FILE *	  arch;	      /* Stream to archive */
577    int		  size;       /* Size of archive member */
578    char	  *cp;	      /* Useful character pointer */
579    char	  magic[SARMAG];
580    LstNode	  ln;	      /* Lst member containing archive descriptor */
581    Arch	  *ar;	      /* Archive descriptor */
582    Hash_Entry	  *he;	      /* Entry containing member's description */
583    struct ar_hdr arh;        /* archive-member header for reading archive */
584    char	  memName[MAXPATHLEN+1];
585    	    	    	    /* Current member name while hashing. */
586
587    /*
588     * Because of space constraints and similar things, files are archived
589     * using their final path components, not the entire thing, so we need
590     * to point 'member' to the final component, if there is one, to make
591     * the comparisons easier...
592     */
593    cp = strrchr(member, '/');
594    if (cp != NULL) {
595	member = cp + 1;
596    }
597
598    ln = Lst_Find(archives, archive, ArchFindArchive);
599    if (ln != NULL) {
600	ar = (Arch *)Lst_Datum(ln);
601
602	he = Hash_FindEntry(&ar->members, member);
603
604	if (he != NULL) {
605	    return ((struct ar_hdr *)Hash_GetValue(he));
606	} else {
607	    /* Try truncated name */
608	    char copy[AR_MAX_NAME_LEN+1];
609	    size_t len = strlen(member);
610
611	    if (len > AR_MAX_NAME_LEN) {
612		len = AR_MAX_NAME_LEN;
613		strncpy(copy, member, AR_MAX_NAME_LEN);
614		copy[AR_MAX_NAME_LEN] = '\0';
615	    }
616	    if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
617		return ((struct ar_hdr *)Hash_GetValue(he));
618	    return NULL;
619	}
620    }
621
622    if (!hash) {
623	/*
624	 * Caller doesn't want the thing hashed, just use ArchFindMember
625	 * to read the header for the member out and close down the stream
626	 * again. Since the archive is not to be hashed, we assume there's
627	 * no need to allocate extra room for the header we're returning,
628	 * so just declare it static.
629	 */
630	 static struct ar_hdr	sarh;
631
632	 arch = ArchFindMember(archive, member, &sarh, "r");
633
634	 if (arch == NULL) {
635	    return NULL;
636	} else {
637	    fclose(arch);
638	    return (&sarh);
639	}
640    }
641
642    /*
643     * We don't have this archive on the list yet, so we want to find out
644     * everything that's in it and cache it so we can get at it quickly.
645     */
646    arch = fopen(archive, "r");
647    if (arch == NULL) {
648	return NULL;
649    }
650
651    /*
652     * We use the ARMAG string to make sure this is an archive we
653     * can handle...
654     */
655    if ((fread(magic, SARMAG, 1, arch) != 1) ||
656    	(strncmp(magic, ARMAG, SARMAG) != 0)) {
657	    fclose(arch);
658	    return NULL;
659    }
660
661    ar = bmake_malloc(sizeof(Arch));
662    ar->name = bmake_strdup(archive);
663    ar->fnametab = NULL;
664    ar->fnamesize = 0;
665    Hash_InitTable(&ar->members, -1);
666    memName[AR_MAX_NAME_LEN] = '\0';
667
668    while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
669	if (strncmp( arh.AR_FMAG, ARFMAG, sizeof(arh.AR_FMAG)) != 0) {
670	    /*
671	     * The header is bogus, so the archive is bad
672	     * and there's no way we can recover...
673	     */
674	    goto badarch;
675	} else {
676	    /*
677	     * We need to advance the stream's pointer to the start of the
678	     * next header. Files are padded with newlines to an even-byte
679	     * boundary, so we need to extract the size of the file from the
680	     * 'size' field of the header and round it up during the seek.
681	     */
682	    arh.AR_SIZE[sizeof(arh.AR_SIZE)-1] = '\0';
683	    size = (int)strtol(arh.AR_SIZE, NULL, 10);
684
685	    (void)strncpy(memName, arh.AR_NAME, sizeof(arh.AR_NAME));
686	    for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
687		continue;
688	    }
689	    cp[1] = '\0';
690
691#ifdef SVR4ARCHIVES
692	    /*
693	     * svr4 names are slash terminated. Also svr4 extended AR format.
694	     */
695	    if (memName[0] == '/') {
696		/*
697		 * svr4 magic mode; handle it
698		 */
699		switch (ArchSVR4Entry(ar, memName, size, arch)) {
700		case -1:  /* Invalid data */
701		    goto badarch;
702		case 0:	  /* List of files entry */
703		    continue;
704		default:  /* Got the entry */
705		    break;
706		}
707	    }
708	    else {
709		if (cp[0] == '/')
710		    cp[0] = '\0';
711	    }
712#endif
713
714#ifdef AR_EFMT1
715	    /*
716	     * BSD 4.4 extended AR format: #1/<namelen>, with name as the
717	     * first <namelen> bytes of the file
718	     */
719	    if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
720		isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) {
721
722		unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
723
724		if (elen > MAXPATHLEN)
725			goto badarch;
726		if (fread(memName, elen, 1, arch) != 1)
727			goto badarch;
728		memName[elen] = '\0';
729		if (fseek(arch, -elen, SEEK_CUR) != 0)
730			goto badarch;
731		if (DEBUG(ARCH) || DEBUG(MAKE)) {
732		    fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName);
733		}
734	    }
735#endif
736
737	    he = Hash_CreateEntry(&ar->members, memName, NULL);
738	    Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
739	    memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
740	}
741	if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
742	    goto badarch;
743    }
744
745    fclose(arch);
746
747    (void)Lst_AtEnd(archives, ar);
748
749    /*
750     * Now that the archive has been read and cached, we can look into
751     * the hash table to find the desired member's header.
752     */
753    he = Hash_FindEntry(&ar->members, member);
754
755    if (he != NULL) {
756	return ((struct ar_hdr *)Hash_GetValue(he));
757    } else {
758	return NULL;
759    }
760
761badarch:
762    fclose(arch);
763    Hash_DeleteTable(&ar->members);
764    free(ar->fnametab);
765    free(ar);
766    return NULL;
767}
768
769#ifdef SVR4ARCHIVES
770/*-
771 *-----------------------------------------------------------------------
772 * ArchSVR4Entry --
773 *	Parse an SVR4 style entry that begins with a slash.
774 *	If it is "//", then load the table of filenames
775 *	If it is "/<offset>", then try to substitute the long file name
776 *	from offset of a table previously read.
777 *
778 * Results:
779 *	-1: Bad data in archive
780 *	 0: A table was loaded from the file
781 *	 1: Name was successfully substituted from table
782 *	 2: Name was not successfully substituted from table
783 *
784 * Side Effects:
785 *	If a table is read, the file pointer is moved to the next archive
786 *	member
787 *
788 *-----------------------------------------------------------------------
789 */
790static int
791ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
792{
793#define ARLONGNAMES1 "//"
794#define ARLONGNAMES2 "/ARFILENAMES"
795    size_t entry;
796    char *ptr, *eptr;
797
798    if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
799	strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
800
801	if (ar->fnametab != NULL) {
802	    if (DEBUG(ARCH)) {
803		fprintf(debug_file, "Attempted to redefine an SVR4 name table\n");
804	    }
805	    return -1;
806	}
807
808	/*
809	 * This is a table of archive names, so we build one for
810	 * ourselves
811	 */
812	ar->fnametab = bmake_malloc(size);
813	ar->fnamesize = size;
814
815	if (fread(ar->fnametab, size, 1, arch) != 1) {
816	    if (DEBUG(ARCH)) {
817		fprintf(debug_file, "Reading an SVR4 name table failed\n");
818	    }
819	    return -1;
820	}
821	eptr = ar->fnametab + size;
822	for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
823	    switch (*ptr) {
824	    case '/':
825		entry++;
826		*ptr = '\0';
827		break;
828
829	    case '\n':
830		break;
831
832	    default:
833		break;
834	    }
835	if (DEBUG(ARCH)) {
836	    fprintf(debug_file, "Found svr4 archive name table with %lu entries\n",
837	            (unsigned long)entry);
838	}
839	return 0;
840    }
841
842    if (name[1] == ' ' || name[1] == '\0')
843	return 2;
844
845    entry = (size_t)strtol(&name[1], &eptr, 0);
846    if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
847	if (DEBUG(ARCH)) {
848	    fprintf(debug_file, "Could not parse SVR4 name %s\n", name);
849	}
850	return 2;
851    }
852    if (entry >= ar->fnamesize) {
853	if (DEBUG(ARCH)) {
854	    fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n",
855		   name, (unsigned long)ar->fnamesize);
856	}
857	return 2;
858    }
859
860    if (DEBUG(ARCH)) {
861	fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
862    }
863
864    (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
865    name[MAXPATHLEN] = '\0';
866    return 1;
867}
868#endif
869
870
871/*-
872 *-----------------------------------------------------------------------
873 * ArchFindMember --
874 *	Locate a member of an archive, given the path of the archive and
875 *	the path of the desired member. If the archive is to be modified,
876 *	the mode should be "r+", if not, it should be "r".
877 *
878 * Input:
879 *	archive		Path to the archive
880 *	member		Name of member. If it is a path, only the last
881 *			component is used.
882 *	arhPtr		Pointer to header structure to be filled in
883 *	mode		The mode for opening the stream
884 *
885 * Results:
886 *	An FILE *, opened for reading and writing, positioned at the
887 *	start of the member's struct ar_hdr, or NULL if the member was
888 *	nonexistent. The current struct ar_hdr for member.
889 *
890 * Side Effects:
891 *	The passed struct ar_hdr structure is filled in.
892 *
893 *-----------------------------------------------------------------------
894 */
895static FILE *
896ArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr,
897    const char *mode)
898{
899    FILE *	  arch;	      /* Stream to archive */
900    int		  size;       /* Size of archive member */
901    char	  *cp;	      /* Useful character pointer */
902    char	  magic[SARMAG];
903    size_t	  len, tlen;
904
905    arch = fopen(archive, mode);
906    if (arch == NULL) {
907	return NULL;
908    }
909
910    /*
911     * We use the ARMAG string to make sure this is an archive we
912     * can handle...
913     */
914    if ((fread(magic, SARMAG, 1, arch) != 1) ||
915    	(strncmp(magic, ARMAG, SARMAG) != 0)) {
916	    fclose(arch);
917	    return NULL;
918    }
919
920    /*
921     * Because of space constraints and similar things, files are archived
922     * using their final path components, not the entire thing, so we need
923     * to point 'member' to the final component, if there is one, to make
924     * the comparisons easier...
925     */
926    cp = strrchr(member, '/');
927    if (cp != NULL) {
928	member = cp + 1;
929    }
930    len = tlen = strlen(member);
931    if (len > sizeof(arhPtr->AR_NAME)) {
932	tlen = sizeof(arhPtr->AR_NAME);
933    }
934
935    while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
936	if (strncmp(arhPtr->AR_FMAG, ARFMAG, sizeof(arhPtr->AR_FMAG) ) != 0) {
937	     /*
938	      * The header is bogus, so the archive is bad
939	      * and there's no way we can recover...
940	      */
941	     fclose(arch);
942	     return NULL;
943	} else if (strncmp(member, arhPtr->AR_NAME, tlen) == 0) {
944	    /*
945	     * If the member's name doesn't take up the entire 'name' field,
946	     * we have to be careful of matching prefixes. Names are space-
947	     * padded to the right, so if the character in 'name' at the end
948	     * of the matched string is anything but a space, this isn't the
949	     * member we sought.
950	     */
951	    if (tlen != sizeof(arhPtr->AR_NAME) && arhPtr->AR_NAME[tlen] != ' '){
952		goto skip;
953	    } else {
954		/*
955		 * To make life easier, we reposition the file at the start
956		 * of the header we just read before we return the stream.
957		 * In a more general situation, it might be better to leave
958		 * the file at the actual member, rather than its header, but
959		 * not here...
960		 */
961		if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) != 0) {
962		    fclose(arch);
963		    return NULL;
964		}
965		return (arch);
966	    }
967	} else
968#ifdef AR_EFMT1
969		/*
970		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
971		 * first <namelen> bytes of the file
972		 */
973	    if (strncmp(arhPtr->AR_NAME, AR_EFMT1,
974					sizeof(AR_EFMT1) - 1) == 0 &&
975		isdigit((unsigned char)arhPtr->AR_NAME[sizeof(AR_EFMT1) - 1])) {
976
977		unsigned int elen = atoi(&arhPtr->AR_NAME[sizeof(AR_EFMT1)-1]);
978		char ename[MAXPATHLEN + 1];
979
980		if (elen > MAXPATHLEN) {
981			fclose(arch);
982			return NULL;
983		}
984		if (fread(ename, elen, 1, arch) != 1) {
985			fclose(arch);
986			return NULL;
987		}
988		ename[elen] = '\0';
989		if (DEBUG(ARCH) || DEBUG(MAKE)) {
990		    fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename);
991		}
992		if (strncmp(ename, member, len) == 0) {
993			/* Found as extended name */
994			if (fseek(arch, -sizeof(struct ar_hdr) - elen,
995				SEEK_CUR) != 0) {
996			    fclose(arch);
997			    return NULL;
998			}
999			return (arch);
1000		}
1001		if (fseek(arch, -elen, SEEK_CUR) != 0) {
1002		    fclose(arch);
1003		    return NULL;
1004		}
1005		goto skip;
1006	} else
1007#endif
1008	{
1009skip:
1010	    /*
1011	     * This isn't the member we're after, so we need to advance the
1012	     * stream's pointer to the start of the next header. Files are
1013	     * padded with newlines to an even-byte boundary, so we need to
1014	     * extract the size of the file from the 'size' field of the
1015	     * header and round it up during the seek.
1016	     */
1017	    arhPtr->AR_SIZE[sizeof(arhPtr->AR_SIZE)-1] = '\0';
1018	    size = (int)strtol(arhPtr->AR_SIZE, NULL, 10);
1019	    if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
1020		fclose(arch);
1021		return NULL;
1022	    }
1023	}
1024    }
1025
1026    /*
1027     * We've looked everywhere, but the member is not to be found. Close the
1028     * archive and return NULL -- an error.
1029     */
1030    fclose(arch);
1031    return NULL;
1032}
1033
1034/*-
1035 *-----------------------------------------------------------------------
1036 * Arch_Touch --
1037 *	Touch a member of an archive.
1038 *
1039 * Input:
1040 *	gn		Node of member to touch
1041 *
1042 * Results:
1043 *	The 'time' field of the member's header is updated.
1044 *
1045 * Side Effects:
1046 *	The modification time of the entire archive is also changed.
1047 *	For a library, this could necessitate the re-ranlib'ing of the
1048 *	whole thing.
1049 *
1050 *-----------------------------------------------------------------------
1051 */
1052void
1053Arch_Touch(GNode *gn)
1054{
1055    FILE *	  arch;	  /* Stream open to archive, positioned properly */
1056    struct ar_hdr arh;	  /* Current header describing member */
1057    char *p1, *p2;
1058
1059    arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
1060			  Var_Value(MEMBER, gn, &p2),
1061			  &arh, "r+");
1062
1063    free(p1);
1064    free(p2);
1065
1066    snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now);
1067
1068    if (arch != NULL) {
1069	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
1070	fclose(arch);
1071    }
1072}
1073
1074/*-
1075 *-----------------------------------------------------------------------
1076 * Arch_TouchLib --
1077 *	Given a node which represents a library, touch the thing, making
1078 *	sure that the table of contents also is touched.
1079 *
1080 * Input:
1081 *	gn		The node of the library to touch
1082 *
1083 * Results:
1084 *	None.
1085 *
1086 * Side Effects:
1087 *	Both the modification time of the library and of the RANLIBMAG
1088 *	member are set to 'now'.
1089 *
1090 *-----------------------------------------------------------------------
1091 */
1092void
1093#if !defined(RANLIBMAG)
1094Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
1095#else
1096Arch_TouchLib(GNode *gn)
1097#endif
1098{
1099#ifdef RANLIBMAG
1100    FILE *	    arch;	/* Stream open to archive */
1101    struct ar_hdr   arh;      	/* Header describing table of contents */
1102    struct utimbuf  times;	/* Times for utime() call */
1103
1104    arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+");
1105    snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now);
1106
1107    if (arch != NULL) {
1108	(void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
1109	fclose(arch);
1110
1111	times.actime = times.modtime = now;
1112	utime(gn->path, &times);
1113    }
1114#endif
1115}
1116
1117/*-
1118 *-----------------------------------------------------------------------
1119 * Arch_MTime --
1120 *	Return the modification time of a member of an archive.
1121 *
1122 * Input:
1123 *	gn		Node describing archive member
1124 *
1125 * Results:
1126 *	The modification time(seconds).
1127 *
1128 * Side Effects:
1129 *	The mtime field of the given node is filled in with the value
1130 *	returned by the function.
1131 *
1132 *-----------------------------------------------------------------------
1133 */
1134time_t
1135Arch_MTime(GNode *gn)
1136{
1137    struct ar_hdr *arhPtr;    /* Header of desired member */
1138    time_t	  modTime;    /* Modification time as an integer */
1139    char *p1, *p2;
1140
1141    arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
1142			     Var_Value(MEMBER, gn, &p2),
1143			     TRUE);
1144
1145    free(p1);
1146    free(p2);
1147
1148    if (arhPtr != NULL) {
1149	modTime = (time_t)strtol(arhPtr->AR_DATE, NULL, 10);
1150    } else {
1151	modTime = 0;
1152    }
1153
1154    gn->mtime = modTime;
1155    return (modTime);
1156}
1157
1158/*-
1159 *-----------------------------------------------------------------------
1160 * Arch_MemMTime --
1161 *	Given a non-existent archive member's node, get its modification
1162 *	time from its archived form, if it exists.
1163 *
1164 * Results:
1165 *	The modification time.
1166 *
1167 * Side Effects:
1168 *	The mtime field is filled in.
1169 *
1170 *-----------------------------------------------------------------------
1171 */
1172time_t
1173Arch_MemMTime(GNode *gn)
1174{
1175    LstNode 	  ln;
1176    GNode   	  *pgn;
1177    char    	  *nameStart,
1178		  *nameEnd;
1179
1180    if (Lst_Open(gn->parents) != SUCCESS) {
1181	gn->mtime = 0;
1182	return (0);
1183    }
1184    while ((ln = Lst_Next(gn->parents)) != NULL) {
1185	pgn = (GNode *)Lst_Datum(ln);
1186
1187	if (pgn->type & OP_ARCHV) {
1188	    /*
1189	     * If the parent is an archive specification and is being made
1190	     * and its member's name matches the name of the node we were
1191	     * given, record the modification time of the parent in the
1192	     * child. We keep searching its parents in case some other
1193	     * parent requires this child to exist...
1194	     */
1195	    nameStart = strchr(pgn->name, '(') + 1;
1196	    nameEnd = strchr(nameStart, ')');
1197
1198	    if ((pgn->flags & REMAKE) &&
1199		strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
1200				     gn->mtime = Arch_MTime(pgn);
1201	    }
1202	} else if (pgn->flags & REMAKE) {
1203	    /*
1204	     * Something which isn't a library depends on the existence of
1205	     * this target, so it needs to exist.
1206	     */
1207	    gn->mtime = 0;
1208	    break;
1209	}
1210    }
1211
1212    Lst_Close(gn->parents);
1213
1214    return (gn->mtime);
1215}
1216
1217/*-
1218 *-----------------------------------------------------------------------
1219 * Arch_FindLib --
1220 *	Search for a library along the given search path.
1221 *
1222 * Input:
1223 *	gn		Node of library to find
1224 *	path		Search path
1225 *
1226 * Results:
1227 *	None.
1228 *
1229 * Side Effects:
1230 *	The node's 'path' field is set to the found path (including the
1231 *	actual file name, not -l...). If the system can handle the -L
1232 *	flag when linking (or we cannot find the library), we assume that
1233 *	the user has placed the .LIBRARIES variable in the final linking
1234 *	command (or the linker will know where to find it) and set the
1235 *	TARGET variable for this node to be the node's name. Otherwise,
1236 *	we set the TARGET variable to be the full path of the library,
1237 *	as returned by Dir_FindFile.
1238 *
1239 *-----------------------------------------------------------------------
1240 */
1241void
1242Arch_FindLib(GNode *gn, Lst path)
1243{
1244    char	    *libName;   /* file name for archive */
1245    size_t	     sz = strlen(gn->name) + 6 - 2;
1246
1247    libName = bmake_malloc(sz);
1248    snprintf(libName, sz, "lib%s.a", &gn->name[2]);
1249
1250    gn->path = Dir_FindFile(libName, path);
1251
1252    free(libName);
1253
1254#ifdef LIBRARIES
1255    Var_Set(TARGET, gn->name, gn, 0);
1256#else
1257    Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn, 0);
1258#endif /* LIBRARIES */
1259}
1260
1261/*-
1262 *-----------------------------------------------------------------------
1263 * Arch_LibOODate --
1264 *	Decide if a node with the OP_LIB attribute is out-of-date. Called
1265 *	from Make_OODate to make its life easier.
1266 *
1267 *	There are several ways for a library to be out-of-date that are
1268 *	not available to ordinary files. In addition, there are ways
1269 *	that are open to regular files that are not available to
1270 *	libraries. A library that is only used as a source is never
1271 *	considered out-of-date by itself. This does not preclude the
1272 *	library's modification time from making its parent be out-of-date.
1273 *	A library will be considered out-of-date for any of these reasons,
1274 *	given that it is a target on a dependency line somewhere:
1275 *	    Its modification time is less than that of one of its
1276 *	    	  sources (gn->mtime < gn->cmgn->mtime).
1277 *	    Its modification time is greater than the time at which the
1278 *	    	  make began (i.e. it's been modified in the course
1279 *	    	  of the make, probably by archiving).
1280 *	    The modification time of one of its sources is greater than
1281 *		  the one of its RANLIBMAG member (i.e. its table of contents
1282 *	    	  is out-of-date). We don't compare of the archive time
1283 *		  vs. TOC time because they can be too close. In my
1284 *		  opinion we should not bother with the TOC at all since
1285 *		  this is used by 'ar' rules that affect the data contents
1286 *		  of the archive, not by ranlib rules, which affect the
1287 *		  TOC.
1288 *
1289 * Input:
1290 *	gn		The library's graph node
1291 *
1292 * Results:
1293 *	TRUE if the library is out-of-date. FALSE otherwise.
1294 *
1295 * Side Effects:
1296 *	The library will be hashed if it hasn't been already.
1297 *
1298 *-----------------------------------------------------------------------
1299 */
1300Boolean
1301Arch_LibOODate(GNode *gn)
1302{
1303    Boolean 	  oodate;
1304
1305    if (gn->type & OP_PHONY) {
1306	oodate = TRUE;
1307    } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
1308	oodate = FALSE;
1309    } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
1310	       (gn->mtime > now) ||
1311	       (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
1312	oodate = TRUE;
1313    } else {
1314#ifdef RANLIBMAG
1315	struct ar_hdr  	*arhPtr;    /* Header for __.SYMDEF */
1316	int 	  	modTimeTOC; /* The table-of-contents's mod time */
1317
1318	arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE);
1319
1320	if (arhPtr != NULL) {
1321	    modTimeTOC = (int)strtol(arhPtr->AR_DATE, NULL, 10);
1322
1323	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
1324		fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1325	    }
1326	    oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
1327	} else {
1328	    /*
1329	     * A library w/o a table of contents is out-of-date
1330	     */
1331	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
1332		fprintf(debug_file, "No t.o.c....");
1333	    }
1334	    oodate = TRUE;
1335	}
1336#else
1337	oodate = FALSE;
1338#endif
1339    }
1340    return (oodate);
1341}
1342
1343/*-
1344 *-----------------------------------------------------------------------
1345 * Arch_Init --
1346 *	Initialize things for this module.
1347 *
1348 * Results:
1349 *	None.
1350 *
1351 * Side Effects:
1352 *	The 'archives' list is initialized.
1353 *
1354 *-----------------------------------------------------------------------
1355 */
1356void
1357Arch_Init(void)
1358{
1359    archives = Lst_Init(FALSE);
1360}
1361
1362
1363
1364/*-
1365 *-----------------------------------------------------------------------
1366 * Arch_End --
1367 *	Cleanup things for this module.
1368 *
1369 * Results:
1370 *	None.
1371 *
1372 * Side Effects:
1373 *	The 'archives' list is freed
1374 *
1375 *-----------------------------------------------------------------------
1376 */
1377void
1378Arch_End(void)
1379{
1380#ifdef CLEANUP
1381    Lst_Destroy(archives, ArchFree);
1382#endif
1383}
1384
1385/*-
1386 *-----------------------------------------------------------------------
1387 * Arch_IsLib --
1388 *	Check if the node is a library
1389 *
1390 * Results:
1391 *	True or False.
1392 *
1393 * Side Effects:
1394 *	None.
1395 *
1396 *-----------------------------------------------------------------------
1397 */
1398int
1399Arch_IsLib(GNode *gn)
1400{
1401    static const char armag[] = "!<arch>\n";
1402    char buf[sizeof(armag)-1];
1403    int fd;
1404
1405    if ((fd = open(gn->path, O_RDONLY)) == -1)
1406	return FALSE;
1407
1408    if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
1409	(void)close(fd);
1410	return FALSE;
1411    }
1412
1413    (void)close(fd);
1414
1415    return memcmp(buf, armag, sizeof(buf)) == 0;
1416}
1417