dir.c revision 1.16 1 /* $NetBSD: dir.c,v 1.16 1997/05/06 20:59:42 mycroft 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.16 1997/05/06 20:59:42 mycroft 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 if (!hasSlash) {
857 /*
858 * If the file did not have originally a slash,
859 * and we've found it after we've added a pathname,
860 * we've found another directory to search. We
861 * know there's a slash in 'file' because we
862 * put one there. We nuke it after finding it
863 * and call Dir_AddDir to add this new directory
864 * onto the existing search path. Once that's
865 * done, we restore the slash and triumphantly
866 * return the file name, knowing that should
867 a file in this directory every be referenced
868 * again in such a manner, we will find it
869 * without having to do numerous numbers of
870 * access calls. Hurrah!
871 */
872 cp = strrchr (file, '/');
873 *cp = '\0';
874 Dir_AddDir (path, file);
875 *cp = '/';
876 }
877
878 /*
879 * Save the modification time so if it's needed, we don't have
880 * to fetch it again.
881 */
882 if (DEBUG(DIR)) {
883 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
884 file);
885 }
886 entry = Hash_CreateEntry(&mtimes, (char *) file,
887 (Boolean *)NULL);
888 Hash_SetValue(entry, (long)stb.st_mtime);
889 nearmisses += 1;
890 return (file);
891 } else {
892 free (file);
893 }
894 }
895
896 if (DEBUG(DIR)) {
897 printf("failed. ");
898 }
899 Lst_Close (path);
900
901 if (checkedDot) {
902 /*
903 * Already checked by the given name, since . was in the path,
904 * so no point in proceeding...
905 */
906 if (DEBUG(DIR)) {
907 printf("Checked . already, returning NULL\n");
908 }
909 return(NULL);
910 }
911 }
912
913 /*
914 * Didn't find it that way, either. Sigh. Phase 3. Add its directory
915 * onto the search path in any case, just in case, then look for the
916 * thing in the hash table. If we find it, grand. We return a new
917 * copy of the name. Otherwise we sadly return a NULL pointer. Sigh.
918 * Note that if the directory holding the file doesn't exist, this will
919 * do an extra search of the final directory on the path. Unless something
920 * weird happens, this search won't succeed and life will be groovy.
921 *
922 * Sigh. We cannot add the directory onto the search path because
923 * of this amusing case:
924 * $(INSTALLDIR)/$(FILE): $(FILE)
925 *
926 * $(FILE) exists in $(INSTALLDIR) but not in the current one.
927 * When searching for $(FILE), we will find it in $(INSTALLDIR)
928 * b/c we added it here. This is not good...
929 */
930 #ifdef notdef
931 cp[-1] = '\0';
932 Dir_AddDir (path, name);
933 cp[-1] = '/';
934
935 bigmisses += 1;
936 ln = Lst_Last (path);
937 if (ln == NILLNODE) {
938 return ((char *) NULL);
939 } else {
940 p = (Path *) Lst_Datum (ln);
941 }
942
943 if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
944 return (estrdup (name));
945 } else {
946 return ((char *) NULL);
947 }
948 #else /* !notdef */
949 if (DEBUG(DIR)) {
950 printf("Looking for \"%s\"...", name);
951 }
952
953 bigmisses += 1;
954 entry = Hash_FindEntry(&mtimes, name);
955 if (entry != (Hash_Entry *)NULL) {
956 if (DEBUG(DIR)) {
957 printf("got it (in mtime cache)\n");
958 }
959 return(estrdup(name));
960 } else if (stat (name, &stb) == 0) {
961 entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL);
962 if (DEBUG(DIR)) {
963 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
964 name);
965 }
966 Hash_SetValue(entry, (long)stb.st_mtime);
967 return (estrdup (name));
968 } else {
969 if (DEBUG(DIR)) {
970 printf("failed. Returning NULL\n");
971 }
972 return ((char *)NULL);
973 }
974 #endif /* notdef */
975 }
976
977 /*-
978 *-----------------------------------------------------------------------
979 * Dir_MTime --
980 * Find the modification time of the file described by gn along the
981 * search path dirSearchPath.
982 *
983 * Results:
984 * The modification time or 0 if it doesn't exist
985 *
986 * Side Effects:
987 * The modification time is placed in the node's mtime slot.
988 * If the node didn't have a path entry before, and Dir_FindFile
989 * found one for it, the full name is placed in the path slot.
990 *-----------------------------------------------------------------------
991 */
992 int
993 Dir_MTime (gn)
994 GNode *gn; /* the file whose modification time is
995 * desired */
996 {
997 char *fullName; /* the full pathname of name */
998 struct stat stb; /* buffer for finding the mod time */
999 Hash_Entry *entry;
1000
1001 if (gn->type & OP_ARCHV) {
1002 return Arch_MTime (gn);
1003 } else if (gn->path == (char *)NULL) {
1004 if (gn->type & OP_PHONY)
1005 fullName = NULL;
1006 else
1007 fullName = Dir_FindFile (gn->name, dirSearchPath);
1008 } else {
1009 fullName = gn->path;
1010 }
1011
1012 if (fullName == (char *)NULL) {
1013 fullName = estrdup(gn->name);
1014 }
1015
1016 entry = Hash_FindEntry(&mtimes, fullName);
1017 if (entry != (Hash_Entry *)NULL) {
1018 /*
1019 * Only do this once -- the second time folks are checking to
1020 * see if the file was actually updated, so we need to actually go
1021 * to the file system.
1022 */
1023 if (DEBUG(DIR)) {
1024 printf("Using cached time %s for %s\n",
1025 Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName);
1026 }
1027 stb.st_mtime = (time_t)(long)Hash_GetValue(entry);
1028 Hash_DeleteEntry(&mtimes, entry);
1029 } else if (stat (fullName, &stb) < 0) {
1030 if (gn->type & OP_MEMBER) {
1031 if (fullName != gn->path)
1032 free(fullName);
1033 return Arch_MemMTime (gn);
1034 } else {
1035 stb.st_mtime = 0;
1036 }
1037 }
1038 if (fullName && gn->path == (char *)NULL) {
1039 gn->path = fullName;
1040 }
1041
1042 gn->mtime = stb.st_mtime;
1043 return (gn->mtime);
1044 }
1045
1046 /*-
1047 *-----------------------------------------------------------------------
1048 * Dir_AddDir --
1049 * Add the given name to the end of the given path. The order of
1050 * the arguments is backwards so ParseDoDependency can do a
1051 * Lst_ForEach of its list of paths...
1052 *
1053 * Results:
1054 * none
1055 *
1056 * Side Effects:
1057 * A structure is added to the list and the directory is
1058 * read and hashed.
1059 *-----------------------------------------------------------------------
1060 */
1061 void
1062 Dir_AddDir (path, name)
1063 Lst path; /* the path to which the directory should be
1064 * added */
1065 char *name; /* the name of the directory to add */
1066 {
1067 LstNode ln; /* node in case Path structure is found */
1068 register Path *p; /* pointer to new Path structure */
1069 DIR *d; /* for reading directory */
1070 register struct dirent *dp; /* entry in directory */
1071
1072 ln = Lst_Find (openDirectories, (ClientData)name, DirFindName);
1073 if (ln != NILLNODE) {
1074 p = (Path *)Lst_Datum (ln);
1075 if (Lst_Member(path, (ClientData)p) == NILLNODE) {
1076 p->refCount += 1;
1077 (void)Lst_AtEnd (path, (ClientData)p);
1078 }
1079 } else {
1080 if (DEBUG(DIR)) {
1081 printf("Caching %s...", name);
1082 fflush(stdout);
1083 }
1084
1085 if ((d = opendir (name)) != (DIR *) NULL) {
1086 p = (Path *) emalloc (sizeof (Path));
1087 p->name = estrdup (name);
1088 p->hits = 0;
1089 p->refCount = 1;
1090 Hash_InitTable (&p->files, -1);
1091
1092 /*
1093 * Skip the first two entries -- these will *always* be . and ..
1094 */
1095 (void)readdir(d);
1096 (void)readdir(d);
1097
1098 while ((dp = readdir (d)) != (struct dirent *) NULL) {
1099 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
1100 /*
1101 * The sun directory library doesn't check for a 0 inode
1102 * (0-inode slots just take up space), so we have to do
1103 * it ourselves.
1104 */
1105 if (dp->d_fileno == 0) {
1106 continue;
1107 }
1108 #endif /* sun && d_ino */
1109 (void)Hash_CreateEntry(&p->files, dp->d_name, (Boolean *)NULL);
1110 }
1111 (void) closedir (d);
1112 (void)Lst_AtEnd (openDirectories, (ClientData)p);
1113 (void)Lst_AtEnd (path, (ClientData)p);
1114 }
1115 if (DEBUG(DIR)) {
1116 printf("done\n");
1117 }
1118 }
1119 }
1120
1121 /*-
1122 *-----------------------------------------------------------------------
1123 * Dir_CopyDir --
1124 * Callback function for duplicating a search path via Lst_Duplicate.
1125 * Ups the reference count for the directory.
1126 *
1127 * Results:
1128 * Returns the Path it was given.
1129 *
1130 * Side Effects:
1131 * The refCount of the path is incremented.
1132 *
1133 *-----------------------------------------------------------------------
1134 */
1135 ClientData
1136 Dir_CopyDir(p)
1137 ClientData p;
1138 {
1139 ((Path *) p)->refCount += 1;
1140
1141 return ((ClientData)p);
1142 }
1143
1144 /*-
1145 *-----------------------------------------------------------------------
1146 * Dir_MakeFlags --
1147 * Make a string by taking all the directories in the given search
1148 * path and preceding them by the given flag. Used by the suffix
1149 * module to create variables for compilers based on suffix search
1150 * paths.
1151 *
1152 * Results:
1153 * The string mentioned above. Note that there is no space between
1154 * the given flag and each directory. The empty string is returned if
1155 * Things don't go well.
1156 *
1157 * Side Effects:
1158 * None
1159 *-----------------------------------------------------------------------
1160 */
1161 char *
1162 Dir_MakeFlags (flag, path)
1163 char *flag; /* flag which should precede each directory */
1164 Lst path; /* list of directories */
1165 {
1166 char *str; /* the string which will be returned */
1167 char *tstr; /* the current directory preceded by 'flag' */
1168 LstNode ln; /* the node of the current directory */
1169 Path *p; /* the structure describing the current directory */
1170
1171 str = estrdup ("");
1172
1173 if (Lst_Open (path) == SUCCESS) {
1174 while ((ln = Lst_Next (path)) != NILLNODE) {
1175 p = (Path *) Lst_Datum (ln);
1176 tstr = str_concat (flag, p->name, 0);
1177 str = str_concat (str, tstr, STR_ADDSPACE | STR_DOFREE);
1178 }
1179 Lst_Close (path);
1180 }
1181
1182 return (str);
1183 }
1184
1185 /*-
1186 *-----------------------------------------------------------------------
1187 * Dir_Destroy --
1188 * Nuke a directory descriptor, if possible. Callback procedure
1189 * for the suffixes module when destroying a search path.
1190 *
1191 * Results:
1192 * None.
1193 *
1194 * Side Effects:
1195 * If no other path references this directory (refCount == 0),
1196 * the Path and all its data are freed.
1197 *
1198 *-----------------------------------------------------------------------
1199 */
1200 void
1201 Dir_Destroy (pp)
1202 ClientData pp; /* The directory descriptor to nuke */
1203 {
1204 Path *p = (Path *) pp;
1205 p->refCount -= 1;
1206
1207 if (p->refCount == 0) {
1208 LstNode ln;
1209
1210 ln = Lst_Member (openDirectories, (ClientData)p);
1211 (void) Lst_Remove (openDirectories, ln);
1212
1213 Hash_DeleteTable (&p->files);
1214 free((Address)p->name);
1215 free((Address)p);
1216 }
1217 }
1218
1219 /*-
1220 *-----------------------------------------------------------------------
1221 * Dir_ClearPath --
1222 * Clear out all elements of the given search path. This is different
1223 * from destroying the list, notice.
1224 *
1225 * Results:
1226 * None.
1227 *
1228 * Side Effects:
1229 * The path is set to the empty list.
1230 *
1231 *-----------------------------------------------------------------------
1232 */
1233 void
1234 Dir_ClearPath(path)
1235 Lst path; /* Path to clear */
1236 {
1237 Path *p;
1238 while (!Lst_IsEmpty(path)) {
1239 p = (Path *)Lst_DeQueue(path);
1240 Dir_Destroy((ClientData) p);
1241 }
1242 }
1243
1244
1245 /*-
1246 *-----------------------------------------------------------------------
1247 * Dir_Concat --
1248 * Concatenate two paths, adding the second to the end of the first.
1249 * Makes sure to avoid duplicates.
1250 *
1251 * Results:
1252 * None
1253 *
1254 * Side Effects:
1255 * Reference counts for added dirs are upped.
1256 *
1257 *-----------------------------------------------------------------------
1258 */
1259 void
1260 Dir_Concat(path1, path2)
1261 Lst path1; /* Dest */
1262 Lst path2; /* Source */
1263 {
1264 LstNode ln;
1265 Path *p;
1266
1267 for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) {
1268 p = (Path *)Lst_Datum(ln);
1269 if (Lst_Member(path1, (ClientData)p) == NILLNODE) {
1270 p->refCount += 1;
1271 (void)Lst_AtEnd(path1, (ClientData)p);
1272 }
1273 }
1274 }
1275
1276 /********** DEBUG INFO **********/
1277 void
1278 Dir_PrintDirectories()
1279 {
1280 LstNode ln;
1281 Path *p;
1282
1283 printf ("#*** Directory Cache:\n");
1284 printf ("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
1285 hits, misses, nearmisses, bigmisses,
1286 (hits+bigmisses+nearmisses ?
1287 hits * 100 / (hits + bigmisses + nearmisses) : 0));
1288 printf ("# %-20s referenced\thits\n", "directory");
1289 if (Lst_Open (openDirectories) == SUCCESS) {
1290 while ((ln = Lst_Next (openDirectories)) != NILLNODE) {
1291 p = (Path *) Lst_Datum (ln);
1292 printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
1293 }
1294 Lst_Close (openDirectories);
1295 }
1296 }
1297
1298 static int DirPrintDir (p, dummy)
1299 ClientData p;
1300 ClientData dummy;
1301 {
1302 printf ("%s ", ((Path *) p)->name);
1303 return (dummy ? 0 : 0);
1304 }
1305
1306 void
1307 Dir_PrintPath (path)
1308 Lst path;
1309 {
1310 Lst_ForEach (path, DirPrintDir, (ClientData)0);
1311 }
1312