1*2bc7c627SLionel Sambuc /* $NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $ */
22e2caf59SThomas Veerman
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman * Copyright (c) 1988, 1989, 1990, 1993
52e2caf59SThomas Veerman * The Regents of the University of California. All rights reserved.
62e2caf59SThomas Veerman *
72e2caf59SThomas Veerman * This code is derived from software contributed to Berkeley by
82e2caf59SThomas Veerman * Adam de Boor.
92e2caf59SThomas Veerman *
102e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without
112e2caf59SThomas Veerman * modification, are permitted provided that the following conditions
122e2caf59SThomas Veerman * are met:
132e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright
142e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer.
152e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
162e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the
172e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution.
182e2caf59SThomas Veerman * 3. Neither the name of the University nor the names of its contributors
192e2caf59SThomas Veerman * may be used to endorse or promote products derived from this software
202e2caf59SThomas Veerman * without specific prior written permission.
212e2caf59SThomas Veerman *
222e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232e2caf59SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242e2caf59SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252e2caf59SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262e2caf59SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272e2caf59SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282e2caf59SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292e2caf59SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302e2caf59SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312e2caf59SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322e2caf59SThomas Veerman * SUCH DAMAGE.
332e2caf59SThomas Veerman */
342e2caf59SThomas Veerman
352e2caf59SThomas Veerman /*
362e2caf59SThomas Veerman * Copyright (c) 1989 by Berkeley Softworks
372e2caf59SThomas Veerman * All rights reserved.
382e2caf59SThomas Veerman *
392e2caf59SThomas Veerman * This code is derived from software contributed to Berkeley by
402e2caf59SThomas Veerman * Adam de Boor.
412e2caf59SThomas Veerman *
422e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without
432e2caf59SThomas Veerman * modification, are permitted provided that the following conditions
442e2caf59SThomas Veerman * are met:
452e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright
462e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer.
472e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
482e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the
492e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution.
502e2caf59SThomas Veerman * 3. All advertising materials mentioning features or use of this software
512e2caf59SThomas Veerman * must display the following acknowledgement:
522e2caf59SThomas Veerman * This product includes software developed by the University of
532e2caf59SThomas Veerman * California, Berkeley and its contributors.
542e2caf59SThomas Veerman * 4. Neither the name of the University nor the names of its contributors
552e2caf59SThomas Veerman * may be used to endorse or promote products derived from this software
562e2caf59SThomas Veerman * without specific prior written permission.
572e2caf59SThomas Veerman *
582e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
592e2caf59SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
602e2caf59SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
612e2caf59SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
622e2caf59SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
632e2caf59SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
642e2caf59SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
652e2caf59SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
662e2caf59SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
672e2caf59SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
682e2caf59SThomas Veerman * SUCH DAMAGE.
692e2caf59SThomas Veerman */
702e2caf59SThomas Veerman
712e2caf59SThomas Veerman #ifndef MAKE_NATIVE
72*2bc7c627SLionel Sambuc static char rcsid[] = "$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $";
732e2caf59SThomas Veerman #else
742e2caf59SThomas Veerman #include <sys/cdefs.h>
752e2caf59SThomas Veerman #ifndef lint
762e2caf59SThomas Veerman #if 0
772e2caf59SThomas Veerman static char sccsid[] = "@(#)arch.c 8.2 (Berkeley) 1/2/94";
782e2caf59SThomas Veerman #else
79*2bc7c627SLionel Sambuc __RCSID("$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $");
802e2caf59SThomas Veerman #endif
812e2caf59SThomas Veerman #endif /* not lint */
822e2caf59SThomas Veerman #endif
832e2caf59SThomas Veerman
842e2caf59SThomas Veerman /*-
852e2caf59SThomas Veerman * arch.c --
862e2caf59SThomas Veerman * Functions to manipulate libraries, archives and their members.
872e2caf59SThomas Veerman *
882e2caf59SThomas Veerman * Once again, cacheing/hashing comes into play in the manipulation
892e2caf59SThomas Veerman * of archives. The first time an archive is referenced, all of its members'
902e2caf59SThomas Veerman * headers are read and hashed and the archive closed again. All hashed
912e2caf59SThomas Veerman * archives are kept on a list which is searched each time an archive member
922e2caf59SThomas Veerman * is referenced.
932e2caf59SThomas Veerman *
942e2caf59SThomas Veerman * The interface to this module is:
952e2caf59SThomas Veerman * Arch_ParseArchive Given an archive specification, return a list
962e2caf59SThomas Veerman * of GNode's, one for each member in the spec.
972e2caf59SThomas Veerman * FAILURE is returned if the specification is
982e2caf59SThomas Veerman * invalid for some reason.
992e2caf59SThomas Veerman *
1002e2caf59SThomas Veerman * Arch_Touch Alter the modification time of the archive
1012e2caf59SThomas Veerman * member described by the given node to be
1022e2caf59SThomas Veerman * the current time.
1032e2caf59SThomas Veerman *
1042e2caf59SThomas Veerman * Arch_TouchLib Update the modification time of the library
1052e2caf59SThomas Veerman * described by the given node. This is special
1062e2caf59SThomas Veerman * because it also updates the modification time
1072e2caf59SThomas Veerman * of the library's table of contents.
1082e2caf59SThomas Veerman *
1092e2caf59SThomas Veerman * Arch_MTime Find the modification time of a member of
1102e2caf59SThomas Veerman * an archive *in the archive*. The time is also
1112e2caf59SThomas Veerman * placed in the member's GNode. Returns the
1122e2caf59SThomas Veerman * modification time.
1132e2caf59SThomas Veerman *
1142e2caf59SThomas Veerman * Arch_MemTime Find the modification time of a member of
1152e2caf59SThomas Veerman * an archive. Called when the member doesn't
1162e2caf59SThomas Veerman * already exist. Looks in the archive for the
1172e2caf59SThomas Veerman * modification time. Returns the modification
1182e2caf59SThomas Veerman * time.
1192e2caf59SThomas Veerman *
1202e2caf59SThomas Veerman * Arch_FindLib Search for a library along a path. The
1212e2caf59SThomas Veerman * library name in the GNode should be in
1222e2caf59SThomas Veerman * -l<name> format.
1232e2caf59SThomas Veerman *
1242e2caf59SThomas Veerman * Arch_LibOODate Special function to decide if a library node
1252e2caf59SThomas Veerman * is out-of-date.
1262e2caf59SThomas Veerman *
1272e2caf59SThomas Veerman * Arch_Init Initialize this module.
1282e2caf59SThomas Veerman *
1292e2caf59SThomas Veerman * Arch_End Cleanup this module.
1302e2caf59SThomas Veerman */
1312e2caf59SThomas Veerman
1322e2caf59SThomas Veerman #include <sys/types.h>
1332e2caf59SThomas Veerman #include <sys/stat.h>
1342e2caf59SThomas Veerman #include <sys/time.h>
1352e2caf59SThomas Veerman #include <sys/param.h>
1362e2caf59SThomas Veerman
1372e2caf59SThomas Veerman #include <ar.h>
1382e2caf59SThomas Veerman #include <ctype.h>
1392e2caf59SThomas Veerman #include <fcntl.h>
1402e2caf59SThomas Veerman #include <stdio.h>
1412e2caf59SThomas Veerman #include <stdlib.h>
1422e2caf59SThomas Veerman #include <utime.h>
1432e2caf59SThomas Veerman
1442e2caf59SThomas Veerman #include "make.h"
1452e2caf59SThomas Veerman #include "hash.h"
1462e2caf59SThomas Veerman #include "dir.h"
1472e2caf59SThomas Veerman #include "config.h"
1482e2caf59SThomas Veerman
1492e2caf59SThomas Veerman #ifdef TARGET_MACHINE
1502e2caf59SThomas Veerman #undef MAKE_MACHINE
1512e2caf59SThomas Veerman #define MAKE_MACHINE TARGET_MACHINE
1522e2caf59SThomas Veerman #endif
1532e2caf59SThomas Veerman #ifdef TARGET_MACHINE_ARCH
1542e2caf59SThomas Veerman #undef MAKE_MACHINE_ARCH
1552e2caf59SThomas Veerman #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
1562e2caf59SThomas Veerman #endif
1572e2caf59SThomas Veerman
1582e2caf59SThomas Veerman static Lst archives; /* Lst of archives we've already examined */
1592e2caf59SThomas Veerman
1602e2caf59SThomas Veerman typedef struct Arch {
1612e2caf59SThomas Veerman char *name; /* Name of archive */
1622e2caf59SThomas Veerman Hash_Table members; /* All the members of the archive described
1632e2caf59SThomas Veerman * by <name, struct ar_hdr *> key/value pairs */
1642e2caf59SThomas Veerman char *fnametab; /* Extended name table strings */
1652e2caf59SThomas Veerman size_t fnamesize; /* Size of the string table */
1662e2caf59SThomas Veerman } Arch;
1672e2caf59SThomas Veerman
1682e2caf59SThomas Veerman static int ArchFindArchive(const void *, const void *);
1692e2caf59SThomas Veerman #ifdef CLEANUP
1702e2caf59SThomas Veerman static void ArchFree(void *);
1712e2caf59SThomas Veerman #endif
1722e2caf59SThomas Veerman static struct ar_hdr *ArchStatMember(char *, char *, Boolean);
1732e2caf59SThomas Veerman static FILE *ArchFindMember(char *, char *, struct ar_hdr *, const char *);
1742e2caf59SThomas Veerman #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
1752e2caf59SThomas Veerman #define SVR4ARCHIVES
1762e2caf59SThomas Veerman static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
1772e2caf59SThomas Veerman #endif
1782e2caf59SThomas Veerman
1792e2caf59SThomas Veerman #ifdef CLEANUP
1802e2caf59SThomas Veerman /*-
1812e2caf59SThomas Veerman *-----------------------------------------------------------------------
1822e2caf59SThomas Veerman * ArchFree --
1832e2caf59SThomas Veerman * Free memory used by an archive
1842e2caf59SThomas Veerman *
1852e2caf59SThomas Veerman * Results:
1862e2caf59SThomas Veerman * None.
1872e2caf59SThomas Veerman *
1882e2caf59SThomas Veerman * Side Effects:
1892e2caf59SThomas Veerman * None.
1902e2caf59SThomas Veerman *
1912e2caf59SThomas Veerman *-----------------------------------------------------------------------
1922e2caf59SThomas Veerman */
1932e2caf59SThomas Veerman static void
ArchFree(void * ap)1942e2caf59SThomas Veerman ArchFree(void *ap)
1952e2caf59SThomas Veerman {
1962e2caf59SThomas Veerman Arch *a = (Arch *)ap;
1972e2caf59SThomas Veerman Hash_Search search;
1982e2caf59SThomas Veerman Hash_Entry *entry;
1992e2caf59SThomas Veerman
2002e2caf59SThomas Veerman /* Free memory from hash entries */
2012e2caf59SThomas Veerman for (entry = Hash_EnumFirst(&a->members, &search);
2022e2caf59SThomas Veerman entry != NULL;
2032e2caf59SThomas Veerman entry = Hash_EnumNext(&search))
2042e2caf59SThomas Veerman free(Hash_GetValue(entry));
2052e2caf59SThomas Veerman
2062e2caf59SThomas Veerman free(a->name);
2072e2caf59SThomas Veerman if (a->fnametab)
2082e2caf59SThomas Veerman free(a->fnametab);
2092e2caf59SThomas Veerman Hash_DeleteTable(&a->members);
2102e2caf59SThomas Veerman free(a);
2112e2caf59SThomas Veerman }
2122e2caf59SThomas Veerman #endif
2132e2caf59SThomas Veerman
2142e2caf59SThomas Veerman
2152e2caf59SThomas Veerman
2162e2caf59SThomas Veerman /*-
2172e2caf59SThomas Veerman *-----------------------------------------------------------------------
2182e2caf59SThomas Veerman * Arch_ParseArchive --
2192e2caf59SThomas Veerman * Parse the archive specification in the given line and find/create
2202e2caf59SThomas Veerman * the nodes for the specified archive members, placing their nodes
2212e2caf59SThomas Veerman * on the given list.
2222e2caf59SThomas Veerman *
2232e2caf59SThomas Veerman * Input:
2242e2caf59SThomas Veerman * linePtr Pointer to start of specification
2252e2caf59SThomas Veerman * nodeLst Lst on which to place the nodes
2262e2caf59SThomas Veerman * ctxt Context in which to expand variables
2272e2caf59SThomas Veerman *
2282e2caf59SThomas Veerman * Results:
2292e2caf59SThomas Veerman * SUCCESS if it was a valid specification. The linePtr is updated
2302e2caf59SThomas Veerman * to point to the first non-space after the archive spec. The
2312e2caf59SThomas Veerman * nodes for the members are placed on the given list.
2322e2caf59SThomas Veerman *
2332e2caf59SThomas Veerman * Side Effects:
2342e2caf59SThomas Veerman * Some nodes may be created. The given list is extended.
2352e2caf59SThomas Veerman *
2362e2caf59SThomas Veerman *-----------------------------------------------------------------------
2372e2caf59SThomas Veerman */
2382e2caf59SThomas Veerman ReturnStatus
Arch_ParseArchive(char ** linePtr,Lst nodeLst,GNode * ctxt)2392e2caf59SThomas Veerman Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
2402e2caf59SThomas Veerman {
2412e2caf59SThomas Veerman char *cp; /* Pointer into line */
2422e2caf59SThomas Veerman GNode *gn; /* New node */
2432e2caf59SThomas Veerman char *libName; /* Library-part of specification */
2442e2caf59SThomas Veerman char *memName; /* Member-part of specification */
2452e2caf59SThomas Veerman char *nameBuf; /* temporary place for node name */
2462e2caf59SThomas Veerman char saveChar; /* Ending delimiter of member-name */
2472e2caf59SThomas Veerman Boolean subLibName; /* TRUE if libName should have/had
2482e2caf59SThomas Veerman * variable substitution performed on it */
2492e2caf59SThomas Veerman
2502e2caf59SThomas Veerman libName = *linePtr;
2512e2caf59SThomas Veerman
2522e2caf59SThomas Veerman subLibName = FALSE;
2532e2caf59SThomas Veerman
2542e2caf59SThomas Veerman for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
2552e2caf59SThomas Veerman if (*cp == '$') {
2562e2caf59SThomas Veerman /*
2572e2caf59SThomas Veerman * Variable spec, so call the Var module to parse the puppy
2582e2caf59SThomas Veerman * so we can safely advance beyond it...
2592e2caf59SThomas Veerman */
2602e2caf59SThomas Veerman int length;
2612e2caf59SThomas Veerman void *freeIt;
2622e2caf59SThomas Veerman char *result;
2632e2caf59SThomas Veerman
2642e2caf59SThomas Veerman result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
2652e2caf59SThomas Veerman if (freeIt)
2662e2caf59SThomas Veerman free(freeIt);
2672e2caf59SThomas Veerman if (result == var_Error) {
2682e2caf59SThomas Veerman return(FAILURE);
2692e2caf59SThomas Veerman } else {
2702e2caf59SThomas Veerman subLibName = TRUE;
2712e2caf59SThomas Veerman }
2722e2caf59SThomas Veerman
2732e2caf59SThomas Veerman cp += length-1;
2742e2caf59SThomas Veerman }
2752e2caf59SThomas Veerman }
2762e2caf59SThomas Veerman
2772e2caf59SThomas Veerman *cp++ = '\0';
2782e2caf59SThomas Veerman if (subLibName) {
2792e2caf59SThomas Veerman libName = Var_Subst(NULL, libName, ctxt, TRUE);
2802e2caf59SThomas Veerman }
2812e2caf59SThomas Veerman
2822e2caf59SThomas Veerman
2832e2caf59SThomas Veerman for (;;) {
2842e2caf59SThomas Veerman /*
2852e2caf59SThomas Veerman * First skip to the start of the member's name, mark that
2862e2caf59SThomas Veerman * place and skip to the end of it (either white-space or
2872e2caf59SThomas Veerman * a close paren).
2882e2caf59SThomas Veerman */
2892e2caf59SThomas Veerman Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
2902e2caf59SThomas Veerman
2912e2caf59SThomas Veerman while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) {
2922e2caf59SThomas Veerman cp++;
2932e2caf59SThomas Veerman }
2942e2caf59SThomas Veerman memName = cp;
2952e2caf59SThomas Veerman while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) {
2962e2caf59SThomas Veerman if (*cp == '$') {
2972e2caf59SThomas Veerman /*
2982e2caf59SThomas Veerman * Variable spec, so call the Var module to parse the puppy
2992e2caf59SThomas Veerman * so we can safely advance beyond it...
3002e2caf59SThomas Veerman */
3012e2caf59SThomas Veerman int length;
3022e2caf59SThomas Veerman void *freeIt;
3032e2caf59SThomas Veerman char *result;
3042e2caf59SThomas Veerman
3052e2caf59SThomas Veerman result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
3062e2caf59SThomas Veerman if (freeIt)
3072e2caf59SThomas Veerman free(freeIt);
3082e2caf59SThomas Veerman if (result == var_Error) {
3092e2caf59SThomas Veerman return(FAILURE);
3102e2caf59SThomas Veerman } else {
3112e2caf59SThomas Veerman doSubst = TRUE;
3122e2caf59SThomas Veerman }
3132e2caf59SThomas Veerman
3142e2caf59SThomas Veerman cp += length;
3152e2caf59SThomas Veerman } else {
3162e2caf59SThomas Veerman cp++;
3172e2caf59SThomas Veerman }
3182e2caf59SThomas Veerman }
3192e2caf59SThomas Veerman
3202e2caf59SThomas Veerman /*
3212e2caf59SThomas Veerman * If the specification ends without a closing parenthesis,
3222e2caf59SThomas Veerman * chances are there's something wrong (like a missing backslash),
3232e2caf59SThomas Veerman * so it's better to return failure than allow such things to happen
3242e2caf59SThomas Veerman */
3252e2caf59SThomas Veerman if (*cp == '\0') {
3262e2caf59SThomas Veerman printf("No closing parenthesis in archive specification\n");
3272e2caf59SThomas Veerman return (FAILURE);
3282e2caf59SThomas Veerman }
3292e2caf59SThomas Veerman
3302e2caf59SThomas Veerman /*
3312e2caf59SThomas Veerman * If we didn't move anywhere, we must be done
3322e2caf59SThomas Veerman */
3332e2caf59SThomas Veerman if (cp == memName) {
3342e2caf59SThomas Veerman break;
3352e2caf59SThomas Veerman }
3362e2caf59SThomas Veerman
3372e2caf59SThomas Veerman saveChar = *cp;
3382e2caf59SThomas Veerman *cp = '\0';
3392e2caf59SThomas Veerman
3402e2caf59SThomas Veerman /*
3412e2caf59SThomas Veerman * XXX: This should be taken care of intelligently by
3422e2caf59SThomas Veerman * SuffExpandChildren, both for the archive and the member portions.
3432e2caf59SThomas Veerman */
3442e2caf59SThomas Veerman /*
3452e2caf59SThomas Veerman * If member contains variables, try and substitute for them.
3462e2caf59SThomas Veerman * This will slow down archive specs with dynamic sources, of course,
3472e2caf59SThomas Veerman * since we'll be (non-)substituting them three times, but them's
3482e2caf59SThomas Veerman * the breaks -- we need to do this since SuffExpandChildren calls
3492e2caf59SThomas Veerman * us, otherwise we could assume the thing would be taken care of
3502e2caf59SThomas Veerman * later.
3512e2caf59SThomas Veerman */
3522e2caf59SThomas Veerman if (doSubst) {
3532e2caf59SThomas Veerman char *buf;
3542e2caf59SThomas Veerman char *sacrifice;
3552e2caf59SThomas Veerman char *oldMemName = memName;
3562e2caf59SThomas Veerman size_t sz;
3572e2caf59SThomas Veerman
3582e2caf59SThomas Veerman memName = Var_Subst(NULL, memName, ctxt, TRUE);
3592e2caf59SThomas Veerman
3602e2caf59SThomas Veerman /*
3612e2caf59SThomas Veerman * Now form an archive spec and recurse to deal with nested
3622e2caf59SThomas Veerman * variables and multi-word variable values.... The results
3632e2caf59SThomas Veerman * are just placed at the end of the nodeLst we're returning.
3642e2caf59SThomas Veerman */
3652e2caf59SThomas Veerman sz = strlen(memName)+strlen(libName)+3;
3662e2caf59SThomas Veerman buf = sacrifice = bmake_malloc(sz);
3672e2caf59SThomas Veerman
3682e2caf59SThomas Veerman snprintf(buf, sz, "%s(%s)", libName, memName);
3692e2caf59SThomas Veerman
3702e2caf59SThomas Veerman if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
3712e2caf59SThomas Veerman /*
3722e2caf59SThomas Veerman * Must contain dynamic sources, so we can't deal with it now.
3732e2caf59SThomas Veerman * Just create an ARCHV node for the thing and let
3742e2caf59SThomas Veerman * SuffExpandChildren handle it...
3752e2caf59SThomas Veerman */
3762e2caf59SThomas Veerman gn = Targ_FindNode(buf, TARG_CREATE);
3772e2caf59SThomas Veerman
3782e2caf59SThomas Veerman if (gn == NULL) {
3792e2caf59SThomas Veerman free(buf);
3802e2caf59SThomas Veerman return(FAILURE);
3812e2caf59SThomas Veerman } else {
3822e2caf59SThomas Veerman gn->type |= OP_ARCHV;
3832e2caf59SThomas Veerman (void)Lst_AtEnd(nodeLst, gn);
3842e2caf59SThomas Veerman }
3852e2caf59SThomas Veerman } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
3862e2caf59SThomas Veerman /*
3872e2caf59SThomas Veerman * Error in nested call -- free buffer and return FAILURE
3882e2caf59SThomas Veerman * ourselves.
3892e2caf59SThomas Veerman */
3902e2caf59SThomas Veerman free(buf);
3912e2caf59SThomas Veerman return(FAILURE);
3922e2caf59SThomas Veerman }
3932e2caf59SThomas Veerman /*
3942e2caf59SThomas Veerman * Free buffer and continue with our work.
3952e2caf59SThomas Veerman */
3962e2caf59SThomas Veerman free(buf);
3972e2caf59SThomas Veerman } else if (Dir_HasWildcards(memName)) {
3982e2caf59SThomas Veerman Lst members = Lst_Init(FALSE);
3992e2caf59SThomas Veerman char *member;
4002e2caf59SThomas Veerman size_t sz = MAXPATHLEN, nsz;
4012e2caf59SThomas Veerman nameBuf = bmake_malloc(sz);
4022e2caf59SThomas Veerman
4032e2caf59SThomas Veerman Dir_Expand(memName, dirSearchPath, members);
4042e2caf59SThomas Veerman while (!Lst_IsEmpty(members)) {
4052e2caf59SThomas Veerman member = (char *)Lst_DeQueue(members);
4062e2caf59SThomas Veerman nsz = strlen(libName) + strlen(member) + 3;
4072e2caf59SThomas Veerman if (sz > nsz)
4082e2caf59SThomas Veerman nameBuf = bmake_realloc(nameBuf, sz = nsz * 2);
4092e2caf59SThomas Veerman
4102e2caf59SThomas Veerman snprintf(nameBuf, sz, "%s(%s)", libName, member);
4112e2caf59SThomas Veerman free(member);
4122e2caf59SThomas Veerman gn = Targ_FindNode(nameBuf, TARG_CREATE);
4132e2caf59SThomas Veerman if (gn == NULL) {
4142e2caf59SThomas Veerman free(nameBuf);
4152e2caf59SThomas Veerman return (FAILURE);
4162e2caf59SThomas Veerman } else {
4172e2caf59SThomas Veerman /*
4182e2caf59SThomas Veerman * We've found the node, but have to make sure the rest of
4192e2caf59SThomas Veerman * the world knows it's an archive member, without having
4202e2caf59SThomas Veerman * to constantly check for parentheses, so we type the
4212e2caf59SThomas Veerman * thing with the OP_ARCHV bit before we place it on the
4222e2caf59SThomas Veerman * end of the provided list.
4232e2caf59SThomas Veerman */
4242e2caf59SThomas Veerman gn->type |= OP_ARCHV;
4252e2caf59SThomas Veerman (void)Lst_AtEnd(nodeLst, gn);
4262e2caf59SThomas Veerman }
4272e2caf59SThomas Veerman }
4282e2caf59SThomas Veerman Lst_Destroy(members, NULL);
4292e2caf59SThomas Veerman free(nameBuf);
4302e2caf59SThomas Veerman } else {
4312e2caf59SThomas Veerman size_t sz = strlen(libName) + strlen(memName) + 3;
4322e2caf59SThomas Veerman nameBuf = bmake_malloc(sz);
4332e2caf59SThomas Veerman snprintf(nameBuf, sz, "%s(%s)", libName, memName);
4342e2caf59SThomas Veerman gn = Targ_FindNode(nameBuf, TARG_CREATE);
4352e2caf59SThomas Veerman free(nameBuf);
4362e2caf59SThomas Veerman if (gn == NULL) {
4372e2caf59SThomas Veerman return (FAILURE);
4382e2caf59SThomas Veerman } else {
4392e2caf59SThomas Veerman /*
4402e2caf59SThomas Veerman * We've found the node, but have to make sure the rest of the
4412e2caf59SThomas Veerman * world knows it's an archive member, without having to
4422e2caf59SThomas Veerman * constantly check for parentheses, so we type the thing with
4432e2caf59SThomas Veerman * the OP_ARCHV bit before we place it on the end of the
4442e2caf59SThomas Veerman * provided list.
4452e2caf59SThomas Veerman */
4462e2caf59SThomas Veerman gn->type |= OP_ARCHV;
4472e2caf59SThomas Veerman (void)Lst_AtEnd(nodeLst, gn);
4482e2caf59SThomas Veerman }
4492e2caf59SThomas Veerman }
4502e2caf59SThomas Veerman if (doSubst) {
4512e2caf59SThomas Veerman free(memName);
4522e2caf59SThomas Veerman }
4532e2caf59SThomas Veerman
4542e2caf59SThomas Veerman *cp = saveChar;
4552e2caf59SThomas Veerman }
4562e2caf59SThomas Veerman
4572e2caf59SThomas Veerman /*
4582e2caf59SThomas Veerman * If substituted libName, free it now, since we need it no longer.
4592e2caf59SThomas Veerman */
4602e2caf59SThomas Veerman if (subLibName) {
4612e2caf59SThomas Veerman free(libName);
4622e2caf59SThomas Veerman }
4632e2caf59SThomas Veerman
4642e2caf59SThomas Veerman /*
4652e2caf59SThomas Veerman * We promised the pointer would be set up at the next non-space, so
4662e2caf59SThomas Veerman * we must advance cp there before setting *linePtr... (note that on
4672e2caf59SThomas Veerman * entrance to the loop, cp is guaranteed to point at a ')')
4682e2caf59SThomas Veerman */
4692e2caf59SThomas Veerman do {
4702e2caf59SThomas Veerman cp++;
4712e2caf59SThomas Veerman } while (*cp != '\0' && isspace ((unsigned char)*cp));
4722e2caf59SThomas Veerman
4732e2caf59SThomas Veerman *linePtr = cp;
4742e2caf59SThomas Veerman return (SUCCESS);
4752e2caf59SThomas Veerman }
4762e2caf59SThomas Veerman
4772e2caf59SThomas Veerman /*-
4782e2caf59SThomas Veerman *-----------------------------------------------------------------------
4792e2caf59SThomas Veerman * ArchFindArchive --
4802e2caf59SThomas Veerman * See if the given archive is the one we are looking for. Called
4812e2caf59SThomas Veerman * From ArchStatMember and ArchFindMember via Lst_Find.
4822e2caf59SThomas Veerman *
4832e2caf59SThomas Veerman * Input:
4842e2caf59SThomas Veerman * ar Current list element
4852e2caf59SThomas Veerman * archName Name we want
4862e2caf59SThomas Veerman *
4872e2caf59SThomas Veerman * Results:
4882e2caf59SThomas Veerman * 0 if it is, non-zero if it isn't.
4892e2caf59SThomas Veerman *
4902e2caf59SThomas Veerman * Side Effects:
4912e2caf59SThomas Veerman * None.
4922e2caf59SThomas Veerman *
4932e2caf59SThomas Veerman *-----------------------------------------------------------------------
4942e2caf59SThomas Veerman */
4952e2caf59SThomas Veerman static int
ArchFindArchive(const void * ar,const void * archName)4962e2caf59SThomas Veerman ArchFindArchive(const void *ar, const void *archName)
4972e2caf59SThomas Veerman {
4982e2caf59SThomas Veerman return (strcmp(archName, ((const Arch *)ar)->name));
4992e2caf59SThomas Veerman }
5002e2caf59SThomas Veerman
5012e2caf59SThomas Veerman /*-
5022e2caf59SThomas Veerman *-----------------------------------------------------------------------
5032e2caf59SThomas Veerman * ArchStatMember --
5042e2caf59SThomas Veerman * Locate a member of an archive, given the path of the archive and
5052e2caf59SThomas Veerman * the path of the desired member.
5062e2caf59SThomas Veerman *
5072e2caf59SThomas Veerman * Input:
5082e2caf59SThomas Veerman * archive Path to the archive
5092e2caf59SThomas Veerman * member Name of member. If it is a path, only the last
5102e2caf59SThomas Veerman * component is used.
5112e2caf59SThomas Veerman * hash TRUE if archive should be hashed if not already so.
5122e2caf59SThomas Veerman *
5132e2caf59SThomas Veerman * Results:
5142e2caf59SThomas Veerman * A pointer to the current struct ar_hdr structure for the member. Note
5152e2caf59SThomas Veerman * That no position is returned, so this is not useful for touching
5162e2caf59SThomas Veerman * archive members. This is mostly because we have no assurances that
5172e2caf59SThomas Veerman * The archive will remain constant after we read all the headers, so
5182e2caf59SThomas Veerman * there's not much point in remembering the position...
5192e2caf59SThomas Veerman *
5202e2caf59SThomas Veerman * Side Effects:
5212e2caf59SThomas Veerman *
5222e2caf59SThomas Veerman *-----------------------------------------------------------------------
5232e2caf59SThomas Veerman */
5242e2caf59SThomas Veerman static struct ar_hdr *
ArchStatMember(char * archive,char * member,Boolean hash)5252e2caf59SThomas Veerman ArchStatMember(char *archive, char *member, Boolean hash)
5262e2caf59SThomas Veerman {
5272e2caf59SThomas Veerman #define AR_MAX_NAME_LEN (sizeof(arh.ar_name)-1)
5282e2caf59SThomas Veerman FILE * arch; /* Stream to archive */
5292e2caf59SThomas Veerman int size; /* Size of archive member */
5302e2caf59SThomas Veerman char *cp; /* Useful character pointer */
5312e2caf59SThomas Veerman char magic[SARMAG];
5322e2caf59SThomas Veerman LstNode ln; /* Lst member containing archive descriptor */
5332e2caf59SThomas Veerman Arch *ar; /* Archive descriptor */
5342e2caf59SThomas Veerman Hash_Entry *he; /* Entry containing member's description */
5352e2caf59SThomas Veerman struct ar_hdr arh; /* archive-member header for reading archive */
5362e2caf59SThomas Veerman char memName[MAXPATHLEN+1];
5372e2caf59SThomas Veerman /* Current member name while hashing. */
5382e2caf59SThomas Veerman
5392e2caf59SThomas Veerman /*
5402e2caf59SThomas Veerman * Because of space constraints and similar things, files are archived
5412e2caf59SThomas Veerman * using their final path components, not the entire thing, so we need
5422e2caf59SThomas Veerman * to point 'member' to the final component, if there is one, to make
5432e2caf59SThomas Veerman * the comparisons easier...
5442e2caf59SThomas Veerman */
5452e2caf59SThomas Veerman cp = strrchr(member, '/');
5462e2caf59SThomas Veerman if (cp != NULL) {
5472e2caf59SThomas Veerman member = cp + 1;
5482e2caf59SThomas Veerman }
5492e2caf59SThomas Veerman
5502e2caf59SThomas Veerman ln = Lst_Find(archives, archive, ArchFindArchive);
5512e2caf59SThomas Veerman if (ln != NULL) {
5522e2caf59SThomas Veerman ar = (Arch *)Lst_Datum(ln);
5532e2caf59SThomas Veerman
5542e2caf59SThomas Veerman he = Hash_FindEntry(&ar->members, member);
5552e2caf59SThomas Veerman
5562e2caf59SThomas Veerman if (he != NULL) {
5572e2caf59SThomas Veerman return ((struct ar_hdr *)Hash_GetValue(he));
5582e2caf59SThomas Veerman } else {
5592e2caf59SThomas Veerman /* Try truncated name */
5602e2caf59SThomas Veerman char copy[AR_MAX_NAME_LEN+1];
5612e2caf59SThomas Veerman size_t len = strlen(member);
5622e2caf59SThomas Veerman
5632e2caf59SThomas Veerman if (len > AR_MAX_NAME_LEN) {
5642e2caf59SThomas Veerman len = AR_MAX_NAME_LEN;
5652e2caf59SThomas Veerman strncpy(copy, member, AR_MAX_NAME_LEN);
5662e2caf59SThomas Veerman copy[AR_MAX_NAME_LEN] = '\0';
5672e2caf59SThomas Veerman }
5682e2caf59SThomas Veerman if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
5692e2caf59SThomas Veerman return ((struct ar_hdr *)Hash_GetValue(he));
5702e2caf59SThomas Veerman return NULL;
5712e2caf59SThomas Veerman }
5722e2caf59SThomas Veerman }
5732e2caf59SThomas Veerman
5742e2caf59SThomas Veerman if (!hash) {
5752e2caf59SThomas Veerman /*
5762e2caf59SThomas Veerman * Caller doesn't want the thing hashed, just use ArchFindMember
5772e2caf59SThomas Veerman * to read the header for the member out and close down the stream
5782e2caf59SThomas Veerman * again. Since the archive is not to be hashed, we assume there's
5792e2caf59SThomas Veerman * no need to allocate extra room for the header we're returning,
5802e2caf59SThomas Veerman * so just declare it static.
5812e2caf59SThomas Veerman */
5822e2caf59SThomas Veerman static struct ar_hdr sarh;
5832e2caf59SThomas Veerman
5842e2caf59SThomas Veerman arch = ArchFindMember(archive, member, &sarh, "r");
5852e2caf59SThomas Veerman
5862e2caf59SThomas Veerman if (arch == NULL) {
5872e2caf59SThomas Veerman return NULL;
5882e2caf59SThomas Veerman } else {
5892e2caf59SThomas Veerman fclose(arch);
5902e2caf59SThomas Veerman return (&sarh);
5912e2caf59SThomas Veerman }
5922e2caf59SThomas Veerman }
5932e2caf59SThomas Veerman
5942e2caf59SThomas Veerman /*
5952e2caf59SThomas Veerman * We don't have this archive on the list yet, so we want to find out
5962e2caf59SThomas Veerman * everything that's in it and cache it so we can get at it quickly.
5972e2caf59SThomas Veerman */
5982e2caf59SThomas Veerman arch = fopen(archive, "r");
5992e2caf59SThomas Veerman if (arch == NULL) {
6002e2caf59SThomas Veerman return NULL;
6012e2caf59SThomas Veerman }
6022e2caf59SThomas Veerman
6032e2caf59SThomas Veerman /*
6042e2caf59SThomas Veerman * We use the ARMAG string to make sure this is an archive we
6052e2caf59SThomas Veerman * can handle...
6062e2caf59SThomas Veerman */
6072e2caf59SThomas Veerman if ((fread(magic, SARMAG, 1, arch) != 1) ||
6082e2caf59SThomas Veerman (strncmp(magic, ARMAG, SARMAG) != 0)) {
6092e2caf59SThomas Veerman fclose(arch);
6102e2caf59SThomas Veerman return NULL;
6112e2caf59SThomas Veerman }
6122e2caf59SThomas Veerman
6132e2caf59SThomas Veerman ar = bmake_malloc(sizeof(Arch));
6142e2caf59SThomas Veerman ar->name = bmake_strdup(archive);
6152e2caf59SThomas Veerman ar->fnametab = NULL;
6162e2caf59SThomas Veerman ar->fnamesize = 0;
6172e2caf59SThomas Veerman Hash_InitTable(&ar->members, -1);
6182e2caf59SThomas Veerman memName[AR_MAX_NAME_LEN] = '\0';
6192e2caf59SThomas Veerman
6202e2caf59SThomas Veerman while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
6212e2caf59SThomas Veerman if (strncmp( arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
6222e2caf59SThomas Veerman /*
6232e2caf59SThomas Veerman * The header is bogus, so the archive is bad
6242e2caf59SThomas Veerman * and there's no way we can recover...
6252e2caf59SThomas Veerman */
6262e2caf59SThomas Veerman goto badarch;
6272e2caf59SThomas Veerman } else {
6282e2caf59SThomas Veerman /*
6292e2caf59SThomas Veerman * We need to advance the stream's pointer to the start of the
6302e2caf59SThomas Veerman * next header. Files are padded with newlines to an even-byte
6312e2caf59SThomas Veerman * boundary, so we need to extract the size of the file from the
6322e2caf59SThomas Veerman * 'size' field of the header and round it up during the seek.
6332e2caf59SThomas Veerman */
6342e2caf59SThomas Veerman arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
6352e2caf59SThomas Veerman size = (int)strtol(arh.ar_size, NULL, 10);
6362e2caf59SThomas Veerman
6372e2caf59SThomas Veerman (void)strncpy(memName, arh.ar_name, sizeof(arh.ar_name));
6382e2caf59SThomas Veerman for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
6392e2caf59SThomas Veerman continue;
6402e2caf59SThomas Veerman }
6412e2caf59SThomas Veerman cp[1] = '\0';
6422e2caf59SThomas Veerman
6432e2caf59SThomas Veerman #ifdef SVR4ARCHIVES
6442e2caf59SThomas Veerman /*
6452e2caf59SThomas Veerman * svr4 names are slash terminated. Also svr4 extended AR format.
6462e2caf59SThomas Veerman */
6472e2caf59SThomas Veerman if (memName[0] == '/') {
6482e2caf59SThomas Veerman /*
6492e2caf59SThomas Veerman * svr4 magic mode; handle it
6502e2caf59SThomas Veerman */
6512e2caf59SThomas Veerman switch (ArchSVR4Entry(ar, memName, size, arch)) {
6522e2caf59SThomas Veerman case -1: /* Invalid data */
6532e2caf59SThomas Veerman goto badarch;
6542e2caf59SThomas Veerman case 0: /* List of files entry */
6552e2caf59SThomas Veerman continue;
6562e2caf59SThomas Veerman default: /* Got the entry */
6572e2caf59SThomas Veerman break;
6582e2caf59SThomas Veerman }
6592e2caf59SThomas Veerman }
6602e2caf59SThomas Veerman else {
6612e2caf59SThomas Veerman if (cp[0] == '/')
6622e2caf59SThomas Veerman cp[0] = '\0';
6632e2caf59SThomas Veerman }
6642e2caf59SThomas Veerman #endif
6652e2caf59SThomas Veerman
6662e2caf59SThomas Veerman #ifdef AR_EFMT1
6672e2caf59SThomas Veerman /*
6682e2caf59SThomas Veerman * BSD 4.4 extended AR format: #1/<namelen>, with name as the
6692e2caf59SThomas Veerman * first <namelen> bytes of the file
6702e2caf59SThomas Veerman */
6712e2caf59SThomas Veerman if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
6722e2caf59SThomas Veerman isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) {
6732e2caf59SThomas Veerman
6742e2caf59SThomas Veerman unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
6752e2caf59SThomas Veerman
6762e2caf59SThomas Veerman if (elen > MAXPATHLEN)
6772e2caf59SThomas Veerman goto badarch;
6782e2caf59SThomas Veerman if (fread(memName, elen, 1, arch) != 1)
6792e2caf59SThomas Veerman goto badarch;
6802e2caf59SThomas Veerman memName[elen] = '\0';
6812e2caf59SThomas Veerman fseek(arch, -elen, SEEK_CUR);
6822e2caf59SThomas Veerman if (DEBUG(ARCH) || DEBUG(MAKE)) {
6832e2caf59SThomas Veerman fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName);
6842e2caf59SThomas Veerman }
6852e2caf59SThomas Veerman }
6862e2caf59SThomas Veerman #endif
6872e2caf59SThomas Veerman
6882e2caf59SThomas Veerman he = Hash_CreateEntry(&ar->members, memName, NULL);
6892e2caf59SThomas Veerman Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
6902e2caf59SThomas Veerman memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
6912e2caf59SThomas Veerman }
6922e2caf59SThomas Veerman fseek(arch, (size + 1) & ~1, SEEK_CUR);
6932e2caf59SThomas Veerman }
6942e2caf59SThomas Veerman
6952e2caf59SThomas Veerman fclose(arch);
6962e2caf59SThomas Veerman
6972e2caf59SThomas Veerman (void)Lst_AtEnd(archives, ar);
6982e2caf59SThomas Veerman
6992e2caf59SThomas Veerman /*
7002e2caf59SThomas Veerman * Now that the archive has been read and cached, we can look into
7012e2caf59SThomas Veerman * the hash table to find the desired member's header.
7022e2caf59SThomas Veerman */
7032e2caf59SThomas Veerman he = Hash_FindEntry(&ar->members, member);
7042e2caf59SThomas Veerman
7052e2caf59SThomas Veerman if (he != NULL) {
7062e2caf59SThomas Veerman return ((struct ar_hdr *)Hash_GetValue(he));
7072e2caf59SThomas Veerman } else {
7082e2caf59SThomas Veerman return NULL;
7092e2caf59SThomas Veerman }
7102e2caf59SThomas Veerman
7112e2caf59SThomas Veerman badarch:
7122e2caf59SThomas Veerman fclose(arch);
7132e2caf59SThomas Veerman Hash_DeleteTable(&ar->members);
7142e2caf59SThomas Veerman if (ar->fnametab)
7152e2caf59SThomas Veerman free(ar->fnametab);
7162e2caf59SThomas Veerman free(ar);
7172e2caf59SThomas Veerman return NULL;
7182e2caf59SThomas Veerman }
7192e2caf59SThomas Veerman
7202e2caf59SThomas Veerman #ifdef SVR4ARCHIVES
7212e2caf59SThomas Veerman /*-
7222e2caf59SThomas Veerman *-----------------------------------------------------------------------
7232e2caf59SThomas Veerman * ArchSVR4Entry --
7242e2caf59SThomas Veerman * Parse an SVR4 style entry that begins with a slash.
7252e2caf59SThomas Veerman * If it is "//", then load the table of filenames
7262e2caf59SThomas Veerman * If it is "/<offset>", then try to substitute the long file name
7272e2caf59SThomas Veerman * from offset of a table previously read.
7282e2caf59SThomas Veerman *
7292e2caf59SThomas Veerman * Results:
7302e2caf59SThomas Veerman * -1: Bad data in archive
7312e2caf59SThomas Veerman * 0: A table was loaded from the file
7322e2caf59SThomas Veerman * 1: Name was successfully substituted from table
7332e2caf59SThomas Veerman * 2: Name was not successfully substituted from table
7342e2caf59SThomas Veerman *
7352e2caf59SThomas Veerman * Side Effects:
7362e2caf59SThomas Veerman * If a table is read, the file pointer is moved to the next archive
7372e2caf59SThomas Veerman * member
7382e2caf59SThomas Veerman *
7392e2caf59SThomas Veerman *-----------------------------------------------------------------------
7402e2caf59SThomas Veerman */
7412e2caf59SThomas Veerman static int
ArchSVR4Entry(Arch * ar,char * name,size_t size,FILE * arch)7422e2caf59SThomas Veerman ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
7432e2caf59SThomas Veerman {
7442e2caf59SThomas Veerman #define ARLONGNAMES1 "//"
7452e2caf59SThomas Veerman #define ARLONGNAMES2 "/ARFILENAMES"
7462e2caf59SThomas Veerman size_t entry;
7472e2caf59SThomas Veerman char *ptr, *eptr;
7482e2caf59SThomas Veerman
7492e2caf59SThomas Veerman if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
7502e2caf59SThomas Veerman strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
7512e2caf59SThomas Veerman
7522e2caf59SThomas Veerman if (ar->fnametab != NULL) {
7532e2caf59SThomas Veerman if (DEBUG(ARCH)) {
7542e2caf59SThomas Veerman fprintf(debug_file, "Attempted to redefine an SVR4 name table\n");
7552e2caf59SThomas Veerman }
7562e2caf59SThomas Veerman return -1;
7572e2caf59SThomas Veerman }
7582e2caf59SThomas Veerman
7592e2caf59SThomas Veerman /*
7602e2caf59SThomas Veerman * This is a table of archive names, so we build one for
7612e2caf59SThomas Veerman * ourselves
7622e2caf59SThomas Veerman */
7632e2caf59SThomas Veerman ar->fnametab = bmake_malloc(size);
7642e2caf59SThomas Veerman ar->fnamesize = size;
7652e2caf59SThomas Veerman
7662e2caf59SThomas Veerman if (fread(ar->fnametab, size, 1, arch) != 1) {
7672e2caf59SThomas Veerman if (DEBUG(ARCH)) {
7682e2caf59SThomas Veerman fprintf(debug_file, "Reading an SVR4 name table failed\n");
7692e2caf59SThomas Veerman }
7702e2caf59SThomas Veerman return -1;
7712e2caf59SThomas Veerman }
7722e2caf59SThomas Veerman eptr = ar->fnametab + size;
7732e2caf59SThomas Veerman for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
7742e2caf59SThomas Veerman switch (*ptr) {
7752e2caf59SThomas Veerman case '/':
7762e2caf59SThomas Veerman entry++;
7772e2caf59SThomas Veerman *ptr = '\0';
7782e2caf59SThomas Veerman break;
7792e2caf59SThomas Veerman
7802e2caf59SThomas Veerman case '\n':
7812e2caf59SThomas Veerman break;
7822e2caf59SThomas Veerman
7832e2caf59SThomas Veerman default:
7842e2caf59SThomas Veerman break;
7852e2caf59SThomas Veerman }
7862e2caf59SThomas Veerman if (DEBUG(ARCH)) {
7872e2caf59SThomas Veerman fprintf(debug_file, "Found svr4 archive name table with %lu entries\n",
7882e2caf59SThomas Veerman (u_long)entry);
7892e2caf59SThomas Veerman }
7902e2caf59SThomas Veerman return 0;
7912e2caf59SThomas Veerman }
7922e2caf59SThomas Veerman
7932e2caf59SThomas Veerman if (name[1] == ' ' || name[1] == '\0')
7942e2caf59SThomas Veerman return 2;
7952e2caf59SThomas Veerman
7962e2caf59SThomas Veerman entry = (size_t)strtol(&name[1], &eptr, 0);
7972e2caf59SThomas Veerman if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
7982e2caf59SThomas Veerman if (DEBUG(ARCH)) {
7992e2caf59SThomas Veerman fprintf(debug_file, "Could not parse SVR4 name %s\n", name);
8002e2caf59SThomas Veerman }
8012e2caf59SThomas Veerman return 2;
8022e2caf59SThomas Veerman }
8032e2caf59SThomas Veerman if (entry >= ar->fnamesize) {
8042e2caf59SThomas Veerman if (DEBUG(ARCH)) {
8052e2caf59SThomas Veerman fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n",
8062e2caf59SThomas Veerman name, (u_long)ar->fnamesize);
8072e2caf59SThomas Veerman }
8082e2caf59SThomas Veerman return 2;
8092e2caf59SThomas Veerman }
8102e2caf59SThomas Veerman
8112e2caf59SThomas Veerman if (DEBUG(ARCH)) {
8122e2caf59SThomas Veerman fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
8132e2caf59SThomas Veerman }
8142e2caf59SThomas Veerman
8152e2caf59SThomas Veerman (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
8162e2caf59SThomas Veerman name[MAXPATHLEN] = '\0';
8172e2caf59SThomas Veerman return 1;
8182e2caf59SThomas Veerman }
8192e2caf59SThomas Veerman #endif
8202e2caf59SThomas Veerman
8212e2caf59SThomas Veerman
8222e2caf59SThomas Veerman /*-
8232e2caf59SThomas Veerman *-----------------------------------------------------------------------
8242e2caf59SThomas Veerman * ArchFindMember --
8252e2caf59SThomas Veerman * Locate a member of an archive, given the path of the archive and
8262e2caf59SThomas Veerman * the path of the desired member. If the archive is to be modified,
8272e2caf59SThomas Veerman * the mode should be "r+", if not, it should be "r".
8282e2caf59SThomas Veerman *
8292e2caf59SThomas Veerman * Input:
8302e2caf59SThomas Veerman * archive Path to the archive
8312e2caf59SThomas Veerman * member Name of member. If it is a path, only the last
8322e2caf59SThomas Veerman * component is used.
8332e2caf59SThomas Veerman * arhPtr Pointer to header structure to be filled in
8342e2caf59SThomas Veerman * mode The mode for opening the stream
8352e2caf59SThomas Veerman *
8362e2caf59SThomas Veerman * Results:
8372e2caf59SThomas Veerman * An FILE *, opened for reading and writing, positioned at the
8382e2caf59SThomas Veerman * start of the member's struct ar_hdr, or NULL if the member was
8392e2caf59SThomas Veerman * nonexistent. The current struct ar_hdr for member.
8402e2caf59SThomas Veerman *
8412e2caf59SThomas Veerman * Side Effects:
8422e2caf59SThomas Veerman * The passed struct ar_hdr structure is filled in.
8432e2caf59SThomas Veerman *
8442e2caf59SThomas Veerman *-----------------------------------------------------------------------
8452e2caf59SThomas Veerman */
8462e2caf59SThomas Veerman static FILE *
ArchFindMember(char * archive,char * member,struct ar_hdr * arhPtr,const char * mode)8472e2caf59SThomas Veerman ArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr,
8482e2caf59SThomas Veerman const char *mode)
8492e2caf59SThomas Veerman {
8502e2caf59SThomas Veerman FILE * arch; /* Stream to archive */
8512e2caf59SThomas Veerman int size; /* Size of archive member */
8522e2caf59SThomas Veerman char *cp; /* Useful character pointer */
8532e2caf59SThomas Veerman char magic[SARMAG];
8542e2caf59SThomas Veerman size_t len, tlen;
8552e2caf59SThomas Veerman
8562e2caf59SThomas Veerman arch = fopen(archive, mode);
8572e2caf59SThomas Veerman if (arch == NULL) {
8582e2caf59SThomas Veerman return NULL;
8592e2caf59SThomas Veerman }
8602e2caf59SThomas Veerman
8612e2caf59SThomas Veerman /*
8622e2caf59SThomas Veerman * We use the ARMAG string to make sure this is an archive we
8632e2caf59SThomas Veerman * can handle...
8642e2caf59SThomas Veerman */
8652e2caf59SThomas Veerman if ((fread(magic, SARMAG, 1, arch) != 1) ||
8662e2caf59SThomas Veerman (strncmp(magic, ARMAG, SARMAG) != 0)) {
8672e2caf59SThomas Veerman fclose(arch);
8682e2caf59SThomas Veerman return NULL;
8692e2caf59SThomas Veerman }
8702e2caf59SThomas Veerman
8712e2caf59SThomas Veerman /*
8722e2caf59SThomas Veerman * Because of space constraints and similar things, files are archived
8732e2caf59SThomas Veerman * using their final path components, not the entire thing, so we need
8742e2caf59SThomas Veerman * to point 'member' to the final component, if there is one, to make
8752e2caf59SThomas Veerman * the comparisons easier...
8762e2caf59SThomas Veerman */
8772e2caf59SThomas Veerman cp = strrchr(member, '/');
8782e2caf59SThomas Veerman if (cp != NULL) {
8792e2caf59SThomas Veerman member = cp + 1;
8802e2caf59SThomas Veerman }
8812e2caf59SThomas Veerman len = tlen = strlen(member);
8822e2caf59SThomas Veerman if (len > sizeof(arhPtr->ar_name)) {
8832e2caf59SThomas Veerman tlen = sizeof(arhPtr->ar_name);
8842e2caf59SThomas Veerman }
8852e2caf59SThomas Veerman
8862e2caf59SThomas Veerman while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
8872e2caf59SThomas Veerman if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
8882e2caf59SThomas Veerman /*
8892e2caf59SThomas Veerman * The header is bogus, so the archive is bad
8902e2caf59SThomas Veerman * and there's no way we can recover...
8912e2caf59SThomas Veerman */
8922e2caf59SThomas Veerman fclose(arch);
8932e2caf59SThomas Veerman return NULL;
8942e2caf59SThomas Veerman } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
8952e2caf59SThomas Veerman /*
8962e2caf59SThomas Veerman * If the member's name doesn't take up the entire 'name' field,
8972e2caf59SThomas Veerman * we have to be careful of matching prefixes. Names are space-
8982e2caf59SThomas Veerman * padded to the right, so if the character in 'name' at the end
8992e2caf59SThomas Veerman * of the matched string is anything but a space, this isn't the
9002e2caf59SThomas Veerman * member we sought.
9012e2caf59SThomas Veerman */
9022e2caf59SThomas Veerman if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
9032e2caf59SThomas Veerman goto skip;
9042e2caf59SThomas Veerman } else {
9052e2caf59SThomas Veerman /*
9062e2caf59SThomas Veerman * To make life easier, we reposition the file at the start
9072e2caf59SThomas Veerman * of the header we just read before we return the stream.
9082e2caf59SThomas Veerman * In a more general situation, it might be better to leave
9092e2caf59SThomas Veerman * the file at the actual member, rather than its header, but
9102e2caf59SThomas Veerman * not here...
9112e2caf59SThomas Veerman */
9122e2caf59SThomas Veerman fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR);
9132e2caf59SThomas Veerman return (arch);
9142e2caf59SThomas Veerman }
9152e2caf59SThomas Veerman } else
9162e2caf59SThomas Veerman #ifdef AR_EFMT1
9172e2caf59SThomas Veerman /*
9182e2caf59SThomas Veerman * BSD 4.4 extended AR format: #1/<namelen>, with name as the
9192e2caf59SThomas Veerman * first <namelen> bytes of the file
9202e2caf59SThomas Veerman */
9212e2caf59SThomas Veerman if (strncmp(arhPtr->ar_name, AR_EFMT1,
9222e2caf59SThomas Veerman sizeof(AR_EFMT1) - 1) == 0 &&
9232e2caf59SThomas Veerman isdigit((unsigned char)arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
9242e2caf59SThomas Veerman
9252e2caf59SThomas Veerman unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
9262e2caf59SThomas Veerman char ename[MAXPATHLEN + 1];
9272e2caf59SThomas Veerman
9282e2caf59SThomas Veerman if (elen > MAXPATHLEN) {
9292e2caf59SThomas Veerman fclose(arch);
9302e2caf59SThomas Veerman return NULL;
9312e2caf59SThomas Veerman }
9322e2caf59SThomas Veerman if (fread(ename, elen, 1, arch) != 1) {
9332e2caf59SThomas Veerman fclose(arch);
9342e2caf59SThomas Veerman return NULL;
9352e2caf59SThomas Veerman }
9362e2caf59SThomas Veerman ename[elen] = '\0';
9372e2caf59SThomas Veerman if (DEBUG(ARCH) || DEBUG(MAKE)) {
9382e2caf59SThomas Veerman fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename);
9392e2caf59SThomas Veerman }
9402e2caf59SThomas Veerman if (strncmp(ename, member, len) == 0) {
9412e2caf59SThomas Veerman /* Found as extended name */
9422e2caf59SThomas Veerman fseek(arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR);
9432e2caf59SThomas Veerman return (arch);
9442e2caf59SThomas Veerman }
9452e2caf59SThomas Veerman fseek(arch, -elen, SEEK_CUR);
9462e2caf59SThomas Veerman goto skip;
9472e2caf59SThomas Veerman } else
9482e2caf59SThomas Veerman #endif
9492e2caf59SThomas Veerman {
9502e2caf59SThomas Veerman skip:
9512e2caf59SThomas Veerman /*
9522e2caf59SThomas Veerman * This isn't the member we're after, so we need to advance the
9532e2caf59SThomas Veerman * stream's pointer to the start of the next header. Files are
9542e2caf59SThomas Veerman * padded with newlines to an even-byte boundary, so we need to
9552e2caf59SThomas Veerman * extract the size of the file from the 'size' field of the
9562e2caf59SThomas Veerman * header and round it up during the seek.
9572e2caf59SThomas Veerman */
9582e2caf59SThomas Veerman arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
9592e2caf59SThomas Veerman size = (int)strtol(arhPtr->ar_size, NULL, 10);
9602e2caf59SThomas Veerman fseek(arch, (size + 1) & ~1, SEEK_CUR);
9612e2caf59SThomas Veerman }
9622e2caf59SThomas Veerman }
9632e2caf59SThomas Veerman
9642e2caf59SThomas Veerman /*
9652e2caf59SThomas Veerman * We've looked everywhere, but the member is not to be found. Close the
9662e2caf59SThomas Veerman * archive and return NULL -- an error.
9672e2caf59SThomas Veerman */
9682e2caf59SThomas Veerman fclose(arch);
9692e2caf59SThomas Veerman return NULL;
9702e2caf59SThomas Veerman }
9712e2caf59SThomas Veerman
9722e2caf59SThomas Veerman /*-
9732e2caf59SThomas Veerman *-----------------------------------------------------------------------
9742e2caf59SThomas Veerman * Arch_Touch --
9752e2caf59SThomas Veerman * Touch a member of an archive.
9762e2caf59SThomas Veerman *
9772e2caf59SThomas Veerman * Input:
9782e2caf59SThomas Veerman * gn Node of member to touch
9792e2caf59SThomas Veerman *
9802e2caf59SThomas Veerman * Results:
9812e2caf59SThomas Veerman * The 'time' field of the member's header is updated.
9822e2caf59SThomas Veerman *
9832e2caf59SThomas Veerman * Side Effects:
9842e2caf59SThomas Veerman * The modification time of the entire archive is also changed.
9852e2caf59SThomas Veerman * For a library, this could necessitate the re-ranlib'ing of the
9862e2caf59SThomas Veerman * whole thing.
9872e2caf59SThomas Veerman *
9882e2caf59SThomas Veerman *-----------------------------------------------------------------------
9892e2caf59SThomas Veerman */
9902e2caf59SThomas Veerman void
Arch_Touch(GNode * gn)9912e2caf59SThomas Veerman Arch_Touch(GNode *gn)
9922e2caf59SThomas Veerman {
9932e2caf59SThomas Veerman FILE * arch; /* Stream open to archive, positioned properly */
9942e2caf59SThomas Veerman struct ar_hdr arh; /* Current header describing member */
9952e2caf59SThomas Veerman char *p1, *p2;
9962e2caf59SThomas Veerman
9972e2caf59SThomas Veerman arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
9982e2caf59SThomas Veerman Var_Value(MEMBER, gn, &p2),
9992e2caf59SThomas Veerman &arh, "r+");
10002e2caf59SThomas Veerman if (p1)
10012e2caf59SThomas Veerman free(p1);
10022e2caf59SThomas Veerman if (p2)
10032e2caf59SThomas Veerman free(p2);
10042e2caf59SThomas Veerman snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
10052e2caf59SThomas Veerman
10062e2caf59SThomas Veerman if (arch != NULL) {
10072e2caf59SThomas Veerman (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
10082e2caf59SThomas Veerman fclose(arch);
10092e2caf59SThomas Veerman }
10102e2caf59SThomas Veerman }
10112e2caf59SThomas Veerman
10122e2caf59SThomas Veerman /*-
10132e2caf59SThomas Veerman *-----------------------------------------------------------------------
10142e2caf59SThomas Veerman * Arch_TouchLib --
10152e2caf59SThomas Veerman * Given a node which represents a library, touch the thing, making
10162e2caf59SThomas Veerman * sure that the table of contents also is touched.
10172e2caf59SThomas Veerman *
10182e2caf59SThomas Veerman * Input:
10192e2caf59SThomas Veerman * gn The node of the library to touch
10202e2caf59SThomas Veerman *
10212e2caf59SThomas Veerman * Results:
10222e2caf59SThomas Veerman * None.
10232e2caf59SThomas Veerman *
10242e2caf59SThomas Veerman * Side Effects:
10252e2caf59SThomas Veerman * Both the modification time of the library and of the RANLIBMAG
10262e2caf59SThomas Veerman * member are set to 'now'.
10272e2caf59SThomas Veerman *
10282e2caf59SThomas Veerman *-----------------------------------------------------------------------
10292e2caf59SThomas Veerman */
10302e2caf59SThomas Veerman void
10312e2caf59SThomas Veerman #if !defined(RANLIBMAG)
Arch_TouchLib(GNode * gn MAKE_ATTR_UNUSED)1032*2bc7c627SLionel Sambuc Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
10332e2caf59SThomas Veerman #else
10342e2caf59SThomas Veerman Arch_TouchLib(GNode *gn)
10352e2caf59SThomas Veerman #endif
10362e2caf59SThomas Veerman {
10372e2caf59SThomas Veerman #ifdef RANLIBMAG
10382e2caf59SThomas Veerman FILE * arch; /* Stream open to archive */
10392e2caf59SThomas Veerman struct ar_hdr arh; /* Header describing table of contents */
10402e2caf59SThomas Veerman struct utimbuf times; /* Times for utime() call */
10412e2caf59SThomas Veerman
10422e2caf59SThomas Veerman arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+");
10432e2caf59SThomas Veerman snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
10442e2caf59SThomas Veerman
10452e2caf59SThomas Veerman if (arch != NULL) {
10462e2caf59SThomas Veerman (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
10472e2caf59SThomas Veerman fclose(arch);
10482e2caf59SThomas Veerman
10492e2caf59SThomas Veerman times.actime = times.modtime = now;
10502e2caf59SThomas Veerman utime(gn->path, ×);
10512e2caf59SThomas Veerman }
10522e2caf59SThomas Veerman #endif
10532e2caf59SThomas Veerman }
10542e2caf59SThomas Veerman
10552e2caf59SThomas Veerman /*-
10562e2caf59SThomas Veerman *-----------------------------------------------------------------------
10572e2caf59SThomas Veerman * Arch_MTime --
10582e2caf59SThomas Veerman * Return the modification time of a member of an archive.
10592e2caf59SThomas Veerman *
10602e2caf59SThomas Veerman * Input:
10612e2caf59SThomas Veerman * gn Node describing archive member
10622e2caf59SThomas Veerman *
10632e2caf59SThomas Veerman * Results:
10642e2caf59SThomas Veerman * The modification time(seconds).
10652e2caf59SThomas Veerman *
10662e2caf59SThomas Veerman * Side Effects:
10672e2caf59SThomas Veerman * The mtime field of the given node is filled in with the value
10682e2caf59SThomas Veerman * returned by the function.
10692e2caf59SThomas Veerman *
10702e2caf59SThomas Veerman *-----------------------------------------------------------------------
10712e2caf59SThomas Veerman */
10722e2caf59SThomas Veerman time_t
Arch_MTime(GNode * gn)10732e2caf59SThomas Veerman Arch_MTime(GNode *gn)
10742e2caf59SThomas Veerman {
10752e2caf59SThomas Veerman struct ar_hdr *arhPtr; /* Header of desired member */
10762e2caf59SThomas Veerman time_t modTime; /* Modification time as an integer */
10772e2caf59SThomas Veerman char *p1, *p2;
10782e2caf59SThomas Veerman
10792e2caf59SThomas Veerman arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
10802e2caf59SThomas Veerman Var_Value(MEMBER, gn, &p2),
10812e2caf59SThomas Veerman TRUE);
10822e2caf59SThomas Veerman if (p1)
10832e2caf59SThomas Veerman free(p1);
10842e2caf59SThomas Veerman if (p2)
10852e2caf59SThomas Veerman free(p2);
10862e2caf59SThomas Veerman
10872e2caf59SThomas Veerman if (arhPtr != NULL) {
10882e2caf59SThomas Veerman modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
10892e2caf59SThomas Veerman } else {
10902e2caf59SThomas Veerman modTime = 0;
10912e2caf59SThomas Veerman }
10922e2caf59SThomas Veerman
10932e2caf59SThomas Veerman gn->mtime = modTime;
10942e2caf59SThomas Veerman return (modTime);
10952e2caf59SThomas Veerman }
10962e2caf59SThomas Veerman
10972e2caf59SThomas Veerman /*-
10982e2caf59SThomas Veerman *-----------------------------------------------------------------------
10992e2caf59SThomas Veerman * Arch_MemMTime --
11002e2caf59SThomas Veerman * Given a non-existent archive member's node, get its modification
11012e2caf59SThomas Veerman * time from its archived form, if it exists.
11022e2caf59SThomas Veerman *
11032e2caf59SThomas Veerman * Results:
11042e2caf59SThomas Veerman * The modification time.
11052e2caf59SThomas Veerman *
11062e2caf59SThomas Veerman * Side Effects:
11072e2caf59SThomas Veerman * The mtime field is filled in.
11082e2caf59SThomas Veerman *
11092e2caf59SThomas Veerman *-----------------------------------------------------------------------
11102e2caf59SThomas Veerman */
11112e2caf59SThomas Veerman time_t
Arch_MemMTime(GNode * gn)11122e2caf59SThomas Veerman Arch_MemMTime(GNode *gn)
11132e2caf59SThomas Veerman {
11142e2caf59SThomas Veerman LstNode ln;
11152e2caf59SThomas Veerman GNode *pgn;
11162e2caf59SThomas Veerman char *nameStart,
11172e2caf59SThomas Veerman *nameEnd;
11182e2caf59SThomas Veerman
11192e2caf59SThomas Veerman if (Lst_Open(gn->parents) != SUCCESS) {
11202e2caf59SThomas Veerman gn->mtime = 0;
11212e2caf59SThomas Veerman return (0);
11222e2caf59SThomas Veerman }
11232e2caf59SThomas Veerman while ((ln = Lst_Next(gn->parents)) != NULL) {
11242e2caf59SThomas Veerman pgn = (GNode *)Lst_Datum(ln);
11252e2caf59SThomas Veerman
11262e2caf59SThomas Veerman if (pgn->type & OP_ARCHV) {
11272e2caf59SThomas Veerman /*
11282e2caf59SThomas Veerman * If the parent is an archive specification and is being made
11292e2caf59SThomas Veerman * and its member's name matches the name of the node we were
11302e2caf59SThomas Veerman * given, record the modification time of the parent in the
11312e2caf59SThomas Veerman * child. We keep searching its parents in case some other
11322e2caf59SThomas Veerman * parent requires this child to exist...
11332e2caf59SThomas Veerman */
11342e2caf59SThomas Veerman nameStart = strchr(pgn->name, '(') + 1;
11352e2caf59SThomas Veerman nameEnd = strchr(nameStart, ')');
11362e2caf59SThomas Veerman
11372e2caf59SThomas Veerman if ((pgn->flags & REMAKE) &&
11382e2caf59SThomas Veerman strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
11392e2caf59SThomas Veerman gn->mtime = Arch_MTime(pgn);
11402e2caf59SThomas Veerman }
11412e2caf59SThomas Veerman } else if (pgn->flags & REMAKE) {
11422e2caf59SThomas Veerman /*
11432e2caf59SThomas Veerman * Something which isn't a library depends on the existence of
11442e2caf59SThomas Veerman * this target, so it needs to exist.
11452e2caf59SThomas Veerman */
11462e2caf59SThomas Veerman gn->mtime = 0;
11472e2caf59SThomas Veerman break;
11482e2caf59SThomas Veerman }
11492e2caf59SThomas Veerman }
11502e2caf59SThomas Veerman
11512e2caf59SThomas Veerman Lst_Close(gn->parents);
11522e2caf59SThomas Veerman
11532e2caf59SThomas Veerman return (gn->mtime);
11542e2caf59SThomas Veerman }
11552e2caf59SThomas Veerman
11562e2caf59SThomas Veerman /*-
11572e2caf59SThomas Veerman *-----------------------------------------------------------------------
11582e2caf59SThomas Veerman * Arch_FindLib --
11592e2caf59SThomas Veerman * Search for a library along the given search path.
11602e2caf59SThomas Veerman *
11612e2caf59SThomas Veerman * Input:
11622e2caf59SThomas Veerman * gn Node of library to find
11632e2caf59SThomas Veerman * path Search path
11642e2caf59SThomas Veerman *
11652e2caf59SThomas Veerman * Results:
11662e2caf59SThomas Veerman * None.
11672e2caf59SThomas Veerman *
11682e2caf59SThomas Veerman * Side Effects:
11692e2caf59SThomas Veerman * The node's 'path' field is set to the found path (including the
11702e2caf59SThomas Veerman * actual file name, not -l...). If the system can handle the -L
11712e2caf59SThomas Veerman * flag when linking (or we cannot find the library), we assume that
11722e2caf59SThomas Veerman * the user has placed the .LIBRARIES variable in the final linking
11732e2caf59SThomas Veerman * command (or the linker will know where to find it) and set the
11742e2caf59SThomas Veerman * TARGET variable for this node to be the node's name. Otherwise,
11752e2caf59SThomas Veerman * we set the TARGET variable to be the full path of the library,
11762e2caf59SThomas Veerman * as returned by Dir_FindFile.
11772e2caf59SThomas Veerman *
11782e2caf59SThomas Veerman *-----------------------------------------------------------------------
11792e2caf59SThomas Veerman */
11802e2caf59SThomas Veerman void
Arch_FindLib(GNode * gn,Lst path)11812e2caf59SThomas Veerman Arch_FindLib(GNode *gn, Lst path)
11822e2caf59SThomas Veerman {
11832e2caf59SThomas Veerman char *libName; /* file name for archive */
11842e2caf59SThomas Veerman size_t sz = strlen(gn->name) + 6 - 2;
11852e2caf59SThomas Veerman
11862e2caf59SThomas Veerman libName = bmake_malloc(sz);
11872e2caf59SThomas Veerman snprintf(libName, sz, "lib%s.a", &gn->name[2]);
11882e2caf59SThomas Veerman
11892e2caf59SThomas Veerman gn->path = Dir_FindFile(libName, path);
11902e2caf59SThomas Veerman
11912e2caf59SThomas Veerman free(libName);
11922e2caf59SThomas Veerman
11932e2caf59SThomas Veerman #ifdef LIBRARIES
11942e2caf59SThomas Veerman Var_Set(TARGET, gn->name, gn, 0);
11952e2caf59SThomas Veerman #else
11962e2caf59SThomas Veerman Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn, 0);
11972e2caf59SThomas Veerman #endif /* LIBRARIES */
11982e2caf59SThomas Veerman }
11992e2caf59SThomas Veerman
12002e2caf59SThomas Veerman /*-
12012e2caf59SThomas Veerman *-----------------------------------------------------------------------
12022e2caf59SThomas Veerman * Arch_LibOODate --
12032e2caf59SThomas Veerman * Decide if a node with the OP_LIB attribute is out-of-date. Called
12042e2caf59SThomas Veerman * from Make_OODate to make its life easier.
12052e2caf59SThomas Veerman *
12062e2caf59SThomas Veerman * There are several ways for a library to be out-of-date that are
12072e2caf59SThomas Veerman * not available to ordinary files. In addition, there are ways
12082e2caf59SThomas Veerman * that are open to regular files that are not available to
12092e2caf59SThomas Veerman * libraries. A library that is only used as a source is never
12102e2caf59SThomas Veerman * considered out-of-date by itself. This does not preclude the
12112e2caf59SThomas Veerman * library's modification time from making its parent be out-of-date.
12122e2caf59SThomas Veerman * A library will be considered out-of-date for any of these reasons,
12132e2caf59SThomas Veerman * given that it is a target on a dependency line somewhere:
12142e2caf59SThomas Veerman * Its modification time is less than that of one of its
12152e2caf59SThomas Veerman * sources (gn->mtime < gn->cmgn->mtime).
12162e2caf59SThomas Veerman * Its modification time is greater than the time at which the
12172e2caf59SThomas Veerman * make began (i.e. it's been modified in the course
12182e2caf59SThomas Veerman * of the make, probably by archiving).
12192e2caf59SThomas Veerman * The modification time of one of its sources is greater than
12202e2caf59SThomas Veerman * the one of its RANLIBMAG member (i.e. its table of contents
12212e2caf59SThomas Veerman * is out-of-date). We don't compare of the archive time
12222e2caf59SThomas Veerman * vs. TOC time because they can be too close. In my
12232e2caf59SThomas Veerman * opinion we should not bother with the TOC at all since
12242e2caf59SThomas Veerman * this is used by 'ar' rules that affect the data contents
12252e2caf59SThomas Veerman * of the archive, not by ranlib rules, which affect the
12262e2caf59SThomas Veerman * TOC.
12272e2caf59SThomas Veerman *
12282e2caf59SThomas Veerman * Input:
12292e2caf59SThomas Veerman * gn The library's graph node
12302e2caf59SThomas Veerman *
12312e2caf59SThomas Veerman * Results:
12322e2caf59SThomas Veerman * TRUE if the library is out-of-date. FALSE otherwise.
12332e2caf59SThomas Veerman *
12342e2caf59SThomas Veerman * Side Effects:
12352e2caf59SThomas Veerman * The library will be hashed if it hasn't been already.
12362e2caf59SThomas Veerman *
12372e2caf59SThomas Veerman *-----------------------------------------------------------------------
12382e2caf59SThomas Veerman */
12392e2caf59SThomas Veerman Boolean
Arch_LibOODate(GNode * gn)12402e2caf59SThomas Veerman Arch_LibOODate(GNode *gn)
12412e2caf59SThomas Veerman {
12422e2caf59SThomas Veerman Boolean oodate;
12432e2caf59SThomas Veerman
12442e2caf59SThomas Veerman if (gn->type & OP_PHONY) {
12452e2caf59SThomas Veerman oodate = TRUE;
12462e2caf59SThomas Veerman } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
12472e2caf59SThomas Veerman oodate = FALSE;
12482e2caf59SThomas Veerman } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
12492e2caf59SThomas Veerman (gn->mtime > now) ||
12502e2caf59SThomas Veerman (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
12512e2caf59SThomas Veerman oodate = TRUE;
12522e2caf59SThomas Veerman } else {
12532e2caf59SThomas Veerman #ifdef RANLIBMAG
12542e2caf59SThomas Veerman struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
12552e2caf59SThomas Veerman int modTimeTOC; /* The table-of-contents's mod time */
12562e2caf59SThomas Veerman
12572e2caf59SThomas Veerman arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE);
12582e2caf59SThomas Veerman
12592e2caf59SThomas Veerman if (arhPtr != NULL) {
12602e2caf59SThomas Veerman modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
12612e2caf59SThomas Veerman
12622e2caf59SThomas Veerman if (DEBUG(ARCH) || DEBUG(MAKE)) {
12632e2caf59SThomas Veerman fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
12642e2caf59SThomas Veerman }
12652e2caf59SThomas Veerman oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
12662e2caf59SThomas Veerman } else {
12672e2caf59SThomas Veerman /*
12682e2caf59SThomas Veerman * A library w/o a table of contents is out-of-date
12692e2caf59SThomas Veerman */
12702e2caf59SThomas Veerman if (DEBUG(ARCH) || DEBUG(MAKE)) {
12712e2caf59SThomas Veerman fprintf(debug_file, "No t.o.c....");
12722e2caf59SThomas Veerman }
12732e2caf59SThomas Veerman oodate = TRUE;
12742e2caf59SThomas Veerman }
12752e2caf59SThomas Veerman #else
12762e2caf59SThomas Veerman oodate = FALSE;
12772e2caf59SThomas Veerman #endif
12782e2caf59SThomas Veerman }
12792e2caf59SThomas Veerman return (oodate);
12802e2caf59SThomas Veerman }
12812e2caf59SThomas Veerman
12822e2caf59SThomas Veerman /*-
12832e2caf59SThomas Veerman *-----------------------------------------------------------------------
12842e2caf59SThomas Veerman * Arch_Init --
12852e2caf59SThomas Veerman * Initialize things for this module.
12862e2caf59SThomas Veerman *
12872e2caf59SThomas Veerman * Results:
12882e2caf59SThomas Veerman * None.
12892e2caf59SThomas Veerman *
12902e2caf59SThomas Veerman * Side Effects:
12912e2caf59SThomas Veerman * The 'archives' list is initialized.
12922e2caf59SThomas Veerman *
12932e2caf59SThomas Veerman *-----------------------------------------------------------------------
12942e2caf59SThomas Veerman */
12952e2caf59SThomas Veerman void
Arch_Init(void)12962e2caf59SThomas Veerman Arch_Init(void)
12972e2caf59SThomas Veerman {
12982e2caf59SThomas Veerman archives = Lst_Init(FALSE);
12992e2caf59SThomas Veerman }
13002e2caf59SThomas Veerman
13012e2caf59SThomas Veerman
13022e2caf59SThomas Veerman
13032e2caf59SThomas Veerman /*-
13042e2caf59SThomas Veerman *-----------------------------------------------------------------------
13052e2caf59SThomas Veerman * Arch_End --
13062e2caf59SThomas Veerman * Cleanup things for this module.
13072e2caf59SThomas Veerman *
13082e2caf59SThomas Veerman * Results:
13092e2caf59SThomas Veerman * None.
13102e2caf59SThomas Veerman *
13112e2caf59SThomas Veerman * Side Effects:
13122e2caf59SThomas Veerman * The 'archives' list is freed
13132e2caf59SThomas Veerman *
13142e2caf59SThomas Veerman *-----------------------------------------------------------------------
13152e2caf59SThomas Veerman */
13162e2caf59SThomas Veerman void
Arch_End(void)13172e2caf59SThomas Veerman Arch_End(void)
13182e2caf59SThomas Veerman {
13192e2caf59SThomas Veerman #ifdef CLEANUP
13202e2caf59SThomas Veerman Lst_Destroy(archives, ArchFree);
13212e2caf59SThomas Veerman #endif
13222e2caf59SThomas Veerman }
13232e2caf59SThomas Veerman
13242e2caf59SThomas Veerman /*-
13252e2caf59SThomas Veerman *-----------------------------------------------------------------------
13262e2caf59SThomas Veerman * Arch_IsLib --
13272e2caf59SThomas Veerman * Check if the node is a library
13282e2caf59SThomas Veerman *
13292e2caf59SThomas Veerman * Results:
13302e2caf59SThomas Veerman * True or False.
13312e2caf59SThomas Veerman *
13322e2caf59SThomas Veerman * Side Effects:
13332e2caf59SThomas Veerman * None.
13342e2caf59SThomas Veerman *
13352e2caf59SThomas Veerman *-----------------------------------------------------------------------
13362e2caf59SThomas Veerman */
13372e2caf59SThomas Veerman int
Arch_IsLib(GNode * gn)13382e2caf59SThomas Veerman Arch_IsLib(GNode *gn)
13392e2caf59SThomas Veerman {
13402e2caf59SThomas Veerman static const char armag[] = "!<arch>\n";
13412e2caf59SThomas Veerman char buf[sizeof(armag)-1];
13422e2caf59SThomas Veerman int fd;
13432e2caf59SThomas Veerman
13442e2caf59SThomas Veerman if ((fd = open(gn->path, O_RDONLY)) == -1)
13452e2caf59SThomas Veerman return FALSE;
13462e2caf59SThomas Veerman
13472e2caf59SThomas Veerman if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
13482e2caf59SThomas Veerman (void)close(fd);
13492e2caf59SThomas Veerman return FALSE;
13502e2caf59SThomas Veerman }
13512e2caf59SThomas Veerman
13522e2caf59SThomas Veerman (void)close(fd);
13532e2caf59SThomas Veerman
13542e2caf59SThomas Veerman return memcmp(buf, armag, sizeof(buf)) == 0;
13552e2caf59SThomas Veerman }
1356