Path: blob/master/tools/power/acpi/os_specific/service_layers/osunixdir.c
26292 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.01/******************************************************************************2*3* Module Name: osunixdir - Unix directory access interfaces4*5* Copyright (C) 2000 - 2025, Intel Corp.6*7*****************************************************************************/89#include <acpi/acpi.h>1011#include <stdio.h>12#include <stdlib.h>13#include <string.h>14#include <dirent.h>15#include <fnmatch.h>16#include <ctype.h>17#include <sys/stat.h>1819/*20* Allocated structure returned from os_open_directory21*/22typedef struct external_find_info {23char *dir_pathname;24DIR *dir_ptr;25char temp_buffer[256];26char *wildcard_spec;27char requested_file_type;2829} external_find_info;3031/*******************************************************************************32*33* FUNCTION: acpi_os_open_directory34*35* PARAMETERS: dir_pathname - Full pathname to the directory36* wildcard_spec - string of the form "*.c", etc.37*38* RETURN: A directory "handle" to be used in subsequent search operations.39* NULL returned on failure.40*41* DESCRIPTION: Open a directory in preparation for a wildcard search42*43******************************************************************************/4445void *acpi_os_open_directory(char *dir_pathname,46char *wildcard_spec, char requested_file_type)47{48struct external_find_info *external_info;49DIR *dir;5051/* Allocate the info struct that will be returned to the caller */5253external_info = calloc(1, sizeof(struct external_find_info));54if (!external_info) {55return (NULL);56}5758/* Get the directory stream */5960dir = opendir(dir_pathname);61if (!dir) {62fprintf(stderr, "Cannot open directory - %s\n", dir_pathname);63free(external_info);64return (NULL);65}6667/* Save the info in the return structure */6869external_info->wildcard_spec = wildcard_spec;70external_info->requested_file_type = requested_file_type;71external_info->dir_pathname = dir_pathname;72external_info->dir_ptr = dir;73return (external_info);74}7576/*******************************************************************************77*78* FUNCTION: acpi_os_get_next_filename79*80* PARAMETERS: dir_handle - Created via acpi_os_open_directory81*82* RETURN: Next filename matched. NULL if no more matches.83*84* DESCRIPTION: Get the next file in the directory that matches the wildcard85* specification.86*87******************************************************************************/8889char *acpi_os_get_next_filename(void *dir_handle)90{91struct external_find_info *external_info = dir_handle;92struct dirent *dir_entry;93char *temp_str;94int str_len;95struct stat temp_stat;96int err;9798while ((dir_entry = readdir(external_info->dir_ptr))) {99if (!fnmatch100(external_info->wildcard_spec, dir_entry->d_name, 0)) {101if (dir_entry->d_name[0] == '.') {102continue;103}104105str_len = strlen(dir_entry->d_name) +106strlen(external_info->dir_pathname) + 2;107108temp_str = calloc(str_len, 1);109if (!temp_str) {110fprintf(stderr,111"Could not allocate buffer for temporary string\n");112return (NULL);113}114115strcpy(temp_str, external_info->dir_pathname);116strcat(temp_str, "/");117strcat(temp_str, dir_entry->d_name);118119err = stat(temp_str, &temp_stat);120if (err == -1) {121fprintf(stderr,122"Cannot stat file (should not happen) - %s\n",123temp_str);124free(temp_str);125return (NULL);126}127128free(temp_str);129130if ((S_ISDIR(temp_stat.st_mode)131&& (external_info->requested_file_type ==132REQUEST_DIR_ONLY))133|| ((!S_ISDIR(temp_stat.st_mode)134&& external_info->requested_file_type ==135REQUEST_FILE_ONLY))) {136137/* copy to a temp buffer because dir_entry struct is on the stack */138139strcpy(external_info->temp_buffer,140dir_entry->d_name);141return (external_info->temp_buffer);142}143}144}145146return (NULL);147}148149/*******************************************************************************150*151* FUNCTION: acpi_os_close_directory152*153* PARAMETERS: dir_handle - Created via acpi_os_open_directory154*155* RETURN: None.156*157* DESCRIPTION: Close the open directory and cleanup.158*159******************************************************************************/160161void acpi_os_close_directory(void *dir_handle)162{163struct external_find_info *external_info = dir_handle;164165/* Close the directory and free allocations */166167closedir(external_info->dir_ptr);168free(dir_handle);169}170171172