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
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)arch.c 8.2 (Berkeley) 1/2/94
39 * $FreeBSD: src/usr.bin/make/arch.c,v 1.15.2.1 2001/02/13 03:13:57 will Exp $
40 * $DragonFly: src/usr.bin/make/arch.c,v 1.14 2004/12/10 19:22:24 okumoto Exp $
45 * Functions to manipulate libraries, archives and their members.
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
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.
59 * Arch_Touch Alter the modification time of the archive
60 * member described by the given node to be
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.
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
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
79 * Arch_FindLib Search for a library along a path. The
80 * library name in the GNode should be in
83 * Arch_LibOODate Special function to decide if a library node
86 * Arch_Init Initialize this module.
88 * Arch_End Cleanup this module.
91 #include <sys/types.h>
94 #include <sys/param.h>
105 static Lst archives; /* Lst of archives we've already examined */
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 char *fnametab; /* Extended name table strings */
112 size_t fnamesize; /* Size of the string table */
115 static int ArchFindArchive(void *, void *);
116 static void ArchFree(void *);
117 static struct ar_hdr *ArchStatMember(char *, char *, Boolean);
118 static FILE *ArchFindMember(char *, char *, struct ar_hdr *, char *);
119 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
121 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
125 *-----------------------------------------------------------------------
127 * Free memory used by an archive
135 *-----------------------------------------------------------------------
140 Arch *a = (Arch *)ap;
144 /* Free memory from hash entries */
145 for (entry = Hash_EnumFirst(&a->members, &search);
147 entry = Hash_EnumNext(&search))
148 free(Hash_GetValue(entry));
152 Hash_DeleteTable(&a->members);
157 *-----------------------------------------------------------------------
158 * Arch_ParseArchive --
159 * Parse the archive specification in the given line and find/create
160 * the nodes for the specified archive members, placing their nodes
161 * on the given list, given the pointer to the start of the
162 * specification, a Lst on which to place the nodes, and a context
163 * in which to expand variables.
166 * SUCCESS if it was a valid specification. The linePtr is updated
167 * to point to the first non-space after the archive spec. The
168 * nodes for the members are placed on the given list.
171 * Some nodes may be created. The given list is extended.
173 *-----------------------------------------------------------------------
176 Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
178 char *cp; /* Pointer into line */
179 GNode *gn; /* New node */
180 char *libName; /* Library-part of specification */
181 char *memName; /* Member-part of specification */
182 char *nameBuf; /* temporary place for node name */
183 char saveChar; /* Ending delimiter of member-name */
184 Boolean subLibName; /* TRUE if libName should have/had
185 * variable substitution performed on it */
191 for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
194 * Variable spec, so call the Var module to parse the puppy
195 * so we can safely advance beyond it...
201 result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
202 if (result == var_Error) {
217 libName = Var_Subst(NULL, libName, ctxt, TRUE);
223 * First skip to the start of the member's name, mark that
224 * place and skip to the end of it (either white-space or
227 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
229 while (*cp != '\0' && *cp != ')' && isspace((unsigned char)*cp)) {
233 while (*cp != '\0' && *cp != ')' && !isspace((unsigned char)*cp)) {
236 * Variable spec, so call the Var module to parse the puppy
237 * so we can safely advance beyond it...
243 result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
244 if (result == var_Error) {
260 * If the specification ends without a closing parenthesis,
261 * chances are there's something wrong (like a missing backslash),
262 * so it's better to return failure than allow such things to happen
265 printf("No closing parenthesis in archive specification\n");
270 * If we didn't move anywhere, we must be done
280 * XXX: This should be taken care of intelligently by
281 * SuffExpandChildren, both for the archive and the member portions.
284 * If member contains variables, try and substitute for them.
285 * This will slow down archive specs with dynamic sources, of course,
286 * since we'll be (non-)substituting them three times, but them's
287 * the breaks -- we need to do this since SuffExpandChildren calls
288 * us, otherwise we could assume the thing would be taken care of
294 char *oldMemName = memName;
297 memName = Var_Subst(NULL, memName, ctxt, TRUE);
300 * Now form an archive spec and recurse to deal with nested
301 * variables and multi-word variable values.... The results
302 * are just placed at the end of the nodeLst we're returning.
304 sz = strlen(memName) + strlen(libName) + 3;
305 buf = sacrifice = emalloc(sz);
306 snprintf(buf, sz, "%s(%s)", libName, memName);
308 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
310 * Must contain dynamic sources, so we can't deal with it now.
311 * Just create an ARCHV node for the thing and let
312 * SuffExpandChildren handle it...
314 gn = Targ_FindNode(buf, TARG_CREATE);
320 gn->type |= OP_ARCHV;
321 Lst_AtEnd(nodeLst, (void *)gn);
323 } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt) != SUCCESS) {
325 * Error in nested call -- free buffer and return FAILURE
332 * Free buffer and continue with our work.
335 } else if (Dir_HasWildcards(memName)) {
336 Lst members = Lst_Init(FALSE);
338 size_t sz = MAXPATHLEN;
341 nameBuf = emalloc(sz);
343 Dir_Expand(memName, dirSearchPath, members);
344 while (!Lst_IsEmpty(members)) {
345 member = (char *)Lst_DeQueue(members);
346 nsz = strlen(libName) + strlen(member) + 3; /* 3 = ()+\0 */
349 nameBuf = erealloc(nameBuf, sz);
351 snprintf(nameBuf, sz, "%s(%s)", libName, member);
353 gn = Targ_FindNode(nameBuf, TARG_CREATE);
359 * We've found the node, but have to make sure the rest of
360 * the world knows it's an archive member, without having
361 * to constantly check for parentheses, so we type the
362 * thing with the OP_ARCHV bit before we place it on the
363 * end of the provided list.
365 gn->type |= OP_ARCHV;
366 Lst_AtEnd(nodeLst, (void *)gn);
369 Lst_Destroy(members, NOFREE);
372 size_t sz = strlen(libName) + strlen(memName) + 3;
373 nameBuf = emalloc(sz);
374 snprintf(nameBuf, sz, "%s(%s)", libName, memName);
375 gn = Targ_FindNode(nameBuf, TARG_CREATE);
381 * We've found the node, but have to make sure the rest of the
382 * world knows it's an archive member, without having to
383 * constantly check for parentheses, so we type the thing with
384 * the OP_ARCHV bit before we place it on the end of the
387 gn->type |= OP_ARCHV;
388 Lst_AtEnd(nodeLst, (void *)gn);
399 * If substituted libName, free it now, since we need it no longer.
406 * We promised the pointer would be set up at the next non-space, so
407 * we must advance cp there before setting *linePtr... (note that on
408 * entrance to the loop, cp is guaranteed to point at a ')')
412 } while (*cp != '\0' && isspace((unsigned char)*cp));
419 *-----------------------------------------------------------------------
421 * See if the given archive is the one we are looking for. Called
422 * From ArchStatMember and ArchFindMember via Lst_Find with the
423 * current list element and the name we want.
426 * 0 if it is, non-zero if it isn't.
431 *-----------------------------------------------------------------------
434 ArchFindArchive(void *ar, void *archName)
436 return (strcmp((char *)archName, ((Arch *)ar)->name));
440 *-----------------------------------------------------------------------
442 * Locate a member of an archive, given the path of the archive and
443 * the path of the desired member, and a boolean representing whether
444 * or not the archive should be hashed (if not already hashed).
447 * A pointer to the current struct ar_hdr structure for the member. Note
448 * That no position is returned, so this is not useful for touching
449 * archive members. This is mostly because we have no assurances that
450 * The archive will remain constant after we read all the headers, so
451 * there's not much point in remembering the position...
455 *-----------------------------------------------------------------------
457 static struct ar_hdr *
458 ArchStatMember(char *archive, char *member, Boolean hash)
460 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name) - 1)
461 FILE * arch; /* Stream to archive */
462 int size; /* Size of archive member */
463 char *cp; /* Useful character pointer */
465 LstNode ln; /* Lst member containing archive descriptor */
466 Arch *ar; /* Archive descriptor */
467 Hash_Entry *he; /* Entry containing member's description */
468 struct ar_hdr arh; /* archive-member header for reading archive */
469 char memName[MAXPATHLEN];
470 /* Current member name while hashing. */
473 * Because of space constraints and similar things, files are archived
474 * using their final path components, not the entire thing, so we need
475 * to point 'member' to the final component, if there is one, to make
476 * the comparisons easier...
478 cp = strrchr(member, '/');
479 if ((cp != NULL) && (strcmp(member, RANLIBMAG) != 0))
482 ln = Lst_Find(archives, (void *)archive, ArchFindArchive);
484 ar = (Arch *)Lst_Datum(ln);
486 he = Hash_FindEntry(&ar->members, member);
489 return ((struct ar_hdr *)Hash_GetValue (he));
491 /* Try truncated name */
492 char copy[AR_MAX_NAME_LEN + 1];
493 size_t len = strlen(member);
495 if (len > AR_MAX_NAME_LEN) {
496 len = AR_MAX_NAME_LEN;
497 strncpy(copy, member, AR_MAX_NAME_LEN);
498 copy[AR_MAX_NAME_LEN] = '\0';
500 if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
501 return ((struct ar_hdr *)Hash_GetValue(he));
508 * Caller doesn't want the thing hashed, just use ArchFindMember
509 * to read the header for the member out and close down the stream
510 * again. Since the archive is not to be hashed, we assume there's
511 * no need to allocate extra room for the header we're returning,
512 * so just declare it static.
514 static struct ar_hdr sarh;
516 arch = ArchFindMember(archive, member, &sarh, "r");
527 * We don't have this archive on the list yet, so we want to find out
528 * everything that's in it and cache it so we can get at it quickly.
530 arch = fopen(archive, "r");
536 * We use the ARMAG string to make sure this is an archive we
539 if ((fread(magic, SARMAG, 1, arch) != 1) ||
540 (strncmp(magic, ARMAG, SARMAG) != 0)) {
545 ar = (Arch *)emalloc(sizeof(Arch));
546 ar->name = estrdup(archive);
549 Hash_InitTable(&ar->members, -1);
550 memName[AR_MAX_NAME_LEN] = '\0';
552 while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
553 if (strncmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
555 * The header is bogus, so the archive is bad
556 * and there's no way we can recover...
561 * We need to advance the stream's pointer to the start of the
562 * next header. Files are padded with newlines to an even-byte
563 * boundary, so we need to extract the size of the file from the
564 * 'size' field of the header and round it up during the seek.
566 arh.ar_size[sizeof(arh.ar_size) - 1] = '\0';
567 size = (int)strtol(arh.ar_size, NULL, 10);
569 strncpy(memName, arh.ar_name, sizeof(arh.ar_name));
570 for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
577 * svr4 names are slash terminated. Also svr4 extended AR format.
579 if (memName[0] == '/') {
581 * svr4 magic mode; handle it
583 switch (ArchSVR4Entry(ar, memName, size, arch)) {
584 case -1: /* Invalid data */
586 case 0: /* List of files entry */
588 default: /* Got the entry */
600 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
601 * first <namelen> bytes of the file
603 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
604 isdigit(memName[sizeof(AR_EFMT1) - 1])) {
606 unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
608 if (elen > MAXPATHLEN)
610 if (fread(memName, elen, 1, arch) != 1)
612 memName[elen] = '\0';
613 fseek(arch, -elen, SEEK_CUR);
614 /* XXX Multiple levels may be asked for, make this conditional
615 * on one, and use DEBUGF.
617 if (DEBUG(ARCH) || DEBUG(MAKE)) {
618 fprintf(stderr, "ArchStat: Extended format entry for %s\n", memName);
623 he = Hash_CreateEntry(&ar->members, memName, NULL);
624 Hash_SetValue(he, (void *)emalloc (sizeof(struct ar_hdr)));
625 memcpy(Hash_GetValue(he), &arh,
626 sizeof(struct ar_hdr));
628 fseek(arch, (size + 1) & ~1, SEEK_CUR);
633 Lst_AtEnd(archives, (void *)ar);
636 * Now that the archive has been read and cached, we can look into
637 * the hash table to find the desired member's header.
639 he = Hash_FindEntry(&ar->members, member);
642 return ((struct ar_hdr *)Hash_GetValue (he));
649 Hash_DeleteTable(&ar->members);
657 *-----------------------------------------------------------------------
659 * Parse an SVR4 style entry that begins with a slash.
660 * If it is "//", then load the table of filenames
661 * If it is "/<offset>", then try to substitute the long file name
662 * from offset of a table previously read.
665 * -1: Bad data in archive
666 * 0: A table was loaded from the file
667 * 1: Name was successfully substituted from table
668 * 2: Name was not successfully substituted from table
671 * If a table is read, the file pointer is moved to the next archive
674 *-----------------------------------------------------------------------
677 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
679 #define ARLONGNAMES1 "//"
680 #define ARLONGNAMES2 "/ARFILENAMES"
684 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
685 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
687 if (ar->fnametab != NULL) {
688 DEBUGF(ARCH, ("Attempted to redefine an SVR4 name table\n"));
693 * This is a table of archive names, so we build one for
696 ar->fnametab = emalloc(size);
697 ar->fnamesize = size;
699 if (fread(ar->fnametab, size, 1, arch) != 1) {
700 DEBUGF(ARCH, ("Reading an SVR4 name table failed\n"));
703 eptr = ar->fnametab + size;
704 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
717 DEBUGF(ARCH, ("Found svr4 archive name table with %zu entries\n", entry));
721 if (name[1] == ' ' || name[1] == '\0')
724 entry = (size_t)strtol(&name[1], &eptr, 0);
725 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
726 DEBUGF(ARCH, ("Could not parse SVR4 name %s\n", name));
729 if (entry >= ar->fnamesize) {
730 DEBUGF(ARCH, ("SVR4 entry offset %s is greater than %zu\n",
731 name, ar->fnamesize));
735 DEBUGF(ARCH, ("Replaced %s with %s\n", name, &ar->fnametab[entry]));
737 strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
738 name[MAXPATHLEN - 1] = '\0';
745 *-----------------------------------------------------------------------
747 * Locate a member of an archive, given the path of the archive and
748 * the path of the desired member. If the archive is to be modified,
749 * the mode should be "r+", if not, it should be "r". arhPtr is a
750 * poitner to the header structure to fill in.
753 * An FILE *, opened for reading and writing, positioned at the
754 * start of the member's struct ar_hdr, or NULL if the member was
755 * nonexistent. The current struct ar_hdr for member.
758 * The passed struct ar_hdr structure is filled in.
760 *-----------------------------------------------------------------------
763 ArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr, char *mode)
765 FILE * arch; /* Stream to archive */
766 int size; /* Size of archive member */
767 char *cp; /* Useful character pointer */
771 arch = fopen(archive, mode);
777 * We use the ARMAG string to make sure this is an archive we
780 if ((fread(magic, SARMAG, 1, arch) != 1) ||
781 (strncmp(magic, ARMAG, SARMAG) != 0)) {
787 * Because of space constraints and similar things, files are archived
788 * using their final path components, not the entire thing, so we need
789 * to point 'member' to the final component, if there is one, to make
790 * the comparisons easier...
792 cp = strrchr(member, '/');
793 if ((cp != NULL) && (strcmp(member, RANLIBMAG) != 0)) {
796 len = tlen = strlen(member);
797 if (len > sizeof(arhPtr->ar_name)) {
798 tlen = sizeof(arhPtr->ar_name);
801 while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
802 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
804 * The header is bogus, so the archive is bad
805 * and there's no way we can recover...
809 } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
811 * If the member's name doesn't take up the entire 'name' field,
812 * we have to be careful of matching prefixes. Names are space-
813 * padded to the right, so if the character in 'name' at the end
814 * of the matched string is anything but a space, this isn't the
817 if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
821 * To make life easier, we reposition the file at the start
822 * of the header we just read before we return the stream.
823 * In a more general situation, it might be better to leave
824 * the file at the actual member, rather than its header, but
827 fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR);
833 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
834 * first <namelen> bytes of the file
836 if (strncmp(arhPtr->ar_name, AR_EFMT1,
837 sizeof(AR_EFMT1) - 1) == 0 &&
838 isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
840 unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
841 char ename[MAXPATHLEN];
843 if (elen > MAXPATHLEN) {
847 if (fread(ename, elen, 1, arch) != 1) {
855 if (DEBUG(ARCH) || DEBUG(MAKE)) {
856 printf("ArchFind: Extended format entry for %s\n", ename);
858 if (strncmp(ename, member, len) == 0) {
859 /* Found as extended name */
860 fseek(arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR);
863 fseek(arch, -elen, SEEK_CUR);
870 * This isn't the member we're after, so we need to advance the
871 * stream's pointer to the start of the next header. Files are
872 * padded with newlines to an even-byte boundary, so we need to
873 * extract the size of the file from the 'size' field of the
874 * header and round it up during the seek.
876 arhPtr->ar_size[sizeof(arhPtr->ar_size) - 1] = '\0';
877 size = (int)strtol(arhPtr->ar_size, NULL, 10);
878 fseek(arch, (size + 1) & ~1, SEEK_CUR);
883 * We've looked everywhere, but the member is not to be found. Close the
884 * archive and return NULL -- an error.
891 *-----------------------------------------------------------------------
893 * Touch a member of an archive.
896 * The 'time' field of the member's header is updated.
899 * The modification time of the entire archive is also changed.
900 * For a library, this could necessitate the re-ranlib'ing of the
903 *-----------------------------------------------------------------------
906 Arch_Touch(GNode *gn)
908 FILE * arch; /* Stream open to archive, positioned properly */
909 struct ar_hdr arh; /* Current header describing member */
912 arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
913 Var_Value(TARGET, gn, &p2),
917 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)now);
920 fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
926 *-----------------------------------------------------------------------
928 * Given a node which represents a library, touch the thing, making
929 * sure that the table of contents also is touched.
935 * Both the modification time of the library and of the RANLIBMAG
936 * member are set to 'now'.
938 *-----------------------------------------------------------------------
941 Arch_TouchLib(GNode *gn)
944 FILE * arch; /* Stream open to archive */
945 struct ar_hdr arh; /* Header describing table of contents */
946 struct utimbuf times; /* Times for utime() call */
948 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
949 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
952 fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
955 times.actime = times.modtime = now;
956 utime(gn->path, ×);
962 *-----------------------------------------------------------------------
964 * Return the modification time of a member of an archive, given its
968 * The modification time(seconds).
971 * The mtime field of the given node is filled in with the value
972 * returned by the function.
974 *-----------------------------------------------------------------------
977 Arch_MTime(GNode *gn)
979 struct ar_hdr *arhPtr; /* Header of desired member */
980 int modTime; /* Modification time as an integer */
983 arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
984 Var_Value(TARGET, gn, &p2),
989 if (arhPtr != NULL) {
990 modTime = (int)strtol(arhPtr->ar_date, NULL, 10);
1000 *-----------------------------------------------------------------------
1002 * Given a non-existent archive member's node, get its modification
1003 * time from its archived form, if it exists.
1006 * The modification time.
1009 * The mtime field is filled in.
1011 *-----------------------------------------------------------------------
1014 Arch_MemMTime(GNode *gn)
1021 if (Lst_Open(gn->parents) != SUCCESS) {
1025 while ((ln = Lst_Next(gn->parents)) != NULL) {
1026 pgn = (GNode *)Lst_Datum(ln);
1028 if (pgn->type & OP_ARCHV) {
1030 * If the parent is an archive specification and is being made
1031 * and its member's name matches the name of the node we were
1032 * given, record the modification time of the parent in the
1033 * child. We keep searching its parents in case some other
1034 * parent requires this child to exist...
1036 nameStart = strchr(pgn->name, '(') + 1;
1037 nameEnd = strchr(nameStart, ')');
1040 strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
1041 gn->mtime = Arch_MTime(pgn);
1043 } else if (pgn->make) {
1045 * Something which isn't a library depends on the existence of
1046 * this target, so it needs to exist.
1053 Lst_Close(gn->parents);
1059 *-----------------------------------------------------------------------
1061 * Search for a named library along the given search path.
1067 * The node's 'path' field is set to the found path (including the
1068 * actual file name, not -l...). If the system can handle the -L
1069 * flag when linking (or we cannot find the library), we assume that
1070 * the user has placed the .LIBRARIES variable in the final linking
1071 * command (or the linker will know where to find it) and set the
1072 * TARGET variable for this node to be the node's name. Otherwise,
1073 * we set the TARGET variable to be the full path of the library,
1074 * as returned by Dir_FindFile.
1076 *-----------------------------------------------------------------------
1079 Arch_FindLib(GNode *gn, Lst path)
1081 char *libName; /* file name for archive */
1084 sz = strlen(gn->name) + 4;
1085 libName = (char *)emalloc(sz);
1086 snprintf(libName, sz, "lib%s.a", &gn->name[2]);
1088 gn->path = Dir_FindFile(libName, path);
1093 Var_Set(TARGET, gn->name, gn);
1095 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
1096 #endif /* LIBRARIES */
1100 *-----------------------------------------------------------------------
1102 * Decide if a node with the OP_LIB attribute is out-of-date. Called
1103 * from Make_OODate to make its life easier, with the library's
1106 * There are several ways for a library to be out-of-date that are
1107 * not available to ordinary files. In addition, there are ways
1108 * that are open to regular files that are not available to
1109 * libraries. A library that is only used as a source is never
1110 * considered out-of-date by itself. This does not preclude the
1111 * library's modification time from making its parent be out-of-date.
1112 * A library will be considered out-of-date for any of these reasons,
1113 * given that it is a target on a dependency line somewhere:
1114 * Its modification time is less than that of one of its
1115 * sources (gn->mtime < gn->cmtime).
1116 * Its modification time is greater than the time at which the
1117 * make began (i.e. it's been modified in the course
1118 * of the make, probably by archiving).
1119 * The modification time of one of its sources is greater than
1120 * the one of its RANLIBMAG member (i.e. its table of contents
1121 * is out-of-date). We don't compare of the archive time
1122 * vs. TOC time because they can be too close. In my
1123 * opinion we should not bother with the TOC at all since
1124 * this is used by 'ar' rules that affect the data contents
1125 * of the archive, not by ranlib rules, which affect the
1129 * TRUE if the library is out-of-date. FALSE otherwise.
1132 * The library will be hashed if it hasn't been already.
1134 *-----------------------------------------------------------------------
1137 Arch_LibOODate(GNode *gn)
1141 if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
1143 } else if ((gn->mtime > now) || (gn->mtime < gn->cmtime)) {
1147 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
1148 int modTimeTOC; /* The table-of-contents's mod time */
1150 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1152 if (arhPtr != NULL) {
1153 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
1155 /* XXX choose one. */
1156 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1157 printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1159 oodate = (gn->cmtime > modTimeTOC);
1162 * A library w/o a table of contents is out-of-date
1164 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1165 printf("No t.o.c....");
1170 oodate = (gn->mtime == 0); /* out-of-date if not present */
1177 *-----------------------------------------------------------------------
1179 * Initialize things for this module.
1185 * The 'archives' list is initialized.
1187 *-----------------------------------------------------------------------
1192 archives = Lst_Init(FALSE);
1196 *-----------------------------------------------------------------------
1198 * Cleanup things for this module.
1204 * The 'archives' list is freed
1206 *-----------------------------------------------------------------------
1211 Lst_Destroy(archives, ArchFree);