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