]>
Commit | Line | Data |
---|---|---|
3e99020d LG |
1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
4 | This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | FindFiles.c \r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | OS-specific functions to assist in finding files in\r | |
19 | subdirectories.\r | |
20 | \r | |
21 | --*/\r | |
22 | \r | |
23 | #include <windows.h>\r | |
24 | #include <direct.h>\r | |
25 | //\r | |
26 | // #include <io.h> // for _chmod()\r | |
27 | //\r | |
28 | #include <sys/stat.h>\r | |
29 | //\r | |
30 | // #include <errno.h> // for errno\r | |
31 | //\r | |
32 | #include <stdio.h>\r | |
33 | #include <string.h>\r | |
34 | #include <stdlib.h>\r | |
35 | #include <ctype.h>\r | |
36 | \r | |
37 | #include "HiiPack.h"\r | |
38 | \r | |
39 | extern\r | |
40 | void\r | |
41 | Error (\r | |
42 | char *Name,\r | |
43 | UINT32 LineNumber,\r | |
44 | UINT32 MessageCode,\r | |
45 | char *Text,\r | |
46 | char *MsgFmt,\r | |
47 | ...\r | |
48 | );\r | |
49 | \r | |
50 | static\r | |
51 | int\r | |
52 | ProcessDirectory (\r | |
53 | char *RootDirectory,\r | |
54 | char *FileMask,\r | |
55 | FIND_FILE_CALLBACK Callback\r | |
56 | );\r | |
57 | \r | |
58 | /*****************************************************************************/\r | |
59 | int\r | |
60 | FindFiles (\r | |
61 | char *RootDirectory,\r | |
62 | char *FileMask,\r | |
63 | FIND_FILE_CALLBACK Callback\r | |
64 | )\r | |
65 | /*++\r | |
66 | \r | |
67 | Routine Description:\r | |
68 | Find files of a given name under a root directory\r | |
69 | \r | |
70 | Arguments:\r | |
71 | RootDirectory - base directory -- look in this directory and\r | |
72 | all its subdirectories for files matching FileMask.\r | |
73 | FileMask - file mask of files to find\r | |
74 | Callback - function to call for each file found \r | |
75 | \r | |
76 | Returns:\r | |
77 | \r | |
78 | --*/\r | |
79 | {\r | |
80 | char FullPath[MAX_PATH];\r | |
81 | //\r | |
82 | // If RootDirectory is relative, then append to cwd.\r | |
83 | //\r | |
84 | if (isalpha (RootDirectory[0]) && (RootDirectory[1] == ':')) {\r | |
85 | strcpy (FullPath, RootDirectory);\r | |
86 | } else {\r | |
87 | //\r | |
88 | // Get current working directory\r | |
89 | //\r | |
90 | if (_getcwd (FullPath, sizeof (FullPath)) == NULL) {\r | |
91 | Error (NULL, 0, 0, "failed to get current working directory", NULL);\r | |
92 | return 1;\r | |
93 | }\r | |
94 | //\r | |
95 | // Append the relative path they passed in\r | |
96 | //\r | |
97 | if (FullPath[strlen (FullPath) - 1] != '\\') {\r | |
98 | strcat (FullPath, "\\");\r | |
99 | }\r | |
100 | \r | |
101 | strcat (FullPath, RootDirectory);\r | |
102 | }\r | |
103 | \r | |
104 | if (FullPath[strlen (FullPath) - 1] == '\\') {\r | |
105 | FullPath[strlen (FullPath) - 1] = 0;\r | |
106 | }\r | |
107 | //\r | |
108 | // Process the directory\r | |
109 | //\r | |
110 | return ProcessDirectory (FullPath, FileMask, Callback);\r | |
111 | }\r | |
112 | \r | |
113 | static\r | |
114 | int\r | |
115 | ProcessDirectory (\r | |
116 | char *RootDirectory,\r | |
117 | char *FileMask,\r | |
118 | FIND_FILE_CALLBACK Callback\r | |
119 | )\r | |
120 | /*++\r | |
121 | \r | |
122 | Routine Description:\r | |
123 | Process a directory to find all files matching a given file mask\r | |
124 | \r | |
125 | Arguments:\r | |
126 | RootDirectory - base directory -- look in this directory and\r | |
127 | all its subdirectories for files matching FileMask.\r | |
128 | FileMask - file mask of files to find\r | |
129 | Callback - function to call for each file found \r | |
130 | \r | |
131 | Returns:\r | |
132 | \r | |
133 | --*/\r | |
134 | {\r | |
135 | HANDLE Handle;\r | |
136 | WIN32_FIND_DATA FindData;\r | |
137 | char TempName[MAX_PATH];\r | |
138 | \r | |
139 | Handle = INVALID_HANDLE_VALUE;\r | |
140 | //\r | |
141 | // Concatenate the filemask to the directory to create the full\r | |
142 | // path\mask path name.\r | |
143 | //\r | |
144 | strcpy (TempName, RootDirectory);\r | |
145 | strcat (TempName, "\\");\r | |
146 | strcat (TempName, FileMask);\r | |
147 | memset (&FindData, 0, sizeof (FindData));\r | |
148 | Handle = FindFirstFile (TempName, &FindData);\r | |
149 | if (Handle != INVALID_HANDLE_VALUE) {\r | |
150 | do {\r | |
151 | if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {\r | |
152 | strcpy (TempName, RootDirectory);\r | |
153 | strcat (TempName, "\\");\r | |
154 | strcat (TempName, FindData.cFileName);\r | |
155 | if (Callback (TempName) != 0) {\r | |
156 | goto Done;\r | |
157 | }\r | |
158 | }\r | |
159 | } while (FindNextFile (Handle, &FindData));\r | |
160 | }\r | |
161 | \r | |
162 | if (Handle != INVALID_HANDLE_VALUE) {\r | |
163 | FindClose (Handle);\r | |
164 | Handle = INVALID_HANDLE_VALUE;\r | |
165 | }\r | |
166 | //\r | |
167 | // Now create a *.* file mask to get all subdirectories and recursive call this\r | |
168 | // function to handle each one found.\r | |
169 | //\r | |
170 | strcpy (TempName, RootDirectory);\r | |
171 | strcat (TempName, "\\*.*");\r | |
172 | memset (&FindData, 0, sizeof (FindData));\r | |
173 | Handle = FindFirstFile (TempName, &FindData);\r | |
174 | //\r | |
175 | // Loop until no more files/directories\r | |
176 | //\r | |
177 | if (Handle != INVALID_HANDLE_VALUE) {\r | |
178 | do {\r | |
179 | if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {\r | |
180 | //\r | |
181 | // Make sure it's not "." or ".."\r | |
182 | //\r | |
183 | if ((strcmp (FindData.cFileName, ".") != 0) && (strcmp (FindData.cFileName, "..") != 0)) {\r | |
184 | //\r | |
185 | // Found a valid directory. Put it all together and make a recursive call\r | |
186 | // to process it.\r | |
187 | //\r | |
188 | strcpy (TempName, RootDirectory);\r | |
189 | strcat (TempName, "\\");\r | |
190 | strcat (TempName, FindData.cFileName);\r | |
191 | if (ProcessDirectory (TempName, FileMask, Callback) != 0) {\r | |
192 | goto Done;\r | |
193 | }\r | |
194 | }\r | |
195 | }\r | |
196 | } while (FindNextFile (Handle, &FindData));\r | |
197 | }\r | |
198 | \r | |
199 | Done:\r | |
200 | //\r | |
201 | // Free the handle\r | |
202 | //\r | |
203 | if (Handle != INVALID_HANDLE_VALUE) {\r | |
204 | FindClose (Handle);\r | |
205 | }\r | |
206 | \r | |
207 | return 0;\r | |
208 | }\r |