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