Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / os_specific / service_layers / oswindir.c
blobdc8916a5be31f8d45a621e37bd314fc0de732736
1 /******************************************************************************
3 * Module Name: oswindir - Windows directory access interfaces
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include <acpi.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <io.h>
51 typedef struct ExternalFindInfo
53 struct _finddata_t DosInfo;
54 char *FullWildcardSpec;
55 long FindHandle;
56 char State;
57 char RequestedFileType;
59 } EXTERNAL_FIND_INFO;
62 /*******************************************************************************
64 * FUNCTION: AcpiOsOpenDirectory
66 * PARAMETERS: DirPathname - Full pathname to the directory
67 * WildcardSpec - string of the form "*.c", etc.
68 * RequestedFileType - Either a directory or normal file
70 * RETURN: A directory "handle" to be used in subsequent search operations.
71 * NULL returned on failure.
73 * DESCRIPTION: Open a directory in preparation for a wildcard search
75 ******************************************************************************/
77 void *
78 AcpiOsOpenDirectory (
79 char *DirPathname,
80 char *WildcardSpec,
81 char RequestedFileType)
83 long FindHandle;
84 char *FullWildcardSpec;
85 EXTERNAL_FIND_INFO *SearchInfo;
88 /* No directory path means "use current directory" - use a dot */
90 if (!DirPathname || strlen (DirPathname) == 0)
92 DirPathname = ".";
95 /* Allocate the info struct that will be returned to the caller */
97 SearchInfo = calloc (sizeof (EXTERNAL_FIND_INFO), 1);
98 if (!SearchInfo)
100 return (NULL);
103 /* Allocate space for the full wildcard path */
105 FullWildcardSpec = calloc (strlen (DirPathname) + strlen (WildcardSpec) + 2, 1);
106 if (!FullWildcardSpec)
108 printf ("Could not allocate buffer for wildcard pathname\n");
109 return (NULL);
112 /* Create the full wildcard path */
114 strcpy (FullWildcardSpec, DirPathname);
115 strcat (FullWildcardSpec, "/");
116 strcat (FullWildcardSpec, WildcardSpec);
118 /* Initialize the find functions, get first match */
120 FindHandle = _findfirst (FullWildcardSpec, &SearchInfo->DosInfo);
121 if (FindHandle == -1)
123 /* Failure means that no match was found */
125 free (FullWildcardSpec);
126 free (SearchInfo);
127 return (NULL);
130 /* Save the info in the return structure */
132 SearchInfo->RequestedFileType = RequestedFileType;
133 SearchInfo->FullWildcardSpec = FullWildcardSpec;
134 SearchInfo->FindHandle = FindHandle;
135 SearchInfo->State = 0;
136 return (SearchInfo);
140 /*******************************************************************************
142 * FUNCTION: AcpiOsGetNextFilename
144 * PARAMETERS: DirHandle - Created via AcpiOsOpenDirectory
146 * RETURN: Next filename matched. NULL if no more matches.
148 * DESCRIPTION: Get the next file in the directory that matches the wildcard
149 * specification.
151 ******************************************************************************/
153 char *
154 AcpiOsGetNextFilename (
155 void *DirHandle)
157 EXTERNAL_FIND_INFO *SearchInfo = DirHandle;
158 int Status;
159 char FileTypeNotMatched = 1;
163 * Loop while we have matched files but not found any files of
164 * the requested type.
166 while (FileTypeNotMatched)
168 /* On the first call, we already have the first match */
170 if (SearchInfo->State == 0)
172 /* No longer the first match */
174 SearchInfo->State = 1;
176 else
178 /* Get the next match */
180 Status = _findnext (SearchInfo->FindHandle, &SearchInfo->DosInfo);
181 if (Status != 0)
183 return (NULL);
188 * Found a match, now check to make sure that the file type
189 * matches the requested file type (directory or normal file)
191 * NOTE: use of the attrib field saves us from doing a very
192 * expensive stat() on the file!
194 switch (SearchInfo->RequestedFileType)
196 case REQUEST_FILE_ONLY:
198 /* Anything other than A_SUBDIR is OK */
200 if (!(SearchInfo->DosInfo.attrib & _A_SUBDIR))
202 FileTypeNotMatched = 0;
204 break;
206 case REQUEST_DIR_ONLY:
208 /* Must have A_SUBDIR bit set */
210 if (SearchInfo->DosInfo.attrib & _A_SUBDIR)
212 FileTypeNotMatched = 0;
214 break;
216 default:
218 return (NULL);
222 return (SearchInfo->DosInfo.name);
226 /*******************************************************************************
228 * FUNCTION: AcpiOsCloseDirectory
230 * PARAMETERS: DirHandle - Created via AcpiOsOpenDirectory
232 * RETURN: None
234 * DESCRIPTION: Close the open directory and cleanup.
236 ******************************************************************************/
238 void
239 AcpiOsCloseDirectory (
240 void *DirHandle)
242 EXTERNAL_FIND_INFO *SearchInfo = DirHandle;
245 /* Close the directory and free allocations */
247 _findclose (SearchInfo->FindHandle);
248 free (SearchInfo->FullWildcardSpec);
249 free (DirHandle);