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