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