VirtualBox

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

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

OS2 / VAC308

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

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