1321964Ssjg/* $NetBSD: arch.c,v 1.70 2017/04/16 20:49:09 riastradh 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 72321964Ssjgstatic char rcsid[] = "$NetBSD: arch.c,v 1.70 2017/04/16 20:49:09 riastradh 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 79321964Ssjg__RCSID("$NetBSD: arch.c,v 1.70 2017/04/16 20:49:09 riastradh 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#ifdef HAVE_AR_H 140236769Sobrien#include <ar.h> 141236769Sobrien#else 142236769Sobrienstruct ar_hdr { 143236769Sobrien char ar_name[16]; /* name */ 144236769Sobrien char ar_date[12]; /* modification time */ 145236769Sobrien char ar_uid[6]; /* user id */ 146236769Sobrien char ar_gid[6]; /* group id */ 147236769Sobrien char ar_mode[8]; /* octal file permissions */ 148236769Sobrien char ar_size[10]; /* size in bytes */ 149236769Sobrien#ifndef ARFMAG 150236769Sobrien#define ARFMAG "`\n" 151236769Sobrien#endif 152236769Sobrien char ar_fmag[2]; /* consistency check */ 153236769Sobrien}; 154236769Sobrien#endif 155236769Sobrien#if defined(HAVE_RANLIB_H) && !(defined(__ELF__) || defined(NO_RANLIB)) 156236769Sobrien#include <ranlib.h> 157236769Sobrien#endif 158236769Sobrien#include <stdio.h> 159236769Sobrien#include <stdlib.h> 160236769Sobrien#ifdef HAVE_UTIME_H 161236769Sobrien#include <utime.h> 162236769Sobrien#endif 163236769Sobrien 164236769Sobrien#include "make.h" 165236769Sobrien#include "hash.h" 166236769Sobrien#include "dir.h" 167236769Sobrien 168236769Sobrien#ifdef TARGET_MACHINE 169236769Sobrien#undef MAKE_MACHINE 170236769Sobrien#define MAKE_MACHINE TARGET_MACHINE 171236769Sobrien#endif 172236769Sobrien#ifdef TARGET_MACHINE_ARCH 173236769Sobrien#undef MAKE_MACHINE_ARCH 174236769Sobrien#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH 175236769Sobrien#endif 176236769Sobrien 177236769Sobrienstatic Lst archives; /* Lst of archives we've already examined */ 178236769Sobrien 179236769Sobrientypedef struct Arch { 180236769Sobrien char *name; /* Name of archive */ 181236769Sobrien Hash_Table members; /* All the members of the archive described 182236769Sobrien * by <name, struct ar_hdr *> key/value pairs */ 183236769Sobrien char *fnametab; /* Extended name table strings */ 184236769Sobrien size_t fnamesize; /* Size of the string table */ 185236769Sobrien} Arch; 186236769Sobrien 187236769Sobrienstatic int ArchFindArchive(const void *, const void *); 188236769Sobrien#ifdef CLEANUP 189236769Sobrienstatic void ArchFree(void *); 190236769Sobrien#endif 191236769Sobrienstatic struct ar_hdr *ArchStatMember(char *, char *, Boolean); 192236769Sobrienstatic FILE *ArchFindMember(char *, char *, struct ar_hdr *, const char *); 193236769Sobrien#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__) 194236769Sobrien#define SVR4ARCHIVES 195236769Sobrienstatic int ArchSVR4Entry(Arch *, char *, size_t, FILE *); 196236769Sobrien#endif 197236769Sobrien 198236769Sobrien 199236769Sobrien#if defined(_AIX) 200236769Sobrien# define AR_NAME _ar_name.ar_name 201236769Sobrien# define AR_FMAG _ar_name.ar_fmag 202236769Sobrien# define SARMAG SAIAMAG 203236769Sobrien# define ARMAG AIAMAG 204236769Sobrien# define ARFMAG AIAFMAG 205236769Sobrien#endif 206236769Sobrien#ifndef AR_NAME 207236769Sobrien# define AR_NAME ar_name 208236769Sobrien#endif 209236769Sobrien#ifndef AR_DATE 210236769Sobrien# define AR_DATE ar_date 211236769Sobrien#endif 212236769Sobrien#ifndef AR_SIZE 213236769Sobrien# define AR_SIZE ar_size 214236769Sobrien#endif 215236769Sobrien#ifndef AR_FMAG 216236769Sobrien# define AR_FMAG ar_fmag 217236769Sobrien#endif 218236769Sobrien#ifndef ARMAG 219236769Sobrien# define ARMAG "!<arch>\n" 220236769Sobrien#endif 221236769Sobrien#ifndef SARMAG 222236769Sobrien# define SARMAG 8 223236769Sobrien#endif 224236769Sobrien 225236769Sobrien#define AR_MAX_NAME_LEN (sizeof(arh.AR_NAME)-1) 226236769Sobrien 227236769Sobrien#ifdef CLEANUP 228236769Sobrien/*- 229236769Sobrien *----------------------------------------------------------------------- 230236769Sobrien * ArchFree -- 231236769Sobrien * Free memory used by an archive 232236769Sobrien * 233236769Sobrien * Results: 234236769Sobrien * None. 235236769Sobrien * 236236769Sobrien * Side Effects: 237236769Sobrien * None. 238236769Sobrien * 239236769Sobrien *----------------------------------------------------------------------- 240236769Sobrien */ 241236769Sobrienstatic void 242236769SobrienArchFree(void *ap) 243236769Sobrien{ 244236769Sobrien Arch *a = (Arch *)ap; 245236769Sobrien Hash_Search search; 246236769Sobrien Hash_Entry *entry; 247236769Sobrien 248236769Sobrien /* Free memory from hash entries */ 249236769Sobrien for (entry = Hash_EnumFirst(&a->members, &search); 250236769Sobrien entry != NULL; 251236769Sobrien entry = Hash_EnumNext(&search)) 252236769Sobrien free(Hash_GetValue(entry)); 253236769Sobrien 254236769Sobrien free(a->name); 255321964Ssjg free(a->fnametab); 256236769Sobrien Hash_DeleteTable(&a->members); 257236769Sobrien free(a); 258236769Sobrien} 259236769Sobrien#endif 260236769Sobrien 261236769Sobrien 262236769Sobrien/*- 263236769Sobrien *----------------------------------------------------------------------- 264236769Sobrien * Arch_ParseArchive -- 265236769Sobrien * Parse the archive specification in the given line and find/create 266236769Sobrien * the nodes for the specified archive members, placing their nodes 267236769Sobrien * on the given list. 268236769Sobrien * 269236769Sobrien * Input: 270236769Sobrien * linePtr Pointer to start of specification 271236769Sobrien * nodeLst Lst on which to place the nodes 272236769Sobrien * ctxt Context in which to expand variables 273236769Sobrien * 274236769Sobrien * Results: 275236769Sobrien * SUCCESS if it was a valid specification. The linePtr is updated 276236769Sobrien * to point to the first non-space after the archive spec. The 277236769Sobrien * nodes for the members are placed on the given list. 278236769Sobrien * 279236769Sobrien * Side Effects: 280236769Sobrien * Some nodes may be created. The given list is extended. 281236769Sobrien * 282236769Sobrien *----------------------------------------------------------------------- 283236769Sobrien */ 284236769SobrienReturnStatus 285236769SobrienArch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt) 286236769Sobrien{ 287236769Sobrien char *cp; /* Pointer into line */ 288236769Sobrien GNode *gn; /* New node */ 289236769Sobrien char *libName; /* Library-part of specification */ 290236769Sobrien char *memName; /* Member-part of specification */ 291236769Sobrien char *nameBuf; /* temporary place for node name */ 292236769Sobrien char saveChar; /* Ending delimiter of member-name */ 293236769Sobrien Boolean subLibName; /* TRUE if libName should have/had 294236769Sobrien * variable substitution performed on it */ 295236769Sobrien 296236769Sobrien libName = *linePtr; 297236769Sobrien 298236769Sobrien subLibName = FALSE; 299236769Sobrien 300236769Sobrien for (cp = libName; *cp != '(' && *cp != '\0'; cp++) { 301236769Sobrien if (*cp == '$') { 302236769Sobrien /* 303236769Sobrien * Variable spec, so call the Var module to parse the puppy 304236769Sobrien * so we can safely advance beyond it... 305236769Sobrien */ 306236769Sobrien int length; 307236769Sobrien void *freeIt; 308236769Sobrien char *result; 309236769Sobrien 310321964Ssjg result = Var_Parse(cp, ctxt, VARF_UNDEFERR|VARF_WANTRES, 311321964Ssjg &length, &freeIt); 312321964Ssjg free(freeIt); 313321964Ssjg 314236769Sobrien if (result == var_Error) { 315236769Sobrien return(FAILURE); 316236769Sobrien } else { 317236769Sobrien subLibName = TRUE; 318236769Sobrien } 319236769Sobrien 320236769Sobrien cp += length-1; 321236769Sobrien } 322236769Sobrien } 323236769Sobrien 324236769Sobrien *cp++ = '\0'; 325236769Sobrien if (subLibName) { 326321964Ssjg libName = Var_Subst(NULL, libName, ctxt, VARF_UNDEFERR|VARF_WANTRES); 327236769Sobrien } 328236769Sobrien 329236769Sobrien 330236769Sobrien for (;;) { 331236769Sobrien /* 332236769Sobrien * First skip to the start of the member's name, mark that 333236769Sobrien * place and skip to the end of it (either white-space or 334236769Sobrien * a close paren). 335236769Sobrien */ 336236769Sobrien Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */ 337236769Sobrien 338236769Sobrien while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) { 339236769Sobrien cp++; 340236769Sobrien } 341236769Sobrien memName = cp; 342236769Sobrien while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) { 343236769Sobrien if (*cp == '$') { 344236769Sobrien /* 345236769Sobrien * Variable spec, so call the Var module to parse the puppy 346236769Sobrien * so we can safely advance beyond it... 347236769Sobrien */ 348236769Sobrien int length; 349236769Sobrien void *freeIt; 350236769Sobrien char *result; 351236769Sobrien 352321964Ssjg result = Var_Parse(cp, ctxt, VARF_UNDEFERR|VARF_WANTRES, 353321964Ssjg &length, &freeIt); 354321964Ssjg free(freeIt); 355321964Ssjg 356236769Sobrien if (result == var_Error) { 357236769Sobrien return(FAILURE); 358236769Sobrien } else { 359236769Sobrien doSubst = TRUE; 360236769Sobrien } 361236769Sobrien 362236769Sobrien cp += length; 363236769Sobrien } else { 364236769Sobrien cp++; 365236769Sobrien } 366236769Sobrien } 367236769Sobrien 368236769Sobrien /* 369236769Sobrien * If the specification ends without a closing parenthesis, 370236769Sobrien * chances are there's something wrong (like a missing backslash), 371236769Sobrien * so it's better to return failure than allow such things to happen 372236769Sobrien */ 373236769Sobrien if (*cp == '\0') { 374236769Sobrien printf("No closing parenthesis in archive specification\n"); 375236769Sobrien return (FAILURE); 376236769Sobrien } 377236769Sobrien 378236769Sobrien /* 379236769Sobrien * If we didn't move anywhere, we must be done 380236769Sobrien */ 381236769Sobrien if (cp == memName) { 382236769Sobrien break; 383236769Sobrien } 384236769Sobrien 385236769Sobrien saveChar = *cp; 386236769Sobrien *cp = '\0'; 387236769Sobrien 388236769Sobrien /* 389236769Sobrien * XXX: This should be taken care of intelligently by 390236769Sobrien * SuffExpandChildren, both for the archive and the member portions. 391236769Sobrien */ 392236769Sobrien /* 393236769Sobrien * If member contains variables, try and substitute for them. 394236769Sobrien * This will slow down archive specs with dynamic sources, of course, 395236769Sobrien * since we'll be (non-)substituting them three times, but them's 396236769Sobrien * the breaks -- we need to do this since SuffExpandChildren calls 397236769Sobrien * us, otherwise we could assume the thing would be taken care of 398236769Sobrien * later. 399236769Sobrien */ 400236769Sobrien if (doSubst) { 401236769Sobrien char *buf; 402236769Sobrien char *sacrifice; 403236769Sobrien char *oldMemName = memName; 404236769Sobrien size_t sz; 405236769Sobrien 406321964Ssjg memName = Var_Subst(NULL, memName, ctxt, 407321964Ssjg VARF_UNDEFERR|VARF_WANTRES); 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'; 729321964Ssjg if (fseek(arch, -elen, SEEK_CUR) != 0) 730321964Ssjg goto badarch; 731236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 732236769Sobrien fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName); 733236769Sobrien } 734236769Sobrien } 735236769Sobrien#endif 736236769Sobrien 737236769Sobrien he = Hash_CreateEntry(&ar->members, memName, NULL); 738236769Sobrien Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr))); 739236769Sobrien memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr)); 740236769Sobrien } 741321964Ssjg if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) 742321964Ssjg goto badarch; 743236769Sobrien } 744236769Sobrien 745236769Sobrien fclose(arch); 746236769Sobrien 747236769Sobrien (void)Lst_AtEnd(archives, ar); 748236769Sobrien 749236769Sobrien /* 750236769Sobrien * Now that the archive has been read and cached, we can look into 751236769Sobrien * the hash table to find the desired member's header. 752236769Sobrien */ 753236769Sobrien he = Hash_FindEntry(&ar->members, member); 754236769Sobrien 755236769Sobrien if (he != NULL) { 756236769Sobrien return ((struct ar_hdr *)Hash_GetValue(he)); 757236769Sobrien } else { 758236769Sobrien return NULL; 759236769Sobrien } 760236769Sobrien 761236769Sobrienbadarch: 762236769Sobrien fclose(arch); 763236769Sobrien Hash_DeleteTable(&ar->members); 764321964Ssjg free(ar->fnametab); 765236769Sobrien free(ar); 766236769Sobrien return NULL; 767236769Sobrien} 768236769Sobrien 769236769Sobrien#ifdef SVR4ARCHIVES 770236769Sobrien/*- 771236769Sobrien *----------------------------------------------------------------------- 772236769Sobrien * ArchSVR4Entry -- 773236769Sobrien * Parse an SVR4 style entry that begins with a slash. 774236769Sobrien * If it is "//", then load the table of filenames 775236769Sobrien * If it is "/<offset>", then try to substitute the long file name 776236769Sobrien * from offset of a table previously read. 777236769Sobrien * 778236769Sobrien * Results: 779236769Sobrien * -1: Bad data in archive 780236769Sobrien * 0: A table was loaded from the file 781236769Sobrien * 1: Name was successfully substituted from table 782236769Sobrien * 2: Name was not successfully substituted from table 783236769Sobrien * 784236769Sobrien * Side Effects: 785236769Sobrien * If a table is read, the file pointer is moved to the next archive 786236769Sobrien * member 787236769Sobrien * 788236769Sobrien *----------------------------------------------------------------------- 789236769Sobrien */ 790236769Sobrienstatic int 791236769SobrienArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch) 792236769Sobrien{ 793236769Sobrien#define ARLONGNAMES1 "//" 794236769Sobrien#define ARLONGNAMES2 "/ARFILENAMES" 795236769Sobrien size_t entry; 796236769Sobrien char *ptr, *eptr; 797236769Sobrien 798236769Sobrien if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 || 799236769Sobrien strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) { 800236769Sobrien 801236769Sobrien if (ar->fnametab != NULL) { 802236769Sobrien if (DEBUG(ARCH)) { 803236769Sobrien fprintf(debug_file, "Attempted to redefine an SVR4 name table\n"); 804236769Sobrien } 805236769Sobrien return -1; 806236769Sobrien } 807236769Sobrien 808236769Sobrien /* 809236769Sobrien * This is a table of archive names, so we build one for 810236769Sobrien * ourselves 811236769Sobrien */ 812236769Sobrien ar->fnametab = bmake_malloc(size); 813236769Sobrien ar->fnamesize = size; 814236769Sobrien 815236769Sobrien if (fread(ar->fnametab, size, 1, arch) != 1) { 816236769Sobrien if (DEBUG(ARCH)) { 817236769Sobrien fprintf(debug_file, "Reading an SVR4 name table failed\n"); 818236769Sobrien } 819236769Sobrien return -1; 820236769Sobrien } 821236769Sobrien eptr = ar->fnametab + size; 822236769Sobrien for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++) 823236769Sobrien switch (*ptr) { 824236769Sobrien case '/': 825236769Sobrien entry++; 826236769Sobrien *ptr = '\0'; 827236769Sobrien break; 828236769Sobrien 829236769Sobrien case '\n': 830236769Sobrien break; 831236769Sobrien 832236769Sobrien default: 833236769Sobrien break; 834236769Sobrien } 835236769Sobrien if (DEBUG(ARCH)) { 836236769Sobrien fprintf(debug_file, "Found svr4 archive name table with %lu entries\n", 837321964Ssjg (unsigned long)entry); 838236769Sobrien } 839236769Sobrien return 0; 840236769Sobrien } 841236769Sobrien 842236769Sobrien if (name[1] == ' ' || name[1] == '\0') 843236769Sobrien return 2; 844236769Sobrien 845236769Sobrien entry = (size_t)strtol(&name[1], &eptr, 0); 846236769Sobrien if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) { 847236769Sobrien if (DEBUG(ARCH)) { 848236769Sobrien fprintf(debug_file, "Could not parse SVR4 name %s\n", name); 849236769Sobrien } 850236769Sobrien return 2; 851236769Sobrien } 852236769Sobrien if (entry >= ar->fnamesize) { 853236769Sobrien if (DEBUG(ARCH)) { 854236769Sobrien fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n", 855321964Ssjg name, (unsigned long)ar->fnamesize); 856236769Sobrien } 857236769Sobrien return 2; 858236769Sobrien } 859236769Sobrien 860236769Sobrien if (DEBUG(ARCH)) { 861236769Sobrien fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]); 862236769Sobrien } 863236769Sobrien 864236769Sobrien (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN); 865236769Sobrien name[MAXPATHLEN] = '\0'; 866236769Sobrien return 1; 867236769Sobrien} 868236769Sobrien#endif 869236769Sobrien 870236769Sobrien 871236769Sobrien/*- 872236769Sobrien *----------------------------------------------------------------------- 873236769Sobrien * ArchFindMember -- 874236769Sobrien * Locate a member of an archive, given the path of the archive and 875236769Sobrien * the path of the desired member. If the archive is to be modified, 876236769Sobrien * the mode should be "r+", if not, it should be "r". 877236769Sobrien * 878236769Sobrien * Input: 879236769Sobrien * archive Path to the archive 880236769Sobrien * member Name of member. If it is a path, only the last 881236769Sobrien * component is used. 882236769Sobrien * arhPtr Pointer to header structure to be filled in 883236769Sobrien * mode The mode for opening the stream 884236769Sobrien * 885236769Sobrien * Results: 886236769Sobrien * An FILE *, opened for reading and writing, positioned at the 887236769Sobrien * start of the member's struct ar_hdr, or NULL if the member was 888236769Sobrien * nonexistent. The current struct ar_hdr for member. 889236769Sobrien * 890236769Sobrien * Side Effects: 891236769Sobrien * The passed struct ar_hdr structure is filled in. 892236769Sobrien * 893236769Sobrien *----------------------------------------------------------------------- 894236769Sobrien */ 895236769Sobrienstatic FILE * 896236769SobrienArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr, 897236769Sobrien const char *mode) 898236769Sobrien{ 899236769Sobrien FILE * arch; /* Stream to archive */ 900236769Sobrien int size; /* Size of archive member */ 901236769Sobrien char *cp; /* Useful character pointer */ 902236769Sobrien char magic[SARMAG]; 903236769Sobrien size_t len, tlen; 904236769Sobrien 905236769Sobrien arch = fopen(archive, mode); 906236769Sobrien if (arch == NULL) { 907236769Sobrien return NULL; 908236769Sobrien } 909236769Sobrien 910236769Sobrien /* 911236769Sobrien * We use the ARMAG string to make sure this is an archive we 912236769Sobrien * can handle... 913236769Sobrien */ 914236769Sobrien if ((fread(magic, SARMAG, 1, arch) != 1) || 915236769Sobrien (strncmp(magic, ARMAG, SARMAG) != 0)) { 916236769Sobrien fclose(arch); 917236769Sobrien return NULL; 918236769Sobrien } 919236769Sobrien 920236769Sobrien /* 921236769Sobrien * Because of space constraints and similar things, files are archived 922236769Sobrien * using their final path components, not the entire thing, so we need 923236769Sobrien * to point 'member' to the final component, if there is one, to make 924236769Sobrien * the comparisons easier... 925236769Sobrien */ 926236769Sobrien cp = strrchr(member, '/'); 927236769Sobrien if (cp != NULL) { 928236769Sobrien member = cp + 1; 929236769Sobrien } 930236769Sobrien len = tlen = strlen(member); 931236769Sobrien if (len > sizeof(arhPtr->AR_NAME)) { 932236769Sobrien tlen = sizeof(arhPtr->AR_NAME); 933236769Sobrien } 934236769Sobrien 935236769Sobrien while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) { 936236769Sobrien if (strncmp(arhPtr->AR_FMAG, ARFMAG, sizeof(arhPtr->AR_FMAG) ) != 0) { 937236769Sobrien /* 938236769Sobrien * The header is bogus, so the archive is bad 939236769Sobrien * and there's no way we can recover... 940236769Sobrien */ 941236769Sobrien fclose(arch); 942236769Sobrien return NULL; 943236769Sobrien } else if (strncmp(member, arhPtr->AR_NAME, tlen) == 0) { 944236769Sobrien /* 945236769Sobrien * If the member's name doesn't take up the entire 'name' field, 946236769Sobrien * we have to be careful of matching prefixes. Names are space- 947236769Sobrien * padded to the right, so if the character in 'name' at the end 948236769Sobrien * of the matched string is anything but a space, this isn't the 949236769Sobrien * member we sought. 950236769Sobrien */ 951236769Sobrien if (tlen != sizeof(arhPtr->AR_NAME) && arhPtr->AR_NAME[tlen] != ' '){ 952236769Sobrien goto skip; 953236769Sobrien } else { 954236769Sobrien /* 955236769Sobrien * To make life easier, we reposition the file at the start 956236769Sobrien * of the header we just read before we return the stream. 957236769Sobrien * In a more general situation, it might be better to leave 958236769Sobrien * the file at the actual member, rather than its header, but 959236769Sobrien * not here... 960236769Sobrien */ 961321964Ssjg if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) != 0) { 962321964Ssjg fclose(arch); 963321964Ssjg return NULL; 964321964Ssjg } 965236769Sobrien return (arch); 966236769Sobrien } 967236769Sobrien } else 968236769Sobrien#ifdef AR_EFMT1 969236769Sobrien /* 970236769Sobrien * BSD 4.4 extended AR format: #1/<namelen>, with name as the 971236769Sobrien * first <namelen> bytes of the file 972236769Sobrien */ 973236769Sobrien if (strncmp(arhPtr->AR_NAME, AR_EFMT1, 974236769Sobrien sizeof(AR_EFMT1) - 1) == 0 && 975236769Sobrien isdigit((unsigned char)arhPtr->AR_NAME[sizeof(AR_EFMT1) - 1])) { 976236769Sobrien 977236769Sobrien unsigned int elen = atoi(&arhPtr->AR_NAME[sizeof(AR_EFMT1)-1]); 978236769Sobrien char ename[MAXPATHLEN + 1]; 979236769Sobrien 980236769Sobrien if (elen > MAXPATHLEN) { 981236769Sobrien fclose(arch); 982236769Sobrien return NULL; 983236769Sobrien } 984236769Sobrien if (fread(ename, elen, 1, arch) != 1) { 985236769Sobrien fclose(arch); 986236769Sobrien return NULL; 987236769Sobrien } 988236769Sobrien ename[elen] = '\0'; 989236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 990236769Sobrien fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename); 991236769Sobrien } 992236769Sobrien if (strncmp(ename, member, len) == 0) { 993236769Sobrien /* Found as extended name */ 994321964Ssjg if (fseek(arch, -sizeof(struct ar_hdr) - elen, 995321964Ssjg SEEK_CUR) != 0) { 996321964Ssjg fclose(arch); 997321964Ssjg return NULL; 998321964Ssjg } 999236769Sobrien return (arch); 1000236769Sobrien } 1001321964Ssjg if (fseek(arch, -elen, SEEK_CUR) != 0) { 1002321964Ssjg fclose(arch); 1003321964Ssjg return NULL; 1004321964Ssjg } 1005236769Sobrien goto skip; 1006236769Sobrien } else 1007236769Sobrien#endif 1008236769Sobrien { 1009236769Sobrienskip: 1010236769Sobrien /* 1011236769Sobrien * This isn't the member we're after, so we need to advance the 1012236769Sobrien * stream's pointer to the start of the next header. Files are 1013236769Sobrien * padded with newlines to an even-byte boundary, so we need to 1014236769Sobrien * extract the size of the file from the 'size' field of the 1015236769Sobrien * header and round it up during the seek. 1016236769Sobrien */ 1017321964Ssjg arhPtr->AR_SIZE[sizeof(arhPtr->AR_SIZE)-1] = '\0'; 1018236769Sobrien size = (int)strtol(arhPtr->AR_SIZE, NULL, 10); 1019321964Ssjg if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) { 1020321964Ssjg fclose(arch); 1021321964Ssjg return NULL; 1022321964Ssjg } 1023236769Sobrien } 1024236769Sobrien } 1025236769Sobrien 1026236769Sobrien /* 1027236769Sobrien * We've looked everywhere, but the member is not to be found. Close the 1028236769Sobrien * archive and return NULL -- an error. 1029236769Sobrien */ 1030236769Sobrien fclose(arch); 1031236769Sobrien return NULL; 1032236769Sobrien} 1033236769Sobrien 1034236769Sobrien/*- 1035236769Sobrien *----------------------------------------------------------------------- 1036236769Sobrien * Arch_Touch -- 1037236769Sobrien * Touch a member of an archive. 1038236769Sobrien * 1039236769Sobrien * Input: 1040236769Sobrien * gn Node of member to touch 1041236769Sobrien * 1042236769Sobrien * Results: 1043236769Sobrien * The 'time' field of the member's header is updated. 1044236769Sobrien * 1045236769Sobrien * Side Effects: 1046236769Sobrien * The modification time of the entire archive is also changed. 1047236769Sobrien * For a library, this could necessitate the re-ranlib'ing of the 1048236769Sobrien * whole thing. 1049236769Sobrien * 1050236769Sobrien *----------------------------------------------------------------------- 1051236769Sobrien */ 1052236769Sobrienvoid 1053236769SobrienArch_Touch(GNode *gn) 1054236769Sobrien{ 1055236769Sobrien FILE * arch; /* Stream open to archive, positioned properly */ 1056236769Sobrien struct ar_hdr arh; /* Current header describing member */ 1057236769Sobrien char *p1, *p2; 1058236769Sobrien 1059236769Sobrien arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1), 1060236769Sobrien Var_Value(MEMBER, gn, &p2), 1061236769Sobrien &arh, "r+"); 1062321964Ssjg 1063321964Ssjg free(p1); 1064321964Ssjg free(p2); 1065321964Ssjg 1066236769Sobrien snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now); 1067236769Sobrien 1068236769Sobrien if (arch != NULL) { 1069236769Sobrien (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 1070236769Sobrien fclose(arch); 1071236769Sobrien } 1072236769Sobrien} 1073236769Sobrien 1074236769Sobrien/*- 1075236769Sobrien *----------------------------------------------------------------------- 1076236769Sobrien * Arch_TouchLib -- 1077236769Sobrien * Given a node which represents a library, touch the thing, making 1078236769Sobrien * sure that the table of contents also is touched. 1079236769Sobrien * 1080236769Sobrien * Input: 1081236769Sobrien * gn The node of the library to touch 1082236769Sobrien * 1083236769Sobrien * Results: 1084236769Sobrien * None. 1085236769Sobrien * 1086236769Sobrien * Side Effects: 1087236769Sobrien * Both the modification time of the library and of the RANLIBMAG 1088236769Sobrien * member are set to 'now'. 1089236769Sobrien * 1090236769Sobrien *----------------------------------------------------------------------- 1091236769Sobrien */ 1092236769Sobrienvoid 1093236769Sobrien#if !defined(RANLIBMAG) 1094237578SobrienArch_TouchLib(GNode *gn MAKE_ATTR_UNUSED) 1095236769Sobrien#else 1096236769SobrienArch_TouchLib(GNode *gn) 1097236769Sobrien#endif 1098236769Sobrien{ 1099236769Sobrien#ifdef RANLIBMAG 1100236769Sobrien FILE * arch; /* Stream open to archive */ 1101236769Sobrien struct ar_hdr arh; /* Header describing table of contents */ 1102236769Sobrien struct utimbuf times; /* Times for utime() call */ 1103236769Sobrien 1104236769Sobrien arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+"); 1105236769Sobrien snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now); 1106236769Sobrien 1107236769Sobrien if (arch != NULL) { 1108236769Sobrien (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 1109236769Sobrien fclose(arch); 1110236769Sobrien 1111236769Sobrien times.actime = times.modtime = now; 1112236769Sobrien utime(gn->path, ×); 1113236769Sobrien } 1114236769Sobrien#endif 1115236769Sobrien} 1116236769Sobrien 1117236769Sobrien/*- 1118236769Sobrien *----------------------------------------------------------------------- 1119236769Sobrien * Arch_MTime -- 1120236769Sobrien * Return the modification time of a member of an archive. 1121236769Sobrien * 1122236769Sobrien * Input: 1123236769Sobrien * gn Node describing archive member 1124236769Sobrien * 1125236769Sobrien * Results: 1126236769Sobrien * The modification time(seconds). 1127236769Sobrien * 1128236769Sobrien * Side Effects: 1129236769Sobrien * The mtime field of the given node is filled in with the value 1130236769Sobrien * returned by the function. 1131236769Sobrien * 1132236769Sobrien *----------------------------------------------------------------------- 1133236769Sobrien */ 1134236769Sobrientime_t 1135236769SobrienArch_MTime(GNode *gn) 1136236769Sobrien{ 1137236769Sobrien struct ar_hdr *arhPtr; /* Header of desired member */ 1138236769Sobrien time_t modTime; /* Modification time as an integer */ 1139236769Sobrien char *p1, *p2; 1140236769Sobrien 1141236769Sobrien arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1), 1142236769Sobrien Var_Value(MEMBER, gn, &p2), 1143236769Sobrien TRUE); 1144236769Sobrien 1145321964Ssjg free(p1); 1146321964Ssjg free(p2); 1147321964Ssjg 1148236769Sobrien if (arhPtr != NULL) { 1149236769Sobrien modTime = (time_t)strtol(arhPtr->AR_DATE, NULL, 10); 1150236769Sobrien } else { 1151236769Sobrien modTime = 0; 1152236769Sobrien } 1153236769Sobrien 1154236769Sobrien gn->mtime = modTime; 1155236769Sobrien return (modTime); 1156236769Sobrien} 1157236769Sobrien 1158236769Sobrien/*- 1159236769Sobrien *----------------------------------------------------------------------- 1160236769Sobrien * Arch_MemMTime -- 1161236769Sobrien * Given a non-existent archive member's node, get its modification 1162236769Sobrien * time from its archived form, if it exists. 1163236769Sobrien * 1164236769Sobrien * Results: 1165236769Sobrien * The modification time. 1166236769Sobrien * 1167236769Sobrien * Side Effects: 1168236769Sobrien * The mtime field is filled in. 1169236769Sobrien * 1170236769Sobrien *----------------------------------------------------------------------- 1171236769Sobrien */ 1172236769Sobrientime_t 1173236769SobrienArch_MemMTime(GNode *gn) 1174236769Sobrien{ 1175236769Sobrien LstNode ln; 1176236769Sobrien GNode *pgn; 1177236769Sobrien char *nameStart, 1178236769Sobrien *nameEnd; 1179236769Sobrien 1180236769Sobrien if (Lst_Open(gn->parents) != SUCCESS) { 1181236769Sobrien gn->mtime = 0; 1182236769Sobrien return (0); 1183236769Sobrien } 1184236769Sobrien while ((ln = Lst_Next(gn->parents)) != NULL) { 1185236769Sobrien pgn = (GNode *)Lst_Datum(ln); 1186236769Sobrien 1187236769Sobrien if (pgn->type & OP_ARCHV) { 1188236769Sobrien /* 1189236769Sobrien * If the parent is an archive specification and is being made 1190236769Sobrien * and its member's name matches the name of the node we were 1191236769Sobrien * given, record the modification time of the parent in the 1192236769Sobrien * child. We keep searching its parents in case some other 1193236769Sobrien * parent requires this child to exist... 1194236769Sobrien */ 1195236769Sobrien nameStart = strchr(pgn->name, '(') + 1; 1196236769Sobrien nameEnd = strchr(nameStart, ')'); 1197236769Sobrien 1198236769Sobrien if ((pgn->flags & REMAKE) && 1199236769Sobrien strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) { 1200236769Sobrien gn->mtime = Arch_MTime(pgn); 1201236769Sobrien } 1202236769Sobrien } else if (pgn->flags & REMAKE) { 1203236769Sobrien /* 1204236769Sobrien * Something which isn't a library depends on the existence of 1205236769Sobrien * this target, so it needs to exist. 1206236769Sobrien */ 1207236769Sobrien gn->mtime = 0; 1208236769Sobrien break; 1209236769Sobrien } 1210236769Sobrien } 1211236769Sobrien 1212236769Sobrien Lst_Close(gn->parents); 1213236769Sobrien 1214236769Sobrien return (gn->mtime); 1215236769Sobrien} 1216236769Sobrien 1217236769Sobrien/*- 1218236769Sobrien *----------------------------------------------------------------------- 1219236769Sobrien * Arch_FindLib -- 1220236769Sobrien * Search for a library along the given search path. 1221236769Sobrien * 1222236769Sobrien * Input: 1223236769Sobrien * gn Node of library to find 1224236769Sobrien * path Search path 1225236769Sobrien * 1226236769Sobrien * Results: 1227236769Sobrien * None. 1228236769Sobrien * 1229236769Sobrien * Side Effects: 1230236769Sobrien * The node's 'path' field is set to the found path (including the 1231236769Sobrien * actual file name, not -l...). If the system can handle the -L 1232236769Sobrien * flag when linking (or we cannot find the library), we assume that 1233236769Sobrien * the user has placed the .LIBRARIES variable in the final linking 1234236769Sobrien * command (or the linker will know where to find it) and set the 1235236769Sobrien * TARGET variable for this node to be the node's name. Otherwise, 1236236769Sobrien * we set the TARGET variable to be the full path of the library, 1237236769Sobrien * as returned by Dir_FindFile. 1238236769Sobrien * 1239236769Sobrien *----------------------------------------------------------------------- 1240236769Sobrien */ 1241236769Sobrienvoid 1242236769SobrienArch_FindLib(GNode *gn, Lst path) 1243236769Sobrien{ 1244236769Sobrien char *libName; /* file name for archive */ 1245236769Sobrien size_t sz = strlen(gn->name) + 6 - 2; 1246236769Sobrien 1247236769Sobrien libName = bmake_malloc(sz); 1248236769Sobrien snprintf(libName, sz, "lib%s.a", &gn->name[2]); 1249236769Sobrien 1250236769Sobrien gn->path = Dir_FindFile(libName, path); 1251236769Sobrien 1252236769Sobrien free(libName); 1253236769Sobrien 1254236769Sobrien#ifdef LIBRARIES 1255236769Sobrien Var_Set(TARGET, gn->name, gn, 0); 1256236769Sobrien#else 1257236769Sobrien Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn, 0); 1258236769Sobrien#endif /* LIBRARIES */ 1259236769Sobrien} 1260236769Sobrien 1261236769Sobrien/*- 1262236769Sobrien *----------------------------------------------------------------------- 1263236769Sobrien * Arch_LibOODate -- 1264236769Sobrien * Decide if a node with the OP_LIB attribute is out-of-date. Called 1265236769Sobrien * from Make_OODate to make its life easier. 1266236769Sobrien * 1267236769Sobrien * There are several ways for a library to be out-of-date that are 1268236769Sobrien * not available to ordinary files. In addition, there are ways 1269236769Sobrien * that are open to regular files that are not available to 1270236769Sobrien * libraries. A library that is only used as a source is never 1271236769Sobrien * considered out-of-date by itself. This does not preclude the 1272236769Sobrien * library's modification time from making its parent be out-of-date. 1273236769Sobrien * A library will be considered out-of-date for any of these reasons, 1274236769Sobrien * given that it is a target on a dependency line somewhere: 1275236769Sobrien * Its modification time is less than that of one of its 1276236769Sobrien * sources (gn->mtime < gn->cmgn->mtime). 1277236769Sobrien * Its modification time is greater than the time at which the 1278236769Sobrien * make began (i.e. it's been modified in the course 1279236769Sobrien * of the make, probably by archiving). 1280236769Sobrien * The modification time of one of its sources is greater than 1281236769Sobrien * the one of its RANLIBMAG member (i.e. its table of contents 1282236769Sobrien * is out-of-date). We don't compare of the archive time 1283236769Sobrien * vs. TOC time because they can be too close. In my 1284236769Sobrien * opinion we should not bother with the TOC at all since 1285236769Sobrien * this is used by 'ar' rules that affect the data contents 1286236769Sobrien * of the archive, not by ranlib rules, which affect the 1287236769Sobrien * TOC. 1288236769Sobrien * 1289236769Sobrien * Input: 1290236769Sobrien * gn The library's graph node 1291236769Sobrien * 1292236769Sobrien * Results: 1293236769Sobrien * TRUE if the library is out-of-date. FALSE otherwise. 1294236769Sobrien * 1295236769Sobrien * Side Effects: 1296236769Sobrien * The library will be hashed if it hasn't been already. 1297236769Sobrien * 1298236769Sobrien *----------------------------------------------------------------------- 1299236769Sobrien */ 1300236769SobrienBoolean 1301236769SobrienArch_LibOODate(GNode *gn) 1302236769Sobrien{ 1303236769Sobrien Boolean oodate; 1304236769Sobrien 1305236769Sobrien if (gn->type & OP_PHONY) { 1306236769Sobrien oodate = TRUE; 1307236769Sobrien } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) { 1308236769Sobrien oodate = FALSE; 1309236769Sobrien } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) || 1310236769Sobrien (gn->mtime > now) || 1311236769Sobrien (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) { 1312236769Sobrien oodate = TRUE; 1313236769Sobrien } else { 1314236769Sobrien#ifdef RANLIBMAG 1315236769Sobrien struct ar_hdr *arhPtr; /* Header for __.SYMDEF */ 1316236769Sobrien int modTimeTOC; /* The table-of-contents's mod time */ 1317236769Sobrien 1318236769Sobrien arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE); 1319236769Sobrien 1320236769Sobrien if (arhPtr != NULL) { 1321236769Sobrien modTimeTOC = (int)strtol(arhPtr->AR_DATE, NULL, 10); 1322236769Sobrien 1323236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 1324236769Sobrien fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC)); 1325236769Sobrien } 1326236769Sobrien oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC); 1327236769Sobrien } else { 1328236769Sobrien /* 1329236769Sobrien * A library w/o a table of contents is out-of-date 1330236769Sobrien */ 1331236769Sobrien if (DEBUG(ARCH) || DEBUG(MAKE)) { 1332236769Sobrien fprintf(debug_file, "No t.o.c...."); 1333236769Sobrien } 1334236769Sobrien oodate = TRUE; 1335236769Sobrien } 1336236769Sobrien#else 1337236769Sobrien oodate = FALSE; 1338236769Sobrien#endif 1339236769Sobrien } 1340236769Sobrien return (oodate); 1341236769Sobrien} 1342236769Sobrien 1343236769Sobrien/*- 1344236769Sobrien *----------------------------------------------------------------------- 1345236769Sobrien * Arch_Init -- 1346236769Sobrien * Initialize things for this module. 1347236769Sobrien * 1348236769Sobrien * Results: 1349236769Sobrien * None. 1350236769Sobrien * 1351236769Sobrien * Side Effects: 1352236769Sobrien * The 'archives' list is initialized. 1353236769Sobrien * 1354236769Sobrien *----------------------------------------------------------------------- 1355236769Sobrien */ 1356236769Sobrienvoid 1357236769SobrienArch_Init(void) 1358236769Sobrien{ 1359236769Sobrien archives = Lst_Init(FALSE); 1360236769Sobrien} 1361236769Sobrien 1362236769Sobrien 1363236769Sobrien 1364236769Sobrien/*- 1365236769Sobrien *----------------------------------------------------------------------- 1366236769Sobrien * Arch_End -- 1367236769Sobrien * Cleanup things for this module. 1368236769Sobrien * 1369236769Sobrien * Results: 1370236769Sobrien * None. 1371236769Sobrien * 1372236769Sobrien * Side Effects: 1373236769Sobrien * The 'archives' list is freed 1374236769Sobrien * 1375236769Sobrien *----------------------------------------------------------------------- 1376236769Sobrien */ 1377236769Sobrienvoid 1378236769SobrienArch_End(void) 1379236769Sobrien{ 1380236769Sobrien#ifdef CLEANUP 1381236769Sobrien Lst_Destroy(archives, ArchFree); 1382236769Sobrien#endif 1383236769Sobrien} 1384236769Sobrien 1385236769Sobrien/*- 1386236769Sobrien *----------------------------------------------------------------------- 1387236769Sobrien * Arch_IsLib -- 1388236769Sobrien * Check if the node is a library 1389236769Sobrien * 1390236769Sobrien * Results: 1391236769Sobrien * True or False. 1392236769Sobrien * 1393236769Sobrien * Side Effects: 1394236769Sobrien * None. 1395236769Sobrien * 1396236769Sobrien *----------------------------------------------------------------------- 1397236769Sobrien */ 1398236769Sobrienint 1399236769SobrienArch_IsLib(GNode *gn) 1400236769Sobrien{ 1401236769Sobrien static const char armag[] = "!<arch>\n"; 1402236769Sobrien char buf[sizeof(armag)-1]; 1403236769Sobrien int fd; 1404236769Sobrien 1405236769Sobrien if ((fd = open(gn->path, O_RDONLY)) == -1) 1406236769Sobrien return FALSE; 1407236769Sobrien 1408236769Sobrien if (read(fd, buf, sizeof(buf)) != sizeof(buf)) { 1409236769Sobrien (void)close(fd); 1410236769Sobrien return FALSE; 1411236769Sobrien } 1412236769Sobrien 1413236769Sobrien (void)close(fd); 1414236769Sobrien 1415236769Sobrien return memcmp(buf, armag, sizeof(buf)) == 0; 1416236769Sobrien} 1417