Annotation of src/usr.bin/make/arch.c, Revision 1.27
1.27 ! espie 1: /* $OpenBSD: arch.c,v 1.26 2000/03/26 16:21:32 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.27 ! espie 46: static char rcsid[] = "$OpenBSD: arch.c,v 1.26 2000/03/26 16:21:32 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
1.25 espie 76: * an archive *in the archive*, return TRUE if
77: * exists. The time is placed in the member's
78: * GNode. Returns the modification time.
1.1 deraadt 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:
1.27 ! espie 132: static int ArchFindArchive __P((void *, void *));
1.16 espie 133: #ifdef CLEANUP
1.27 ! espie 134: static void ArchFree __P((void *));
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)
1.27 ! espie 161: void *ap;
1.1 deraadt 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))
1.27 ! espie 171: free(Hash_GetValue(entry));
1.1 deraadt 172:
173: free(a->name);
1.14 espie 174: efree(a->fnametab);
1.1 deraadt 175: Hash_DeleteTable(&a->members);
1.27 ! espie 176: free(a);
1.1 deraadt 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: */
1.24 espie 224: size_t length;
1.1 deraadt 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: */
1.24 espie 266: size_t length;
1.1 deraadt 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.26 espie 347: Lst_AtEnd(nodeLst, 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.26 espie 382: Lst_AtEnd(nodeLst, 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.26 espie 400: Lst_AtEnd(nodeLst, 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)
1.27 ! espie 446: void *ar; /* Current list element */
! 447: void *archName; /* Name we want */
1.1 deraadt 448: {
1.27 ! espie 449: return strcmp ((char *)archName, ((Arch *)ar)->name);
1.1 deraadt 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.26 espie 499: ln = Lst_Find(archives, ArchFindArchive, 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.27 ! espie 637: he = Hash_CreateEntry(&ar->members, memName, NULL);
! 638: Hash_SetValue(he, emalloc(sizeof(struct ar_hdr)));
! 639: memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
1.1 deraadt 640: }
1.13 millert 641: fseek (arch, (size + 1) & ~1, SEEK_CUR);
1.1 deraadt 642: }
643:
644: fclose (arch);
645:
1.26 espie 646: Lst_AtEnd(archives, ar);
1.1 deraadt 647:
648: /*
649: * Now that the archive has been read and cached, we can look into
650: * the hash table to find the desired member's header.
651: */
652: he = Hash_FindEntry (&ar->members, member);
653:
1.10 kstailey 654: if (he != NULL) {
1.1 deraadt 655: return ((struct ar_hdr *) Hash_GetValue (he));
656: } else {
1.10 kstailey 657: return (NULL);
1.1 deraadt 658: }
1.6 briggs 659:
660: badarch:
1.27 ! espie 661: fclose(arch);
! 662: Hash_DeleteTable(&ar->members);
1.14 espie 663: efree(ar->fnametab);
1.27 ! espie 664: free(ar);
! 665: return NULL;
1.1 deraadt 666: }
1.6 briggs 667:
668: #ifdef SVR4ARCHIVES
669: /*-
670: *-----------------------------------------------------------------------
671: * ArchSVR4Entry --
672: * Parse an SVR4 style entry that begins with a slash.
673: * If it is "//", then load the table of filenames
674: * If it is "/<offset>", then try to substitute the long file name
675: * from offset of a table previously read.
676: *
677: * Results:
678: * -1: Bad data in archive
679: * 0: A table was loaded from the file
680: * 1: Name was successfully substituted from table
681: * 2: Name was not successfully substituted from table
682: *
683: * Side Effects:
684: * If a table is read, the file pointer is moved to the next archive
685: * member
686: *
687: *-----------------------------------------------------------------------
688: */
689: static int
690: ArchSVR4Entry(ar, name, size, arch)
691: Arch *ar;
692: char *name;
693: size_t size;
694: FILE *arch;
695: {
696: #define ARLONGNAMES1 "//"
697: #define ARLONGNAMES2 "/ARFILENAMES"
698: size_t entry;
699: char *ptr, *eptr;
700:
701: if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
702: strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
703:
704: if (ar->fnametab != NULL) {
705: if (DEBUG(ARCH)) {
706: printf("Attempted to redefine an SVR4 name table\n");
707: }
708: return -1;
709: }
710:
711: /*
712: * This is a table of archive names, so we build one for
713: * ourselves
714: */
715: ar->fnametab = emalloc(size);
716: ar->fnamesize = size;
717:
718: if (fread(ar->fnametab, size, 1, arch) != 1) {
719: if (DEBUG(ARCH)) {
720: printf("Reading an SVR4 name table failed\n");
721: }
722: return -1;
723: }
724: eptr = ar->fnametab + size;
725: for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
726: switch (*ptr) {
727: case '/':
728: entry++;
729: *ptr = '\0';
730: break;
731:
732: case '\n':
733: break;
734:
735: default:
736: break;
737: }
738: if (DEBUG(ARCH)) {
1.14 espie 739: printf("Found svr4 archive name table with %lu entries\n",
740: (u_long)entry);
1.6 briggs 741: }
742: return 0;
743: }
744:
745: if (name[1] == ' ' || name[1] == '\0')
746: return 2;
747:
748: entry = (size_t) strtol(&name[1], &eptr, 0);
749: if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
750: if (DEBUG(ARCH)) {
751: printf("Could not parse SVR4 name %s\n", name);
752: }
753: return 2;
754: }
755: if (entry >= ar->fnamesize) {
756: if (DEBUG(ARCH)) {
1.14 espie 757: printf("SVR4 entry offset %s is greater than %lu\n",
758: name, (u_long)ar->fnamesize);
1.6 briggs 759: }
760: return 2;
761: }
762:
763: if (DEBUG(ARCH)) {
764: printf("Replaced %s with %s\n", name, &ar->fnametab[entry]);
765: }
766:
767: (void) strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
768: name[MAXPATHLEN] = '\0';
769: return 1;
770: }
771: #endif
772:
1.1 deraadt 773:
774: /*-
775: *-----------------------------------------------------------------------
776: * ArchFindMember --
777: * Locate a member of an archive, given the path of the archive and
778: * the path of the desired member. If the archive is to be modified,
779: * the mode should be "r+", if not, it should be "r".
780: *
781: * Results:
782: * An FILE *, opened for reading and writing, positioned at the
783: * start of the member's struct ar_hdr, or NULL if the member was
784: * nonexistent. The current struct ar_hdr for member.
785: *
786: * Side Effects:
787: * The passed struct ar_hdr structure is filled in.
788: *
789: *-----------------------------------------------------------------------
790: */
791: static FILE *
792: ArchFindMember (archive, member, arhPtr, mode)
793: char *archive; /* Path to the archive */
794: char *member; /* Name of member. If it is a path, only the
795: * last component is used. */
796: struct ar_hdr *arhPtr; /* Pointer to header structure to be filled in */
797: char *mode; /* The mode for opening the stream */
798: {
799: FILE * arch; /* Stream to archive */
800: int size; /* Size of archive member */
801: char *cp; /* Useful character pointer */
802: char magic[SARMAG];
803: int len, tlen;
804:
805: arch = fopen (archive, mode);
1.10 kstailey 806: if (arch == NULL) {
807: return (NULL);
1.1 deraadt 808: }
1.9 millert 809:
1.1 deraadt 810: /*
811: * We use the ARMAG string to make sure this is an archive we
812: * can handle...
813: */
814: if ((fread (magic, SARMAG, 1, arch) != 1) ||
815: (strncmp (magic, ARMAG, SARMAG) != 0)) {
816: fclose (arch);
1.10 kstailey 817: return (NULL);
1.1 deraadt 818: }
819:
820: /*
821: * Because of space constraints and similar things, files are archived
822: * using their final path components, not the entire thing, so we need
823: * to point 'member' to the final component, if there is one, to make
824: * the comparisons easier...
825: */
826: cp = strrchr (member, '/');
827: if (cp != (char *) NULL) {
828: member = cp + 1;
829: }
830: len = tlen = strlen (member);
831: if (len > sizeof (arhPtr->ar_name)) {
832: tlen = sizeof (arhPtr->ar_name);
833: }
1.9 millert 834:
1.1 deraadt 835: while (fread ((char *)arhPtr, sizeof (struct ar_hdr), 1, arch) == 1) {
836: if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof (arhPtr->ar_fmag) ) != 0) {
837: /*
838: * The header is bogus, so the archive is bad
839: * and there's no way we can recover...
840: */
841: fclose (arch);
1.10 kstailey 842: return (NULL);
1.1 deraadt 843: } else if (strncmp (member, arhPtr->ar_name, tlen) == 0) {
844: /*
845: * If the member's name doesn't take up the entire 'name' field,
846: * we have to be careful of matching prefixes. Names are space-
847: * padded to the right, so if the character in 'name' at the end
848: * of the matched string is anything but a space, this isn't the
849: * member we sought.
850: */
851: if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
852: goto skip;
853: } else {
854: /*
855: * To make life easier, we reposition the file at the start
856: * of the header we just read before we return the stream.
857: * In a more general situation, it might be better to leave
858: * the file at the actual member, rather than its header, but
859: * not here...
860: */
1.13 millert 861: fseek (arch, -sizeof(struct ar_hdr), SEEK_CUR);
1.1 deraadt 862: return (arch);
863: }
864: } else
865: #ifdef AR_EFMT1
866: /*
867: * BSD 4.4 extended AR format: #1/<namelen>, with name as the
868: * first <namelen> bytes of the file
869: */
870: if (strncmp(arhPtr->ar_name, AR_EFMT1,
871: sizeof(AR_EFMT1) - 1) == 0 &&
872: isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
873:
874: unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
875: char ename[MAXPATHLEN];
876:
877: if (elen > MAXPATHLEN) {
878: fclose (arch);
879: return NULL;
880: }
881: if (fread (ename, elen, 1, arch) != 1) {
882: fclose (arch);
883: return NULL;
884: }
885: ename[elen] = '\0';
886: if (DEBUG(ARCH) || DEBUG(MAKE)) {
887: printf("ArchFind: Extended format entry for %s\n", ename);
888: }
889: if (strncmp(ename, member, len) == 0) {
890: /* Found as extended name */
1.13 millert 891: fseek (arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR);
1.1 deraadt 892: return (arch);
893: }
1.13 millert 894: fseek (arch, -elen, SEEK_CUR);
1.1 deraadt 895: goto skip;
896: } else
897: #endif
898: {
899: skip:
900: /*
901: * This isn't the member we're after, so we need to advance the
902: * stream's pointer to the start of the next header. Files are
903: * padded with newlines to an even-byte boundary, so we need to
904: * extract the size of the file from the 'size' field of the
905: * header and round it up during the seek.
906: */
907: arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
908: size = (int) strtol(arhPtr->ar_size, NULL, 10);
1.13 millert 909: fseek (arch, (size + 1) & ~1, SEEK_CUR);
1.1 deraadt 910: }
911: }
912:
913: /*
914: * We've looked everywhere, but the member is not to be found. Close the
915: * archive and return NULL -- an error.
916: */
917: fclose (arch);
1.10 kstailey 918: return (NULL);
1.1 deraadt 919: }
920:
921: /*-
922: *-----------------------------------------------------------------------
923: * Arch_Touch --
924: * Touch a member of an archive.
925: *
926: * Results:
927: * The 'time' field of the member's header is updated.
928: *
929: * Side Effects:
930: * The modification time of the entire archive is also changed.
931: * For a library, this could necessitate the re-ranlib'ing of the
932: * whole thing.
933: *
934: *-----------------------------------------------------------------------
935: */
936: void
937: Arch_Touch (gn)
938: GNode *gn; /* Node of member to touch */
939: {
940: FILE * arch; /* Stream open to archive, positioned properly */
941: struct ar_hdr arh; /* Current header describing member */
942:
1.17 espie 943: arch = ArchFindMember(Var_Value(ARCHIVE, gn),
944: Var_Value(MEMBER, gn),
1.1 deraadt 945: &arh, "r+");
946: sprintf(arh.ar_date, "%-12ld", (long) now);
947:
1.10 kstailey 948: if (arch != NULL) {
1.1 deraadt 949: (void)fwrite ((char *)&arh, sizeof (struct ar_hdr), 1, arch);
950: fclose (arch);
951: }
952: }
953:
954: /*-
955: *-----------------------------------------------------------------------
956: * Arch_TouchLib --
957: * Given a node which represents a library, touch the thing, making
958: * sure that the table of contents also is touched.
959: *
960: * Results:
961: * None.
962: *
963: * Side Effects:
964: * Both the modification time of the library and of the RANLIBMAG
965: * member are set to 'now'.
966: *
967: *-----------------------------------------------------------------------
968: */
969: void
970: Arch_TouchLib (gn)
971: GNode *gn; /* The node of the library to touch */
972: {
1.3 niklas 973: #ifdef RANLIBMAG
1.1 deraadt 974: FILE * arch; /* Stream open to archive */
975: struct ar_hdr arh; /* Header describing table of contents */
1.3 niklas 976: struct utimbuf times; /* Times for utime() call */
1.1 deraadt 977:
978: arch = ArchFindMember (gn->path, RANLIBMAG, &arh, "r+");
979: sprintf(arh.ar_date, "%-12ld", (long) now);
980:
1.10 kstailey 981: if (arch != NULL) {
1.1 deraadt 982: (void)fwrite ((char *)&arh, sizeof (struct ar_hdr), 1, arch);
983: fclose (arch);
984:
1.3 niklas 985: times.actime = times.modtime = now;
986: utime(gn->path, ×);
1.1 deraadt 987: }
1.3 niklas 988: #endif
1.1 deraadt 989: }
990:
991: /*-
992: *-----------------------------------------------------------------------
993: * Arch_MTime --
994: * Return the modification time of a member of an archive.
995: *
996: * Results:
1.25 espie 997: * TRUE if found.
1.1 deraadt 998: *
999: * Side Effects:
1000: * The mtime field of the given node is filled in with the value
1001: * returned by the function.
1002: *
1003: *-----------------------------------------------------------------------
1004: */
1.25 espie 1005: Boolean
1.1 deraadt 1006: Arch_MTime (gn)
1007: GNode *gn; /* Node describing archive member */
1008: {
1009: struct ar_hdr *arhPtr; /* Header of desired member */
1.14 espie 1010: time_t modTime; /* Modification time as an integer */
1.1 deraadt 1011:
1.17 espie 1012: arhPtr = ArchStatMember (Var_Value(ARCHIVE, gn),
1013: Var_Value(MEMBER, gn),
1.1 deraadt 1014: TRUE);
1.10 kstailey 1015: if (arhPtr != NULL) {
1.25 espie 1016: gn->mtime = (time_t) strtol(arhPtr->ar_date, NULL, 10);
1017: return TRUE;
1.1 deraadt 1018: } else {
1.25 espie 1019: gn->mtime = OUT_OF_DATE;
1020: return FALSE;
1.1 deraadt 1021: }
1022: }
1023:
1024: /*-
1025: *-----------------------------------------------------------------------
1026: * Arch_MemMTime --
1027: * Given a non-existent archive member's node, get its modification
1028: * time from its archived form, if it exists.
1029: *
1030: * Results:
1.25 espie 1031: * TRUE if found.
1.1 deraadt 1032: *
1033: * Side Effects:
1034: * The mtime field is filled in.
1035: *
1036: *-----------------------------------------------------------------------
1037: */
1.25 espie 1038: Boolean
1.1 deraadt 1039: Arch_MemMTime (gn)
1040: GNode *gn;
1041: {
1042: LstNode ln;
1043: GNode *pgn;
1044: char *nameStart,
1045: *nameEnd;
1046:
1047: if (Lst_Open (gn->parents) != SUCCESS) {
1.25 espie 1048: gn->mtime = OUT_OF_DATE;
1049: return FALSE;
1.1 deraadt 1050: }
1.20 espie 1051: while ((ln = Lst_Next (gn->parents)) != NULL) {
1.1 deraadt 1052: pgn = (GNode *) Lst_Datum (ln);
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:
1086: Lst_Close (gn->parents);
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
1126: Var_Set (TARGET, gn->name, gn);
1127: #else
1.10 kstailey 1128: Var_Set (TARGET, 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.1 deraadt 1174: if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
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
1223: Arch_Init ()
1224: {
1.19 espie 1225: archives = Lst_Init();
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.1 deraadt 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: }