Home | History | Annotate | Line # | Download | only in service_layers
osunixdir.c revision 1.1.1.2.8.2
      1 
      2 /******************************************************************************
      3  *
      4  * Module Name: osunixdir - Unix directory access interfaces
      5  *
      6  *****************************************************************************/
      7 
      8 /*
      9  * Copyright (C) 2000 - 2011, Intel Corp.
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions, and the following disclaimer,
     17  *    without modification.
     18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     19  *    substantially similar to the "NO WARRANTY" disclaimer below
     20  *    ("Disclaimer") and any redistribution must be conditioned upon
     21  *    including a substantially similar Disclaimer requirement for further
     22  *    binary redistribution.
     23  * 3. Neither the names of the above-listed copyright holders nor the names
     24  *    of any contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * Alternatively, this software may be distributed under the terms of the
     28  * GNU General Public License ("GPL") version 2 as published by the Free
     29  * Software Foundation.
     30  *
     31  * NO WARRANTY
     32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     42  * POSSIBILITY OF SUCH DAMAGES.
     43  */
     44 
     45 
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <dirent.h>
     50 #include <fnmatch.h>
     51 #include <ctype.h>
     52 #include <sys/stat.h>
     53 
     54 #include "acpisrc.h"
     55 
     56 /*
     57  * Allocated structure returned from OsOpenDirectory
     58  */
     59 typedef struct ExternalFindInfo
     60 {
     61     char                        *DirPathname;
     62     DIR                         *DirPtr;
     63     char                        temp_buffer[128];
     64     char                        *WildcardSpec;
     65     char                        RequestedFileType;
     66 
     67 } EXTERNAL_FIND_INFO;
     68 
     69 
     70 /*******************************************************************************
     71  *
     72  * FUNCTION:    AcpiOsOpenDirectory
     73  *
     74  * PARAMETERS:  DirPathname         - Full pathname to the directory
     75  *              WildcardSpec        - string of the form "*.c", etc.
     76  *
     77  * RETURN:      A directory "handle" to be used in subsequent search operations.
     78  *              NULL returned on failure.
     79  *
     80  * DESCRIPTION: Open a directory in preparation for a wildcard search
     81  *
     82  ******************************************************************************/
     83 
     84 void *
     85 AcpiOsOpenDirectory (
     86     char                    *DirPathname,
     87     char                    *WildcardSpec,
     88     char                    RequestedFileType)
     89 {
     90     EXTERNAL_FIND_INFO      *ExternalInfo;
     91     DIR                     *dir;
     92 
     93 
     94     /* Allocate the info struct that will be returned to the caller */
     95 
     96     ExternalInfo = calloc (sizeof (EXTERNAL_FIND_INFO), 1);
     97     if (!ExternalInfo)
     98     {
     99         return (NULL);
    100     }
    101 
    102     /* Get the directory stream */
    103 
    104     dir = opendir (DirPathname);
    105     if (!dir)
    106     {
    107         free (ExternalInfo);
    108         return (NULL);
    109     }
    110 
    111     /* Save the info in the return structure */
    112 
    113     ExternalInfo->WildcardSpec = WildcardSpec;
    114     ExternalInfo->RequestedFileType = RequestedFileType;
    115     ExternalInfo->DirPathname = DirPathname;
    116     ExternalInfo->DirPtr = dir;
    117     return (ExternalInfo);
    118 }
    119 
    120 
    121 /*******************************************************************************
    122  *
    123  * FUNCTION:    AcpiOsGetNextFilename
    124  *
    125  * PARAMETERS:  DirHandle           - Created via AcpiOsOpenDirectory
    126  *
    127  * RETURN:      Next filename matched. NULL if no more matches.
    128  *
    129  * DESCRIPTION: Get the next file in the directory that matches the wildcard
    130  *              specification.
    131  *
    132  ******************************************************************************/
    133 
    134 char *
    135 AcpiOsGetNextFilename (
    136     void                    *DirHandle)
    137 {
    138     EXTERNAL_FIND_INFO      *ExternalInfo = DirHandle;
    139     struct dirent           *dir_entry;
    140     char                    *temp_str;
    141     int                     str_len;
    142     struct stat             temp_stat;
    143     int                     err;
    144 
    145 
    146     while ((dir_entry = readdir (ExternalInfo->DirPtr)))
    147     {
    148         if (!fnmatch (ExternalInfo->WildcardSpec, dir_entry->d_name, 0))
    149         {
    150             if (dir_entry->d_name[0] == '.')
    151             {
    152                 continue;
    153             }
    154 
    155             str_len = strlen (dir_entry->d_name) +
    156                         strlen (ExternalInfo->DirPathname) + 2;
    157 
    158             temp_str = calloc (str_len, 1);
    159             if (!temp_str)
    160             {
    161                 printf ("Could not allocate buffer for temporary string\n");
    162                 return (NULL);
    163             }
    164 
    165             strcpy (temp_str, ExternalInfo->DirPathname);
    166             strcat (temp_str, "/");
    167             strcat (temp_str, dir_entry->d_name);
    168 
    169             err = stat (temp_str, &temp_stat);
    170             free (temp_str);
    171             if (err == -1)
    172             {
    173                 printf ("stat() error - should not happen\n");
    174                 return (NULL);
    175             }
    176 
    177             if ((S_ISDIR (temp_stat.st_mode)
    178                 && (ExternalInfo->RequestedFileType == REQUEST_DIR_ONLY))
    179                ||
    180                ((!S_ISDIR (temp_stat.st_mode)
    181                 && ExternalInfo->RequestedFileType == REQUEST_FILE_ONLY)))
    182             {
    183                 /* copy to a temp buffer because dir_entry struct is on the stack */
    184 
    185                 strcpy (ExternalInfo->temp_buffer, dir_entry->d_name);
    186                 return (ExternalInfo->temp_buffer);
    187             }
    188         }
    189     }
    190 
    191     return (NULL);
    192 }
    193 
    194 
    195 /*******************************************************************************
    196  *
    197  * FUNCTION:    AcpiOsCloseDirectory
    198  *
    199  * PARAMETERS:  DirHandle           - Created via AcpiOsOpenDirectory
    200  *
    201  * RETURN:      None.
    202  *
    203  * DESCRIPTION: Close the open directory and cleanup.
    204  *
    205  ******************************************************************************/
    206 
    207 void
    208 AcpiOsCloseDirectory (
    209     void                    *DirHandle)
    210 {
    211     EXTERNAL_FIND_INFO      *ExternalInfo = DirHandle;
    212 
    213 
    214     /* Close the directory and free allocations */
    215 
    216     closedir (ExternalInfo->DirPtr);
    217     free (DirHandle);
    218 }
    219 
    220 
    221 /* Other functions acpisrc uses but that aren't standard on Unix */
    222 
    223 /*******************************************************************************
    224  *
    225  * FUNCTION:    strlwr
    226  *
    227  * PARAMETERS:  str                 - String to be lowercased.
    228  *
    229  * RETURN:      str.
    230  *
    231  * DESCRIPTION: Lowercase a string in-place.
    232  *
    233  ******************************************************************************/
    234 
    235 char *
    236 strlwr  (
    237    char         *str)
    238 {
    239     int         length;
    240     int         i;
    241 
    242 
    243     length = strlen (str);
    244 
    245     for (i = 0; i < length; i++)
    246     {
    247         str[i] = tolower ((int) str[i]);
    248     }
    249 
    250     return (str);
    251 }
    252