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