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