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