xref: /titanic_44/usr/src/cmd/acpi/acpidump/osunixdir.c (revision fd435bccec40cb841f712413108d8577422fd9ab)
1 /******************************************************************************
2  *
3  * Module Name: osunixdir - Unix directory access interfaces
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, 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 <dirent.h>
50 #include <fnmatch.h>
51 #include <sys/stat.h>
52 
53 /*
54  * Allocated structure returned from OsOpenDirectory
55  */
56 typedef struct ExternalFindInfo
57 {
58     char                        *DirPathname;
59     DIR                         *DirPtr;
60     char                        temp_buffer[256];
61     char                        *WildcardSpec;
62     char                        RequestedFileType;
63 
64 } EXTERNAL_FIND_INFO;
65 
66 
67 /*******************************************************************************
68  *
69  * FUNCTION:    AcpiOsOpenDirectory
70  *
71  * PARAMETERS:  DirPathname         - Full pathname to the directory
72  *              WildcardSpec        - string of the form "*.c", etc.
73  *
74  * RETURN:      A directory "handle" to be used in subsequent search operations.
75  *              NULL returned on failure.
76  *
77  * DESCRIPTION: Open a directory in preparation for a wildcard search
78  *
79  ******************************************************************************/
80 
81 void *
82 AcpiOsOpenDirectory (
83     char                    *DirPathname,
84     char                    *WildcardSpec,
85     char                    RequestedFileType)
86 {
87     EXTERNAL_FIND_INFO      *ExternalInfo;
88     DIR                     *dir;
89 
90 
91     /* Allocate the info struct that will be returned to the caller */
92 
93     ExternalInfo = calloc (1, sizeof (EXTERNAL_FIND_INFO));
94     if (!ExternalInfo)
95     {
96         return (NULL);
97     }
98 
99     /* Get the directory stream */
100 
101     dir = opendir (DirPathname);
102     if (!dir)
103     {
104         fprintf (stderr, "Cannot open directory - %s\n", DirPathname);
105         free (ExternalInfo);
106         return (NULL);
107     }
108 
109     /* Save the info in the return structure */
110 
111     ExternalInfo->WildcardSpec = WildcardSpec;
112     ExternalInfo->RequestedFileType = RequestedFileType;
113     ExternalInfo->DirPathname = DirPathname;
114     ExternalInfo->DirPtr = dir;
115     return (ExternalInfo);
116 }
117 
118 
119 /*******************************************************************************
120  *
121  * FUNCTION:    AcpiOsGetNextFilename
122  *
123  * PARAMETERS:  DirHandle           - Created via AcpiOsOpenDirectory
124  *
125  * RETURN:      Next filename matched. NULL if no more matches.
126  *
127  * DESCRIPTION: Get the next file in the directory that matches the wildcard
128  *              specification.
129  *
130  ******************************************************************************/
131 
132 char *
133 AcpiOsGetNextFilename (
134     void                    *DirHandle)
135 {
136     EXTERNAL_FIND_INFO      *ExternalInfo = DirHandle;
137     struct dirent           *dir_entry;
138     char                    *temp_str;
139     int                     str_len;
140     struct stat             temp_stat;
141     int                     err;
142 
143 
144     while ((dir_entry = readdir (ExternalInfo->DirPtr)))
145     {
146         if (!fnmatch (ExternalInfo->WildcardSpec, dir_entry->d_name, 0))
147         {
148             if (dir_entry->d_name[0] == '.')
149             {
150                 continue;
151             }
152 
153             str_len = strlen (dir_entry->d_name) +
154                         strlen (ExternalInfo->DirPathname) + 2;
155 
156             temp_str = calloc (str_len, 1);
157             if (!temp_str)
158             {
159                 fprintf (stderr,
160                     "Could not allocate buffer for temporary string\n");
161                 return (NULL);
162             }
163 
164             strcpy (temp_str, ExternalInfo->DirPathname);
165             strcat (temp_str, "/");
166             strcat (temp_str, dir_entry->d_name);
167 
168             err = stat (temp_str, &temp_stat);
169             if (err == -1)
170             {
171                 fprintf (stderr,
172                     "Cannot stat file (should not happen) - %s\n",
173                     temp_str);
174                 free (temp_str);
175                 return (NULL);
176             }
177 
178             free (temp_str);
179 
180             if ((S_ISDIR (temp_stat.st_mode)
181                 && (ExternalInfo->RequestedFileType == REQUEST_DIR_ONLY))
182                ||
183                ((!S_ISDIR (temp_stat.st_mode)
184                 && ExternalInfo->RequestedFileType == REQUEST_FILE_ONLY)))
185             {
186                 /* copy to a temp buffer because dir_entry struct is on the stack */
187 
188                 strcpy (ExternalInfo->temp_buffer, dir_entry->d_name);
189                 return (ExternalInfo->temp_buffer);
190             }
191         }
192     }
193 
194     return (NULL);
195 }
196 
197 
198 /*******************************************************************************
199  *
200  * FUNCTION:    AcpiOsCloseDirectory
201  *
202  * PARAMETERS:  DirHandle           - Created via AcpiOsOpenDirectory
203  *
204  * RETURN:      None.
205  *
206  * DESCRIPTION: Close the open directory and cleanup.
207  *
208  ******************************************************************************/
209 
210 void
211 AcpiOsCloseDirectory (
212     void                    *DirHandle)
213 {
214     EXTERNAL_FIND_INFO      *ExternalInfo = DirHandle;
215 
216 
217     /* Close the directory and free allocations */
218 
219     closedir (ExternalInfo->DirPtr);
220     free (DirHandle);
221 }
222