1237578Sobrien/* $NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $ */ 2236769Sobrien 3236769Sobrien/* 4236769Sobrien * Copyright (c) 1988, 1989, 1990, 1993 5236769Sobrien * The Regents of the University of California. All rights reserved. 6236769Sobrien * 7236769Sobrien * This code is derived from software contributed to Berkeley by 8236769Sobrien * Adam de Boor. 9236769Sobrien * 10236769Sobrien * Redistribution and use in source and binary forms, with or without 11236769Sobrien * modification, are permitted provided that the following conditions 12236769Sobrien * are met: 13236769Sobrien * 1. Redistributions of source code must retain the above copyright 14236769Sobrien * notice, this list of conditions and the following disclaimer. 15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16236769Sobrien * notice, this list of conditions and the following disclaimer in the 17236769Sobrien * documentation and/or other materials provided with the distribution. 18236769Sobrien * 3. Neither the name of the University nor the names of its contributors 19236769Sobrien * may be used to endorse or promote products derived from this software 20236769Sobrien * without specific prior written permission. 21236769Sobrien * 22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32236769Sobrien * SUCH DAMAGE. 33236769Sobrien */ 34236769Sobrien 35236769Sobrien/* 36236769Sobrien * Copyright (c) 1989 by Berkeley Softworks 37236769Sobrien * All rights reserved. 38236769Sobrien * 39236769Sobrien * This code is derived from software contributed to Berkeley by 40236769Sobrien * Adam de Boor. 41236769Sobrien * 42236769Sobrien * Redistribution and use in source and binary forms, with or without 43236769Sobrien * modification, are permitted provided that the following conditions 44236769Sobrien * are met: 45236769Sobrien * 1. Redistributions of source code must retain the above copyright 46236769Sobrien * notice, this list of conditions and the following disclaimer. 47236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 48236769Sobrien * notice, this list of conditions and the following disclaimer in the 49236769Sobrien * documentation and/or other materials provided with the distribution. 50236769Sobrien * 3. All advertising materials mentioning features or use of this software 51236769Sobrien * must display the following acknowledgement: 52236769Sobrien * This product includes software developed by the University of 53236769Sobrien * California, Berkeley and its contributors. 54236769Sobrien * 4. Neither the name of the University nor the names of its contributors 55236769Sobrien * may be used to endorse or promote products derived from this software 56236769Sobrien * without specific prior written permission. 57236769Sobrien * 58236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68236769Sobrien * SUCH DAMAGE. 69236769Sobrien */ 70236769Sobrien 71236769Sobrien#ifndef MAKE_NATIVE 72237578Sobrienstatic char rcsid[] = "$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $"; 73236769Sobrien#else 74236769Sobrien#include <sys/cdefs.h> 75236769Sobrien#ifndef lint 76236769Sobrien#if 0 77236769Sobrienstatic char sccsid[] = "@(#)arch.c 8.2 (Berkeley) 1/2/94"; 78236769Sobrien#else 79237578Sobrien__RCSID("$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $"); 80236769Sobrien#endif 81236769Sobrien#endif /* not lint */ 82236769Sobrien#endif 83236769Sobrien 84236769Sobrien/*- 85236769Sobrien * arch.c -- 86236769Sobrien * Functions to manipulate libraries, archives and their members. 87236769Sobrien * 88236769Sobrien * Once again, cacheing/hashing comes into play in the manipulation 89236769Sobrien * of archives. The first time an archive is referenced, all of its members' 90236769Sobrien * headers are read and hashed and the archive closed again. All hashed 91236769Sobrien * archives are kept on a list which is searched each time an archive member 92236769Sobrien * is referenced. 93236769Sobrien * 94236769Sobrien * The interface to this module is: 95236769Sobrien * Arch_ParseArchive Given an archive specification, return a list 96236769Sobrien * of GNode's, one for each member in the spec. 97236769Sobrien * FAILURE is returned if the specification is 98236769Sobrien * invalid for some reason. 99236769Sobrien * 100236769Sobrien * Arch_Touch Alter the modification time of the archive 101236769Sobrien * member described by the given node to be 102236769Sobrien * the current time. 103236769Sobrien * 104236769Sobrien * Arch_TouchLib Update the modification time of the library 105236769Sobrien * described by the given node. This is special 106236769Sobrien * because it also updates the modification time 107236769Sobrien * of the library's table of contents. 108236769Sobrien * 109236769Sobrien * Arch_MTime Find the modification time of a member of 110236769Sobrien * an archive *in the archive*. The time is also 111236769Sobrien * placed in the member's GNode. Returns the 112236769Sobrien * modification time. 113236769Sobrien * 114236769Sobrien * Arch_MemTime Find the modification time of a member of 115236769Sobrien * an archive. Called when the member doesn't 116236769Sobrien * already exist. Looks in the archive for the 117236769Sobrien * modification time. Returns the modification 118236769Sobrien * time. 119236769Sobrien * 120236769Sobrien * Arch_FindLib Search for a library along a path. The 121236769Sobrien * library name in the GNode should be in 122236769Sobrien * -l<name> format. 123236769Sobrien * 124236769Sobrien * Arch_LibOODate Special function to decide if a library node 125236769Sobrien * is out-of-date. 126236769Sobrien * 127236769Sobrien * Arch_Init Initialize this module. 128236769Sobrien * 129236769Sobrien * Arch_End Cleanup this module. 130236769Sobrien */ 131236769Sobrien 132236769Sobrien#ifdef HAVE_CONFIG_H 133236769Sobrien# include "config.h" 134236769Sobrien#endif 135236769Sobrien#include <sys/types.h> 136236769Sobrien#include <sys/stat.h> 137236769Sobrien#include <sys/time.h> 138236769Sobrien#include <sys/param.h> 139236769Sobrien#include <ctype.h> 140236769Sobrien#ifdef HAVE_AR_H 141236769Sobrien#include <ar.h> 142236769Sobrien#else 143236769Sobrienstruct ar_hdr { 144236769Sobrien char ar_name[16]; /* name */ 145236769Sobrien char ar_date[12]; /* modification time */ 146236769Sobrien char ar_uid[6]; /* user id */ 147236769Sobrien char ar_gid[6]; /* group id */ 148236769Sobrien char ar_mode[8]; /* octal file permissions */ 149236769Sobrien char ar_size[10]; /* size in bytes */ 150236769Sobrien#ifndef ARFMAG 151236769Sobrien#define ARFMAG "`\n" 152236769Sobrien#endif 153236769Sobrien char ar_fmag[2]; /* consistency check */ 154236769Sobrien}; 155236769Sobrien#endif 156236769Sobrien#if defined(HAVE_RANLIB_H) && !(defined(__ELF__) || defined(NO_RANLIB)) 157236769Sobrien#include <ranlib.h> 158236769Sobrien#endif 159236769Sobrien#include <fcntl.h> 160236769Sobrien#include <stdio.h> 161236769Sobrien#include <stdlib.h> 162236769Sobrien#ifdef HAVE_UTIME_H 163236769Sobrien#include <utime.h> 164236769Sobrien#endif 165236769Sobrien 166236769Sobrien#include "make.h" 167236769Sobrien#include "hash.h" 168236769Sobrien#include "dir.h" 169236769Sobrien 170236769Sobrien#ifdef TARGET_MACHINE 171236769Sobrien#undef MAKE_MACHINE 172236769Sobrien#define MAKE_MACHINE TARGET_MACHINE 173236769Sobrien#endif 174236769Sobrien#ifdef TARGET_MACHINE_ARCH 175236769Sobrien#undef MAKE_MACHINE_ARCH 176236769Sobrien#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH 177236769Sobrien#endif 178236769Sobrien 179236769Sobrienstatic Lst archives; /* Lst of archives we've already examined */ 180236769Sobrien 181236769Sobrientypedef struct Arch { 182236769Sobrien char *name; /* Name of archive */ 183236769Sobrien Hash_Table members; /* All the members of the archive described 184236769Sobrien * by <name, struct ar_hdr *> key/value pairs */ 185236769Sobrien char *fnametab; /* Extended name table strings */ 186236769Sobrien size_t fnamesize; /* Size of the string table */ 187236769Sobrien} Arch; 188236769Sobrien 189236769Sobrienstatic int ArchFindArchive(const void *, const void *); 190236769Sobrien#ifdef CLEANUP 191236769Sobrienstatic void ArchFree(void *); 192236769Sobrien#endif 193236769Sobrienstatic struct ar_hdr *ArchStatMember(char *, char *, Boolean); 194236769Sobrienstatic FILE *ArchFindMember(char *, char *, struct ar_hdr *, const char *); 195236769Sobrien#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__) 196236769Sobrien#define SVR4ARCHIVES 197236769Sobrienstatic int ArchSVR4Entry(Arch *, char *, size_t, FILE *); 198236769Sobrien#endif 199236769Sobrien 200236769Sobrien 201236769Sobrien#if defined(_AIX) 202236769Sobrien# define AR_NAME _ar_name.ar_name 203236769Sobrien# define AR_FMAG _ar_name.ar_fmag 204236769Sobrien# define SARMAG SAIAMAG 205236769Sobrien# define ARMAG AIAMAG 206236769Sobrien# define ARFMAG AIAFMAG 207236769Sobrien#endif 208236769Sobrien#ifndef AR_NAME 209236769Sobrien# define AR_NAME ar_name 210236769Sobrien#endif 211236769Sobrien#ifndef AR_DATE 212236769Sobrien# define AR_DATE ar_date 213236769Sobrien#endif 214236769Sobrien#ifndef AR_SIZE 215236769Sobrien# define AR_SIZE ar_size 216236769Sobrien#endif 217236769Sobrien#ifndef AR_FMAG 218236769Sobrien# define AR_FMAG ar_fmag 219236769Sobrien#endif 220236769Sobrien#ifndef ARMAG 221236769Sobrien# define ARMAG "!<arch>\n" 222236769Sobrien#endif 223236769Sobrien#ifndef SARMAG 224236769Sobrien# define SARMAG 8 225236769Sobrien#endif 226236769Sobrien 227236769Sobrien#define AR_MAX_NAME_LEN (sizeof(arh.AR_NAME)-1) 228236769Sobrien 229236769Sobrien#ifdef CLEANUP 230236769Sobrien/*- 231236769Sobrien *----------------------------------------------------------------------- 232236769Sobrien * ArchFree -- 233236769Sobrien * Free memory used by an archive 234236769Sobrien * 235236769Sobrien * Results: 236236769Sobrien * None. 237236769Sobrien * 238236769Sobrien * Side Effects: 239236769Sobrien * None. 240236769Sobrien * 241236769Sobrien *----------------------------------------------------------------------- 242236769Sobrien */ 243236769Sobrienstatic void 244236769SobrienArchFree(void *ap) 245236769Sobrien{ 246236769Sobrien Arch *a = (Arch *)ap; 247236769Sobrien Hash_Search search; 248236769Sobrien Hash_Entry *entry; 249236769Sobrien 250236769Sobrien /* Free memory from hash entries */ 251236769Sobrien for (entry = Hash_EnumFirst(&a->members, &search); 252236769Sobrien entry != NULL; 253236769Sobrien entry = Hash_EnumNext(&search)) 254236769Sobrien free(Hash_GetValue(entry)); 255236769Sobrien 256236769Sobrien free(a->name); 257236769Sobrien if (a->fnametab) 258236769Sobrien free(a->fnametab); 259236769Sobrien Hash_DeleteTable(&a->members); 260236769Sobrien free(a); 261236769Sobrien} 262236769Sobrien#endif 263236769Sobrien 264236769Sobrien 265236769Sobrien/*- 266236769Sobrien *----------------------------------------------------------------------- 267236769Sobrien * Arch_ParseArchive -- 268236769Sobrien * Parse the archive specification in the given line and find/create 269236769Sobrien * the nodes for the specified archive members, placing their nodes 270236769Sobrien * on the given list. 271236769Sobrien * 272236769Sobrien * Input: 273236769Sobrien * linePtr Pointer to start of specification 274236769Sobrien * nodeLst Lst on which to place the nodes 275236769Sobrien * ctxt Context in which to expand variables 276236769Sobrien * 277236769Sobrien * Results: 278236769Sobrien * SUCCESS if it was a valid specification. The linePtr is updated 279236769Sobrien * to point to the first non-space after the archive spec. The 280236769Sobrien * nodes for the members are placed on the given list. 281236769Sobrien * 282236769Sobrien * Side Effects: 283236769Sobrien * Some nodes may be created. The given list is extended. 284236769Sobrien * 285236769Sobrien *----------------------------------------------------------------------- 286236769Sobrien */ 287236769SobrienReturnStatus 288236769SobrienArch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt) 289236769Sobrien{ 290236769Sobrien char *cp; /* Pointer into line */ 291236769Sobrien GNode *gn; /* New node */ 292236769Sobrien char *libName; /* Library-part of specification */ 293236769Sobrien char *memName; /* Member-part of specification */ 294236769Sobrien char *nameBuf; /* temporary place for node name */ 295236769Sobrien char saveChar; /* Ending delimiter of member-name */ 296236769Sobrien Boolean subLibName; /* TRUE if libName should have/had 297236769Sobrien * variable substitution performed on it */ 298236769Sobrien 299236769Sobrien libName = *linePtr; 300236769Sobrien 301236769Sobrien subLibName = FALSE; 302236769Sobrien 303236769Sobrien for (cp = libName; *cp != '(' && *cp != '\0'; cp++) { 304236769Sobrien if (*cp == '$') { 305236769Sobrien /* 306236769Sobrien * Variable spec, so call the Var module to parse the puppy 307236769Sobrien * so we can safely advance beyond it... 308236769Sobrien */ 309236769Sobrien int length; 310236769Sobrien void *freeIt; 311236769Sobrien char *result; 312236769Sobrien 313236769Sobrien result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt); 314236769Sobrien if (freeIt) 315236769Sobrien free(freeIt); 316236769Sobrien if (result == var_Error) { 317236769Sobrien return(FAILURE); 318236769Sobrien } else { 319236769Sobrien subLibName = TRUE; 320236769Sobrien } 321236769Sobrien 322236769Sobrien cp += length-1; 323236769Sobrien } 324236769Sobrien } 325236769Sobrien 326236769Sobrien *cp++ = '\0'; 327236769Sobrien if (subLibName) { 328236769Sobrien libName = Var_Subst(NULL, libName, ctxt, TRUE); 329236769Sobrien } 330236769Sobrien 331236769Sobrien 332236769Sobrien for (;;) { 333236769Sobrien /* 334236769Sobrien * First skip to the start of the member's name, mark that 335236769Sobrien * place and skip to the end of it (either white-space or 336236769Sobrien * a close paren). 337236769Sobrien */ 338236769Sobrien Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */ 339236769Sobrien 340236769Sobrien while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) { 341236769Sobrien cp++; 342236769Sobrien } 343236769Sobrien memName = cp; 344236769Sobrien while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) { 345236769Sobrien if (*cp == '$') { 346236769Sobrien /* 347236769Sobrien * Variable spec, so call the Var module to parse the puppy 348236769Sobrien * so we can safely advance beyond it... 349236769Sobrien */ 350236769Sobrien int length; 351236769Sobrien void *freeIt; 352236769Sobrien char *result; 353236769Sobrien 354236769Sobrien result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt); 355236769Sobrien if (freeIt) 356236769Sobrien free(freeIt); 357236769Sobrien if (result == var_Error) { 358236769Sobrien return(FAILURE); 359236769Sobrien } else { 360236769Sobrien doSubst = TRUE; 361236769Sobrien } 362236769Sobrien 363236769Sobrien cp += length; 364236769Sobrien } else { 365236769Sobrien cp++; 366236769Sobrien } 367236769Sobrien } 368236769Sobrien 369236769Sobrien /* 370236769Sobrien * If the specification ends without a closing parenthesis, 371236769Sobrien * chances are there's something wrong (like a missing backslash), 372236769Sobrien * so it's better to return failure than allow such things to happen 373236769Sobrien */ 374236769Sobrien if (*cp == '\0') { 375236769Sobrien printf("No closing parenthesis in archive specification\n"); 376236769Sobrien return (FAILURE); 377236769Sobrien } 378236769Sobrien 379236769Sobrien /* 380236769Sobrien * If we didn't move anywhere, we must be done 381236769Sobrien */ 382236769Sobrien if (cp == memName) { 383236769Sobrien break; 384236769Sobrien } 385236769Sobrien 386236769Sobrien saveChar = *cp; 387236769Sobrien *cp = '\0'; 388236769Sobrien 389236769Sobrien /* 390236769Sobrien * XXX: This should be taken care of intelligently by 391236769Sobrien * SuffExpandChildren, both for the archive and the member portions. 392236769Sobrien */ 393236769Sobrien /* 394236769Sobrien * If member contains variables, try and substitute for them. 395236769Sobrien * This will slow down archive specs with dynamic sources, of course, 396236769Sobrien * since we'll be (non-)substituting them three times, but them's 397236769Sobrien * the breaks -- we need to do this since SuffExpandChildren calls 398236769Sobrien * us, otherwise we could assume the thing would be taken care of 399236769Sobrien * later. 400236769Sobrien */ 401236769Sobrien if (doSubst) { 402236769Sobrien char *buf; 403236769Sobrien char *sacrifice; 404236769Sobrien char *oldMemName = memName; 405236769Sobrien size_t sz; 406236769Sobrien 407236769Sobrien memName = Var_Subst(NULL, memName, ctxt, TRUE); 408236769Sobrien 409236769Sobrien /* 410236769Sobrien * Now form an archive spec and recurse to deal with nested 411236769Sobrien * variables and multi-word variable values.... The results 412236769Sobrien * are just placed at the end of the nodeLst we're returning. 413236769Sobrien */ 414236769Sobrien sz = strlen(memName)+strlen(libName)+3; 415236769Sobrien buf = sacrifice = bmake_malloc(sz); 416236769Sobrien 417236769Sobrien snprintf(buf, sz, "%s(%s)", libName, memName); 418236769Sobrien 419236769Sobrien if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) { 420236769Sobrien /* 421236769Sobrien * Must contain dynamic sources, so we can't deal with it now. 422236769Sobrien * Just create an ARCHV node for the thing and let 423236769Sobrien * SuffExpandChildren handle it... 424236769Sobrien */ 425236769Sobrien gn = Targ_FindNode(buf, TARG_CREATE); 426236769Sobrien 427236769Sobrien if (gn == NULL) { 428236769Sobrien free(buf); 429236769Sobrien return(FAILURE); 430236769Sobrien } else { 431236769Sobrien gn->type |= OP_ARCHV; 432236769Sobrien (void)Lst_AtEnd(nodeLst, gn); 433236769Sobrien } 434236769Sobrien } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) { 435236769Sobrien /* 436236769Sobrien * Error in nested call -- free buffer and return FAILURE 437236769Sobrien * ourselves. 438236769Sobrien */ 439236769Sobrien free(buf); 440236769Sobrien return(FAILURE); 441236769Sobrien } 442236769Sobrien /* 443236769Sobrien * Free buffer and continue with our work. 444236769Sobrien */ 445236769Sobrien free(buf); 446236769Sobrien } else if (Dir_HasWildcards(memName)) { 447236769Sobrien Lst members = Lst_Init(FALSE); 448236769Sobrien char *member; 449236769Sobrien size_t sz = MAXPATHLEN, nsz; 450236769Sobrien nameBuf = bmake_malloc(sz); 451236769Sobrien 452236769Sobrien Dir_Expand(memName, dirSearchPath, members); 453236769Sobrien while (!Lst_IsEmpty(members)) { 454236769Sobrien member = (char *)Lst_DeQueue(members); 455236769Sobrien nsz = strlen(libName) + strlen(member) + 3; 456236769Sobrien if (sz > nsz) 457236769Sobrien nameBuf = bmake_realloc(nameBuf, sz = nsz * 2); 458236769Sobrien 459236769Sobrien snprintf(nameBuf, sz, "%s(%s)", libName, member); 460236769Sobrien free(member); 461236769Sobrien gn = Targ_FindNode(nameBuf, TARG_CREATE); 462236769Sobrien if (gn == NULL) { 463236769Sobrien free(nameBuf); 464236769Sobrien return (FAILURE); 465236769Sobrien } else { 466236769Sobrien /* 467236769Sobrien * We've found the node, but have to make sure the rest of 468236769Sobrien * the world knows it's an archive member, without having 469236769Sobrien * to constantly check for parentheses, so we type the 470236769Sobrien * thing with the OP_ARCHV bit before we place it on the 471236769Sobrien * end of the provided list. 472236769Sobrien */ 473236769Sobrien gn->type |= OP_ARCHV; 474236769Sobrien (void)Lst_AtEnd(nodeLst, gn); 475236769Sobrien } 476236769Sobrien } 477236769Sobrien Lst_Destroy(members, NULL); 478236769Sobrien free(nameBuf); 479236769Sobrien } else { 480236769Sobrien size_t sz = strlen(libName) + strlen(memName) + 3; 481236769Sobrien nameBuf = bmake_malloc(sz); 482236769Sobrien snprintf(nameBuf, sz, "%s(%s)", libName, memName); 483236769Sobrien gn = Targ_FindNode(nameBuf, TARG_CREATE); 484236769Sobrien free(nameBuf); 485236769Sobrien if (gn == NULL) { 486236769Sobrien return (FAILURE); 487236769Sobrien } else { 488236769Sobrien /* 489236769Sobrien * We've found the node, but have to make sure the rest of the 490236769Sobrien * world knows it's an archive member, without having to 491236769Sobrien * constantly check for parentheses, so we type the thing with 492236769Sobrien * the OP_ARCHV bit before we place it on the end of the 493236769Sobrien * provided list. 494236769Sobrien */ 495236769Sobrien gn->type |= OP_ARCHV; 496236769Sobrien (void)Lst_AtEnd(nodeLst, gn); 497236769Sobrien } 498236769Sobrien } 499236769Sobrien if (doSubst) { 500236769Sobrien free(memName); 501236769Sobrien } 502236769Sobrien 503236769Sobrien *cp = saveChar; 504236769Sobrien } 505236769Sobrien 506236769Sobrien /* 507236769Sobrien * If substituted libName, free it now, since we need it no longer. 508236769Sobrien */ 509236769Sobrien if (subLibName) { 510236769Sobrien free(libName); 511236769Sobrien } 512236769Sobrien 513236769Sobrien /* 514236769Sobrien * We promised the pointer would be set up at the next non-space, so 515236769Sobrien * we must advance cp there before setting *linePtr... (note that on 516236769Sobrien * entrance to the loop, cp is guaranteed to point at a ')') 517236769Sobrien */ 518236769Sobrien do { 519236769Sobrien cp++; 520236769Sobrien } while (*cp != '\0' && isspace ((unsigned char)*cp)); 521236769Sobrien 522236769Sobrien *linePtr = cp; 523236769Sobrien return (SUCCESS); 524236769Sobrien} 525236769Sobrien 526236769Sobrien/*- 527236769Sobrien *----------------------------------------------------------------------- 528236769Sobrien * ArchFindArchive -- 529236769Sobrien * See if the given archive is the one we are looking for. Called 530236769Sobrien * From ArchStatMember and ArchFindMember via Lst_Find. 531236769Sobrien * 532236769Sobrien * Input: 533236769Sobrien * ar Current list element 534236769Sobrien * archName Name we want 535236769Sobrien * 536236769Sobrien * Results: 537236769Sobrien * 0 if it is, non-zero if it isn't. 538236769Sobrien * 539236769Sobrien * Side Effects: 540236769Sobrien * None. 541236769Sobrien * 542236769Sobrien *----------------------------------------------------------------------- 543236769Sobrien */ 544236769Sobrienstatic int 545236769SobrienArchFindArchive(const void *ar, const void *archName) 546236769Sobrien{ 547236769Sobrien return (strcmp(archName, ((const Arch *)ar)->name)); 548236769Sobrien} 549236769Sobrien 550236769Sobrien/*- 551236769Sobrien *----------------------------------------------------------------------- 552236769Sobrien * ArchStatMember -- 553236769Sobrien * Locate a member of an archive, given the path of the archive and 554236769Sobrien * the path of the desired member. 555236769Sobrien * 556236769Sobrien * Input: 557236769Sobrien * archive Path to the archive 558236769Sobrien * member Name of member. If it is a path, only the last 559236769Sobrien * component is used. 560236769Sobrien * hash TRUE if archive should be hashed if not already so. 561236769Sobrien * 562236769Sobrien * Results: 563236769Sobrien * A pointer to the current struct ar_hdr structure for the member. Note 564236769Sobrien * That no position is returned, so this is not useful for touching 565236769Sobrien * archive members. This is mostly because we have no assurances that 566236769Sobrien * The archive will remain constant after we read all the headers, so 567236769Sobrien * there's not much point in remembering the position... 568236769Sobrien * 569236769Sobrien * Side Effects: 570236769Sobrien * 571236769Sobrien *----------------------------------------------------------------------- 572236769Sobrien */ 573236769Sobrienstatic struct ar_hdr * 574236769SobrienArchStatMember(char *archive, char *member, Boolean hash) 575236769Sobrien{ 576236769Sobrien FILE * arch; /* Stream to archive */ 577236769Sobrien int size; /* Size of archive member */ 578236769Sobrien char *cp; /* Useful character pointer */ 579236769Sobrien char magic[SARMAG]; 580236769Sobrien LstNode ln; /* Lst member containing archive descriptor */ 581236769Sobrien Arch *ar; /* Archive descriptor */ 582236769Sobrien Hash_Entry *he; /* Entry containing member's description */ 583236769Sobrien struct ar_hdr arh; /* archive-member header for reading archive */ 584236769Sobrien char memName[MAXPATHLEN+1]; 585236769Sobrien /* Current member name while hashing. */ 586236769Sobrien 587236769Sobrien /* 588236769Sobrien * Because of space constraints and similar things, files are archived 589236769Sobrien * using their final path components, not the entire thing, so we need 590236769Sobrien * to point 'member' to the final component, if there is one, to make 591236769Sobrien * the comparisons easier... 592236769Sobrien */ 593236769Sobrien cp = strrchr(member, '/'); 594236769Sobrien if (cp != NULL) { 595236769Sobrien member = cp + 1; 596236769Sobrien } 597236769Sobrien 598236769Sobrien ln = Lst_Find(archives, archive, ArchFindArchive); 599236769Sobrien if (ln != NULL) { 600236769Sobrien ar = (Arch *)Lst_Datum(ln); 601236769Sobrien 602236769Sobrien he = Hash_FindEntry(&ar->members, member); 603236769Sobrien 604236769Sobrien if (he != NULL) { 605236769Sobrien return ((struct ar_hdr *)Hash_GetValue(he)); 606236769Sobrien } else { 607236769Sobrien /* Try truncated name */ 608236769Sobrien char copy[AR_MAX_NAME_LEN+1]; 609236769Sobrien size_t len = strlen(member); 610236769Sobrien 611236769Sobrien if (len > AR_MAX_NAME_LEN) { 612236769Sobrien len = AR_MAX_NAME_LEN; 613236769Sobrien strncpy(copy, member, AR_MAX_NAME_LEN); 614236769Sobrien copy[AR_MAX_NAME_LEN] = '\0'; 615236769Sobrien } 616236769Sobrien if ((he = Hash_FindEntry(&ar->members, copy)) != NULL) 617236769Sobrien return ((struct ar_hdr *)Hash_GetValue(he)); 618236769Sobrien return NULL; 619236769Sobrien } 620236769Sobrien } 621236769Sobrien 622236769Sobrien if (!hash) { 623236769Sobrien /* 624236769Sobrien * Caller doesn't want the thing hashed, just use ArchFindMember 625236769Sobrien * to read the header for the member out and close down the stream 626236769Sobrien * again. Since the archive is not to be hashed, we assume there's 627236769Sobrien * no need to allocate extra room for the header we're returning, 628236769Sobrien * so just declare it static. 629236769Sobrien */ 630236769Sobrien static struct ar_hdr sarh; 631236769Sobrien 632236769Sobrien arch = ArchFindMember(archive, member, &sarh, "r"); 633236769Sobrien 634236769Sobrien if (arch == NULL) { 635236769Sobrien return NULL; 636236769Sobrien } else { 637236769Sobrien fclose(arch); 638236769Sobrien return (&sarh); 639236769Sobrien } 640236769Sobrien } 641236769Sobrien 642236769Sobrien /* 643236769Sobrien * We don't have this archive on the list yet, so we want to find out 644236769Sobrien * everything that's in it and cache it so we can get at it quickly. 645236769Sobrien */ 646236769Sobrien arch = fopen(archive, "r"); 647236769Sobrien if (arch == NULL) { 648236769Sobrien return NULL; 649236769Sobrien } 650236769Sobrien 651236769Sobrien /* 652236769Sobrien * We use the ARMAG string to make sure this is an archive we 653236769Sobrien * can handle... 654236769Sobrien */ 655236769Sobrien if ((fread(magic, SARMAG, 1, arch) != 1) || 656236769Sobrien (strncmp(magic, ARMAG, SARMAG) != 0)) { 657236769Sobrien fclose(arch); 658236769Sobrien return NULL; 659236769Sobrien } 660236769Sobrien 661236769Sobrien ar = bmake_malloc(sizeof(Arch)); 662236769Sobrien ar->name = bmake_strdup(archive); 663236769Sobrien ar->fnametab = NULL; 664236769Sobrien ar->fnamesize = 0; 665236769Sobrien Hash_InitTable(&ar->members, -1); 666236769Sobrien memName[AR_MAX_NAME_LEN] = '\0'; 667236769Sobrien 668236769Sobrien while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) { 669236769Sobrien if (strncmp( arh.AR_FMAG, ARFMAG, sizeof(arh.AR_FMAG)) != 0) { 670236769Sobrien /* 671236769Sobrien * The header is bogus, so the archive is bad 672236769Sobrien * and there's no way we can recover... 673236769Sobrien */ 674236769Sobrien goto badarch; 675236769Sobrien } else { 676236769Sobrien /* 677236769Sobrien * We need to advance the stream's pointer to the start of the 678236769Sobrien * next header. Files are padded with newlines to an even-byte 679236769Sobrien * boundary, so we need to extract the size of the file from the 680236769Sobrien * 'size' field of the header and round it up during the seek. 681236769Sobrien */ 682236769Sobrien arh.AR_SIZE[sizeof(arh.AR_SIZE)-1] = '\0'; 683236769Sobrien size = (int)strtol(arh.AR_SIZE, NULL, 10); 684236769Sobrien 685236769Sobrien (void)strncpy(memName, arh.AR_NAME, sizeof(arh.AR_NAME)); 686236769Sobrien for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) { 687236769Sobrien continue; 688236769Sobrien } 689236769Sobrien cp[1] = '\0'; 690236769Sobrien 691236769Sobrien#ifdef SVR4ARCHIVES 692236769Sobrien /* 693236769Sobrien * svr4 names are slash terminated. Also svr4 extended AR format. 694236769Sobrien */ 695236769Sobrien if (memName[0] == '/') { 696236769Sobrien /* 697236769Sobrien * svr4 magic mode; handle it 698236769Sobrien */ 699236769Sobrien switch (ArchSVR4Entry(ar, memName, size, arch)) { 700236769Sobrien case -1: /* Invalid data */ 701236769Sobrien goto badarch; 702236769Sobrien case 0: /* List of files entry */ 703236769Sobrien continue; 704236769Sobrien default: /* Got the entry */ 705236769Sobrien break; 706236769Sobrien } 707236769Sobrien } 708236769Sobrien else { 709236769Sobrien if (cp[0] == '/') 710236769Sobrien cp[0] = '\0'; 711236769Sobrien } 712236769Sobrien#endif 713236769Sobrien 714236769Sobrien#ifdef AR_EFMT1 715236769Sobrien /* 716236769Sobrien * BSD 4.4 extended AR format: #1/<namelen>, with name as the 717236769Sobrien * first <namelen> bytes of the file 718236769Sobrien */ 719236769Sobrien if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 && 720236769Sobrien isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) { 721236769Sobrien 722236769Sobrien unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]); 723236769Sobrien 724236769Sobrien if (elen > MAXPATHLEN) 725236769Sobrien goto badarch; 726236769Sobrien if (fread(memName, elen, 1, arch) != 1) 727236769Sobrien goto badarch; 728236769Sobrien memName[elen] = '\0'; 729236769Sobrien fseek(arch, -elen, SEEK_CUR); 730236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 731236769Sobrien fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName); 732236769Sobrien } 733236769Sobrien } 734236769Sobrien#endif 735236769Sobrien 736236769Sobrien he = Hash_CreateEntry(&ar->members, memName, NULL); 737236769Sobrien Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr))); 738236769Sobrien memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr)); 739236769Sobrien } 740236769Sobrien fseek(arch, (size + 1) & ~1, SEEK_CUR); 741236769Sobrien } 742236769Sobrien 743236769Sobrien fclose(arch); 744236769Sobrien 745236769Sobrien (void)Lst_AtEnd(archives, ar); 746236769Sobrien 747236769Sobrien /* 748236769Sobrien * Now that the archive has been read and cached, we can look into 749236769Sobrien * the hash table to find the desired member's header. 750236769Sobrien */ 751236769Sobrien he = Hash_FindEntry(&ar->members, member); 752236769Sobrien 753236769Sobrien if (he != NULL) { 754236769Sobrien return ((struct ar_hdr *)Hash_GetValue(he)); 755236769Sobrien } else { 756236769Sobrien return NULL; 757236769Sobrien } 758236769Sobrien 759236769Sobrienbadarch: 760236769Sobrien fclose(arch); 761236769Sobrien Hash_DeleteTable(&ar->members); 762236769Sobrien if (ar->fnametab) 763236769Sobrien free(ar->fnametab); 764236769Sobrien free(ar); 765236769Sobrien return NULL; 766236769Sobrien} 767236769Sobrien 768236769Sobrien#ifdef SVR4ARCHIVES 769236769Sobrien/*- 770236769Sobrien *----------------------------------------------------------------------- 771236769Sobrien * ArchSVR4Entry -- 772236769Sobrien * Parse an SVR4 style entry that begins with a slash. 773236769Sobrien * If it is "//", then load the table of filenames 774236769Sobrien * If it is "/<offset>", then try to substitute the long file name 775236769Sobrien * from offset of a table previously read. 776236769Sobrien * 777236769Sobrien * Results: 778236769Sobrien * -1: Bad data in archive 779236769Sobrien * 0: A table was loaded from the file 780236769Sobrien * 1: Name was successfully substituted from table 781236769Sobrien * 2: Name was not successfully substituted from table 782236769Sobrien * 783236769Sobrien * Side Effects: 784236769Sobrien * If a table is read, the file pointer is moved to the next archive 785236769Sobrien * member 786236769Sobrien * 787236769Sobrien *----------------------------------------------------------------------- 788236769Sobrien */ 789236769Sobrienstatic int 790236769SobrienArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch) 791236769Sobrien{ 792236769Sobrien#define ARLONGNAMES1 "//" 793236769Sobrien#define ARLONGNAMES2 "/ARFILENAMES" 794236769Sobrien size_t entry; 795236769Sobrien char *ptr, *eptr; 796236769Sobrien 797236769Sobrien if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 || 798236769Sobrien strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) { 799236769Sobrien 800236769Sobrien if (ar->fnametab != NULL) { 801236769Sobrien if (DEBUG(ARCH)) { 802236769Sobrien fprintf(debug_file, "Attempted to redefine an SVR4 name table\n"); 803236769Sobrien } 804236769Sobrien return -1; 805236769Sobrien } 806236769Sobrien 807236769Sobrien /* 808236769Sobrien * This is a table of archive names, so we build one for 809236769Sobrien * ourselves 810236769Sobrien */ 811236769Sobrien ar->fnametab = bmake_malloc(size); 812236769Sobrien ar->fnamesize = size; 813236769Sobrien 814236769Sobrien if (fread(ar->fnametab, size, 1, arch) != 1) { 815236769Sobrien if (DEBUG(ARCH)) { 816236769Sobrien fprintf(debug_file, "Reading an SVR4 name table failed\n"); 817236769Sobrien } 818236769Sobrien return -1; 819236769Sobrien } 820236769Sobrien eptr = ar->fnametab + size; 821236769Sobrien for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++) 822236769Sobrien switch (*ptr) { 823236769Sobrien case '/': 824236769Sobrien entry++; 825236769Sobrien *ptr = '\0'; 826236769Sobrien break; 827236769Sobrien 828236769Sobrien case '\n': 829236769Sobrien break; 830236769Sobrien 831236769Sobrien default: 832236769Sobrien break; 833236769Sobrien } 834236769Sobrien if (DEBUG(ARCH)) { 835236769Sobrien fprintf(debug_file, "Found svr4 archive name table with %lu entries\n", 836236769Sobrien (u_long)entry); 837236769Sobrien } 838236769Sobrien return 0; 839236769Sobrien } 840236769Sobrien 841236769Sobrien if (name[1] == ' ' || name[1] == '\0') 842236769Sobrien return 2; 843236769Sobrien 844236769Sobrien entry = (size_t)strtol(&name[1], &eptr, 0); 845236769Sobrien if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) { 846236769Sobrien if (DEBUG(ARCH)) { 847236769Sobrien fprintf(debug_file, "Could not parse SVR4 name %s\n", name); 848236769Sobrien } 849236769Sobrien return 2; 850236769Sobrien } 851236769Sobrien if (entry >= ar->fnamesize) { 852236769Sobrien if (DEBUG(ARCH)) { 853236769Sobrien fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n", 854236769Sobrien name, (u_long)ar->fnamesize); 855236769Sobrien } 856236769Sobrien return 2; 857236769Sobrien } 858236769Sobrien 859236769Sobrien if (DEBUG(ARCH)) { 860236769Sobrien fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]); 861236769Sobrien } 862236769Sobrien 863236769Sobrien (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN); 864236769Sobrien name[MAXPATHLEN] = '\0'; 865236769Sobrien return 1; 866236769Sobrien} 867236769Sobrien#endif 868236769Sobrien 869236769Sobrien 870236769Sobrien/*- 871236769Sobrien *----------------------------------------------------------------------- 872236769Sobrien * ArchFindMember -- 873236769Sobrien * Locate a member of an archive, given the path of the archive and 874236769Sobrien * the path of the desired member. If the archive is to be modified, 875236769Sobrien * the mode should be "r+", if not, it should be "r". 876236769Sobrien * 877236769Sobrien * Input: 878236769Sobrien * archive Path to the archive 879236769Sobrien * member Name of member. If it is a path, only the last 880236769Sobrien * component is used. 881236769Sobrien * arhPtr Pointer to header structure to be filled in 882236769Sobrien * mode The mode for opening the stream 883236769Sobrien * 884236769Sobrien * Results: 885236769Sobrien * An FILE *, opened for reading and writing, positioned at the 886236769Sobrien * start of the member's struct ar_hdr, or NULL if the member was 887236769Sobrien * nonexistent. The current struct ar_hdr for member. 888236769Sobrien * 889236769Sobrien * Side Effects: 890236769Sobrien * The passed struct ar_hdr structure is filled in. 891236769Sobrien * 892236769Sobrien *----------------------------------------------------------------------- 893236769Sobrien */ 894236769Sobrienstatic FILE * 895236769SobrienArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr, 896236769Sobrien const char *mode) 897236769Sobrien{ 898236769Sobrien FILE * arch; /* Stream to archive */ 899236769Sobrien int size; /* Size of archive member */ 900236769Sobrien char *cp; /* Useful character pointer */ 901236769Sobrien char magic[SARMAG]; 902236769Sobrien size_t len, tlen; 903236769Sobrien 904236769Sobrien arch = fopen(archive, mode); 905236769Sobrien if (arch == NULL) { 906236769Sobrien return NULL; 907236769Sobrien } 908236769Sobrien 909236769Sobrien /* 910236769Sobrien * We use the ARMAG string to make sure this is an archive we 911236769Sobrien * can handle... 912236769Sobrien */ 913236769Sobrien if ((fread(magic, SARMAG, 1, arch) != 1) || 914236769Sobrien (strncmp(magic, ARMAG, SARMAG) != 0)) { 915236769Sobrien fclose(arch); 916236769Sobrien return NULL; 917236769Sobrien } 918236769Sobrien 919236769Sobrien /* 920236769Sobrien * Because of space constraints and similar things, files are archived 921236769Sobrien * using their final path components, not the entire thing, so we need 922236769Sobrien * to point 'member' to the final component, if there is one, to make 923236769Sobrien * the comparisons easier... 924236769Sobrien */ 925236769Sobrien cp = strrchr(member, '/'); 926236769Sobrien if (cp != NULL) { 927236769Sobrien member = cp + 1; 928236769Sobrien } 929236769Sobrien len = tlen = strlen(member); 930236769Sobrien if (len > sizeof(arhPtr->AR_NAME)) { 931236769Sobrien tlen = sizeof(arhPtr->AR_NAME); 932236769Sobrien } 933236769Sobrien 934236769Sobrien while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) { 935236769Sobrien if (strncmp(arhPtr->AR_FMAG, ARFMAG, sizeof(arhPtr->AR_FMAG) ) != 0) { 936236769Sobrien /* 937236769Sobrien * The header is bogus, so the archive is bad 938236769Sobrien * and there's no way we can recover... 939236769Sobrien */ 940236769Sobrien fclose(arch); 941236769Sobrien return NULL; 942236769Sobrien } else if (strncmp(member, arhPtr->AR_NAME, tlen) == 0) { 943236769Sobrien /* 944236769Sobrien * If the member's name doesn't take up the entire 'name' field, 945236769Sobrien * we have to be careful of matching prefixes. Names are space- 946236769Sobrien * padded to the right, so if the character in 'name' at the end 947236769Sobrien * of the matched string is anything but a space, this isn't the 948236769Sobrien * member we sought. 949236769Sobrien */ 950236769Sobrien if (tlen != sizeof(arhPtr->AR_NAME) && arhPtr->AR_NAME[tlen] != ' '){ 951236769Sobrien goto skip; 952236769Sobrien } else { 953236769Sobrien /* 954236769Sobrien * To make life easier, we reposition the file at the start 955236769Sobrien * of the header we just read before we return the stream. 956236769Sobrien * In a more general situation, it might be better to leave 957236769Sobrien * the file at the actual member, rather than its header, but 958236769Sobrien * not here... 959236769Sobrien */ 960236769Sobrien fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR); 961236769Sobrien return (arch); 962236769Sobrien } 963236769Sobrien } else 964236769Sobrien#ifdef AR_EFMT1 965236769Sobrien /* 966236769Sobrien * BSD 4.4 extended AR format: #1/<namelen>, with name as the 967236769Sobrien * first <namelen> bytes of the file 968236769Sobrien */ 969236769Sobrien if (strncmp(arhPtr->AR_NAME, AR_EFMT1, 970236769Sobrien sizeof(AR_EFMT1) - 1) == 0 && 971236769Sobrien isdigit((unsigned char)arhPtr->AR_NAME[sizeof(AR_EFMT1) - 1])) { 972236769Sobrien 973236769Sobrien unsigned int elen = atoi(&arhPtr->AR_NAME[sizeof(AR_EFMT1)-1]); 974236769Sobrien char ename[MAXPATHLEN + 1]; 975236769Sobrien 976236769Sobrien if (elen > MAXPATHLEN) { 977236769Sobrien fclose(arch); 978236769Sobrien return NULL; 979236769Sobrien } 980236769Sobrien if (fread(ename, elen, 1, arch) != 1) { 981236769Sobrien fclose(arch); 982236769Sobrien return NULL; 983236769Sobrien } 984236769Sobrien ename[elen] = '\0'; 985236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 986236769Sobrien fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename); 987236769Sobrien } 988236769Sobrien if (strncmp(ename, member, len) == 0) { 989236769Sobrien /* Found as extended name */ 990236769Sobrien fseek(arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR); 991236769Sobrien return (arch); 992236769Sobrien } 993236769Sobrien fseek(arch, -elen, SEEK_CUR); 994236769Sobrien goto skip; 995236769Sobrien } else 996236769Sobrien#endif 997236769Sobrien { 998236769Sobrienskip: 999236769Sobrien /* 1000236769Sobrien * This isn't the member we're after, so we need to advance the 1001236769Sobrien * stream's pointer to the start of the next header. Files are 1002236769Sobrien * padded with newlines to an even-byte boundary, so we need to 1003236769Sobrien * extract the size of the file from the 'size' field of the 1004236769Sobrien * header and round it up during the seek. 1005236769Sobrien */ 1006236769Sobrien arhPtr->ar_size[sizeof(arhPtr->AR_SIZE)-1] = '\0'; 1007236769Sobrien size = (int)strtol(arhPtr->AR_SIZE, NULL, 10); 1008236769Sobrien fseek(arch, (size + 1) & ~1, SEEK_CUR); 1009236769Sobrien } 1010236769Sobrien } 1011236769Sobrien 1012236769Sobrien /* 1013236769Sobrien * We've looked everywhere, but the member is not to be found. Close the 1014236769Sobrien * archive and return NULL -- an error. 1015236769Sobrien */ 1016236769Sobrien fclose(arch); 1017236769Sobrien return NULL; 1018236769Sobrien} 1019236769Sobrien 1020236769Sobrien/*- 1021236769Sobrien *----------------------------------------------------------------------- 1022236769Sobrien * Arch_Touch -- 1023236769Sobrien * Touch a member of an archive. 1024236769Sobrien * 1025236769Sobrien * Input: 1026236769Sobrien * gn Node of member to touch 1027236769Sobrien * 1028236769Sobrien * Results: 1029236769Sobrien * The 'time' field of the member's header is updated. 1030236769Sobrien * 1031236769Sobrien * Side Effects: 1032236769Sobrien * The modification time of the entire archive is also changed. 1033236769Sobrien * For a library, this could necessitate the re-ranlib'ing of the 1034236769Sobrien * whole thing. 1035236769Sobrien * 1036236769Sobrien *----------------------------------------------------------------------- 1037236769Sobrien */ 1038236769Sobrienvoid 1039236769SobrienArch_Touch(GNode *gn) 1040236769Sobrien{ 1041236769Sobrien FILE * arch; /* Stream open to archive, positioned properly */ 1042236769Sobrien struct ar_hdr arh; /* Current header describing member */ 1043236769Sobrien char *p1, *p2; 1044236769Sobrien 1045236769Sobrien arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1), 1046236769Sobrien Var_Value(MEMBER, gn, &p2), 1047236769Sobrien &arh, "r+"); 1048236769Sobrien if (p1) 1049236769Sobrien free(p1); 1050236769Sobrien if (p2) 1051236769Sobrien free(p2); 1052236769Sobrien snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now); 1053236769Sobrien 1054236769Sobrien if (arch != NULL) { 1055236769Sobrien (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 1056236769Sobrien fclose(arch); 1057236769Sobrien } 1058236769Sobrien} 1059236769Sobrien 1060236769Sobrien/*- 1061236769Sobrien *----------------------------------------------------------------------- 1062236769Sobrien * Arch_TouchLib -- 1063236769Sobrien * Given a node which represents a library, touch the thing, making 1064236769Sobrien * sure that the table of contents also is touched. 1065236769Sobrien * 1066236769Sobrien * Input: 1067236769Sobrien * gn The node of the library to touch 1068236769Sobrien * 1069236769Sobrien * Results: 1070236769Sobrien * None. 1071236769Sobrien * 1072236769Sobrien * Side Effects: 1073236769Sobrien * Both the modification time of the library and of the RANLIBMAG 1074236769Sobrien * member are set to 'now'. 1075236769Sobrien * 1076236769Sobrien *----------------------------------------------------------------------- 1077236769Sobrien */ 1078236769Sobrienvoid 1079236769Sobrien#if !defined(RANLIBMAG) 1080237578SobrienArch_TouchLib(GNode *gn MAKE_ATTR_UNUSED) 1081236769Sobrien#else 1082236769SobrienArch_TouchLib(GNode *gn) 1083236769Sobrien#endif 1084236769Sobrien{ 1085236769Sobrien#ifdef RANLIBMAG 1086236769Sobrien FILE * arch; /* Stream open to archive */ 1087236769Sobrien struct ar_hdr arh; /* Header describing table of contents */ 1088236769Sobrien struct utimbuf times; /* Times for utime() call */ 1089236769Sobrien 1090236769Sobrien arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+"); 1091236769Sobrien snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now); 1092236769Sobrien 1093236769Sobrien if (arch != NULL) { 1094236769Sobrien (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 1095236769Sobrien fclose(arch); 1096236769Sobrien 1097236769Sobrien times.actime = times.modtime = now; 1098236769Sobrien utime(gn->path, ×); 1099236769Sobrien } 1100236769Sobrien#endif 1101236769Sobrien} 1102236769Sobrien 1103236769Sobrien/*- 1104236769Sobrien *----------------------------------------------------------------------- 1105236769Sobrien * Arch_MTime -- 1106236769Sobrien * Return the modification time of a member of an archive. 1107236769Sobrien * 1108236769Sobrien * Input: 1109236769Sobrien * gn Node describing archive member 1110236769Sobrien * 1111236769Sobrien * Results: 1112236769Sobrien * The modification time(seconds). 1113236769Sobrien * 1114236769Sobrien * Side Effects: 1115236769Sobrien * The mtime field of the given node is filled in with the value 1116236769Sobrien * returned by the function. 1117236769Sobrien * 1118236769Sobrien *----------------------------------------------------------------------- 1119236769Sobrien */ 1120236769Sobrientime_t 1121236769SobrienArch_MTime(GNode *gn) 1122236769Sobrien{ 1123236769Sobrien struct ar_hdr *arhPtr; /* Header of desired member */ 1124236769Sobrien time_t modTime; /* Modification time as an integer */ 1125236769Sobrien char *p1, *p2; 1126236769Sobrien 1127236769Sobrien arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1), 1128236769Sobrien Var_Value(MEMBER, gn, &p2), 1129236769Sobrien TRUE); 1130236769Sobrien if (p1) 1131236769Sobrien free(p1); 1132236769Sobrien if (p2) 1133236769Sobrien free(p2); 1134236769Sobrien 1135236769Sobrien if (arhPtr != NULL) { 1136236769Sobrien modTime = (time_t)strtol(arhPtr->AR_DATE, NULL, 10); 1137236769Sobrien } else { 1138236769Sobrien modTime = 0; 1139236769Sobrien } 1140236769Sobrien 1141236769Sobrien gn->mtime = modTime; 1142236769Sobrien return (modTime); 1143236769Sobrien} 1144236769Sobrien 1145236769Sobrien/*- 1146236769Sobrien *----------------------------------------------------------------------- 1147236769Sobrien * Arch_MemMTime -- 1148236769Sobrien * Given a non-existent archive member's node, get its modification 1149236769Sobrien * time from its archived form, if it exists. 1150236769Sobrien * 1151236769Sobrien * Results: 1152236769Sobrien * The modification time. 1153236769Sobrien * 1154236769Sobrien * Side Effects: 1155236769Sobrien * The mtime field is filled in. 1156236769Sobrien * 1157236769Sobrien *----------------------------------------------------------------------- 1158236769Sobrien */ 1159236769Sobrientime_t 1160236769SobrienArch_MemMTime(GNode *gn) 1161236769Sobrien{ 1162236769Sobrien LstNode ln; 1163236769Sobrien GNode *pgn; 1164236769Sobrien char *nameStart, 1165236769Sobrien *nameEnd; 1166236769Sobrien 1167236769Sobrien if (Lst_Open(gn->parents) != SUCCESS) { 1168236769Sobrien gn->mtime = 0; 1169236769Sobrien return (0); 1170236769Sobrien } 1171236769Sobrien while ((ln = Lst_Next(gn->parents)) != NULL) { 1172236769Sobrien pgn = (GNode *)Lst_Datum(ln); 1173236769Sobrien 1174236769Sobrien if (pgn->type & OP_ARCHV) { 1175236769Sobrien /* 1176236769Sobrien * If the parent is an archive specification and is being made 1177236769Sobrien * and its member's name matches the name of the node we were 1178236769Sobrien * given, record the modification time of the parent in the 1179236769Sobrien * child. We keep searching its parents in case some other 1180236769Sobrien * parent requires this child to exist... 1181236769Sobrien */ 1182236769Sobrien nameStart = strchr(pgn->name, '(') + 1; 1183236769Sobrien nameEnd = strchr(nameStart, ')'); 1184236769Sobrien 1185236769Sobrien if ((pgn->flags & REMAKE) && 1186236769Sobrien strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) { 1187236769Sobrien gn->mtime = Arch_MTime(pgn); 1188236769Sobrien } 1189236769Sobrien } else if (pgn->flags & REMAKE) { 1190236769Sobrien /* 1191236769Sobrien * Something which isn't a library depends on the existence of 1192236769Sobrien * this target, so it needs to exist. 1193236769Sobrien */ 1194236769Sobrien gn->mtime = 0; 1195236769Sobrien break; 1196236769Sobrien } 1197236769Sobrien } 1198236769Sobrien 1199236769Sobrien Lst_Close(gn->parents); 1200236769Sobrien 1201236769Sobrien return (gn->mtime); 1202236769Sobrien} 1203236769Sobrien 1204236769Sobrien/*- 1205236769Sobrien *----------------------------------------------------------------------- 1206236769Sobrien * Arch_FindLib -- 1207236769Sobrien * Search for a library along the given search path. 1208236769Sobrien * 1209236769Sobrien * Input: 1210236769Sobrien * gn Node of library to find 1211236769Sobrien * path Search path 1212236769Sobrien * 1213236769Sobrien * Results: 1214236769Sobrien * None. 1215236769Sobrien * 1216236769Sobrien * Side Effects: 1217236769Sobrien * The node's 'path' field is set to the found path (including the 1218236769Sobrien * actual file name, not -l...). If the system can handle the -L 1219236769Sobrien * flag when linking (or we cannot find the library), we assume that 1220236769Sobrien * the user has placed the .LIBRARIES variable in the final linking 1221236769Sobrien * command (or the linker will know where to find it) and set the 1222236769Sobrien * TARGET variable for this node to be the node's name. Otherwise, 1223236769Sobrien * we set the TARGET variable to be the full path of the library, 1224236769Sobrien * as returned by Dir_FindFile. 1225236769Sobrien * 1226236769Sobrien *----------------------------------------------------------------------- 1227236769Sobrien */ 1228236769Sobrienvoid 1229236769SobrienArch_FindLib(GNode *gn, Lst path) 1230236769Sobrien{ 1231236769Sobrien char *libName; /* file name for archive */ 1232236769Sobrien size_t sz = strlen(gn->name) + 6 - 2; 1233236769Sobrien 1234236769Sobrien libName = bmake_malloc(sz); 1235236769Sobrien snprintf(libName, sz, "lib%s.a", &gn->name[2]); 1236236769Sobrien 1237236769Sobrien gn->path = Dir_FindFile(libName, path); 1238236769Sobrien 1239236769Sobrien free(libName); 1240236769Sobrien 1241236769Sobrien#ifdef LIBRARIES 1242236769Sobrien Var_Set(TARGET, gn->name, gn, 0); 1243236769Sobrien#else 1244236769Sobrien Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn, 0); 1245236769Sobrien#endif /* LIBRARIES */ 1246236769Sobrien} 1247236769Sobrien 1248236769Sobrien/*- 1249236769Sobrien *----------------------------------------------------------------------- 1250236769Sobrien * Arch_LibOODate -- 1251236769Sobrien * Decide if a node with the OP_LIB attribute is out-of-date. Called 1252236769Sobrien * from Make_OODate to make its life easier. 1253236769Sobrien * 1254236769Sobrien * There are several ways for a library to be out-of-date that are 1255236769Sobrien * not available to ordinary files. In addition, there are ways 1256236769Sobrien * that are open to regular files that are not available to 1257236769Sobrien * libraries. A library that is only used as a source is never 1258236769Sobrien * considered out-of-date by itself. This does not preclude the 1259236769Sobrien * library's modification time from making its parent be out-of-date. 1260236769Sobrien * A library will be considered out-of-date for any of these reasons, 1261236769Sobrien * given that it is a target on a dependency line somewhere: 1262236769Sobrien * Its modification time is less than that of one of its 1263236769Sobrien * sources (gn->mtime < gn->cmgn->mtime). 1264236769Sobrien * Its modification time is greater than the time at which the 1265236769Sobrien * make began (i.e. it's been modified in the course 1266236769Sobrien * of the make, probably by archiving). 1267236769Sobrien * The modification time of one of its sources is greater than 1268236769Sobrien * the one of its RANLIBMAG member (i.e. its table of contents 1269236769Sobrien * is out-of-date). We don't compare of the archive time 1270236769Sobrien * vs. TOC time because they can be too close. In my 1271236769Sobrien * opinion we should not bother with the TOC at all since 1272236769Sobrien * this is used by 'ar' rules that affect the data contents 1273236769Sobrien * of the archive, not by ranlib rules, which affect the 1274236769Sobrien * TOC. 1275236769Sobrien * 1276236769Sobrien * Input: 1277236769Sobrien * gn The library's graph node 1278236769Sobrien * 1279236769Sobrien * Results: 1280236769Sobrien * TRUE if the library is out-of-date. FALSE otherwise. 1281236769Sobrien * 1282236769Sobrien * Side Effects: 1283236769Sobrien * The library will be hashed if it hasn't been already. 1284236769Sobrien * 1285236769Sobrien *----------------------------------------------------------------------- 1286236769Sobrien */ 1287236769SobrienBoolean 1288236769SobrienArch_LibOODate(GNode *gn) 1289236769Sobrien{ 1290236769Sobrien Boolean oodate; 1291236769Sobrien 1292236769Sobrien if (gn->type & OP_PHONY) { 1293236769Sobrien oodate = TRUE; 1294236769Sobrien } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) { 1295236769Sobrien oodate = FALSE; 1296236769Sobrien } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) || 1297236769Sobrien (gn->mtime > now) || 1298236769Sobrien (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) { 1299236769Sobrien oodate = TRUE; 1300236769Sobrien } else { 1301236769Sobrien#ifdef RANLIBMAG 1302236769Sobrien struct ar_hdr *arhPtr; /* Header for __.SYMDEF */ 1303236769Sobrien int modTimeTOC; /* The table-of-contents's mod time */ 1304236769Sobrien 1305236769Sobrien arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE); 1306236769Sobrien 1307236769Sobrien if (arhPtr != NULL) { 1308236769Sobrien modTimeTOC = (int)strtol(arhPtr->AR_DATE, NULL, 10); 1309236769Sobrien 1310236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 1311236769Sobrien fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC)); 1312236769Sobrien } 1313236769Sobrien oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC); 1314236769Sobrien } else { 1315236769Sobrien /* 1316236769Sobrien * A library w/o a table of contents is out-of-date 1317236769Sobrien */ 1318236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 1319236769Sobrien fprintf(debug_file, "No t.o.c...."); 1320236769Sobrien } 1321236769Sobrien oodate = TRUE; 1322236769Sobrien } 1323236769Sobrien#else 1324236769Sobrien oodate = FALSE; 1325236769Sobrien#endif 1326236769Sobrien } 1327236769Sobrien return (oodate); 1328236769Sobrien} 1329236769Sobrien 1330236769Sobrien/*- 1331236769Sobrien *----------------------------------------------------------------------- 1332236769Sobrien * Arch_Init -- 1333236769Sobrien * Initialize things for this module. 1334236769Sobrien * 1335236769Sobrien * Results: 1336236769Sobrien * None. 1337236769Sobrien * 1338236769Sobrien * Side Effects: 1339236769Sobrien * The 'archives' list is initialized. 1340236769Sobrien * 1341236769Sobrien *----------------------------------------------------------------------- 1342236769Sobrien */ 1343236769Sobrienvoid 1344236769SobrienArch_Init(void) 1345236769Sobrien{ 1346236769Sobrien archives = Lst_Init(FALSE); 1347236769Sobrien} 1348236769Sobrien 1349236769Sobrien 1350236769Sobrien 1351236769Sobrien/*- 1352236769Sobrien *----------------------------------------------------------------------- 1353236769Sobrien * Arch_End -- 1354236769Sobrien * Cleanup things for this module. 1355236769Sobrien * 1356236769Sobrien * Results: 1357236769Sobrien * None. 1358236769Sobrien * 1359236769Sobrien * Side Effects: 1360236769Sobrien * The 'archives' list is freed 1361236769Sobrien * 1362236769Sobrien *----------------------------------------------------------------------- 1363236769Sobrien */ 1364236769Sobrienvoid 1365236769SobrienArch_End(void) 1366236769Sobrien{ 1367236769Sobrien#ifdef CLEANUP 1368236769Sobrien Lst_Destroy(archives, ArchFree); 1369236769Sobrien#endif 1370236769Sobrien} 1371236769Sobrien 1372236769Sobrien/*- 1373236769Sobrien *----------------------------------------------------------------------- 1374236769Sobrien * Arch_IsLib -- 1375236769Sobrien * Check if the node is a library 1376236769Sobrien * 1377236769Sobrien * Results: 1378236769Sobrien * True or False. 1379236769Sobrien * 1380236769Sobrien * Side Effects: 1381236769Sobrien * None. 1382236769Sobrien * 1383236769Sobrien *----------------------------------------------------------------------- 1384236769Sobrien */ 1385236769Sobrienint 1386236769SobrienArch_IsLib(GNode *gn) 1387236769Sobrien{ 1388236769Sobrien static const char armag[] = "!<arch>\n"; 1389236769Sobrien char buf[sizeof(armag)-1]; 1390236769Sobrien int fd; 1391236769Sobrien 1392236769Sobrien if ((fd = open(gn->path, O_RDONLY)) == -1) 1393236769Sobrien return FALSE; 1394236769Sobrien 1395236769Sobrien if (read(fd, buf, sizeof(buf)) != sizeof(buf)) { 1396236769Sobrien (void)close(fd); 1397236769Sobrien return FALSE; 1398236769Sobrien } 1399236769Sobrien 1400236769Sobrien (void)close(fd); 1401236769Sobrien 1402236769Sobrien return memcmp(buf, armag, sizeof(buf)) == 0; 1403236769Sobrien} 1404