1 /* $NetBSD: dir.c,v 1.14 1997/03/29 16:51:26 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * Copyright (c) 1988, 1989 by Adam de Boor 6 * Copyright (c) 1989 by Berkeley Softworks 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Adam de Boor. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 1/2/94"; 44 #else 45 static char rcsid[] = "$NetBSD: dir.c,v 1.14 1997/03/29 16:51:26 christos Exp $"; 46 #endif 47 #endif /* not lint */ 48 49 /*- 50 * dir.c -- 51 * Directory searching using wildcards and/or normal names... 52 * Used both for source wildcarding in the Makefile and for finding 53 * implicit sources. 54 * 55 * The interface for this module is: 56 * Dir_Init Initialize the module. 57 * 58 * Dir_End Cleanup the module. 59 * 60 * Dir_HasWildcards Returns TRUE if the name given it needs to 61 * be wildcard-expanded. 62 * 63 * Dir_Expand Given a pattern and a path, return a Lst of names 64 * which match the pattern on the search path. 65 * 66 * Dir_FindFile Searches for a file on a given search path. 67 * If it exists, the entire path is returned. 68 * Otherwise NULL is returned. 69 * 70 * Dir_MTime Return the modification time of a node. The file 71 * is searched for along the default search path. 72 * The path and mtime fields of the node are filled 73 * in. 74 * 75 * Dir_AddDir Add a directory to a search path. 76 * 77 * Dir_MakeFlags Given a search path and a command flag, create 78 * a string with each of the directories in the path 79 * preceded by the command flag and all of them 80 * separated by a space. 81 * 82 * Dir_Destroy Destroy an element of a search path. Frees up all 83 * things that can be freed for the element as long 84 * as the element is no longer referenced by any other 85 * search path. 86 * Dir_ClearPath Resets a search path to the empty list. 87 * 88 * For debugging: 89 * Dir_PrintDirectories Print stats about the directory cache. 90 */ 91 92 #include <stdio.h> 93 #include <sys/types.h> 94 #include <dirent.h> 95 #include <sys/stat.h> 96 #include "make.h" 97 #include "hash.h" 98 #include "dir.h" 99 100 /* 101 * A search path consists of a Lst of Path structures. A Path structure 102 * has in it the name of the directory and a hash table of all the files 103 * in the directory. This is used to cut down on the number of system 104 * calls necessary to find implicit dependents and their like. Since 105 * these searches are made before any actions are taken, we need not 106 * worry about the directory changing due to creation commands. If this 107 * hampers the style of some makefiles, they must be changed. 108 * 109 * A list of all previously-read directories is kept in the 110 * openDirectories Lst. This list is checked first before a directory 111 * is opened. 112 * 113 * The need for the caching of whole directories is brought about by 114 * the multi-level transformation code in suff.c, which tends to search 115 * for far more files than regular make does. In the initial 116 * implementation, the amount of time spent performing "stat" calls was 117 * truly astronomical. The problem with hashing at the start is, 118 * of course, that pmake doesn't then detect changes to these directories 119 * during the course of the make. Three possibilities suggest themselves: 120 * 121 * 1) just use stat to test for a file's existence. As mentioned 122 * above, this is very inefficient due to the number of checks 123 * engendered by the multi-level transformation code. 124 * 2) use readdir() and company to search the directories, keeping 125 * them open between checks. I have tried this and while it 126 * didn't slow down the process too much, it could severely 127 * affect the amount of parallelism available as each directory 128 * open would take another file descriptor out of play for 129 * handling I/O for another job. Given that it is only recently 130 * that UNIX OS's have taken to allowing more than 20 or 32 131 * file descriptors for a process, this doesn't seem acceptable 132 * to me. 133 * 3) record the mtime of the directory in the Path structure and 134 * verify the directory hasn't changed since the contents were 135 * hashed. This will catch the creation or deletion of files, 136 * but not the updating of files. However, since it is the 137 * creation and deletion that is the problem, this could be 138 * a good thing to do. Unfortunately, if the directory (say ".") 139 * were fairly large and changed fairly frequently, the constant 140 * rehashing could seriously degrade performance. It might be 141 * good in such cases to keep track of the number of rehashes 142 * and if the number goes over a (small) limit, resort to using 143 * stat in its place. 144 * 145 * An additional thing to consider is that pmake is used primarily 146 * to create C programs and until recently pcc-based compilers refused 147 * to allow you to specify where the resulting object file should be 148 * placed. This forced all objects to be created in the current 149 * directory. This isn't meant as a full excuse, just an explanation of 150 * some of the reasons for the caching used here. 151 * 152 * One more note: the location of a target's file is only performed 153 * on the downward traversal of the graph and then only for terminal 154 * nodes in the graph. This could be construed as wrong in some cases, 155 * but prevents inadvertent modification of files when the "installed" 156 * directory for a file is provided in the search path. 157 * 158 * Another data structure maintained by this module is an mtime 159 * cache used when the searching of cached directories fails to find 160 * a file. In the past, Dir_FindFile would simply perform an access() 161 * call in such a case to determine if the file could be found using 162 * just the name given. When this hit, however, all that was gained 163 * was the knowledge that the file existed. Given that an access() is 164 * essentially a stat() without the copyout() call, and that the same 165 * filesystem overhead would have to be incurred in Dir_MTime, it made 166 * sense to replace the access() with a stat() and record the mtime 167 * in a cache for when Dir_MTime was actually called. 168 */ 169 170 Lst dirSearchPath; /* main search path */ 171 172 static Lst openDirectories; /* the list of all open directories */ 173 174 /* 175 * Variables for gathering statistics on the efficiency of the hashing 176 * mechanism. 177 */ 178 static int hits, /* Found in directory cache */ 179 misses, /* Sad, but not evil misses */ 180 nearmisses, /* Found under search path */ 181 bigmisses; /* Sought by itself */ 182 183 static Path *dot; /* contents of current directory */ 184 static Hash_Table mtimes; /* Results of doing a last-resort stat in 185 * Dir_FindFile -- if we have to go to the 186 * system to find the file, we might as well 187 * have its mtime on record. XXX: If this is done 188 * way early, there's a chance other rules will 189 * have already updated the file, in which case 190 * we'll update it again. Generally, there won't 191 * be two rules to update a single file, so this 192 * should be ok, but... */ 193 194 195 static int DirFindName __P((ClientData, ClientData)); 196 static int DirMatchFiles __P((char *, Path *, Lst)); 197 static void DirExpandCurly __P((char *, char *, Lst, Lst)); 198 static void DirExpandInt __P((char *, Lst, Lst)); 199 static int DirPrintWord __P((ClientData, ClientData)); 200 static int DirPrintDir __P((ClientData, ClientData)); 201 202 /*- 203 *----------------------------------------------------------------------- 204 * Dir_Init -- 205 * initialize things for this module 206 * 207 * Results: 208 * none 209 * 210 * Side Effects: 211 * some directories may be opened. 212 *----------------------------------------------------------------------- 213 */ 214 void 215 Dir_Init () 216 { 217 dirSearchPath = Lst_Init (FALSE); 218 openDirectories = Lst_Init (FALSE); 219 Hash_InitTable(&mtimes, 0); 220 221 /* 222 * Since the Path structure is placed on both openDirectories and 223 * the path we give Dir_AddDir (which in this case is openDirectories), 224 * we need to remove "." from openDirectories and what better time to 225 * do it than when we have to fetch the thing anyway? 226 */ 227 Dir_AddDir (openDirectories, "."); 228 dot = (Path *) Lst_DeQueue (openDirectories); 229 230 /* 231 * We always need to have dot around, so we increment its reference count 232 * to make sure it's not destroyed. 233 */ 234 dot->refCount += 1; 235 } 236 237 /*- 238 *----------------------------------------------------------------------- 239 * Dir_End -- 240 * cleanup things for this module 241 * 242 * Results: 243 * none 244 * 245 * Side Effects: 246 * none 247 *----------------------------------------------------------------------- 248 */ 249 void 250 Dir_End() 251 { 252 dot->refCount -= 1; 253 Dir_Destroy((ClientData) dot); 254 Dir_ClearPath(dirSearchPath); 255 Lst_Destroy(dirSearchPath, NOFREE); 256 Dir_ClearPath(openDirectories); 257 Lst_Destroy(openDirectories, NOFREE); 258 Hash_DeleteTable(&mtimes); 259 } 260 261 /*- 262 *----------------------------------------------------------------------- 263 * DirFindName -- 264 * See if the Path structure describes the same directory as the 265 * given one by comparing their names. Called from Dir_AddDir via 266 * Lst_Find when searching the list of open directories. 267 * 268 * Results: 269 * 0 if it is the same. Non-zero otherwise 270 * 271 * Side Effects: 272 * None 273 *----------------------------------------------------------------------- 274 */ 275 static int 276 DirFindName (p, dname) 277 ClientData p; /* Current name */ 278 ClientData dname; /* Desired name */ 279 { 280 return (strcmp (((Path *)p)->name, (char *) dname)); 281 } 282 283 /*- 284 *----------------------------------------------------------------------- 285 * Dir_HasWildcards -- 286 * see if the given name has any wildcard characters in it 287 * be careful not to expand unmatching brackets or braces. 288 * XXX: This code is not 100% correct. ([^]] fails etc.) 289 * I really don't think that make(1) should be expanding 290 * patterns, because then you have to set a mechanism for 291 * escaping the expansion! 292 * 293 * Results: 294 * returns TRUE if the word should be expanded, FALSE otherwise 295 * 296 * Side Effects: 297 * none 298 *----------------------------------------------------------------------- 299 */ 300 Boolean 301 Dir_HasWildcards (name) 302 char *name; /* name to check */ 303 { 304 register char *cp; 305 int wild = 0, brace = 0, bracket = 0; 306 307 for (cp = name; *cp; cp++) { 308 switch(*cp) { 309 case '{': 310 brace++; 311 wild = 1; 312 break; 313 case '}': 314 brace--; 315 break; 316 case '[': 317 bracket++; 318 wild = 1; 319 break; 320 case ']': 321 bracket--; 322 break; 323 case '?': 324 case '*': 325 wild = 1; 326 break; 327 default: 328 break; 329 } 330 } 331 return wild && bracket == 0 && brace == 0; 332 } 333 334 /*- 335 *----------------------------------------------------------------------- 336 * DirMatchFiles -- 337 * Given a pattern and a Path structure, see if any files 338 * match the pattern and add their names to the 'expansions' list if 339 * any do. This is incomplete -- it doesn't take care of patterns like 340 * src / *src / *.c properly (just *.c on any of the directories), but it 341 * will do for now. 342 * 343 * Results: 344 * Always returns 0 345 * 346 * Side Effects: 347 * File names are added to the expansions lst. The directory will be 348 * fully hashed when this is done. 349 *----------------------------------------------------------------------- 350 */ 351 static int 352 DirMatchFiles (pattern, p, expansions) 353 char *pattern; /* Pattern to look for */ 354 Path *p; /* Directory to search */ 355 Lst expansions; /* Place to store the results */ 356 { 357 Hash_Search search; /* Index into the directory's table */ 358 Hash_Entry *entry; /* Current entry in the table */ 359 Boolean isDot; /* TRUE if the directory being searched is . */ 360 361 isDot = (*p->name == '.' && p->name[1] == '\0'); 362 363 for (entry = Hash_EnumFirst(&p->files, &search); 364 entry != (Hash_Entry *)NULL; 365 entry = Hash_EnumNext(&search)) 366 { 367 /* 368 * See if the file matches the given pattern. Note we follow the UNIX 369 * convention that dot files will only be found if the pattern 370 * begins with a dot (note also that as a side effect of the hashing 371 * scheme, .* won't match . or .. since they aren't hashed). 372 */ 373 if (Str_Match(entry->name, pattern) && 374 ((entry->name[0] != '.') || 375 (pattern[0] == '.'))) 376 { 377 (void)Lst_AtEnd(expansions, 378 (isDot ? estrdup(entry->name) : 379 str_concat(p->name, entry->name, 380 STR_ADDSLASH))); 381 } 382 } 383 return (0); 384 } 385 386 /*- 387 *----------------------------------------------------------------------- 388 * DirExpandCurly -- 389 * Expand curly braces like the C shell. Does this recursively. 390 * Note the special case: if after the piece of the curly brace is 391 * done there are no wildcard characters in the result, the result is 392 * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE. 393 * 394 * Results: 395 * None. 396 * 397 * Side Effects: 398 * The given list is filled with the expansions... 399 * 400 *----------------------------------------------------------------------- 401 */ 402 static void 403 DirExpandCurly(word, brace, path, expansions) 404 char *word; /* Entire word to expand */ 405 char *brace; /* First curly brace in it */ 406 Lst path; /* Search path to use */ 407 Lst expansions; /* Place to store the expansions */ 408 { 409 char *end; /* Character after the closing brace */ 410 char *cp; /* Current position in brace clause */ 411 char *start; /* Start of current piece of brace clause */ 412 int bracelevel; /* Number of braces we've seen. If we see a 413 * right brace when this is 0, we've hit the 414 * end of the clause. */ 415 char *file; /* Current expansion */ 416 int otherLen; /* The length of the other pieces of the 417 * expansion (chars before and after the 418 * clause in 'word') */ 419 char *cp2; /* Pointer for checking for wildcards in 420 * expansion before calling Dir_Expand */ 421 422 start = brace+1; 423 424 /* 425 * Find the end of the brace clause first, being wary of nested brace 426 * clauses. 427 */ 428 for (end = start, bracelevel = 0; *end != '\0'; end++) { 429 if (*end == '{') { 430 bracelevel++; 431 } else if ((*end == '}') && (bracelevel-- == 0)) { 432 break; 433 } 434 } 435 if (*end == '\0') { 436 Error("Unterminated {} clause \"%s\"", start); 437 return; 438 } else { 439 end++; 440 } 441 otherLen = brace - word + strlen(end); 442 443 for (cp = start; cp < end; cp++) { 444 /* 445 * Find the end of this piece of the clause. 446 */ 447 bracelevel = 0; 448 while (*cp != ',') { 449 if (*cp == '{') { 450 bracelevel++; 451 } else if ((*cp == '}') && (bracelevel-- <= 0)) { 452 break; 453 } 454 cp++; 455 } 456 /* 457 * Allocate room for the combination and install the three pieces. 458 */ 459 file = emalloc(otherLen + cp - start + 1); 460 if (brace != word) { 461 strncpy(file, word, brace-word); 462 } 463 if (cp != start) { 464 strncpy(&file[brace-word], start, cp-start); 465 } 466 strcpy(&file[(brace-word)+(cp-start)], end); 467 468 /* 469 * See if the result has any wildcards in it. If we find one, call 470 * Dir_Expand right away, telling it to place the result on our list 471 * of expansions. 472 */ 473 for (cp2 = file; *cp2 != '\0'; cp2++) { 474 switch(*cp2) { 475 case '*': 476 case '?': 477 case '{': 478 case '[': 479 Dir_Expand(file, path, expansions); 480 goto next; 481 } 482 } 483 if (*cp2 == '\0') { 484 /* 485 * Hit the end w/o finding any wildcards, so stick the expansion 486 * on the end of the list. 487 */ 488 (void)Lst_AtEnd(expansions, file); 489 } else { 490 next: 491 free(file); 492 } 493 start = cp+1; 494 } 495 } 496 497 498 /*- 499 *----------------------------------------------------------------------- 500 * DirExpandInt -- 501 * Internal expand routine. Passes through the directories in the 502 * path one by one, calling DirMatchFiles for each. NOTE: This still 503 * doesn't handle patterns in directories... 504 * 505 * Results: 506 * None. 507 * 508 * Side Effects: 509 * Things are added to the expansions list. 510 * 511 *----------------------------------------------------------------------- 512 */ 513 static void 514 DirExpandInt(word, path, expansions) 515 char *word; /* Word to expand */ 516 Lst path; /* Path on which to look */ 517 Lst expansions; /* Place to store the result */ 518 { 519 LstNode ln; /* Current node */ 520 Path *p; /* Directory in the node */ 521 522 if (Lst_Open(path) == SUCCESS) { 523 while ((ln = Lst_Next(path)) != NILLNODE) { 524 p = (Path *)Lst_Datum(ln); 525 DirMatchFiles(word, p, expansions); 526 } 527 Lst_Close(path); 528 } 529 } 530 531 /*- 532 *----------------------------------------------------------------------- 533 * DirPrintWord -- 534 * Print a word in the list of expansions. Callback for Dir_Expand 535 * when DEBUG(DIR), via Lst_ForEach. 536 * 537 * Results: 538 * === 0 539 * 540 * Side Effects: 541 * The passed word is printed, followed by a space. 542 * 543 *----------------------------------------------------------------------- 544 */ 545 static int 546 DirPrintWord(word, dummy) 547 ClientData word; 548 ClientData dummy; 549 { 550 printf("%s ", (char *) word); 551 552 return(dummy ? 0 : 0); 553 } 554 555 /*- 556 *----------------------------------------------------------------------- 557 * Dir_Expand -- 558 * Expand the given word into a list of words by globbing it looking 559 * in the directories on the given search path. 560 * 561 * Results: 562 * A list of words consisting of the files which exist along the search 563 * path matching the given pattern. 564 * 565 * Side Effects: 566 * Directories may be opened. Who knows? 567 *----------------------------------------------------------------------- 568 */ 569 void 570 Dir_Expand (word, path, expansions) 571 char *word; /* the word to expand */ 572 Lst path; /* the list of directories in which to find 573 * the resulting files */ 574 Lst expansions; /* the list on which to place the results */ 575 { 576 char *cp; 577 578 if (DEBUG(DIR)) { 579 printf("expanding \"%s\"...", word); 580 } 581 582 cp = strchr(word, '{'); 583 if (cp) { 584 DirExpandCurly(word, cp, path, expansions); 585 } else { 586 cp = strchr(word, '/'); 587 if (cp) { 588 /* 589 * The thing has a directory component -- find the first wildcard 590 * in the string. 591 */ 592 for (cp = word; *cp; cp++) { 593 if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') { 594 break; 595 } 596 } 597 if (*cp == '{') { 598 /* 599 * This one will be fun. 600 */ 601 DirExpandCurly(word, cp, path, expansions); 602 return; 603 } else if (*cp != '\0') { 604 /* 605 * Back up to the start of the component 606 */ 607 char *dirpath; 608 609 while (cp > word && *cp != '/') { 610 cp--; 611 } 612 if (cp != word) { 613 char sc; 614 /* 615 * If the glob isn't in the first component, try and find 616 * all the components up to the one with a wildcard. 617 */ 618 sc = cp[1]; 619 cp[1] = '\0'; 620 dirpath = Dir_FindFile(word, path); 621 cp[1] = sc; 622 /* 623 * dirpath is null if can't find the leading component 624 * XXX: Dir_FindFile won't find internal components. 625 * i.e. if the path contains ../Etc/Object and we're 626 * looking for Etc, it won't be found. Ah well. 627 * Probably not important. 628 */ 629 if (dirpath != (char *)NULL) { 630 char *dp = &dirpath[strlen(dirpath) - 1]; 631 if (*dp == '/') 632 *dp = '\0'; 633 path = Lst_Init(FALSE); 634 Dir_AddDir(path, dirpath); 635 DirExpandInt(cp+1, path, expansions); 636 Lst_Destroy(path, NOFREE); 637 } 638 } else { 639 /* 640 * Start the search from the local directory 641 */ 642 DirExpandInt(word, path, expansions); 643 } 644 } else { 645 /* 646 * Return the file -- this should never happen. 647 */ 648 DirExpandInt(word, path, expansions); 649 } 650 } else { 651 /* 652 * First the files in dot 653 */ 654 DirMatchFiles(word, dot, expansions); 655 656 /* 657 * Then the files in every other directory on the path. 658 */ 659 DirExpandInt(word, path, expansions); 660 } 661 } 662 if (DEBUG(DIR)) { 663 Lst_ForEach(expansions, DirPrintWord, (ClientData) 0); 664 fputc('\n', stdout); 665 } 666 } 667 668 /*- 669 *----------------------------------------------------------------------- 670 * Dir_FindFile -- 671 * Find the file with the given name along the given search path. 672 * 673 * Results: 674 * The path to the file or NULL. This path is guaranteed to be in a 675 * different part of memory than name and so may be safely free'd. 676 * 677 * Side Effects: 678 * If the file is found in a directory which is not on the path 679 * already (either 'name' is absolute or it is a relative path 680 * [ dir1/.../dirn/file ] which exists below one of the directories 681 * already on the search path), its directory is added to the end 682 * of the path on the assumption that there will be more files in 683 * that directory later on. Sometimes this is true. Sometimes not. 684 *----------------------------------------------------------------------- 685 */ 686 char * 687 Dir_FindFile (name, path) 688 char *name; /* the file to find */ 689 Lst path; /* the Lst of directories to search */ 690 { 691 register char *p1; /* pointer into p->name */ 692 register char *p2; /* pointer into name */ 693 LstNode ln; /* a list element */ 694 register char *file; /* the current filename to check */ 695 register Path *p; /* current path member */ 696 register char *cp; /* index of first slash, if any */ 697 Boolean hasSlash; /* true if 'name' contains a / */ 698 struct stat stb; /* Buffer for stat, if necessary */ 699 Hash_Entry *entry; /* Entry for mtimes table */ 700 701 /* 702 * Find the final component of the name and note whether it has a 703 * slash in it (the name, I mean) 704 */ 705 cp = strrchr (name, '/'); 706 if (cp) { 707 hasSlash = TRUE; 708 cp += 1; 709 } else { 710 hasSlash = FALSE; 711 cp = name; 712 } 713 714 if (DEBUG(DIR)) { 715 printf("Searching for %s...", name); 716 } 717 /* 718 * No matter what, we always look for the file in the current directory 719 * before anywhere else and we *do not* add the ./ to it if it exists. 720 * This is so there are no conflicts between what the user specifies 721 * (fish.c) and what pmake finds (./fish.c). 722 */ 723 if ((!hasSlash || (cp - name == 2 && *name == '.')) && 724 (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL)) { 725 if (DEBUG(DIR)) { 726 printf("in '.'\n"); 727 } 728 hits += 1; 729 dot->hits += 1; 730 return (estrdup (name)); 731 } 732 733 if (Lst_Open (path) == FAILURE) { 734 if (DEBUG(DIR)) { 735 printf("couldn't open path, file not found\n"); 736 } 737 misses += 1; 738 return ((char *) NULL); 739 } 740 741 /* 742 * We look through all the directories on the path seeking one which 743 * contains the final component of the given name and whose final 744 * component(s) match the name's initial component(s). If such a beast 745 * is found, we concatenate the directory name and the final component 746 * and return the resulting string. If we don't find any such thing, 747 * we go on to phase two... 748 */ 749 while ((ln = Lst_Next (path)) != NILLNODE) { 750 p = (Path *) Lst_Datum (ln); 751 if (DEBUG(DIR)) { 752 printf("%s...", p->name); 753 } 754 if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) { 755 if (DEBUG(DIR)) { 756 printf("here..."); 757 } 758 if (hasSlash) { 759 /* 760 * If the name had a slash, its initial components and p's 761 * final components must match. This is false if a mismatch 762 * is encountered before all of the initial components 763 * have been checked (p2 > name at the end of the loop), or 764 * we matched only part of one of the components of p 765 * along with all the rest of them (*p1 != '/'). 766 */ 767 p1 = p->name + strlen (p->name) - 1; 768 p2 = cp - 2; 769 while (p2 >= name && p1 >= p->name && *p1 == *p2) { 770 p1 -= 1; p2 -= 1; 771 } 772 if (p2 >= name || (p1 >= p->name && *p1 != '/')) { 773 if (DEBUG(DIR)) { 774 printf("component mismatch -- continuing..."); 775 } 776 continue; 777 } 778 } 779 file = str_concat (p->name, cp, STR_ADDSLASH); 780 if (DEBUG(DIR)) { 781 printf("returning %s\n", file); 782 } 783 Lst_Close (path); 784 p->hits += 1; 785 hits += 1; 786 return (file); 787 } else if (hasSlash) { 788 /* 789 * If the file has a leading path component and that component 790 * exactly matches the entire name of the current search 791 * directory, we assume the file doesn't exist and return NULL. 792 */ 793 for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) { 794 continue; 795 } 796 if (*p1 == '\0' && p2 == cp - 1) { 797 if (DEBUG(DIR)) { 798 printf("must be here but isn't -- returing NULL\n"); 799 } 800 Lst_Close (path); 801 return ((char *) NULL); 802 } 803 } 804 } 805 806 /* 807 * We didn't find the file on any existing members of the directory. 808 * If the name doesn't contain a slash, that means it doesn't exist. 809 * If it *does* contain a slash, however, there is still hope: it 810 * could be in a subdirectory of one of the members of the search 811 * path. (eg. /usr/include and sys/types.h. The above search would 812 * fail to turn up types.h in /usr/include, but it *is* in 813 * /usr/include/sys/types.h) If we find such a beast, we assume there 814 * will be more (what else can we assume?) and add all but the last 815 * component of the resulting name onto the search path (at the 816 * end). This phase is only performed if the file is *not* absolute. 817 */ 818 if (!hasSlash) { 819 if (DEBUG(DIR)) { 820 printf("failed.\n"); 821 } 822 misses += 1; 823 return ((char *) NULL); 824 } 825 826 if (*name != '/') { 827 Boolean checkedDot = FALSE; 828 829 if (DEBUG(DIR)) { 830 printf("failed. Trying subdirectories..."); 831 } 832 (void) Lst_Open (path); 833 while ((ln = Lst_Next (path)) != NILLNODE) { 834 p = (Path *) Lst_Datum (ln); 835 if (p != dot) { 836 file = str_concat (p->name, name, STR_ADDSLASH); 837 } else { 838 /* 839 * Checking in dot -- DON'T put a leading ./ on the thing. 840 */ 841 file = estrdup(name); 842 checkedDot = TRUE; 843 } 844 if (DEBUG(DIR)) { 845 printf("checking %s...", file); 846 } 847 848 849 if (stat (file, &stb) == 0) { 850 if (DEBUG(DIR)) { 851 printf("got it.\n"); 852 } 853 854 Lst_Close (path); 855 856 /* 857 * We've found another directory to search. We know there's 858 * a slash in 'file' because we put one there. We nuke it after 859 * finding it and call Dir_AddDir to add this new directory 860 * onto the existing search path. Once that's done, we restore 861 * the slash and triumphantly return the file name, knowing 862 * that should a file in this directory every be referenced 863 * again in such a manner, we will find it without having to do 864 * numerous numbers of access calls. Hurrah! 865 */ 866 cp = strrchr (file, '/'); 867 *cp = '\0'; 868 Dir_AddDir (path, file); 869 *cp = '/'; 870 871 /* 872 * Save the modification time so if it's needed, we don't have 873 * to fetch it again. 874 */ 875 if (DEBUG(DIR)) { 876 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), 877 file); 878 } 879 entry = Hash_CreateEntry(&mtimes, (char *) file, 880 (Boolean *)NULL); 881 Hash_SetValue(entry, (long)stb.st_mtime); 882 nearmisses += 1; 883 return (file); 884 } else { 885 free (file); 886 } 887 } 888 889 if (DEBUG(DIR)) { 890 printf("failed. "); 891 } 892 Lst_Close (path); 893 894 if (checkedDot) { 895 /* 896 * Already checked by the given name, since . was in the path, 897 * so no point in proceeding... 898 */ 899 if (DEBUG(DIR)) { 900 printf("Checked . already, returning NULL\n"); 901 } 902 return(NULL); 903 } 904 } 905 906 /* 907 * Didn't find it that way, either. Sigh. Phase 3. Add its directory 908 * onto the search path in any case, just in case, then look for the 909 * thing in the hash table. If we find it, grand. We return a new 910 * copy of the name. Otherwise we sadly return a NULL pointer. Sigh. 911 * Note that if the directory holding the file doesn't exist, this will 912 * do an extra search of the final directory on the path. Unless something 913 * weird happens, this search won't succeed and life will be groovy. 914 * 915 * Sigh. We cannot add the directory onto the search path because 916 * of this amusing case: 917 * $(INSTALLDIR)/$(FILE): $(FILE) 918 * 919 * $(FILE) exists in $(INSTALLDIR) but not in the current one. 920 * When searching for $(FILE), we will find it in $(INSTALLDIR) 921 * b/c we added it here. This is not good... 922 */ 923 #ifdef notdef 924 cp[-1] = '\0'; 925 Dir_AddDir (path, name); 926 cp[-1] = '/'; 927 928 bigmisses += 1; 929 ln = Lst_Last (path); 930 if (ln == NILLNODE) { 931 return ((char *) NULL); 932 } else { 933 p = (Path *) Lst_Datum (ln); 934 } 935 936 if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) { 937 return (estrdup (name)); 938 } else { 939 return ((char *) NULL); 940 } 941 #else /* !notdef */ 942 if (DEBUG(DIR)) { 943 printf("Looking for \"%s\"...", name); 944 } 945 946 bigmisses += 1; 947 entry = Hash_FindEntry(&mtimes, name); 948 if (entry != (Hash_Entry *)NULL) { 949 if (DEBUG(DIR)) { 950 printf("got it (in mtime cache)\n"); 951 } 952 return(estrdup(name)); 953 } else if (stat (name, &stb) == 0) { 954 entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL); 955 if (DEBUG(DIR)) { 956 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), 957 name); 958 } 959 Hash_SetValue(entry, (long)stb.st_mtime); 960 return (estrdup (name)); 961 } else { 962 if (DEBUG(DIR)) { 963 printf("failed. Returning NULL\n"); 964 } 965 return ((char *)NULL); 966 } 967 #endif /* notdef */ 968 } 969 970 /*- 971 *----------------------------------------------------------------------- 972 * Dir_MTime -- 973 * Find the modification time of the file described by gn along the 974 * search path dirSearchPath. 975 * 976 * Results: 977 * The modification time or 0 if it doesn't exist 978 * 979 * Side Effects: 980 * The modification time is placed in the node's mtime slot. 981 * If the node didn't have a path entry before, and Dir_FindFile 982 * found one for it, the full name is placed in the path slot. 983 *----------------------------------------------------------------------- 984 */ 985 int 986 Dir_MTime (gn) 987 GNode *gn; /* the file whose modification time is 988 * desired */ 989 { 990 char *fullName; /* the full pathname of name */ 991 struct stat stb; /* buffer for finding the mod time */ 992 Hash_Entry *entry; 993 994 if (gn->type & OP_ARCHV) { 995 return Arch_MTime (gn); 996 } else if (gn->path == (char *)NULL) { 997 fullName = Dir_FindFile (gn->name, dirSearchPath); 998 } else { 999 fullName = gn->path; 1000 } 1001 1002 if (fullName == (char *)NULL) { 1003 fullName = estrdup(gn->name); 1004 } 1005 1006 entry = Hash_FindEntry(&mtimes, fullName); 1007 if (entry != (Hash_Entry *)NULL) { 1008 /* 1009 * Only do this once -- the second time folks are checking to 1010 * see if the file was actually updated, so we need to actually go 1011 * to the file system. 1012 */ 1013 if (DEBUG(DIR)) { 1014 printf("Using cached time %s for %s\n", 1015 Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName); 1016 } 1017 stb.st_mtime = (time_t)(long)Hash_GetValue(entry); 1018 Hash_DeleteEntry(&mtimes, entry); 1019 } else if (stat (fullName, &stb) < 0) { 1020 if (gn->type & OP_MEMBER) { 1021 if (fullName != gn->path) 1022 free(fullName); 1023 return Arch_MemMTime (gn); 1024 } else { 1025 stb.st_mtime = 0; 1026 } 1027 } 1028 if (fullName && gn->path == (char *)NULL) { 1029 gn->path = fullName; 1030 } 1031 1032 gn->mtime = stb.st_mtime; 1033 return (gn->mtime); 1034 } 1035 1036 /*- 1037 *----------------------------------------------------------------------- 1038 * Dir_AddDir -- 1039 * Add the given name to the end of the given path. The order of 1040 * the arguments is backwards so ParseDoDependency can do a 1041 * Lst_ForEach of its list of paths... 1042 * 1043 * Results: 1044 * none 1045 * 1046 * Side Effects: 1047 * A structure is added to the list and the directory is 1048 * read and hashed. 1049 *----------------------------------------------------------------------- 1050 */ 1051 void 1052 Dir_AddDir (path, name) 1053 Lst path; /* the path to which the directory should be 1054 * added */ 1055 char *name; /* the name of the directory to add */ 1056 { 1057 LstNode ln; /* node in case Path structure is found */ 1058 register Path *p; /* pointer to new Path structure */ 1059 DIR *d; /* for reading directory */ 1060 register struct dirent *dp; /* entry in directory */ 1061 1062 ln = Lst_Find (openDirectories, (ClientData)name, DirFindName); 1063 if (ln != NILLNODE) { 1064 p = (Path *)Lst_Datum (ln); 1065 if (Lst_Member(path, (ClientData)p) == NILLNODE) { 1066 p->refCount += 1; 1067 (void)Lst_AtEnd (path, (ClientData)p); 1068 } 1069 } else { 1070 if (DEBUG(DIR)) { 1071 printf("Caching %s...", name); 1072 fflush(stdout); 1073 } 1074 1075 if ((d = opendir (name)) != (DIR *) NULL) { 1076 p = (Path *) emalloc (sizeof (Path)); 1077 p->name = estrdup (name); 1078 p->hits = 0; 1079 p->refCount = 1; 1080 Hash_InitTable (&p->files, -1); 1081 1082 /* 1083 * Skip the first two entries -- these will *always* be . and .. 1084 */ 1085 (void)readdir(d); 1086 (void)readdir(d); 1087 1088 while ((dp = readdir (d)) != (struct dirent *) NULL) { 1089 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */ 1090 /* 1091 * The sun directory library doesn't check for a 0 inode 1092 * (0-inode slots just take up space), so we have to do 1093 * it ourselves. 1094 */ 1095 if (dp->d_fileno == 0) { 1096 continue; 1097 } 1098 #endif /* sun && d_ino */ 1099 (void)Hash_CreateEntry(&p->files, dp->d_name, (Boolean *)NULL); 1100 } 1101 (void) closedir (d); 1102 (void)Lst_AtEnd (openDirectories, (ClientData)p); 1103 (void)Lst_AtEnd (path, (ClientData)p); 1104 } 1105 if (DEBUG(DIR)) { 1106 printf("done\n"); 1107 } 1108 } 1109 } 1110 1111 /*- 1112 *----------------------------------------------------------------------- 1113 * Dir_CopyDir -- 1114 * Callback function for duplicating a search path via Lst_Duplicate. 1115 * Ups the reference count for the directory. 1116 * 1117 * Results: 1118 * Returns the Path it was given. 1119 * 1120 * Side Effects: 1121 * The refCount of the path is incremented. 1122 * 1123 *----------------------------------------------------------------------- 1124 */ 1125 ClientData 1126 Dir_CopyDir(p) 1127 ClientData p; 1128 { 1129 ((Path *) p)->refCount += 1; 1130 1131 return ((ClientData)p); 1132 } 1133 1134 /*- 1135 *----------------------------------------------------------------------- 1136 * Dir_MakeFlags -- 1137 * Make a string by taking all the directories in the given search 1138 * path and preceding them by the given flag. Used by the suffix 1139 * module to create variables for compilers based on suffix search 1140 * paths. 1141 * 1142 * Results: 1143 * The string mentioned above. Note that there is no space between 1144 * the given flag and each directory. The empty string is returned if 1145 * Things don't go well. 1146 * 1147 * Side Effects: 1148 * None 1149 *----------------------------------------------------------------------- 1150 */ 1151 char * 1152 Dir_MakeFlags (flag, path) 1153 char *flag; /* flag which should precede each directory */ 1154 Lst path; /* list of directories */ 1155 { 1156 char *str; /* the string which will be returned */ 1157 char *tstr; /* the current directory preceded by 'flag' */ 1158 LstNode ln; /* the node of the current directory */ 1159 Path *p; /* the structure describing the current directory */ 1160 1161 str = estrdup (""); 1162 1163 if (Lst_Open (path) == SUCCESS) { 1164 while ((ln = Lst_Next (path)) != NILLNODE) { 1165 p = (Path *) Lst_Datum (ln); 1166 tstr = str_concat (flag, p->name, 0); 1167 str = str_concat (str, tstr, STR_ADDSPACE | STR_DOFREE); 1168 } 1169 Lst_Close (path); 1170 } 1171 1172 return (str); 1173 } 1174 1175 /*- 1176 *----------------------------------------------------------------------- 1177 * Dir_Destroy -- 1178 * Nuke a directory descriptor, if possible. Callback procedure 1179 * for the suffixes module when destroying a search path. 1180 * 1181 * Results: 1182 * None. 1183 * 1184 * Side Effects: 1185 * If no other path references this directory (refCount == 0), 1186 * the Path and all its data are freed. 1187 * 1188 *----------------------------------------------------------------------- 1189 */ 1190 void 1191 Dir_Destroy (pp) 1192 ClientData pp; /* The directory descriptor to nuke */ 1193 { 1194 Path *p = (Path *) pp; 1195 p->refCount -= 1; 1196 1197 if (p->refCount == 0) { 1198 LstNode ln; 1199 1200 ln = Lst_Member (openDirectories, (ClientData)p); 1201 (void) Lst_Remove (openDirectories, ln); 1202 1203 Hash_DeleteTable (&p->files); 1204 free((Address)p->name); 1205 free((Address)p); 1206 } 1207 } 1208 1209 /*- 1210 *----------------------------------------------------------------------- 1211 * Dir_ClearPath -- 1212 * Clear out all elements of the given search path. This is different 1213 * from destroying the list, notice. 1214 * 1215 * Results: 1216 * None. 1217 * 1218 * Side Effects: 1219 * The path is set to the empty list. 1220 * 1221 *----------------------------------------------------------------------- 1222 */ 1223 void 1224 Dir_ClearPath(path) 1225 Lst path; /* Path to clear */ 1226 { 1227 Path *p; 1228 while (!Lst_IsEmpty(path)) { 1229 p = (Path *)Lst_DeQueue(path); 1230 Dir_Destroy((ClientData) p); 1231 } 1232 } 1233 1234 1235 /*- 1236 *----------------------------------------------------------------------- 1237 * Dir_Concat -- 1238 * Concatenate two paths, adding the second to the end of the first. 1239 * Makes sure to avoid duplicates. 1240 * 1241 * Results: 1242 * None 1243 * 1244 * Side Effects: 1245 * Reference counts for added dirs are upped. 1246 * 1247 *----------------------------------------------------------------------- 1248 */ 1249 void 1250 Dir_Concat(path1, path2) 1251 Lst path1; /* Dest */ 1252 Lst path2; /* Source */ 1253 { 1254 LstNode ln; 1255 Path *p; 1256 1257 for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) { 1258 p = (Path *)Lst_Datum(ln); 1259 if (Lst_Member(path1, (ClientData)p) == NILLNODE) { 1260 p->refCount += 1; 1261 (void)Lst_AtEnd(path1, (ClientData)p); 1262 } 1263 } 1264 } 1265 1266 /********** DEBUG INFO **********/ 1267 void 1268 Dir_PrintDirectories() 1269 { 1270 LstNode ln; 1271 Path *p; 1272 1273 printf ("#*** Directory Cache:\n"); 1274 printf ("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n", 1275 hits, misses, nearmisses, bigmisses, 1276 (hits+bigmisses+nearmisses ? 1277 hits * 100 / (hits + bigmisses + nearmisses) : 0)); 1278 printf ("# %-20s referenced\thits\n", "directory"); 1279 if (Lst_Open (openDirectories) == SUCCESS) { 1280 while ((ln = Lst_Next (openDirectories)) != NILLNODE) { 1281 p = (Path *) Lst_Datum (ln); 1282 printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits); 1283 } 1284 Lst_Close (openDirectories); 1285 } 1286 } 1287 1288 static int DirPrintDir (p, dummy) 1289 ClientData p; 1290 ClientData dummy; 1291 { 1292 printf ("%s ", ((Path *) p)->name); 1293 return (dummy ? 0 : 0); 1294 } 1295 1296 void 1297 Dir_PrintPath (path) 1298 Lst path; 1299 { 1300 Lst_ForEach (path, DirPrintDir, (ClientData)0); 1301 } 1302