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