1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: osunixdir - Unix directory access interfaces
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
21 * Allocated structure returned from os_open_directory
23 typedef struct external_find_info
{
26 char temp_buffer
[256];
28 char requested_file_type
;
32 /*******************************************************************************
34 * FUNCTION: acpi_os_open_directory
36 * PARAMETERS: dir_pathname - Full pathname to the directory
37 * wildcard_spec - string of the form "*.c", etc.
39 * RETURN: A directory "handle" to be used in subsequent search operations.
40 * NULL returned on failure.
42 * DESCRIPTION: Open a directory in preparation for a wildcard search
44 ******************************************************************************/
46 void *acpi_os_open_directory(char *dir_pathname
,
47 char *wildcard_spec
, char requested_file_type
)
49 struct external_find_info
*external_info
;
52 /* Allocate the info struct that will be returned to the caller */
54 external_info
= calloc(1, sizeof(struct external_find_info
));
59 /* Get the directory stream */
61 dir
= opendir(dir_pathname
);
63 fprintf(stderr
, "Cannot open directory - %s\n", dir_pathname
);
68 /* Save the info in the return structure */
70 external_info
->wildcard_spec
= wildcard_spec
;
71 external_info
->requested_file_type
= requested_file_type
;
72 external_info
->dir_pathname
= dir_pathname
;
73 external_info
->dir_ptr
= dir
;
74 return (external_info
);
77 /*******************************************************************************
79 * FUNCTION: acpi_os_get_next_filename
81 * PARAMETERS: dir_handle - Created via acpi_os_open_directory
83 * RETURN: Next filename matched. NULL if no more matches.
85 * DESCRIPTION: Get the next file in the directory that matches the wildcard
88 ******************************************************************************/
90 char *acpi_os_get_next_filename(void *dir_handle
)
92 struct external_find_info
*external_info
= dir_handle
;
93 struct dirent
*dir_entry
;
96 struct stat temp_stat
;
99 while ((dir_entry
= readdir(external_info
->dir_ptr
))) {
101 (external_info
->wildcard_spec
, dir_entry
->d_name
, 0)) {
102 if (dir_entry
->d_name
[0] == '.') {
106 str_len
= strlen(dir_entry
->d_name
) +
107 strlen(external_info
->dir_pathname
) + 2;
109 temp_str
= calloc(str_len
, 1);
112 "Could not allocate buffer for temporary string\n");
116 strcpy(temp_str
, external_info
->dir_pathname
);
117 strcat(temp_str
, "/");
118 strcat(temp_str
, dir_entry
->d_name
);
120 err
= stat(temp_str
, &temp_stat
);
123 "Cannot stat file (should not happen) - %s\n",
131 if ((S_ISDIR(temp_stat
.st_mode
)
132 && (external_info
->requested_file_type
==
134 || ((!S_ISDIR(temp_stat
.st_mode
)
135 && external_info
->requested_file_type
==
136 REQUEST_FILE_ONLY
))) {
138 /* copy to a temp buffer because dir_entry struct is on the stack */
140 strcpy(external_info
->temp_buffer
,
142 return (external_info
->temp_buffer
);
150 /*******************************************************************************
152 * FUNCTION: acpi_os_close_directory
154 * PARAMETERS: dir_handle - Created via acpi_os_open_directory
158 * DESCRIPTION: Close the open directory and cleanup.
160 ******************************************************************************/
162 void acpi_os_close_directory(void *dir_handle
)
164 struct external_find_info
*external_info
= dir_handle
;
166 /* Close the directory and free allocations */
168 closedir(external_info
->dir_ptr
);