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