source: trunk/src/kmk/arch.c@ 23

Last change on this file since 23 was 9, checked in by bird, 23 years ago

Initial revision

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