xref: /netbsd-src/usr.bin/make/arch.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*
2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3  * Copyright (c) 1988, 1989 by Adam de Boor
4  * Copyright (c) 1989 by Berkeley Softworks
5  * 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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 static char sccsid[] = "@(#)arch.c	5.7 (Berkeley) 12/28/90";
41 #endif /* not lint */
42 
43 /*-
44  * arch.c --
45  *	Functions to manipulate libraries, archives and their members.
46  *
47  *	Once again, cacheing/hashing comes into play in the manipulation
48  * of archives. The first time an archive is referenced, all of its members'
49  * headers are read and hashed and the archive closed again. All hashed
50  * archives are kept on a list which is searched each time an archive member
51  * is referenced.
52  *
53  * The interface to this module is:
54  *	Arch_ParseArchive   	Given an archive specification, return a list
55  *	    	  	    	of GNode's, one for each member in the spec.
56  *	    	  	    	FAILURE is returned if the specification is
57  *	    	  	    	invalid for some reason.
58  *
59  *	Arch_Touch	    	Alter the modification time of the archive
60  *	    	  	    	member described by the given node to be
61  *	    	  	    	the current time.
62  *
63  *	Arch_TouchLib	    	Update the modification time of the library
64  *	    	  	    	described by the given node. This is special
65  *	    	  	    	because it also updates the modification time
66  *	    	  	    	of the library's table of contents.
67  *
68  *	Arch_MTime	    	Find the modification time of a member of
69  *	    	  	    	an archive *in the archive*. The time is also
70  *	    	  	    	placed in the member's GNode. Returns the
71  *	    	  	    	modification time.
72  *
73  *	Arch_MemTime	    	Find the modification time of a member of
74  *	    	  	    	an archive. Called when the member doesn't
75  *	    	  	    	already exist. Looks in the archive for the
76  *	    	  	    	modification time. Returns the modification
77  *	    	  	    	time.
78  *
79  *	Arch_FindLib	    	Search for a library along a path. The
80  *	    	  	    	library name in the GNode should be in
81  *	    	  	    	-l<name> format.
82  *
83  *	Arch_LibOODate	    	Special function to decide if a library node
84  *	    	  	    	is out-of-date.
85  *
86  *	Arch_Init 	    	Initialize this module.
87  */
88 
89 #include    <sys/types.h>
90 #include    <sys/stat.h>
91 #include    <sys/time.h>
92 #include    <ctype.h>
93 #include    <ar.h>
94 #include <ranlib.h>
95 #include    <stdio.h>
96 #include    "make.h"
97 #include    "hash.h"
98 
99 #ifndef RANLIBMAG
100 #define RANLIBMAG  "__.SYMDEF"
101 #endif
102 
103 static Lst	  archives;   /* Lst of archives we've already examined */
104 
105 typedef struct Arch {
106     char	  *name;      /* Name of archive */
107     Hash_Table	  members;    /* All the members of the archive described
108 			       * by <name, struct ar_hdr *> key/value pairs */
109 } Arch;
110 
111 static FILE *ArchFindMember();
112 
113 /*-
114  *-----------------------------------------------------------------------
115  * Arch_ParseArchive --
116  *	Parse the archive specification in the given line and find/create
117  *	the nodes for the specified archive members, placing their nodes
118  *	on the given list.
119  *
120  * Results:
121  *	SUCCESS if it was a valid specification. The linePtr is updated
122  *	to point to the first non-space after the archive spec. The
123  *	nodes for the members are placed on the given list.
124  *
125  * Side Effects:
126  *	Some nodes may be created. The given list is extended.
127  *
128  *-----------------------------------------------------------------------
129  */
130 ReturnStatus
131 Arch_ParseArchive (linePtr, nodeLst, ctxt)
132     char	    **linePtr;      /* Pointer to start of specification */
133     Lst	    	    nodeLst;   	    /* Lst on which to place the nodes */
134     GNode   	    *ctxt;  	    /* Context in which to expand variables */
135 {
136     register char   *cp;	    /* Pointer into line */
137     GNode	    *gn;     	    /* New node */
138     char	    *libName;  	    /* Library-part of specification */
139     char	    *memName;  	    /* Member-part of specification */
140     char	    nameBuf[BSIZE]; /* temporary place for node name */
141     char	    saveChar;  	    /* Ending delimiter of member-name */
142     Boolean 	    subLibName;	    /* TRUE if libName should have/had
143 				     * variable substitution performed on it */
144 
145     libName = *linePtr;
146 
147     subLibName = FALSE;
148 
149     for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
150 	if (*cp == '$') {
151 	    /*
152 	     * Variable spec, so call the Var module to parse the puppy
153 	     * so we can safely advance beyond it...
154 	     */
155 	    int 	length;
156 	    Boolean	freeIt;
157 	    char	*result;
158 
159 	    result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
160 	    if (result == var_Error) {
161 		return(FAILURE);
162 	    } else {
163 		subLibName = TRUE;
164 	    }
165 
166 	    if (freeIt) {
167 		free(result);
168 	    }
169 	    cp += length-1;
170 	}
171     }
172 
173     *cp++ = '\0';
174     if (subLibName) {
175 	libName = Var_Subst(libName, ctxt, TRUE);
176     }
177 
178 
179     while (1) {
180 	/*
181 	 * First skip to the start of the member's name, mark that
182 	 * place and skip to the end of it (either white-space or
183 	 * a close paren).
184 	 */
185 	Boolean	doSubst = FALSE; /* TRUE if need to substitute in memName */
186 
187 	while (*cp != '\0' && *cp != ')' && isspace (*cp)) {
188 	    cp++;
189 	}
190 	memName = cp;
191 	while (*cp != '\0' && *cp != ')' && !isspace (*cp)) {
192 	    if (*cp == '$') {
193 		/*
194 		 * Variable spec, so call the Var module to parse the puppy
195 		 * so we can safely advance beyond it...
196 		 */
197 		int 	length;
198 		Boolean	freeIt;
199 		char	*result;
200 
201 		result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
202 		if (result == var_Error) {
203 		    return(FAILURE);
204 		} else {
205 		    doSubst = TRUE;
206 		}
207 
208 		if (freeIt) {
209 		    free(result);
210 		}
211 		cp += length;
212 	    } else {
213 		cp++;
214 	    }
215 	}
216 
217 	/*
218 	 * If the specification ends without a closing parenthesis,
219 	 * chances are there's something wrong (like a missing backslash),
220 	 * so it's better to return failure than allow such things to happen
221 	 */
222 	if (*cp == '\0') {
223 	    printf("No closing parenthesis in archive specification\n");
224 	    return (FAILURE);
225 	}
226 
227 	/*
228 	 * If we didn't move anywhere, we must be done
229 	 */
230 	if (cp == memName) {
231 	    break;
232 	}
233 
234 	saveChar = *cp;
235 	*cp = '\0';
236 
237 	/*
238 	 * XXX: This should be taken care of intelligently by
239 	 * SuffExpandChildren, both for the archive and the member portions.
240 	 */
241 	/*
242 	 * If member contains variables, try and substitute for them.
243 	 * This will slow down archive specs with dynamic sources, of course,
244 	 * since we'll be (non-)substituting them three times, but them's
245 	 * the breaks -- we need to do this since SuffExpandChildren calls
246 	 * us, otherwise we could assume the thing would be taken care of
247 	 * later.
248 	 */
249 	if (doSubst) {
250 	    char    *buf;
251 	    char    *sacrifice;
252 	    char    *oldMemName = memName;
253 
254 	    memName = Var_Subst(memName, ctxt, TRUE);
255 
256 	    /*
257 	     * Now form an archive spec and recurse to deal with nested
258 	     * variables and multi-word variable values.... The results
259 	     * are just placed at the end of the nodeLst we're returning.
260 	     */
261 	    buf = sacrifice = emalloc(strlen(memName)+strlen(libName)+3);
262 
263 	    sprintf(buf, "%s(%s)", libName, memName);
264 
265 	    if (index(memName, '$') && strcmp(memName, oldMemName) == 0) {
266 		/*
267 		 * Must contain dynamic sources, so we can't deal with it now.
268 		 * Just create an ARCHV node for the thing and let
269 		 * SuffExpandChildren handle it...
270 		 */
271 		gn = Targ_FindNode(buf, TARG_CREATE);
272 
273 		if (gn == NILGNODE) {
274 		    free(buf);
275 		    return(FAILURE);
276 		} else {
277 		    gn->type |= OP_ARCHV;
278 		    (void)Lst_AtEnd(nodeLst, (ClientData)gn);
279 		}
280 	    } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
281 		/*
282 		 * Error in nested call -- free buffer and return FAILURE
283 		 * ourselves.
284 		 */
285 		free(buf);
286 		return(FAILURE);
287 	    }
288 	    /*
289 	     * Free buffer and continue with our work.
290 	     */
291 	    free(buf);
292 	} else if (Dir_HasWildcards(memName)) {
293 	    Lst	  members = Lst_Init(FALSE);
294 	    char  *member;
295 
296 	    Dir_Expand(memName, dirSearchPath, members);
297 	    while (!Lst_IsEmpty(members)) {
298 		member = (char *)Lst_DeQueue(members);
299 
300 		sprintf(nameBuf, "%s(%s)", libName, member);
301 		free(member);
302 		gn = Targ_FindNode (nameBuf, TARG_CREATE);
303 		if (gn == NILGNODE) {
304 		    return (FAILURE);
305 		} else {
306 		    /*
307 		     * We've found the node, but have to make sure the rest of
308 		     * the world knows it's an archive member, without having
309 		     * to constantly check for parentheses, so we type the
310 		     * thing with the OP_ARCHV bit before we place it on the
311 		     * end of the provided list.
312 		     */
313 		    gn->type |= OP_ARCHV;
314 		    (void) Lst_AtEnd (nodeLst, (ClientData)gn);
315 		}
316 	    }
317 	    Lst_Destroy(members, NOFREE);
318 	} else {
319 	    sprintf(nameBuf, "%s(%s)", libName, memName);
320 	    gn = Targ_FindNode (nameBuf, TARG_CREATE);
321 	    if (gn == NILGNODE) {
322 		return (FAILURE);
323 	    } else {
324 		/*
325 		 * We've found the node, but have to make sure the rest of the
326 		 * world knows it's an archive member, without having to
327 		 * constantly check for parentheses, so we type the thing with
328 		 * the OP_ARCHV bit before we place it on the end of the
329 		 * provided list.
330 		 */
331 		gn->type |= OP_ARCHV;
332 		(void) Lst_AtEnd (nodeLst, (ClientData)gn);
333 	    }
334 	}
335 	if (doSubst) {
336 	    free(memName);
337 	}
338 
339 	*cp = saveChar;
340     }
341 
342     /*
343      * If substituted libName, free it now, since we need it no longer.
344      */
345     if (subLibName) {
346 	free(libName);
347     }
348 
349     /*
350      * We promised the pointer would be set up at the next non-space, so
351      * we must advance cp there before setting *linePtr... (note that on
352      * entrance to the loop, cp is guaranteed to point at a ')')
353      */
354     do {
355 	cp++;
356     } while (*cp != '\0' && isspace (*cp));
357 
358     *linePtr = cp;
359     return (SUCCESS);
360 }
361 
362 /*-
363  *-----------------------------------------------------------------------
364  * ArchFindArchive --
365  *	See if the given archive is the one we are looking for. Called
366  *	From ArchStatMember and ArchFindMember via Lst_Find.
367  *
368  * Results:
369  *	0 if it is, non-zero if it isn't.
370  *
371  * Side Effects:
372  *	None.
373  *
374  *-----------------------------------------------------------------------
375  */
376 static int
377 ArchFindArchive (ar, archName)
378     Arch	  *ar;	      	  /* Current list element */
379     char	  *archName;  	  /* Name we want */
380 {
381     return (strcmp (archName, ar->name));
382 }
383 
384 /*-
385  *-----------------------------------------------------------------------
386  * ArchStatMember --
387  *	Locate a member of an archive, given the path of the archive and
388  *	the path of the desired member.
389  *
390  * Results:
391  *	A pointer to the current struct ar_hdr structure for the member. Note
392  *	That no position is returned, so this is not useful for touching
393  *	archive members. This is mostly because we have no assurances that
394  *	The archive will remain constant after we read all the headers, so
395  *	there's not much point in remembering the position...
396  *
397  * Side Effects:
398  *
399  *-----------------------------------------------------------------------
400  */
401 static struct ar_hdr *
402 ArchStatMember (archive, member, hash)
403     char	  *archive;   /* Path to the archive */
404     char	  *member;    /* Name of member. If it is a path, only the
405 			       * last component is used. */
406     Boolean	  hash;	      /* TRUE if archive should be hashed if not
407     			       * already so. */
408 {
409 #define AR_MAX_NAME_LEN	    (sizeof(arh.ar_name)-1)
410     FILE *	  arch;	      /* Stream to archive */
411     int		  size;       /* Size of archive member */
412     char	  *cp;	      /* Useful character pointer */
413     char	  magic[SARMAG];
414     int		  len;
415     LstNode	  ln;	      /* Lst member containing archive descriptor */
416     Arch	  *ar;	      /* Archive descriptor */
417     Hash_Entry	  *he;	      /* Entry containing member's description */
418     struct ar_hdr arh;        /* archive-member header for reading archive */
419     char	  memName[AR_MAX_NAME_LEN+1];
420     	    	    	    /* Current member name while hashing. The name is
421 			     * truncated to AR_MAX_NAME_LEN bytes, but we need
422 			     * room for the null byte... */
423     char    	  copy[AR_MAX_NAME_LEN+1];
424     	    	    	    /* Holds copy of last path element from member, if
425 			     * it has to be truncated, so we don't have to
426 			     * figure it out again once the table is hashed. */
427 
428     /*
429      * Because of space constraints and similar things, files are archived
430      * using their final path components, not the entire thing, so we need
431      * to point 'member' to the final component, if there is one, to make
432      * the comparisons easier...
433      */
434     cp = rindex (member, '/');
435     if (cp != (char *) NULL) {
436 	member = cp + 1;
437     }
438     len = strlen (member);
439     if (len > AR_MAX_NAME_LEN) {
440 	len = AR_MAX_NAME_LEN;
441 	strncpy(copy, member, AR_MAX_NAME_LEN);
442 	copy[AR_MAX_NAME_LEN] = '\0';
443 	member = copy;
444     }
445 
446     ln = Lst_Find (archives, (ClientData) archive, ArchFindArchive);
447     if (ln != NILLNODE) {
448 	ar = (Arch *) Lst_Datum (ln);
449 
450 	he = Hash_FindEntry (&ar->members, member);
451 
452 	if (he != (Hash_Entry *) NULL) {
453 	    return ((struct ar_hdr *) Hash_GetValue (he));
454 	} else {
455 	    return ((struct ar_hdr *) NULL);
456 	}
457     }
458 
459     if (!hash) {
460 	/*
461 	 * Caller doesn't want the thing hashed, just use ArchFindMember
462 	 * to read the header for the member out and close down the stream
463 	 * again. Since the archive is not to be hashed, we assume there's
464 	 * no need to allocate extra room for the header we're returning,
465 	 * so just declare it static.
466 	 */
467 	 static struct ar_hdr	sarh;
468 
469 	 arch = ArchFindMember(archive, member, &sarh, "r");
470 
471 	 if (arch == (FILE *)NULL) {
472 	    return ((struct ar_hdr *)NULL);
473 	} else {
474 	    fclose(arch);
475 	    return (&sarh);
476 	}
477     }
478 
479     /*
480      * We don't have this archive on the list yet, so we want to find out
481      * everything that's in it and cache it so we can get at it quickly.
482      */
483     arch = fopen (archive, "r");
484     if (arch == (FILE *) NULL) {
485 	return ((struct ar_hdr *) NULL);
486     }
487 
488     /*
489      * We use the ARMAG string to make sure this is an archive we
490      * can handle...
491      */
492     if ((fread (magic, SARMAG, 1, arch) != 1) ||
493     	(strncmp (magic, ARMAG, SARMAG) != 0)) {
494 	    fclose (arch);
495 	    return ((struct ar_hdr *) NULL);
496     }
497 
498     ar = (Arch *)emalloc (sizeof (Arch));
499     ar->name = strdup (archive);
500     Hash_InitTable (&ar->members, -1);
501     memName[AR_MAX_NAME_LEN] = '\0';
502 
503     while (fread ((char *)&arh, sizeof (struct ar_hdr), 1, arch) == 1) {
504 	if (strncmp ( arh.ar_fmag, ARFMAG, sizeof (arh.ar_fmag)) != 0) {
505 				 /*
506 				  * The header is bogus, so the archive is bad
507 				  * and there's no way we can recover...
508 				  */
509 				 fclose (arch);
510 				 Hash_DeleteTable (&ar->members);
511 				 free ((Address)ar);
512 				 return ((struct ar_hdr *) NULL);
513 	} else {
514 	    (void) strncpy (memName, arh.ar_name, sizeof(arh.ar_name));
515 	    for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
516 		continue;
517 	    }
518 	    cp[1] = '\0';
519 
520 	    he = Hash_CreateEntry (&ar->members, strdup (memName),
521 				   (Boolean *)NULL);
522 	    Hash_SetValue (he, (ClientData)emalloc (sizeof (struct ar_hdr)));
523 	    bcopy ((Address)&arh, (Address)Hash_GetValue (he),
524 		sizeof (struct ar_hdr));
525 	}
526 	/*
527 	 * We need to advance the stream's pointer to the start of the
528 	 * next header. Files are padded with newlines to an even-byte
529 	 * boundary, so we need to extract the size of the file from the
530 	 * 'size' field of the header and round it up during the seek.
531 	 */
532 	arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
533 	(void) sscanf (arh.ar_size, "%10d", &size);
534 	fseek (arch, (size + 1) & ~1, 1);
535     }
536 
537     fclose (arch);
538 
539     (void) Lst_AtEnd (archives, (ClientData) ar);
540 
541     /*
542      * Now that the archive has been read and cached, we can look into
543      * the hash table to find the desired member's header.
544      */
545     he = Hash_FindEntry (&ar->members, member);
546 
547     if (he != (Hash_Entry *) NULL) {
548 	return ((struct ar_hdr *) Hash_GetValue (he));
549     } else {
550 	return ((struct ar_hdr *) NULL);
551     }
552 }
553 
554 /*-
555  *-----------------------------------------------------------------------
556  * ArchFindMember --
557  *	Locate a member of an archive, given the path of the archive and
558  *	the path of the desired member. If the archive is to be modified,
559  *	the mode should be "r+", if not, it should be "r".
560  *
561  * Results:
562  *	An FILE *, opened for reading and writing, positioned at the
563  *	start of the member's struct ar_hdr, or NULL if the member was
564  *	nonexistent. The current struct ar_hdr for member.
565  *
566  * Side Effects:
567  *	The passed struct ar_hdr structure is filled in.
568  *
569  *-----------------------------------------------------------------------
570  */
571 static FILE *
572 ArchFindMember (archive, member, arhPtr, mode)
573     char	  *archive;   /* Path to the archive */
574     char	  *member;    /* Name of member. If it is a path, only the
575 			       * last component is used. */
576     struct ar_hdr *arhPtr;    /* Pointer to header structure to be filled in */
577     char	  *mode;      /* The mode for opening the stream */
578 {
579     FILE *	  arch;	      /* Stream to archive */
580     int		  size;       /* Size of archive member */
581     char	  *cp;	      /* Useful character pointer */
582     char	  magic[SARMAG];
583     int		  len;
584 
585     arch = fopen (archive, mode);
586     if (arch == (FILE *) NULL) {
587 	return ((FILE *) NULL);
588     }
589 
590     /*
591      * We use the ARMAG string to make sure this is an archive we
592      * can handle...
593      */
594     if ((fread (magic, SARMAG, 1, arch) != 1) ||
595     	(strncmp (magic, ARMAG, SARMAG) != 0)) {
596 	    fclose (arch);
597 	    return ((FILE *) NULL);
598     }
599 
600     /*
601      * Because of space constraints and similar things, files are archived
602      * using their final path components, not the entire thing, so we need
603      * to point 'member' to the final component, if there is one, to make
604      * the comparisons easier...
605      */
606     cp = rindex (member, '/');
607     if (cp != (char *) NULL) {
608 	member = cp + 1;
609     }
610     len = strlen (member);
611     if (len > sizeof (arhPtr->ar_name)) {
612 	len = sizeof (arhPtr->ar_name);
613     }
614 
615     while (fread ((char *)arhPtr, sizeof (struct ar_hdr), 1, arch) == 1) {
616 	if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof (arhPtr->ar_fmag) ) != 0) {
617 	     /*
618 	      * The header is bogus, so the archive is bad
619 	      * and there's no way we can recover...
620 	      */
621 	     fclose (arch);
622 	     return ((FILE *) NULL);
623 	} else if (strncmp (member, arhPtr->ar_name, len) == 0) {
624 	    /*
625 	     * If the member's name doesn't take up the entire 'name' field,
626 	     * we have to be careful of matching prefixes. Names are space-
627 	     * padded to the right, so if the character in 'name' at the end
628 	     * of the matched string is anything but a space, this isn't the
629 	     * member we sought.
630 	     */
631 	    if (len != sizeof(arhPtr->ar_name) && arhPtr->ar_name[len] != ' '){
632 		continue;
633 	    } else {
634 		/*
635 		 * To make life easier, we reposition the file at the start
636 		 * of the header we just read before we return the stream.
637 		 * In a more general situation, it might be better to leave
638 		 * the file at the actual member, rather than its header, but
639 		 * not here...
640 		 */
641 		fseek (arch, -sizeof(struct ar_hdr), 1);
642 		return (arch);
643 	    }
644 	} else {
645 	    /*
646 	     * This isn't the member we're after, so we need to advance the
647 	     * stream's pointer to the start of the next header. Files are
648 	     * padded with newlines to an even-byte boundary, so we need to
649 	     * extract the size of the file from the 'size' field of the
650 	     * header and round it up during the seek.
651 	     */
652 	    arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
653 	    (void)sscanf (arhPtr->ar_size, "%10d", &size);
654 	    fseek (arch, (size + 1) & ~1, 1);
655 	}
656     }
657 
658     /*
659      * We've looked everywhere, but the member is not to be found. Close the
660      * archive and return NULL -- an error.
661      */
662     fclose (arch);
663     return ((FILE *) NULL);
664 }
665 
666 /*-
667  *-----------------------------------------------------------------------
668  * Arch_Touch --
669  *	Touch a member of an archive.
670  *
671  * Results:
672  *	The 'time' field of the member's header is updated.
673  *
674  * Side Effects:
675  *	The modification time of the entire archive is also changed.
676  *	For a library, this could necessitate the re-ranlib'ing of the
677  *	whole thing.
678  *
679  *-----------------------------------------------------------------------
680  */
681 void
682 Arch_Touch (gn)
683     GNode	  *gn;	  /* Node of member to touch */
684 {
685     FILE *	  arch;	  /* Stream open to archive, positioned properly */
686     struct ar_hdr arh;	  /* Current header describing member */
687 
688     arch = ArchFindMember(Var_Value (ARCHIVE, gn),
689 			  Var_Value (TARGET, gn),
690 			  &arh, "r+");
691     sprintf(arh.ar_date, "%-12d", now);
692 
693     if (arch != (FILE *) NULL) {
694 	(void)fwrite ((char *)&arh, sizeof (struct ar_hdr), 1, arch);
695 	fclose (arch);
696     }
697 }
698 
699 /*-
700  *-----------------------------------------------------------------------
701  * Arch_TouchLib --
702  *	Given a node which represents a library, touch the thing, making
703  *	sure that the table of contents also is touched.
704  *
705  * Results:
706  *	None.
707  *
708  * Side Effects:
709  *	Both the modification time of the library and of the RANLIBMAG
710  *	member are set to 'now'.
711  *
712  *-----------------------------------------------------------------------
713  */
714 void
715 Arch_TouchLib (gn)
716     GNode	    *gn;      	/* The node of the library to touch */
717 {
718     FILE *	    arch;	/* Stream open to archive */
719     struct ar_hdr   arh;      	/* Header describing table of contents */
720     struct timeval  times[2];	/* Times for utimes() call */
721 
722     arch = ArchFindMember (gn->path, RANLIBMAG, &arh, "r+");
723     sprintf(arh.ar_date, "%-12d", now);
724 
725     if (arch != (FILE *) NULL) {
726 	(void)fwrite ((char *)&arh, sizeof (struct ar_hdr), 1, arch);
727 	fclose (arch);
728 
729 	times[0].tv_sec = times[1].tv_sec = now;
730 	times[0].tv_usec = times[1].tv_usec = 0;
731 	utimes(gn->path, times);
732     }
733 }
734 
735 /*-
736  *-----------------------------------------------------------------------
737  * Arch_MTime --
738  *	Return the modification time of a member of an archive.
739  *
740  * Results:
741  *	The modification time (seconds).
742  *
743  * Side Effects:
744  *	The mtime field of the given node is filled in with the value
745  *	returned by the function.
746  *
747  *-----------------------------------------------------------------------
748  */
749 int
750 Arch_MTime (gn)
751     GNode	  *gn;	      /* Node describing archive member */
752 {
753     struct ar_hdr *arhPtr;    /* Header of desired member */
754     int		  modTime;    /* Modification time as an integer */
755 
756     arhPtr = ArchStatMember (Var_Value (ARCHIVE, gn),
757 			     Var_Value (TARGET, gn),
758 			     TRUE);
759     if (arhPtr != (struct ar_hdr *) NULL) {
760 	(void)sscanf (arhPtr->ar_date, "%12d", &modTime);
761     } else {
762 	modTime = 0;
763     }
764 
765     gn->mtime = modTime;
766     return (modTime);
767 }
768 
769 /*-
770  *-----------------------------------------------------------------------
771  * Arch_MemMTime --
772  *	Given a non-existent archive member's node, get its modification
773  *	time from its archived form, if it exists.
774  *
775  * Results:
776  *	The modification time.
777  *
778  * Side Effects:
779  *	The mtime field is filled in.
780  *
781  *-----------------------------------------------------------------------
782  */
783 int
784 Arch_MemMTime (gn)
785     GNode   	  *gn;
786 {
787     LstNode 	  ln;
788     GNode   	  *pgn;
789     char    	  *nameStart,
790 		  *nameEnd;
791 
792     if (Lst_Open (gn->parents) != SUCCESS) {
793 	gn->mtime = 0;
794 	return (0);
795     }
796     while ((ln = Lst_Next (gn->parents)) != NILLNODE) {
797 	pgn = (GNode *) Lst_Datum (ln);
798 
799 	if (pgn->type & OP_ARCHV) {
800 	    /*
801 	     * If the parent is an archive specification and is being made
802 	     * and its member's name matches the name of the node we were
803 	     * given, record the modification time of the parent in the
804 	     * child. We keep searching its parents in case some other
805 	     * parent requires this child to exist...
806 	     */
807 	    nameStart = index (pgn->name, '(') + 1;
808 	    nameEnd = index (nameStart, ')');
809 
810 	    if (pgn->make &&
811 		strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
812 				     gn->mtime = Arch_MTime(pgn);
813 	    }
814 	} else if (pgn->make) {
815 	    /*
816 	     * Something which isn't a library depends on the existence of
817 	     * this target, so it needs to exist.
818 	     */
819 	    gn->mtime = 0;
820 	    break;
821 	}
822     }
823 
824     Lst_Close (gn->parents);
825 
826     return (gn->mtime);
827 }
828 
829 /*-
830  *-----------------------------------------------------------------------
831  * Arch_FindLib --
832  *	Search for a library along the given search path.
833  *
834  * Results:
835  *	None.
836  *
837  * Side Effects:
838  *	The node's 'path' field is set to the found path (including the
839  *	actual file name, not -l...). If the system can handle the -L
840  *	flag when linking (or we cannot find the library), we assume that
841  *	the user has placed the .LIBRARIES variable in the final linking
842  *	command (or the linker will know where to find it) and set the
843  *	TARGET variable for this node to be the node's name. Otherwise,
844  *	we set the TARGET variable to be the full path of the library,
845  *	as returned by Dir_FindFile.
846  *
847  *-----------------------------------------------------------------------
848  */
849 void
850 Arch_FindLib (gn, path)
851     GNode	    *gn;	      /* Node of library to find */
852     Lst	    	    path;	      /* Search path */
853 {
854     char	    *libName;   /* file name for archive */
855 
856     libName = (char *)emalloc (strlen (gn->name) + 6 - 2);
857     sprintf(libName, "lib%s.a", &gn->name[2]);
858 
859     gn->path = Dir_FindFile (libName, path);
860 
861     free (libName);
862 
863 #ifdef LIBRARIES
864     Var_Set (TARGET, gn->name, gn);
865 #else
866     Var_Set (TARGET, gn->path == (char *) NULL ? gn->name : gn->path, gn);
867 #endif LIBRARIES
868 }
869 
870 /*-
871  *-----------------------------------------------------------------------
872  * Arch_LibOODate --
873  *	Decide if a node with the OP_LIB attribute is out-of-date. Called
874  *	from Make_OODate to make its life easier.
875  *
876  *	There are several ways for a library to be out-of-date that are
877  *	not available to ordinary files. In addition, there are ways
878  *	that are open to regular files that are not available to
879  *	libraries. A library that is only used as a source is never
880  *	considered out-of-date by itself. This does not preclude the
881  *	library's modification time from making its parent be out-of-date.
882  *	A library will be considered out-of-date for any of these reasons,
883  *	given that it is a target on a dependency line somewhere:
884  *	    Its modification time is less than that of one of its
885  *	    	  sources (gn->mtime < gn->cmtime).
886  *	    Its modification time is greater than the time at which the
887  *	    	  make began (i.e. it's been modified in the course
888  *	    	  of the make, probably by archiving).
889  *	    Its modification time doesn't agree with the modification
890  *	    	  time of its RANLIBMAG member (i.e. its table of contents
891  *	    	  is out-of-date).
892  *
893  *
894  * Results:
895  *	TRUE if the library is out-of-date. FALSE otherwise.
896  *
897  * Side Effects:
898  *	The library will be hashed if it hasn't been already.
899  *
900  *-----------------------------------------------------------------------
901  */
902 Boolean
903 Arch_LibOODate (gn)
904     GNode   	  *gn;  	/* The library's graph node */
905 {
906     Boolean 	  oodate;
907 
908     if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
909 	oodate = FALSE;
910     } else if ((gn->mtime > now) || (gn->mtime < gn->cmtime)) {
911 	oodate = TRUE;
912     } else {
913 	struct ar_hdr  	*arhPtr;    /* Header for __.SYMDEF */
914 	int 	  	modTimeTOC; /* The table-of-contents's mod time */
915 
916 	arhPtr = ArchStatMember (gn->path, RANLIBMAG, FALSE);
917 
918 	if (arhPtr != (struct ar_hdr *)NULL) {
919 	    (void)sscanf (arhPtr->ar_date, "%12d", &modTimeTOC);
920 
921 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
922 		printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
923 	    }
924 	    oodate = (gn->mtime > modTimeTOC);
925 	} else {
926 	    /*
927 	     * A library w/o a table of contents is out-of-date
928 	     */
929 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
930 		printf("No t.o.c....");
931 	    }
932 	    oodate = TRUE;
933 	}
934     }
935     return (oodate);
936 }
937 
938 /*-
939  *-----------------------------------------------------------------------
940  * Arch_Init --
941  *	Initialize things for this module.
942  *
943  * Results:
944  *	None.
945  *
946  * Side Effects:
947  *	The 'archives' list is initialized.
948  *
949  *-----------------------------------------------------------------------
950  */
951 void
952 Arch_Init ()
953 {
954     archives = Lst_Init (FALSE);
955 }
956