Changeset 24 for branches/FREEBSD/src/kmk/dir.c
- Timestamp:
- Nov 26, 2002, 10:24:54 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FREEBSD/src/kmk/dir.c
r10 r24 1 1 /* 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 4 3 * Copyright (c) 1988, 1989 by Adam de Boor 5 4 * Copyright (c) 1989 by Berkeley Softworks … … 36 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 36 * SUCH DAMAGE. 38 * 39 * @(#)dir.c 8.2 (Berkeley) 1/2/94 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD: src/usr.bin/make/dir.c,v 1.30 2002/10/09 03:42:10 jmallett Exp $"); 37 */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 1/2/94"; 42 #else 43 static const char rcsid[] = 44 "$FreeBSD: src/usr.bin/make/dir.c,v 1.10.2.1 2001/02/13 03:13:57 will Exp $"; 45 #endif 46 #endif /* not lint */ 44 47 45 48 /*- … … 88 91 #include <stdio.h> 89 92 #include <sys/types.h> 93 #include <dirent.h> 90 94 #include <sys/stat.h> 91 #include <dirent.h>92 #include <err.h>93 95 #include "make.h" 94 96 #include "hash.h" … … 190 192 191 193 192 static int DirFindName (void *, void *);193 static int DirMatchFiles (char *, Path *, Lst);194 static void DirExpandCurly (char *, char *, Lst, Lst);195 static void DirExpandInt (char *, Lst, Lst);196 static int DirPrintWord (void *, void *);197 static int DirPrintDir (void *, void *);194 static int DirFindName __P((ClientData, ClientData)); 195 static int DirMatchFiles __P((char *, Path *, Lst)); 196 static void DirExpandCurly __P((char *, char *, Lst, Lst)); 197 static void DirExpandInt __P((char *, Lst, Lst)); 198 static int DirPrintWord __P((ClientData, ClientData)); 199 static int DirPrintDir __P((ClientData, ClientData)); 198 200 199 201 /*- … … 210 212 */ 211 213 void 212 Dir_Init ( void)214 Dir_Init () 213 215 { 214 216 dirSearchPath = Lst_Init (FALSE); … … 247 249 */ 248 250 void 249 Dir_End( void)251 Dir_End() 250 252 { 251 253 dot->refCount -= 1; 252 Dir_Destroy(( void *) dot);254 Dir_Destroy((ClientData) dot); 253 255 Dir_ClearPath(dirSearchPath); 254 256 Lst_Destroy(dirSearchPath, NOFREE); … … 273 275 */ 274 276 static int 275 DirFindName (void *p, void *dname) 277 DirFindName (p, dname) 278 ClientData p; /* Current name */ 279 ClientData dname; /* Desired name */ 276 280 { 277 281 return (strcmp (((Path *)p)->name, (char *) dname)); … … 281 285 *----------------------------------------------------------------------- 282 286 * Dir_HasWildcards -- 283 * See if the given name has any wildcard characters in it.287 * see if the given name has any wildcard characters in it 284 288 * 285 289 * Results: … … 291 295 */ 292 296 Boolean 293 Dir_HasWildcards ( char *name)294 { 295 char *cp; 296 int wild = 0, brace = 0, bracket = 0;297 Dir_HasWildcards (name) 298 char *name; /* name to check */ 299 { 300 register char *cp; 297 301 298 302 for (cp = name; *cp; cp++) { 299 303 switch(*cp) { 300 304 case '{': 301 brace++;302 wild = 1;303 break;304 case '}':305 brace--;306 break;307 305 case '[': 308 bracket++;309 wild = 1;310 break;311 case ']':312 bracket--;313 break;314 306 case '?': 315 307 case '*': 316 wild = 1; 317 break; 318 default: 319 break; 320 } 321 } 322 return wild && bracket == 0 && brace == 0; 308 return (TRUE); 309 } 310 } 311 return (FALSE); 323 312 } 324 313 … … 341 330 */ 342 331 static int 343 DirMatchFiles (char *pattern, Path *p, Lst expansions) 332 DirMatchFiles (pattern, p, expansions) 333 char *pattern; /* Pattern to look for */ 334 Path *p; /* Directory to search */ 335 Lst expansions; /* Place to store the results */ 344 336 { 345 337 Hash_Search search; /* Index into the directory's table */ … … 378 370 * Note the special case: if after the piece of the curly brace is 379 371 * done there are no wildcard characters in the result, the result is 380 * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE. The 381 * given arguments are the entire word to expand, the first curly 382 * brace in the word, the search path, and the list to store the 383 * expansions in. 372 * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE. 384 373 * 385 374 * Results: … … 392 381 */ 393 382 static void 394 DirExpandCurly(char *word, char *brace, Lst path, Lst expansions) 383 DirExpandCurly(word, brace, path, expansions) 384 char *word; /* Entire word to expand */ 385 char *brace; /* First curly brace in it */ 386 Lst path; /* Search path to use */ 387 Lst expansions; /* Place to store the expansions */ 395 388 { 396 389 char *end; /* Character after the closing brace */ … … 466 459 Dir_Expand(file, path, expansions); 467 460 goto next; 468 default:469 break;470 461 } 471 462 } … … 490 481 * Internal expand routine. Passes through the directories in the 491 482 * path one by one, calling DirMatchFiles for each. NOTE: This still 492 * doesn't handle patterns in directories... Works given a word to 493 * expand, a path to look in, and a list to store expansions in. 483 * doesn't handle patterns in directories... 494 484 * 495 485 * Results: … … 502 492 */ 503 493 static void 504 DirExpandInt(char *word, Lst path, Lst expansions) 494 DirExpandInt(word, path, expansions) 495 char *word; /* Word to expand */ 496 Lst path; /* Path on which to look */ 497 Lst expansions; /* Place to store the result */ 505 498 { 506 499 LstNode ln; /* Current node */ … … 508 501 509 502 if (Lst_Open(path) == SUCCESS) { 510 while ((ln = Lst_Next(path)) != N ULL) {503 while ((ln = Lst_Next(path)) != NILLNODE) { 511 504 p = (Path *)Lst_Datum(ln); 512 505 DirMatchFiles(word, p, expansions); … … 531 524 */ 532 525 static int 533 DirPrintWord(void *word, void *dummy __unused) 534 { 535 DEBUGF(DIR, ("%s ", (char *) word)); 536 537 return (0); 526 DirPrintWord(word, dummy) 527 ClientData word; 528 ClientData dummy; 529 { 530 printf("%s ", (char *) word); 531 532 return(dummy ? 0 : 0); 538 533 } 539 534 … … 546 541 * Results: 547 542 * A list of words consisting of the files which exist along the search 548 * path matching the given pattern is placed in expansions.543 * path matching the given pattern. 549 544 * 550 545 * Side Effects: … … 553 548 */ 554 549 void 555 Dir_Expand (char *word, Lst path, Lst expansions) 550 Dir_Expand (word, path, expansions) 551 char *word; /* the word to expand */ 552 Lst path; /* the list of directories in which to find 553 * the resulting files */ 554 Lst expansions; /* the list on which to place the results */ 556 555 { 557 556 char *cp; 558 557 559 DEBUGF(DIR, ("expanding \"%s\"...", word)); 558 if (DEBUG(DIR)) { 559 printf("expanding \"%s\"...", word); 560 } 560 561 561 562 cp = strchr(word, '{'); … … 640 641 } 641 642 if (DEBUG(DIR)) { 642 Lst_ForEach(expansions, DirPrintWord, ( void *) 0);643 DEBUGF(DIR, ("\n"));643 Lst_ForEach(expansions, DirPrintWord, (ClientData) 0); 644 fputc('\n', stdout); 644 645 } 645 646 } … … 664 665 */ 665 666 char * 666 Dir_FindFile (char *name, Lst path) 667 { 668 char *p1; /* pointer into p->name */ 669 char *p2; /* pointer into name */ 667 Dir_FindFile (name, path) 668 char *name; /* the file to find */ 669 Lst path; /* the Lst of directories to search */ 670 { 671 register char *p1; /* pointer into p->name */ 672 register char *p2; /* pointer into name */ 670 673 LstNode ln; /* a list element */ 671 char*file; /* the current filename to check */672 Path*p; /* current path member */673 char*cp; /* index of first slash, if any */674 register char *file; /* the current filename to check */ 675 register Path *p; /* current path member */ 676 register char *cp; /* index of first slash, if any */ 674 677 Boolean hasSlash; /* true if 'name' contains a / */ 675 678 struct stat stb; /* Buffer for stat, if necessary */ … … 689 692 } 690 693 691 DEBUGF(DIR, ("Searching for %s...", name)); 694 if (DEBUG(DIR)) { 695 printf("Searching for %s...", name); 696 } 692 697 /* 693 698 * No matter what, we always look for the file in the current directory … … 698 703 if ((!hasSlash || (cp - name == 2 && *name == '.')) && 699 704 (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL)) { 700 DEBUGF(DIR, ("in '.'\n")); 705 if (DEBUG(DIR)) { 706 printf("in '.'\n"); 707 } 701 708 hits += 1; 702 709 dot->hits += 1; … … 705 712 706 713 if (Lst_Open (path) == FAILURE) { 707 DEBUGF(DIR, ("couldn't open path, file not found\n")); 714 if (DEBUG(DIR)) { 715 printf("couldn't open path, file not found\n"); 716 } 708 717 misses += 1; 709 718 return ((char *) NULL); … … 718 727 * we go on to phase two... 719 728 */ 720 while ((ln = Lst_Next (path)) != N ULL) {729 while ((ln = Lst_Next (path)) != NILLNODE) { 721 730 p = (Path *) Lst_Datum (ln); 722 DEBUGF(DIR, ("%s...", p->name)); 731 if (DEBUG(DIR)) { 732 printf("%s...", p->name); 733 } 723 734 if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) { 724 DEBUGF(DIR, ("here...")); 735 if (DEBUG(DIR)) { 736 printf("here..."); 737 } 725 738 if (hasSlash) { 726 739 /* … … 738 751 } 739 752 if (p2 >= name || (p1 >= p->name && *p1 != '/')) { 740 DEBUGF(DIR, ("component mismatch -- continuing...")); 753 if (DEBUG(DIR)) { 754 printf("component mismatch -- continuing..."); 755 } 741 756 continue; 742 757 } 743 758 } 744 759 file = str_concat (p->name, cp, STR_ADDSLASH); 745 DEBUGF(DIR, ("returning %s\n", file)); 760 if (DEBUG(DIR)) { 761 printf("returning %s\n", file); 762 } 746 763 Lst_Close (path); 747 764 p->hits += 1; … … 758 775 } 759 776 if (*p1 == '\0' && p2 == cp - 1) { 760 DEBUGF(DIR, ("must be here but isn't -- returing NULL\n")); 777 if (DEBUG(DIR)) { 778 printf("must be here but isn't -- returing NULL\n"); 779 } 761 780 Lst_Close (path); 762 781 return ((char *) NULL); … … 778 797 */ 779 798 if (!hasSlash) { 780 DEBUGF(DIR, ("failed.\n")); 799 if (DEBUG(DIR)) { 800 printf("failed.\n"); 801 } 781 802 misses += 1; 782 803 return ((char *) NULL); … … 786 807 Boolean checkedDot = FALSE; 787 808 788 DEBUGF(DIR, ("failed. Trying subdirectories...")); 809 if (DEBUG(DIR)) { 810 printf("failed. Trying subdirectories..."); 811 } 789 812 (void) Lst_Open (path); 790 while ((ln = Lst_Next (path)) != N ULL) {813 while ((ln = Lst_Next (path)) != NILLNODE) { 791 814 p = (Path *) Lst_Datum (ln); 792 815 if (p != dot) { … … 799 822 checkedDot = TRUE; 800 823 } 801 DEBUGF(DIR, ("checking %s...", file)); 824 if (DEBUG(DIR)) { 825 printf("checking %s...", file); 826 } 827 802 828 803 829 if (stat (file, &stb) == 0) { 804 DEBUGF(DIR, ("got it.\n")); 830 if (DEBUG(DIR)) { 831 printf("got it.\n"); 832 } 805 833 806 834 Lst_Close (path); … … 825 853 * to fetch it again. 826 854 */ 827 DEBUGF(DIR, ("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), file)); 855 if (DEBUG(DIR)) { 856 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), 857 file); 858 } 828 859 entry = Hash_CreateEntry(&mtimes, (char *) file, 829 860 (Boolean *)NULL); … … 836 867 } 837 868 838 DEBUGF(DIR, ("failed. ")); 869 if (DEBUG(DIR)) { 870 printf("failed. "); 871 } 839 872 Lst_Close (path); 840 873 … … 844 877 * so no point in proceeding... 845 878 */ 846 DEBUGF(DIR, ("Checked . already, returning NULL\n")); 879 if (DEBUG(DIR)) { 880 printf("Checked . already, returning NULL\n"); 881 } 847 882 return(NULL); 848 883 } … … 873 908 bigmisses += 1; 874 909 ln = Lst_Last (path); 875 if (ln == N ULL) {910 if (ln == NILLNODE) { 876 911 return ((char *) NULL); 877 912 } else { … … 885 920 } 886 921 #else /* !notdef */ 887 DEBUGF(DIR, ("Looking for \"%s\"...", name)); 922 if (DEBUG(DIR)) { 923 printf("Looking for \"%s\"...", name); 924 } 888 925 889 926 bigmisses += 1; 890 927 entry = Hash_FindEntry(&mtimes, name); 891 928 if (entry != (Hash_Entry *)NULL) { 892 DEBUGF(DIR, ("got it (in mtime cache)\n")); 893 return (estrdup(name)); 929 if (DEBUG(DIR)) { 930 printf("got it (in mtime cache)\n"); 931 } 932 return(estrdup(name)); 894 933 } else if (stat (name, &stb) == 0) { 895 934 entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL); 896 DEBUGF(DIR, ("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), name)); 935 if (DEBUG(DIR)) { 936 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), 937 name); 938 } 897 939 Hash_SetValue(entry, (long)stb.st_mtime); 898 940 return (estrdup (name)); 899 941 } else { 900 DEBUGF(DIR, ("failed. Returning NULL\n")); 942 if (DEBUG(DIR)) { 943 printf("failed. Returning NULL\n"); 944 } 901 945 return ((char *)NULL); 902 946 } … … 920 964 */ 921 965 int 922 Dir_MTime (GNode *gn) 966 Dir_MTime (gn) 967 GNode *gn; /* the file whose modification time is 968 * desired */ 923 969 { 924 970 char *fullName; /* the full pathname of name */ … … 943 989 * Only do this once -- the second time folks are checking to 944 990 * see if the file was actually updated, so we need to actually go 945 * to the file system.991 * to the file system. 946 992 */ 947 DEBUGF(DIR, ("Using cached time %s for %s\n", 948 Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName)); 993 if (DEBUG(DIR)) { 994 printf("Using cached time %s for %s\n", 995 Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName); 996 } 949 997 stb.st_mtime = (time_t)(long)Hash_GetValue(entry); 950 998 Hash_DeleteEntry(&mtimes, entry); … … 982 1030 */ 983 1031 void 984 Dir_AddDir (Lst path, char *name) 1032 Dir_AddDir (path, name) 1033 Lst path; /* the path to which the directory should be 1034 * added */ 1035 char *name; /* the name of the directory to add */ 985 1036 { 986 1037 LstNode ln; /* node in case Path structure is found */ 987 Path*p; /* pointer to new Path structure */1038 register Path *p; /* pointer to new Path structure */ 988 1039 DIR *d; /* for reading directory */ 989 struct dirent *dp;/* entry in directory */990 991 ln = Lst_Find (openDirectories, ( void *)name, DirFindName);992 if (ln != N ULL) {1040 register struct dirent *dp; /* entry in directory */ 1041 1042 ln = Lst_Find (openDirectories, (ClientData)name, DirFindName); 1043 if (ln != NILLNODE) { 993 1044 p = (Path *)Lst_Datum (ln); 994 if (Lst_Member(path, ( void *)p) == NULL) {1045 if (Lst_Member(path, (ClientData)p) == NILLNODE) { 995 1046 p->refCount += 1; 996 (void)Lst_AtEnd (path, ( void *)p);1047 (void)Lst_AtEnd (path, (ClientData)p); 997 1048 } 998 1049 } else { 999 DEBUGF(DIR, ("Caching %s...", name)); 1050 if (DEBUG(DIR)) { 1051 printf("Caching %s...", name); 1052 fflush(stdout); 1053 } 1000 1054 1001 1055 if ((d = opendir (name)) != (DIR *) NULL) { … … 1007 1061 1008 1062 while ((dp = readdir (d)) != (struct dirent *) NULL) { 1009 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define 1063 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */ 1010 1064 /* 1011 1065 * The sun directory library doesn't check for a 0 inode … … 1022 1076 * that order when first going through a directory. This is 1023 1077 * needed for XFS over NFS filesystems since SGI does not 1024 * guarantee that these are the first two entries returned1078 * guarantee that these are * the first two entries returned 1025 1079 * from readdir(). 1026 1080 */ … … 1031 1085 } 1032 1086 (void) closedir (d); 1033 (void)Lst_AtEnd (openDirectories, (void *)p); 1034 (void)Lst_AtEnd (path, (void *)p); 1035 } 1036 DEBUGF(DIR, ("done\n")); 1087 (void)Lst_AtEnd (openDirectories, (ClientData)p); 1088 (void)Lst_AtEnd (path, (ClientData)p); 1089 } 1090 if (DEBUG(DIR)) { 1091 printf("done\n"); 1092 } 1037 1093 } 1038 1094 } … … 1052 1108 *----------------------------------------------------------------------- 1053 1109 */ 1054 void * 1055 Dir_CopyDir(void *p) 1110 ClientData 1111 Dir_CopyDir(p) 1112 ClientData p; 1056 1113 { 1057 1114 ((Path *) p)->refCount += 1; 1058 1115 1059 return (( void *)p);1116 return ((ClientData)p); 1060 1117 } 1061 1118 … … 1078 1135 */ 1079 1136 char * 1080 Dir_MakeFlags (char *flag, Lst path) 1137 Dir_MakeFlags (flag, path) 1138 char *flag; /* flag which should precede each directory */ 1139 Lst path; /* list of directories */ 1081 1140 { 1082 1141 char *str; /* the string which will be returned */ … … 1088 1147 1089 1148 if (Lst_Open (path) == SUCCESS) { 1090 while ((ln = Lst_Next (path)) != N ULL) {1149 while ((ln = Lst_Next (path)) != NILLNODE) { 1091 1150 p = (Path *) Lst_Datum (ln); 1092 1151 tstr = str_concat (flag, p->name, 0); … … 1115 1174 */ 1116 1175 void 1117 Dir_Destroy (void *pp) 1176 Dir_Destroy (pp) 1177 ClientData pp; /* The directory descriptor to nuke */ 1118 1178 { 1119 1179 Path *p = (Path *) pp; … … 1123 1183 LstNode ln; 1124 1184 1125 ln = Lst_Member (openDirectories, ( void *)p);1185 ln = Lst_Member (openDirectories, (ClientData)p); 1126 1186 (void) Lst_Remove (openDirectories, ln); 1127 1187 1128 1188 Hash_DeleteTable (&p->files); 1129 free( p->name);1130 free( p);1189 free((Address)p->name); 1190 free((Address)p); 1131 1191 } 1132 1192 } … … 1147 1207 */ 1148 1208 void 1149 Dir_ClearPath(Lst path) 1209 Dir_ClearPath(path) 1210 Lst path; /* Path to clear */ 1150 1211 { 1151 1212 Path *p; 1152 1213 while (!Lst_IsEmpty(path)) { 1153 1214 p = (Path *)Lst_DeQueue(path); 1154 Dir_Destroy(( void *) p);1215 Dir_Destroy((ClientData) p); 1155 1216 } 1156 1217 } … … 1172 1233 */ 1173 1234 void 1174 Dir_Concat(Lst path1, Lst path2) 1235 Dir_Concat(path1, path2) 1236 Lst path1; /* Dest */ 1237 Lst path2; /* Source */ 1175 1238 { 1176 1239 LstNode ln; 1177 1240 Path *p; 1178 1241 1179 for (ln = Lst_First(path2); ln != N ULL; ln = Lst_Succ(ln)) {1242 for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) { 1180 1243 p = (Path *)Lst_Datum(ln); 1181 if (Lst_Member(path1, ( void *)p) == NULL) {1244 if (Lst_Member(path1, (ClientData)p) == NILLNODE) { 1182 1245 p->refCount += 1; 1183 (void)Lst_AtEnd(path1, ( void *)p);1246 (void)Lst_AtEnd(path1, (ClientData)p); 1184 1247 } 1185 1248 } … … 1188 1251 /********** DEBUG INFO **********/ 1189 1252 void 1190 Dir_PrintDirectories( void)1253 Dir_PrintDirectories() 1191 1254 { 1192 1255 LstNode ln; … … 1200 1263 printf ("# %-20s referenced\thits\n", "directory"); 1201 1264 if (Lst_Open (openDirectories) == SUCCESS) { 1202 while ((ln = Lst_Next (openDirectories)) != N ULL) {1265 while ((ln = Lst_Next (openDirectories)) != NILLNODE) { 1203 1266 p = (Path *) Lst_Datum (ln); 1204 1267 printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits); … … 1208 1271 } 1209 1272 1210 static int 1211 DirPrintDir (void *p, void *dummy __unused) 1273 static int DirPrintDir (p, dummy) 1274 ClientData p; 1275 ClientData dummy; 1212 1276 { 1213 1277 printf ("%s ", ((Path *) p)->name); 1214 1215 return (0); 1278 return (dummy ? 0 : 0); 1216 1279 } 1217 1280 1218 1281 void 1219 Dir_PrintPath (Lst path) 1220 { 1221 Lst_ForEach (path, DirPrintDir, (void *)0); 1222 } 1282 Dir_PrintPath (path) 1283 Lst path; 1284 { 1285 Lst_ForEach (path, DirPrintDir, (ClientData)0); 1286 }
Note:
See TracChangeset
for help on using the changeset viewer.