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