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