WIP FPC-III support
[linux/fpc-iii.git] / tools / power / acpi / os_specific / service_layers / osunixdir.c
blobfd05ddee240f9ad9326ba3a2fcc53ca2b4f3480c
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>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <dirent.h>
16 #include <fnmatch.h>
17 #include <ctype.h>
18 #include <sys/stat.h>
21 * Allocated structure returned from os_open_directory
23 typedef struct external_find_info {
24 char *dir_pathname;
25 DIR *dir_ptr;
26 char temp_buffer[256];
27 char *wildcard_spec;
28 char requested_file_type;
30 } external_find_info;
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;
50 DIR *dir;
52 /* Allocate the info struct that will be returned to the caller */
54 external_info = calloc(1, sizeof(struct external_find_info));
55 if (!external_info) {
56 return (NULL);
59 /* Get the directory stream */
61 dir = opendir(dir_pathname);
62 if (!dir) {
63 fprintf(stderr, "Cannot open directory - %s\n", dir_pathname);
64 free(external_info);
65 return (NULL);
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
86 * specification.
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;
94 char *temp_str;
95 int str_len;
96 struct stat temp_stat;
97 int err;
99 while ((dir_entry = readdir(external_info->dir_ptr))) {
100 if (!fnmatch
101 (external_info->wildcard_spec, dir_entry->d_name, 0)) {
102 if (dir_entry->d_name[0] == '.') {
103 continue;
106 str_len = strlen(dir_entry->d_name) +
107 strlen(external_info->dir_pathname) + 2;
109 temp_str = calloc(str_len, 1);
110 if (!temp_str) {
111 fprintf(stderr,
112 "Could not allocate buffer for temporary string\n");
113 return (NULL);
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);
121 if (err == -1) {
122 fprintf(stderr,
123 "Cannot stat file (should not happen) - %s\n",
124 temp_str);
125 free(temp_str);
126 return (NULL);
129 free(temp_str);
131 if ((S_ISDIR(temp_stat.st_mode)
132 && (external_info->requested_file_type ==
133 REQUEST_DIR_ONLY))
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,
141 dir_entry->d_name);
142 return (external_info->temp_buffer);
147 return (NULL);
150 /*******************************************************************************
152 * FUNCTION: acpi_os_close_directory
154 * PARAMETERS: dir_handle - Created via acpi_os_open_directory
156 * RETURN: None.
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);
169 free(dir_handle);