1 /* $NetBSD: arch.c,v 1.48 2005/08/08 16:42:54 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1989 by Berkeley Softworks 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Adam de Boor. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #ifndef MAKE_NATIVE 72 static char rcsid[] = "$NetBSD: arch.c,v 1.48 2005/08/08 16:42:54 christos Exp $"; 73 #else 74 #include <sys/cdefs.h> 75 #ifndef lint 76 #if 0 77 static char sccsid[] = "@(#)arch.c 8.2 (Berkeley) 1/2/94"; 78 #else 79 __RCSID("$NetBSD: arch.c,v 1.48 2005/08/08 16:42:54 christos Exp $"); 80 #endif 81 #endif /* not lint */ 82 #endif 83 84 /*- 85 * arch.c -- 86 * Functions to manipulate libraries, archives and their members. 87 * 88 * Once again, cacheing/hashing comes into play in the manipulation 89 * of archives. The first time an archive is referenced, all of its members' 90 * headers are read and hashed and the archive closed again. All hashed 91 * archives are kept on a list which is searched each time an archive member 92 * is referenced. 93 * 94 * The interface to this module is: 95 * Arch_ParseArchive Given an archive specification, return a list 96 * of GNode's, one for each member in the spec. 97 * FAILURE is returned if the specification is 98 * invalid for some reason. 99 * 100 * Arch_Touch Alter the modification time of the archive 101 * member described by the given node to be 102 * the current time. 103 * 104 * Arch_TouchLib Update the modification time of the library 105 * described by the given node. This is special 106 * because it also updates the modification time 107 * of the library's table of contents. 108 * 109 * Arch_MTime Find the modification time of a member of 110 * an archive *in the archive*. The time is also 111 * placed in the member's GNode. Returns the 112 * modification time. 113 * 114 * Arch_MemTime Find the modification time of a member of 115 * an archive. Called when the member doesn't 116 * already exist. Looks in the archive for the 117 * modification time. Returns the modification 118 * time. 119 * 120 * Arch_FindLib Search for a library along a path. The 121 * library name in the GNode should be in 122 * -l<name> format. 123 * 124 * Arch_LibOODate Special function to decide if a library node 125 * is out-of-date. 126 * 127 * Arch_Init Initialize this module. 128 * 129 * Arch_End Cleanup this module. 130 */ 131 132 #include <sys/types.h> 133 #include <sys/stat.h> 134 #include <sys/time.h> 135 #include <sys/param.h> 136 137 #include <ar.h> 138 #include <ctype.h> 139 #include <fcntl.h> 140 #include <stdio.h> 141 #include <stdlib.h> 142 #include <utime.h> 143 144 #include "make.h" 145 #include "hash.h" 146 #include "dir.h" 147 #include "config.h" 148 149 #ifdef TARGET_MACHINE 150 #undef MAKE_MACHINE 151 #define MAKE_MACHINE TARGET_MACHINE 152 #endif 153 #ifdef TARGET_MACHINE_ARCH 154 #undef MAKE_MACHINE_ARCH 155 #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH 156 #endif 157 158 static Lst archives; /* Lst of archives we've already examined */ 159 160 typedef struct Arch { 161 char *name; /* Name of archive */ 162 Hash_Table members; /* All the members of the archive described 163 * by <name, struct ar_hdr *> key/value pairs */ 164 char *fnametab; /* Extended name table strings */ 165 size_t fnamesize; /* Size of the string table */ 166 } Arch; 167 168 static int ArchFindArchive(ClientData, ClientData); 169 #ifdef CLEANUP 170 static void ArchFree(ClientData); 171 #endif 172 static struct ar_hdr *ArchStatMember(char *, char *, Boolean); 173 static FILE *ArchFindMember(char *, char *, struct ar_hdr *, const char *); 174 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__) 175 #define SVR4ARCHIVES 176 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *); 177 #endif 178 179 #ifdef CLEANUP 180 /*- 181 *----------------------------------------------------------------------- 182 * ArchFree -- 183 * Free memory used by an archive 184 * 185 * Results: 186 * None. 187 * 188 * Side Effects: 189 * None. 190 * 191 *----------------------------------------------------------------------- 192 */ 193 static void 194 ArchFree(ClientData ap) 195 { 196 Arch *a = (Arch *)ap; 197 Hash_Search search; 198 Hash_Entry *entry; 199 200 /* Free memory from hash entries */ 201 for (entry = Hash_EnumFirst(&a->members, &search); 202 entry != NULL; 203 entry = Hash_EnumNext(&search)) 204 free(Hash_GetValue(entry)); 205 206 free(a->name); 207 if (a->fnametab) 208 free(a->fnametab); 209 Hash_DeleteTable(&a->members); 210 free(a); 211 } 212 #endif 213 214 215 216 /*- 217 *----------------------------------------------------------------------- 218 * Arch_ParseArchive -- 219 * Parse the archive specification in the given line and find/create 220 * the nodes for the specified archive members, placing their nodes 221 * on the given list. 222 * 223 * Input: 224 * linePtr Pointer to start of specification 225 * nodeLst Lst on which to place the nodes 226 * ctxt Context in which to expand variables 227 * 228 * Results: 229 * SUCCESS if it was a valid specification. The linePtr is updated 230 * to point to the first non-space after the archive spec. The 231 * nodes for the members are placed on the given list. 232 * 233 * Side Effects: 234 * Some nodes may be created. The given list is extended. 235 * 236 *----------------------------------------------------------------------- 237 */ 238 ReturnStatus 239 Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt) 240 { 241 char *cp; /* Pointer into line */ 242 GNode *gn; /* New node */ 243 char *libName; /* Library-part of specification */ 244 char *memName; /* Member-part of specification */ 245 char *nameBuf; /* temporary place for node name */ 246 char saveChar; /* Ending delimiter of member-name */ 247 Boolean subLibName; /* TRUE if libName should have/had 248 * variable substitution performed on it */ 249 250 libName = *linePtr; 251 252 subLibName = FALSE; 253 254 for (cp = libName; *cp != '(' && *cp != '\0'; cp++) { 255 if (*cp == '$') { 256 /* 257 * Variable spec, so call the Var module to parse the puppy 258 * so we can safely advance beyond it... 259 */ 260 int length; 261 Boolean freeIt; 262 char *result; 263 264 result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt); 265 if (result == var_Error) { 266 return(FAILURE); 267 } else { 268 subLibName = TRUE; 269 } 270 271 if (freeIt) { 272 free(result); 273 } 274 cp += length-1; 275 } 276 } 277 278 *cp++ = '\0'; 279 if (subLibName) { 280 libName = Var_Subst(NULL, libName, ctxt, TRUE); 281 } 282 283 284 for (;;) { 285 /* 286 * First skip to the start of the member's name, mark that 287 * place and skip to the end of it (either white-space or 288 * a close paren). 289 */ 290 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */ 291 292 while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) { 293 cp++; 294 } 295 memName = cp; 296 while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) { 297 if (*cp == '$') { 298 /* 299 * Variable spec, so call the Var module to parse the puppy 300 * so we can safely advance beyond it... 301 */ 302 int length; 303 Boolean freeIt; 304 char *result; 305 306 result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt); 307 if (result == var_Error) { 308 return(FAILURE); 309 } else { 310 doSubst = TRUE; 311 } 312 313 if (freeIt) { 314 free(result); 315 } 316 cp += length; 317 } else { 318 cp++; 319 } 320 } 321 322 /* 323 * If the specification ends without a closing parenthesis, 324 * chances are there's something wrong (like a missing backslash), 325 * so it's better to return failure than allow such things to happen 326 */ 327 if (*cp == '\0') { 328 printf("No closing parenthesis in archive specification\n"); 329 return (FAILURE); 330 } 331 332 /* 333 * If we didn't move anywhere, we must be done 334 */ 335 if (cp == memName) { 336 break; 337 } 338 339 saveChar = *cp; 340 *cp = '\0'; 341 342 /* 343 * XXX: This should be taken care of intelligently by 344 * SuffExpandChildren, both for the archive and the member portions. 345 */ 346 /* 347 * If member contains variables, try and substitute for them. 348 * This will slow down archive specs with dynamic sources, of course, 349 * since we'll be (non-)substituting them three times, but them's 350 * the breaks -- we need to do this since SuffExpandChildren calls 351 * us, otherwise we could assume the thing would be taken care of 352 * later. 353 */ 354 if (doSubst) { 355 char *buf; 356 char *sacrifice; 357 char *oldMemName = memName; 358 size_t sz; 359 360 memName = Var_Subst(NULL, memName, ctxt, TRUE); 361 362 /* 363 * Now form an archive spec and recurse to deal with nested 364 * variables and multi-word variable values.... The results 365 * are just placed at the end of the nodeLst we're returning. 366 */ 367 sz = strlen(memName)+strlen(libName)+3; 368 buf = sacrifice = emalloc(sz); 369 370 snprintf(buf, sz, "%s(%s)", libName, memName); 371 372 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) { 373 /* 374 * Must contain dynamic sources, so we can't deal with it now. 375 * Just create an ARCHV node for the thing and let 376 * SuffExpandChildren handle it... 377 */ 378 gn = Targ_FindNode(buf, TARG_CREATE); 379 380 if (gn == NILGNODE) { 381 free(buf); 382 return(FAILURE); 383 } else { 384 gn->type |= OP_ARCHV; 385 (void)Lst_AtEnd(nodeLst, (ClientData)gn); 386 } 387 } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) { 388 /* 389 * Error in nested call -- free buffer and return FAILURE 390 * ourselves. 391 */ 392 free(buf); 393 return(FAILURE); 394 } 395 /* 396 * Free buffer and continue with our work. 397 */ 398 free(buf); 399 } else if (Dir_HasWildcards(memName)) { 400 Lst members = Lst_Init(FALSE); 401 char *member; 402 size_t sz = MAXPATHLEN, nsz; 403 nameBuf = emalloc(sz); 404 405 Dir_Expand(memName, dirSearchPath, members); 406 while (!Lst_IsEmpty(members)) { 407 member = (char *)Lst_DeQueue(members); 408 nsz = strlen(libName) + strlen(member) + 3; 409 if (sz > nsz) 410 nameBuf = erealloc(nameBuf, sz = nsz * 2); 411 412 snprintf(nameBuf, sz, "%s(%s)", libName, member); 413 free(member); 414 gn = Targ_FindNode(nameBuf, TARG_CREATE); 415 if (gn == NILGNODE) { 416 free(nameBuf); 417 return (FAILURE); 418 } else { 419 /* 420 * We've found the node, but have to make sure the rest of 421 * the world knows it's an archive member, without having 422 * to constantly check for parentheses, so we type the 423 * thing with the OP_ARCHV bit before we place it on the 424 * end of the provided list. 425 */ 426 gn->type |= OP_ARCHV; 427 (void)Lst_AtEnd(nodeLst, (ClientData)gn); 428 } 429 } 430 Lst_Destroy(members, NOFREE); 431 free(nameBuf); 432 } else { 433 size_t sz = strlen(libName) + strlen(memName) + 3; 434 nameBuf = emalloc(sz); 435 snprintf(nameBuf, sz, "%s(%s)", libName, memName); 436 gn = Targ_FindNode(nameBuf, TARG_CREATE); 437 free(nameBuf); 438 if (gn == NILGNODE) { 439 return (FAILURE); 440 } else { 441 /* 442 * We've found the node, but have to make sure the rest of the 443 * world knows it's an archive member, without having to 444 * constantly check for parentheses, so we type the thing with 445 * the OP_ARCHV bit before we place it on the end of the 446 * provided list. 447 */ 448 gn->type |= OP_ARCHV; 449 (void)Lst_AtEnd(nodeLst, (ClientData)gn); 450 } 451 } 452 if (doSubst) { 453 free(memName); 454 } 455 456 *cp = saveChar; 457 } 458 459 /* 460 * If substituted libName, free it now, since we need it no longer. 461 */ 462 if (subLibName) { 463 free(libName); 464 } 465 466 /* 467 * We promised the pointer would be set up at the next non-space, so 468 * we must advance cp there before setting *linePtr... (note that on 469 * entrance to the loop, cp is guaranteed to point at a ')') 470 */ 471 do { 472 cp++; 473 } while (*cp != '\0' && isspace ((unsigned char)*cp)); 474 475 *linePtr = cp; 476 return (SUCCESS); 477 } 478 479 /*- 480 *----------------------------------------------------------------------- 481 * ArchFindArchive -- 482 * See if the given archive is the one we are looking for. Called 483 * From ArchStatMember and ArchFindMember via Lst_Find. 484 * 485 * Input: 486 * ar Current list element 487 * archName Name we want 488 * 489 * Results: 490 * 0 if it is, non-zero if it isn't. 491 * 492 * Side Effects: 493 * None. 494 * 495 *----------------------------------------------------------------------- 496 */ 497 static int 498 ArchFindArchive(ClientData ar, ClientData archName) 499 { 500 return (strcmp((char *)archName, ((Arch *)ar)->name)); 501 } 502 503 /*- 504 *----------------------------------------------------------------------- 505 * ArchStatMember -- 506 * Locate a member of an archive, given the path of the archive and 507 * the path of the desired member. 508 * 509 * Input: 510 * archive Path to the archive 511 * member Name of member. If it is a path, only the last 512 * component is used. 513 * hash TRUE if archive should be hashed if not already so. 514 * 515 * Results: 516 * A pointer to the current struct ar_hdr structure for the member. Note 517 * That no position is returned, so this is not useful for touching 518 * archive members. This is mostly because we have no assurances that 519 * The archive will remain constant after we read all the headers, so 520 * there's not much point in remembering the position... 521 * 522 * Side Effects: 523 * 524 *----------------------------------------------------------------------- 525 */ 526 static struct ar_hdr * 527 ArchStatMember(char *archive, char *member, Boolean hash) 528 { 529 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name)-1) 530 FILE * arch; /* Stream to archive */ 531 int size; /* Size of archive member */ 532 char *cp; /* Useful character pointer */ 533 char magic[SARMAG]; 534 LstNode ln; /* Lst member containing archive descriptor */ 535 Arch *ar; /* Archive descriptor */ 536 Hash_Entry *he; /* Entry containing member's description */ 537 struct ar_hdr arh; /* archive-member header for reading archive */ 538 char memName[MAXPATHLEN+1]; 539 /* Current member name while hashing. */ 540 541 /* 542 * Because of space constraints and similar things, files are archived 543 * using their final path components, not the entire thing, so we need 544 * to point 'member' to the final component, if there is one, to make 545 * the comparisons easier... 546 */ 547 cp = strrchr(member, '/'); 548 if (cp != NULL) { 549 member = cp + 1; 550 } 551 552 ln = Lst_Find(archives, (ClientData) archive, ArchFindArchive); 553 if (ln != NILLNODE) { 554 ar = (Arch *)Lst_Datum(ln); 555 556 he = Hash_FindEntry(&ar->members, member); 557 558 if (he != NULL) { 559 return ((struct ar_hdr *)Hash_GetValue(he)); 560 } else { 561 /* Try truncated name */ 562 char copy[AR_MAX_NAME_LEN+1]; 563 int len = strlen(member); 564 565 if (len > AR_MAX_NAME_LEN) { 566 len = AR_MAX_NAME_LEN; 567 strncpy(copy, member, AR_MAX_NAME_LEN); 568 copy[AR_MAX_NAME_LEN] = '\0'; 569 } 570 if ((he = Hash_FindEntry(&ar->members, copy)) != NULL) 571 return ((struct ar_hdr *)Hash_GetValue(he)); 572 return (NULL); 573 } 574 } 575 576 if (!hash) { 577 /* 578 * Caller doesn't want the thing hashed, just use ArchFindMember 579 * to read the header for the member out and close down the stream 580 * again. Since the archive is not to be hashed, we assume there's 581 * no need to allocate extra room for the header we're returning, 582 * so just declare it static. 583 */ 584 static struct ar_hdr sarh; 585 586 arch = ArchFindMember(archive, member, &sarh, "r"); 587 588 if (arch == NULL) { 589 return (NULL); 590 } else { 591 fclose(arch); 592 return (&sarh); 593 } 594 } 595 596 /* 597 * We don't have this archive on the list yet, so we want to find out 598 * everything that's in it and cache it so we can get at it quickly. 599 */ 600 arch = fopen(archive, "r"); 601 if (arch == NULL) { 602 return (NULL); 603 } 604 605 /* 606 * We use the ARMAG string to make sure this is an archive we 607 * can handle... 608 */ 609 if ((fread(magic, SARMAG, 1, arch) != 1) || 610 (strncmp(magic, ARMAG, SARMAG) != 0)) { 611 fclose(arch); 612 return (NULL); 613 } 614 615 ar = emalloc(sizeof(Arch)); 616 ar->name = estrdup(archive); 617 ar->fnametab = NULL; 618 ar->fnamesize = 0; 619 Hash_InitTable(&ar->members, -1); 620 memName[AR_MAX_NAME_LEN] = '\0'; 621 622 while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) { 623 if (strncmp( arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) { 624 /* 625 * The header is bogus, so the archive is bad 626 * and there's no way we can recover... 627 */ 628 goto badarch; 629 } else { 630 /* 631 * We need to advance the stream's pointer to the start of the 632 * next header. Files are padded with newlines to an even-byte 633 * boundary, so we need to extract the size of the file from the 634 * 'size' field of the header and round it up during the seek. 635 */ 636 arh.ar_size[sizeof(arh.ar_size)-1] = '\0'; 637 size = (int)strtol(arh.ar_size, NULL, 10); 638 639 (void)strncpy(memName, arh.ar_name, sizeof(arh.ar_name)); 640 for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) { 641 continue; 642 } 643 cp[1] = '\0'; 644 645 #ifdef SVR4ARCHIVES 646 /* 647 * svr4 names are slash terminated. Also svr4 extended AR format. 648 */ 649 if (memName[0] == '/') { 650 /* 651 * svr4 magic mode; handle it 652 */ 653 switch (ArchSVR4Entry(ar, memName, size, arch)) { 654 case -1: /* Invalid data */ 655 goto badarch; 656 case 0: /* List of files entry */ 657 continue; 658 default: /* Got the entry */ 659 break; 660 } 661 } 662 else { 663 if (cp[0] == '/') 664 cp[0] = '\0'; 665 } 666 #endif 667 668 #ifdef AR_EFMT1 669 /* 670 * BSD 4.4 extended AR format: #1/<namelen>, with name as the 671 * first <namelen> bytes of the file 672 */ 673 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 && 674 isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) { 675 676 unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]); 677 678 if (elen > MAXPATHLEN) 679 goto badarch; 680 if (fread(memName, elen, 1, arch) != 1) 681 goto badarch; 682 memName[elen] = '\0'; 683 fseek(arch, -elen, SEEK_CUR); 684 if (DEBUG(ARCH) || DEBUG(MAKE)) { 685 printf("ArchStat: Extended format entry for %s\n", memName); 686 } 687 } 688 #endif 689 690 he = Hash_CreateEntry(&ar->members, memName, NULL); 691 Hash_SetValue(he, emalloc(sizeof(struct ar_hdr))); 692 memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr)); 693 } 694 fseek(arch, (size + 1) & ~1, SEEK_CUR); 695 } 696 697 fclose(arch); 698 699 (void)Lst_AtEnd(archives, (ClientData) ar); 700 701 /* 702 * Now that the archive has been read and cached, we can look into 703 * the hash table to find the desired member's header. 704 */ 705 he = Hash_FindEntry(&ar->members, member); 706 707 if (he != NULL) { 708 return ((struct ar_hdr *)Hash_GetValue(he)); 709 } else { 710 return (NULL); 711 } 712 713 badarch: 714 fclose(arch); 715 Hash_DeleteTable(&ar->members); 716 if (ar->fnametab) 717 free(ar->fnametab); 718 free(ar); 719 return (NULL); 720 } 721 722 #ifdef SVR4ARCHIVES 723 /*- 724 *----------------------------------------------------------------------- 725 * ArchSVR4Entry -- 726 * Parse an SVR4 style entry that begins with a slash. 727 * If it is "//", then load the table of filenames 728 * If it is "/<offset>", then try to substitute the long file name 729 * from offset of a table previously read. 730 * 731 * Results: 732 * -1: Bad data in archive 733 * 0: A table was loaded from the file 734 * 1: Name was successfully substituted from table 735 * 2: Name was not successfully substituted from table 736 * 737 * Side Effects: 738 * If a table is read, the file pointer is moved to the next archive 739 * member 740 * 741 *----------------------------------------------------------------------- 742 */ 743 static int 744 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch) 745 { 746 #define ARLONGNAMES1 "//" 747 #define ARLONGNAMES2 "/ARFILENAMES" 748 size_t entry; 749 char *ptr, *eptr; 750 751 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 || 752 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) { 753 754 if (ar->fnametab != NULL) { 755 if (DEBUG(ARCH)) { 756 printf("Attempted to redefine an SVR4 name table\n"); 757 } 758 return -1; 759 } 760 761 /* 762 * This is a table of archive names, so we build one for 763 * ourselves 764 */ 765 ar->fnametab = emalloc(size); 766 ar->fnamesize = size; 767 768 if (fread(ar->fnametab, size, 1, arch) != 1) { 769 if (DEBUG(ARCH)) { 770 printf("Reading an SVR4 name table failed\n"); 771 } 772 return -1; 773 } 774 eptr = ar->fnametab + size; 775 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++) 776 switch (*ptr) { 777 case '/': 778 entry++; 779 *ptr = '\0'; 780 break; 781 782 case '\n': 783 break; 784 785 default: 786 break; 787 } 788 if (DEBUG(ARCH)) { 789 printf("Found svr4 archive name table with %lu entries\n", 790 (u_long)entry); 791 } 792 return 0; 793 } 794 795 if (name[1] == ' ' || name[1] == '\0') 796 return 2; 797 798 entry = (size_t)strtol(&name[1], &eptr, 0); 799 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) { 800 if (DEBUG(ARCH)) { 801 printf("Could not parse SVR4 name %s\n", name); 802 } 803 return 2; 804 } 805 if (entry >= ar->fnamesize) { 806 if (DEBUG(ARCH)) { 807 printf("SVR4 entry offset %s is greater than %lu\n", 808 name, (u_long)ar->fnamesize); 809 } 810 return 2; 811 } 812 813 if (DEBUG(ARCH)) { 814 printf("Replaced %s with %s\n", name, &ar->fnametab[entry]); 815 } 816 817 (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN); 818 name[MAXPATHLEN] = '\0'; 819 return 1; 820 } 821 #endif 822 823 824 /*- 825 *----------------------------------------------------------------------- 826 * ArchFindMember -- 827 * Locate a member of an archive, given the path of the archive and 828 * the path of the desired member. If the archive is to be modified, 829 * the mode should be "r+", if not, it should be "r". 830 * 831 * Input: 832 * archive Path to the archive 833 * member Name of member. If it is a path, only the last 834 * component is used. 835 * arhPtr Pointer to header structure to be filled in 836 * mode The mode for opening the stream 837 * 838 * Results: 839 * An FILE *, opened for reading and writing, positioned at the 840 * start of the member's struct ar_hdr, or NULL if the member was 841 * nonexistent. The current struct ar_hdr for member. 842 * 843 * Side Effects: 844 * The passed struct ar_hdr structure is filled in. 845 * 846 *----------------------------------------------------------------------- 847 */ 848 static FILE * 849 ArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr, 850 const char *mode) 851 { 852 FILE * arch; /* Stream to archive */ 853 int size; /* Size of archive member */ 854 char *cp; /* Useful character pointer */ 855 char magic[SARMAG]; 856 int len, tlen; 857 858 arch = fopen(archive, mode); 859 if (arch == NULL) { 860 return (NULL); 861 } 862 863 /* 864 * We use the ARMAG string to make sure this is an archive we 865 * can handle... 866 */ 867 if ((fread(magic, SARMAG, 1, arch) != 1) || 868 (strncmp(magic, ARMAG, SARMAG) != 0)) { 869 fclose(arch); 870 return (NULL); 871 } 872 873 /* 874 * Because of space constraints and similar things, files are archived 875 * using their final path components, not the entire thing, so we need 876 * to point 'member' to the final component, if there is one, to make 877 * the comparisons easier... 878 */ 879 cp = strrchr(member, '/'); 880 if (cp != NULL) { 881 member = cp + 1; 882 } 883 len = tlen = strlen(member); 884 if (len > sizeof(arhPtr->ar_name)) { 885 tlen = sizeof(arhPtr->ar_name); 886 } 887 888 while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) { 889 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) { 890 /* 891 * The header is bogus, so the archive is bad 892 * and there's no way we can recover... 893 */ 894 fclose(arch); 895 return (NULL); 896 } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) { 897 /* 898 * If the member's name doesn't take up the entire 'name' field, 899 * we have to be careful of matching prefixes. Names are space- 900 * padded to the right, so if the character in 'name' at the end 901 * of the matched string is anything but a space, this isn't the 902 * member we sought. 903 */ 904 if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){ 905 goto skip; 906 } else { 907 /* 908 * To make life easier, we reposition the file at the start 909 * of the header we just read before we return the stream. 910 * In a more general situation, it might be better to leave 911 * the file at the actual member, rather than its header, but 912 * not here... 913 */ 914 fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR); 915 return (arch); 916 } 917 } else 918 #ifdef AR_EFMT1 919 /* 920 * BSD 4.4 extended AR format: #1/<namelen>, with name as the 921 * first <namelen> bytes of the file 922 */ 923 if (strncmp(arhPtr->ar_name, AR_EFMT1, 924 sizeof(AR_EFMT1) - 1) == 0 && 925 isdigit((unsigned char)arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) { 926 927 unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]); 928 char ename[MAXPATHLEN]; 929 930 if (elen > MAXPATHLEN) { 931 fclose(arch); 932 return NULL; 933 } 934 if (fread(ename, elen, 1, arch) != 1) { 935 fclose(arch); 936 return NULL; 937 } 938 ename[elen] = '\0'; 939 if (DEBUG(ARCH) || DEBUG(MAKE)) { 940 printf("ArchFind: Extended format entry for %s\n", ename); 941 } 942 if (strncmp(ename, member, len) == 0) { 943 /* Found as extended name */ 944 fseek(arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR); 945 return (arch); 946 } 947 fseek(arch, -elen, SEEK_CUR); 948 goto skip; 949 } else 950 #endif 951 { 952 skip: 953 /* 954 * This isn't the member we're after, so we need to advance the 955 * stream's pointer to the start of the next header. Files are 956 * padded with newlines to an even-byte boundary, so we need to 957 * extract the size of the file from the 'size' field of the 958 * header and round it up during the seek. 959 */ 960 arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0'; 961 size = (int)strtol(arhPtr->ar_size, NULL, 10); 962 fseek(arch, (size + 1) & ~1, SEEK_CUR); 963 } 964 } 965 966 /* 967 * We've looked everywhere, but the member is not to be found. Close the 968 * archive and return NULL -- an error. 969 */ 970 fclose(arch); 971 return (NULL); 972 } 973 974 /*- 975 *----------------------------------------------------------------------- 976 * Arch_Touch -- 977 * Touch a member of an archive. 978 * 979 * Input: 980 * gn Node of member to touch 981 * 982 * Results: 983 * The 'time' field of the member's header is updated. 984 * 985 * Side Effects: 986 * The modification time of the entire archive is also changed. 987 * For a library, this could necessitate the re-ranlib'ing of the 988 * whole thing. 989 * 990 *----------------------------------------------------------------------- 991 */ 992 void 993 Arch_Touch(GNode *gn) 994 { 995 FILE * arch; /* Stream open to archive, positioned properly */ 996 struct ar_hdr arh; /* Current header describing member */ 997 char *p1, *p2; 998 999 arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1), 1000 Var_Value(MEMBER, gn, &p2), 1001 &arh, "r+"); 1002 if (p1) 1003 free(p1); 1004 if (p2) 1005 free(p2); 1006 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now); 1007 1008 if (arch != NULL) { 1009 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 1010 fclose(arch); 1011 } 1012 } 1013 1014 /*- 1015 *----------------------------------------------------------------------- 1016 * Arch_TouchLib -- 1017 * Given a node which represents a library, touch the thing, making 1018 * sure that the table of contents also is touched. 1019 * 1020 * Input: 1021 * gn The node of the library to touch 1022 * 1023 * Results: 1024 * None. 1025 * 1026 * Side Effects: 1027 * Both the modification time of the library and of the RANLIBMAG 1028 * member are set to 'now'. 1029 * 1030 *----------------------------------------------------------------------- 1031 */ 1032 void 1033 #if !defined(RANLIBMAG) 1034 Arch_TouchLib(GNode *gn __unused) 1035 #else 1036 Arch_TouchLib(GNode *gn) 1037 #endif 1038 { 1039 #ifdef RANLIBMAG 1040 FILE * arch; /* Stream open to archive */ 1041 struct ar_hdr arh; /* Header describing table of contents */ 1042 struct utimbuf times; /* Times for utime() call */ 1043 1044 arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+"); 1045 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now); 1046 1047 if (arch != NULL) { 1048 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 1049 fclose(arch); 1050 1051 times.actime = times.modtime = now; 1052 utime(gn->path, ×); 1053 } 1054 #endif 1055 } 1056 1057 /*- 1058 *----------------------------------------------------------------------- 1059 * Arch_MTime -- 1060 * Return the modification time of a member of an archive. 1061 * 1062 * Input: 1063 * gn Node describing archive member 1064 * 1065 * Results: 1066 * The modification time(seconds). 1067 * 1068 * Side Effects: 1069 * The mtime field of the given node is filled in with the value 1070 * returned by the function. 1071 * 1072 *----------------------------------------------------------------------- 1073 */ 1074 time_t 1075 Arch_MTime(GNode *gn) 1076 { 1077 struct ar_hdr *arhPtr; /* Header of desired member */ 1078 time_t modTime; /* Modification time as an integer */ 1079 char *p1, *p2; 1080 1081 arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1), 1082 Var_Value(MEMBER, gn, &p2), 1083 TRUE); 1084 if (p1) 1085 free(p1); 1086 if (p2) 1087 free(p2); 1088 1089 if (arhPtr != NULL) { 1090 modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10); 1091 } else { 1092 modTime = 0; 1093 } 1094 1095 gn->mtime = modTime; 1096 return (modTime); 1097 } 1098 1099 /*- 1100 *----------------------------------------------------------------------- 1101 * Arch_MemMTime -- 1102 * Given a non-existent archive member's node, get its modification 1103 * time from its archived form, if it exists. 1104 * 1105 * Results: 1106 * The modification time. 1107 * 1108 * Side Effects: 1109 * The mtime field is filled in. 1110 * 1111 *----------------------------------------------------------------------- 1112 */ 1113 time_t 1114 Arch_MemMTime(GNode *gn) 1115 { 1116 LstNode ln; 1117 GNode *pgn; 1118 char *nameStart, 1119 *nameEnd; 1120 1121 if (Lst_Open(gn->parents) != SUCCESS) { 1122 gn->mtime = 0; 1123 return (0); 1124 } 1125 while ((ln = Lst_Next(gn->parents)) != NILLNODE) { 1126 pgn = (GNode *)Lst_Datum(ln); 1127 1128 if (pgn->type & OP_ARCHV) { 1129 /* 1130 * If the parent is an archive specification and is being made 1131 * and its member's name matches the name of the node we were 1132 * given, record the modification time of the parent in the 1133 * child. We keep searching its parents in case some other 1134 * parent requires this child to exist... 1135 */ 1136 nameStart = strchr(pgn->name, '(') + 1; 1137 nameEnd = strchr(nameStart, ')'); 1138 1139 if ((pgn->flags & REMAKE) && 1140 strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) { 1141 gn->mtime = Arch_MTime(pgn); 1142 } 1143 } else if (pgn->flags & REMAKE) { 1144 /* 1145 * Something which isn't a library depends on the existence of 1146 * this target, so it needs to exist. 1147 */ 1148 gn->mtime = 0; 1149 break; 1150 } 1151 } 1152 1153 Lst_Close(gn->parents); 1154 1155 return (gn->mtime); 1156 } 1157 1158 /*- 1159 *----------------------------------------------------------------------- 1160 * Arch_FindLib -- 1161 * Search for a library along the given search path. 1162 * 1163 * Input: 1164 * gn Node of library to find 1165 * path Search path 1166 * 1167 * Results: 1168 * None. 1169 * 1170 * Side Effects: 1171 * The node's 'path' field is set to the found path (including the 1172 * actual file name, not -l...). If the system can handle the -L 1173 * flag when linking (or we cannot find the library), we assume that 1174 * the user has placed the .LIBRARIES variable in the final linking 1175 * command (or the linker will know where to find it) and set the 1176 * TARGET variable for this node to be the node's name. Otherwise, 1177 * we set the TARGET variable to be the full path of the library, 1178 * as returned by Dir_FindFile. 1179 * 1180 *----------------------------------------------------------------------- 1181 */ 1182 void 1183 Arch_FindLib(GNode *gn, Lst path) 1184 { 1185 char *libName; /* file name for archive */ 1186 size_t sz = strlen(gn->name) + 6 - 2; 1187 1188 libName = emalloc(sz); 1189 snprintf(libName, sz, "lib%s.a", &gn->name[2]); 1190 1191 gn->path = Dir_FindFile(libName, path); 1192 1193 free(libName); 1194 1195 #ifdef LIBRARIES 1196 Var_Set(TARGET, gn->name, gn, 0); 1197 #else 1198 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn, 0); 1199 #endif /* LIBRARIES */ 1200 } 1201 1202 /*- 1203 *----------------------------------------------------------------------- 1204 * Arch_LibOODate -- 1205 * Decide if a node with the OP_LIB attribute is out-of-date. Called 1206 * from Make_OODate to make its life easier. 1207 * 1208 * There are several ways for a library to be out-of-date that are 1209 * not available to ordinary files. In addition, there are ways 1210 * that are open to regular files that are not available to 1211 * libraries. A library that is only used as a source is never 1212 * considered out-of-date by itself. This does not preclude the 1213 * library's modification time from making its parent be out-of-date. 1214 * A library will be considered out-of-date for any of these reasons, 1215 * given that it is a target on a dependency line somewhere: 1216 * Its modification time is less than that of one of its 1217 * sources (gn->mtime < gn->cmtime). 1218 * Its modification time is greater than the time at which the 1219 * make began (i.e. it's been modified in the course 1220 * of the make, probably by archiving). 1221 * The modification time of one of its sources is greater than 1222 * the one of its RANLIBMAG member (i.e. its table of contents 1223 * is out-of-date). We don't compare of the archive time 1224 * vs. TOC time because they can be too close. In my 1225 * opinion we should not bother with the TOC at all since 1226 * this is used by 'ar' rules that affect the data contents 1227 * of the archive, not by ranlib rules, which affect the 1228 * TOC. 1229 * 1230 * Input: 1231 * gn The library's graph node 1232 * 1233 * Results: 1234 * TRUE if the library is out-of-date. FALSE otherwise. 1235 * 1236 * Side Effects: 1237 * The library will be hashed if it hasn't been already. 1238 * 1239 *----------------------------------------------------------------------- 1240 */ 1241 Boolean 1242 Arch_LibOODate(GNode *gn) 1243 { 1244 Boolean oodate; 1245 1246 if (gn->type & OP_PHONY) { 1247 oodate = TRUE; 1248 } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) { 1249 oodate = FALSE; 1250 } else if ((!Lst_IsEmpty(gn->children) && gn->cmtime == 0) || 1251 (gn->mtime > now) || (gn->mtime < gn->cmtime)) { 1252 oodate = TRUE; 1253 } else { 1254 #ifdef RANLIBMAG 1255 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */ 1256 int modTimeTOC; /* The table-of-contents's mod time */ 1257 1258 arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE); 1259 1260 if (arhPtr != NULL) { 1261 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10); 1262 1263 if (DEBUG(ARCH) || DEBUG(MAKE)) { 1264 printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC)); 1265 } 1266 oodate = (gn->cmtime > modTimeTOC); 1267 } else { 1268 /* 1269 * A library w/o a table of contents is out-of-date 1270 */ 1271 if (DEBUG(ARCH) || DEBUG(MAKE)) { 1272 printf("No t.o.c...."); 1273 } 1274 oodate = TRUE; 1275 } 1276 #else 1277 oodate = FALSE; 1278 #endif 1279 } 1280 return (oodate); 1281 } 1282 1283 /*- 1284 *----------------------------------------------------------------------- 1285 * Arch_Init -- 1286 * Initialize things for this module. 1287 * 1288 * Results: 1289 * None. 1290 * 1291 * Side Effects: 1292 * The 'archives' list is initialized. 1293 * 1294 *----------------------------------------------------------------------- 1295 */ 1296 void 1297 Arch_Init(void) 1298 { 1299 archives = Lst_Init(FALSE); 1300 } 1301 1302 1303 1304 /*- 1305 *----------------------------------------------------------------------- 1306 * Arch_End -- 1307 * Cleanup things for this module. 1308 * 1309 * Results: 1310 * None. 1311 * 1312 * Side Effects: 1313 * The 'archives' list is freed 1314 * 1315 *----------------------------------------------------------------------- 1316 */ 1317 void 1318 Arch_End(void) 1319 { 1320 #ifdef CLEANUP 1321 Lst_Destroy(archives, ArchFree); 1322 #endif 1323 } 1324 1325 /*- 1326 *----------------------------------------------------------------------- 1327 * Arch_IsLib -- 1328 * Check if the node is a library 1329 * 1330 * Results: 1331 * True or False. 1332 * 1333 * Side Effects: 1334 * None. 1335 * 1336 *----------------------------------------------------------------------- 1337 */ 1338 int 1339 Arch_IsLib(GNode *gn) 1340 { 1341 static const char armag[] = "!<arch>\n"; 1342 char buf[sizeof(armag)-1]; 1343 int fd; 1344 1345 if ((fd = open(gn->path, O_RDONLY)) == -1) 1346 return FALSE; 1347 1348 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) { 1349 (void)close(fd); 1350 return FALSE; 1351 } 1352 1353 (void)close(fd); 1354 1355 return memcmp(buf, armag, sizeof(buf)) == 0; 1356 } 1357