VirtualBox

source: kBuild/trunk/src/kmk/arch.c@ 151

Last change on this file since 151 was 46, checked in by bird, 22 years ago

kMk changes. Made extensions configurable from config.h. fixed parents.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette