oswindir.c revision 1.1.1.2.4.2 1
2 /******************************************************************************
3 *
4 * Module Name: oswindir - Windows directory access interfaces
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
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 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45 #include <acpi.h>
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <io.h>
51
52 typedef struct ExternalFindInfo
53 {
54 struct _finddata_t DosInfo;
55 char *FullWildcardSpec;
56 long FindHandle;
57 char State;
58 char RequestedFileType;
59
60 } EXTERNAL_FIND_INFO;
61
62
63 /*******************************************************************************
64 *
65 * FUNCTION: AcpiOsOpenDirectory
66 *
67 * PARAMETERS: DirPathname - Full pathname to the directory
68 * WildcardSpec - string of the form "*.c", etc.
69 * RequestedFileType - Either a directory or normal file
70 *
71 * RETURN: A directory "handle" to be used in subsequent search operations.
72 * NULL returned on failure.
73 *
74 * DESCRIPTION: Open a directory in preparation for a wildcard search
75 *
76 ******************************************************************************/
77
78 void *
79 AcpiOsOpenDirectory (
80 char *DirPathname,
81 char *WildcardSpec,
82 char RequestedFileType)
83 {
84 long FindHandle;
85 char *FullWildcardSpec;
86 EXTERNAL_FIND_INFO *SearchInfo;
87
88
89 /* No directory path means "use current directory" - use a dot */
90
91 if (!DirPathname || strlen (DirPathname) == 0)
92 {
93 DirPathname = ".";
94 }
95
96 /* Allocate the info struct that will be returned to the caller */
97
98 SearchInfo = calloc (sizeof (EXTERNAL_FIND_INFO), 1);
99 if (!SearchInfo)
100 {
101 return (NULL);
102 }
103
104 /* Allocate space for the full wildcard path */
105
106 FullWildcardSpec = calloc (strlen (DirPathname) + strlen (WildcardSpec) + 2, 1);
107 if (!FullWildcardSpec)
108 {
109 printf ("Could not allocate buffer for wildcard pathname\n");
110 return (NULL);
111 }
112
113 /* Create the full wildcard path */
114
115 strcpy (FullWildcardSpec, DirPathname);
116 strcat (FullWildcardSpec, "/");
117 strcat (FullWildcardSpec, WildcardSpec);
118
119 /* Initialize the find functions, get first match */
120
121 FindHandle = _findfirst (FullWildcardSpec, &SearchInfo->DosInfo);
122 if (FindHandle == -1)
123 {
124 /* Failure means that no match was found */
125
126 free (FullWildcardSpec);
127 free (SearchInfo);
128 return (NULL);
129 }
130
131 /* Save the info in the return structure */
132
133 SearchInfo->RequestedFileType = RequestedFileType;
134 SearchInfo->FullWildcardSpec = FullWildcardSpec;
135 SearchInfo->FindHandle = FindHandle;
136 SearchInfo->State = 0;
137 return (SearchInfo);
138 }
139
140
141 /*******************************************************************************
142 *
143 * FUNCTION: AcpiOsGetNextFilename
144 *
145 * PARAMETERS: DirHandle - Created via AcpiOsOpenDirectory
146 *
147 * RETURN: Next filename matched. NULL if no more matches.
148 *
149 * DESCRIPTION: Get the next file in the directory that matches the wildcard
150 * specification.
151 *
152 ******************************************************************************/
153
154 char *
155 AcpiOsGetNextFilename (
156 void *DirHandle)
157 {
158 EXTERNAL_FIND_INFO *SearchInfo = DirHandle;
159 int Status;
160 char FileTypeNotMatched = 1;
161
162
163 /*
164 * Loop while we have matched files but not found any files of
165 * the requested type.
166 */
167 while (FileTypeNotMatched)
168 {
169 /* On the first call, we already have the first match */
170
171 if (SearchInfo->State == 0)
172 {
173 /* No longer the first match */
174
175 SearchInfo->State = 1;
176 }
177 else
178 {
179 /* Get the next match */
180
181 Status = _findnext (SearchInfo->FindHandle, &SearchInfo->DosInfo);
182 if (Status != 0)
183 {
184 return (NULL);
185 }
186 }
187
188 /*
189 * Found a match, now check to make sure that the file type
190 * matches the requested file type (directory or normal file)
191 *
192 * NOTE: use of the attrib field saves us from doing a very
193 * expensive stat() on the file!
194 */
195 switch (SearchInfo->RequestedFileType)
196 {
197 case REQUEST_FILE_ONLY:
198
199 /* Anything other than A_SUBDIR is OK */
200
201 if (!(SearchInfo->DosInfo.attrib & _A_SUBDIR))
202 {
203 FileTypeNotMatched = 0;
204 }
205 break;
206
207 case REQUEST_DIR_ONLY:
208
209 /* Must have A_SUBDIR bit set */
210
211 if (SearchInfo->DosInfo.attrib & _A_SUBDIR)
212 {
213 FileTypeNotMatched = 0;
214 }
215 break;
216
217 default:
218 return (NULL);
219 }
220 }
221
222 return (SearchInfo->DosInfo.name);
223 }
224
225
226 /*******************************************************************************
227 *
228 * FUNCTION: AcpiOsCloseDirectory
229 *
230 * PARAMETERS: DirHandle - Created via AcpiOsOpenDirectory
231 *
232 * RETURN: None
233 *
234 * DESCRIPTION: Close the open directory and cleanup.
235 *
236 ******************************************************************************/
237
238 void
239 AcpiOsCloseDirectory (
240 void *DirHandle)
241 {
242 EXTERNAL_FIND_INFO *SearchInfo = DirHandle;
243
244
245 /* Close the directory and free allocations */
246
247 _findclose (SearchInfo->FindHandle);
248 free (SearchInfo->FullWildcardSpec);
249 free (DirHandle);
250 }
251
252