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