Annotation of src/usr.bin/make/arch.c, Revision 1.8
1.8 ! niklas 1: /* $OpenBSD: arch.c,v 1.7 1996/09/21 19:36:27 niklas Exp $ */
1.6 briggs 2: /* $NetBSD: arch.c,v 1.16 1996/08/13 16:42:00 christos Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
6: * Copyright (c) 1988, 1989 by Adam de Boor
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
44: static char sccsid[] = "@(#)arch.c 5.7 (Berkeley) 12/28/90";
1.4 niklas 45: static char rcsid[] = "$NetBSD: arch.c,v 1.14 1996/03/12 18:04:27 christos Exp $";
1.1 deraadt 46: #else
1.8 ! niklas 47: static char rcsid[] = "$OpenBSD: arch.c,v 1.7 1996/09/21 19:36:27 niklas Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: /*-
52: * arch.c --
53: * Functions to manipulate libraries, archives and their members.
54: *
55: * Once again, cacheing/hashing comes into play in the manipulation
56: * of archives. The first time an archive is referenced, all of its members'
57: * headers are read and hashed and the archive closed again. All hashed
58: * archives are kept on a list which is searched each time an archive member
59: * is referenced.
60: *
61: * The interface to this module is:
62: * Arch_ParseArchive Given an archive specification, return a list
63: * of GNode's, one for each member in the spec.
64: * FAILURE is returned if the specification is
65: * invalid for some reason.
66: *
67: * Arch_Touch Alter the modification time of the archive
68: * member described by the given node to be
69: * the current time.
70: *
71: * Arch_TouchLib Update the modification time of the library
72: * described by the given node. This is special
73: * because it also updates the modification time
74: * of the library's table of contents.
75: *
76: * Arch_MTime Find the modification time of a member of
77: * an archive *in the archive*. The time is also
78: * placed in the member's GNode. Returns the
79: * modification time.
80: *
81: * Arch_MemTime Find the modification time of a member of
82: * an archive. Called when the member doesn't
83: * already exist. Looks in the archive for the
84: * modification time. Returns the modification
85: * time.
86: *
87: * Arch_FindLib Search for a library along a path. The
88: * library name in the GNode should be in
89: * -l<name> format.
90: *
91: * Arch_LibOODate Special function to decide if a library node
92: * is out-of-date.
93: *
94: * Arch_Init Initialize this module.
95: *
96: * Arch_End Cleanup this module.
97: */
98:
99: #include <sys/types.h>
100: #include <sys/stat.h>
101: #include <sys/time.h>
102: #include <sys/param.h>
103: #include <ctype.h>
104: #include <ar.h>
1.3 niklas 105: #include <utime.h>
1.1 deraadt 106: #include <stdio.h>
107: #include <stdlib.h>
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;
153:
154: /* Free memory from hash entries */
155: for (entry = Hash_EnumFirst(&a->members, &search);
156: entry != (Hash_Entry *)NULL;
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: }
166:
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;
202:
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;
214:
215: result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
216: if (result == var_Error) {
217: return(FAILURE);
218: } else {
219: subLibName = TRUE;
220: }
221:
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: }
233:
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;
309:
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);
355:
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: }
394:
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, '/');
484: if (cp != (char *) NULL) {
485: member = cp + 1;
486: }
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:
494: if (he != (Hash_Entry *) NULL) {
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));
508: return ((struct ar_hdr *) NULL);
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:
524: if (arch == (FILE *)NULL) {
525: return ((struct ar_hdr *)NULL);
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");
537: if (arch == (FILE *) NULL) {
538: return ((struct ar_hdr *) NULL);
539: }
540:
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);
548: return ((struct ar_hdr *) NULL);
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';
557:
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:
626: he = Hash_CreateEntry (&ar->members, memName, (Boolean *)NULL);
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:
644: if (he != (Hash_Entry *) NULL) {
645: return ((struct ar_hdr *) Hash_GetValue (he));
646: } else {
647: return ((struct ar_hdr *) NULL);
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);
656: return ((struct ar_hdr *) 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);
796: if (arch == (FILE *) NULL) {
797: return ((FILE *) NULL);
798: }
799:
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);
807: return ((FILE *) NULL);
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: }
824:
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);
832: return ((FILE *) NULL);
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);
908: return ((FILE *) NULL);
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:
943: if (arch != (FILE *) NULL) {
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:
976: if (arch != (FILE *) NULL) {
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:
1016: if (arhPtr != (struct ar_hdr *) NULL) {
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 --
1089: * Search for a library along the given search path.
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
1123: Var_Set (TARGET, gn->path == (char *) 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
1153: * TOC.
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;
1168:
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:
1180: if (arhPtr != (struct ar_hdr *)NULL) {
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);
1241: }