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