1 /******************************************************************************
2  *
3  * Module Name: oswindir - Windows directory access interfaces
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include <acpi.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <io.h>
50 
51 typedef struct ExternalFindInfo
52 {
53     struct _finddata_t          DosInfo;
54     char                        *FullWildcardSpec;
55     long                        FindHandle;
56     char                        State;
57     char                        RequestedFileType;
58 
59 } EXTERNAL_FIND_INFO;
60 
61 
62 /*******************************************************************************
63  *
64  * FUNCTION:    AcpiOsOpenDirectory
65  *
66  * PARAMETERS:  DirPathname         - Full pathname to the directory
67  *              WildcardSpec        - string of the form "*.c", etc.
68  *              RequestedFileType   - Either a directory or normal file
69  *
70  * RETURN:      A directory "handle" to be used in subsequent search operations.
71  *              NULL returned on failure.
72  *
73  * DESCRIPTION: Open a directory in preparation for a wildcard search
74  *
75  ******************************************************************************/
76 
77 void *
78 AcpiOsOpenDirectory (
79     char                    *DirPathname,
80     char                    *WildcardSpec,
81     char                    RequestedFileType)
82 {
83     long                    FindHandle;
84     char                    *FullWildcardSpec;
85     EXTERNAL_FIND_INFO      *SearchInfo;
86 
87 
88     /* No directory path means "use current directory" - use a dot */
89 
90     if (!DirPathname || strlen (DirPathname) == 0)
91     {
92         DirPathname = ".";
93     }
94 
95     /* Allocate the info struct that will be returned to the caller */
96 
97     SearchInfo = calloc (sizeof (EXTERNAL_FIND_INFO), 1);
98     if (!SearchInfo)
99     {
100         return (NULL);
101     }
102 
103     /* Allocate space for the full wildcard path */
104 
105     FullWildcardSpec = calloc (strlen (DirPathname) + strlen (WildcardSpec) + 2, 1);
106     if (!FullWildcardSpec)
107     {
108         printf ("Could not allocate buffer for wildcard pathname\n");
109         free (SearchInfo);
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 
219             return (NULL);
220         }
221     }
222 
223     return (SearchInfo->DosInfo.name);
224 }
225 
226 
227 /*******************************************************************************
228  *
229  * FUNCTION:    AcpiOsCloseDirectory
230  *
231  * PARAMETERS:  DirHandle           - Created via AcpiOsOpenDirectory
232  *
233  * RETURN:      None
234  *
235  * DESCRIPTION: Close the open directory and cleanup.
236  *
237  ******************************************************************************/
238 
239 void
240 AcpiOsCloseDirectory (
241     void                    *DirHandle)
242 {
243     EXTERNAL_FIND_INFO      *SearchInfo = DirHandle;
244 
245 
246     /* Close the directory and free allocations */
247 
248     _findclose (SearchInfo->FindHandle);
249     free (SearchInfo->FullWildcardSpec);
250     free (DirHandle);
251 }
252