Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: osunixdir - Unix directory access interfaces
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #include <acpi/acpi.h>
0011 
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <string.h>
0015 #include <dirent.h>
0016 #include <fnmatch.h>
0017 #include <ctype.h>
0018 #include <sys/stat.h>
0019 
0020 /*
0021  * Allocated structure returned from os_open_directory
0022  */
0023 typedef struct external_find_info {
0024     char *dir_pathname;
0025     DIR *dir_ptr;
0026     char temp_buffer[256];
0027     char *wildcard_spec;
0028     char requested_file_type;
0029 
0030 } external_find_info;
0031 
0032 /*******************************************************************************
0033  *
0034  * FUNCTION:    acpi_os_open_directory
0035  *
0036  * PARAMETERS:  dir_pathname        - Full pathname to the directory
0037  *              wildcard_spec       - string of the form "*.c", etc.
0038  *
0039  * RETURN:      A directory "handle" to be used in subsequent search operations.
0040  *              NULL returned on failure.
0041  *
0042  * DESCRIPTION: Open a directory in preparation for a wildcard search
0043  *
0044  ******************************************************************************/
0045 
0046 void *acpi_os_open_directory(char *dir_pathname,
0047                  char *wildcard_spec, char requested_file_type)
0048 {
0049     struct external_find_info *external_info;
0050     DIR *dir;
0051 
0052     /* Allocate the info struct that will be returned to the caller */
0053 
0054     external_info = calloc(1, sizeof(struct external_find_info));
0055     if (!external_info) {
0056         return (NULL);
0057     }
0058 
0059     /* Get the directory stream */
0060 
0061     dir = opendir(dir_pathname);
0062     if (!dir) {
0063         fprintf(stderr, "Cannot open directory - %s\n", dir_pathname);
0064         free(external_info);
0065         return (NULL);
0066     }
0067 
0068     /* Save the info in the return structure */
0069 
0070     external_info->wildcard_spec = wildcard_spec;
0071     external_info->requested_file_type = requested_file_type;
0072     external_info->dir_pathname = dir_pathname;
0073     external_info->dir_ptr = dir;
0074     return (external_info);
0075 }
0076 
0077 /*******************************************************************************
0078  *
0079  * FUNCTION:    acpi_os_get_next_filename
0080  *
0081  * PARAMETERS:  dir_handle          - Created via acpi_os_open_directory
0082  *
0083  * RETURN:      Next filename matched. NULL if no more matches.
0084  *
0085  * DESCRIPTION: Get the next file in the directory that matches the wildcard
0086  *              specification.
0087  *
0088  ******************************************************************************/
0089 
0090 char *acpi_os_get_next_filename(void *dir_handle)
0091 {
0092     struct external_find_info *external_info = dir_handle;
0093     struct dirent *dir_entry;
0094     char *temp_str;
0095     int str_len;
0096     struct stat temp_stat;
0097     int err;
0098 
0099     while ((dir_entry = readdir(external_info->dir_ptr))) {
0100         if (!fnmatch
0101             (external_info->wildcard_spec, dir_entry->d_name, 0)) {
0102             if (dir_entry->d_name[0] == '.') {
0103                 continue;
0104             }
0105 
0106             str_len = strlen(dir_entry->d_name) +
0107                 strlen(external_info->dir_pathname) + 2;
0108 
0109             temp_str = calloc(str_len, 1);
0110             if (!temp_str) {
0111                 fprintf(stderr,
0112                     "Could not allocate buffer for temporary string\n");
0113                 return (NULL);
0114             }
0115 
0116             strcpy(temp_str, external_info->dir_pathname);
0117             strcat(temp_str, "/");
0118             strcat(temp_str, dir_entry->d_name);
0119 
0120             err = stat(temp_str, &temp_stat);
0121             if (err == -1) {
0122                 fprintf(stderr,
0123                     "Cannot stat file (should not happen) - %s\n",
0124                     temp_str);
0125                 free(temp_str);
0126                 return (NULL);
0127             }
0128 
0129             free(temp_str);
0130 
0131             if ((S_ISDIR(temp_stat.st_mode)
0132                  && (external_info->requested_file_type ==
0133                  REQUEST_DIR_ONLY))
0134                 || ((!S_ISDIR(temp_stat.st_mode)
0135                  && external_info->requested_file_type ==
0136                  REQUEST_FILE_ONLY))) {
0137 
0138                 /* copy to a temp buffer because dir_entry struct is on the stack */
0139 
0140                 strcpy(external_info->temp_buffer,
0141                        dir_entry->d_name);
0142                 return (external_info->temp_buffer);
0143             }
0144         }
0145     }
0146 
0147     return (NULL);
0148 }
0149 
0150 /*******************************************************************************
0151  *
0152  * FUNCTION:    acpi_os_close_directory
0153  *
0154  * PARAMETERS:  dir_handle          - Created via acpi_os_open_directory
0155  *
0156  * RETURN:      None.
0157  *
0158  * DESCRIPTION: Close the open directory and cleanup.
0159  *
0160  ******************************************************************************/
0161 
0162 void acpi_os_close_directory(void *dir_handle)
0163 {
0164     struct external_find_info *external_info = dir_handle;
0165 
0166     /* Close the directory and free allocations */
0167 
0168     closedir(external_info->dir_ptr);
0169     free(dir_handle);
0170 }