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