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