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