Home | History | Annotate | Download | only in HiiPack
      1 /*++
      2 
      3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
      4 This program and the accompanying materials
      5 are licensed and made available under the terms and conditions of the BSD License
      6 which accompanies this distribution.  The full text of the license may be found at
      7 http://opensource.org/licenses/bsd-license.php
      8 
      9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     11 
     12 Module Name:
     13 
     14   FindFiles.c
     15 
     16 Abstract:
     17 
     18   OS-specific functions to assist in finding files in
     19   subdirectories.
     20 
     21 --*/
     22 
     23 #include <windows.h>
     24 #include <direct.h>
     25 //
     26 // #include <io.h>         // for _chmod()
     27 //
     28 #include <sys/stat.h>
     29 //
     30 // #include <errno.h>      // for errno
     31 //
     32 #include <stdio.h>
     33 #include <string.h>
     34 #include <stdlib.h>
     35 #include <ctype.h>
     36 
     37 #include "HiiPack.h"
     38 
     39 extern
     40 void
     41 Error (
     42   char    *Name,
     43   UINT32  LineNumber,
     44   UINT32  MessageCode,
     45   char    *Text,
     46   char    *MsgFmt,
     47   ...
     48   );
     49 
     50 static
     51 int
     52 ProcessDirectory (
     53   char                *RootDirectory,
     54   char                *FileMask,
     55   FIND_FILE_CALLBACK  Callback
     56   );
     57 
     58 /*****************************************************************************/
     59 int
     60 FindFiles (
     61   char                *RootDirectory,
     62   char                *FileMask,
     63   FIND_FILE_CALLBACK  Callback
     64   )
     65 /*++
     66 
     67 Routine Description:
     68   Find files of a given name under a root directory
     69 
     70 Arguments:
     71   RootDirectory - base directory -- look in this directory and
     72                   all its subdirectories for files matching FileMask.
     73   FileMask      - file mask of files to find
     74   Callback      - function to call for each file found
     75 
     76 Returns:
     77 
     78 --*/
     79 {
     80   char  FullPath[MAX_PATH];
     81   //
     82   // If RootDirectory is relative, then append to cwd.
     83   //
     84   if (isalpha (RootDirectory[0]) && (RootDirectory[1] == ':')) {
     85     strcpy (FullPath, RootDirectory);
     86   } else {
     87     //
     88     // Get current working directory
     89     //
     90     if (_getcwd (FullPath, sizeof (FullPath)) == NULL) {
     91       Error (NULL, 0, 0, "failed to get current working directory", NULL);
     92       return 1;
     93     }
     94     //
     95     // Append the relative path they passed in
     96     //
     97     if (FullPath[strlen (FullPath) - 1] != '\\') {
     98       strcat (FullPath, "\\");
     99     }
    100 
    101     strcat (FullPath, RootDirectory);
    102   }
    103 
    104   if (FullPath[strlen (FullPath) - 1] == '\\') {
    105     FullPath[strlen (FullPath) - 1] = 0;
    106   }
    107   //
    108   // Process the directory
    109   //
    110   return ProcessDirectory (FullPath, FileMask, Callback);
    111 }
    112 
    113 static
    114 int
    115 ProcessDirectory (
    116   char                *RootDirectory,
    117   char                *FileMask,
    118   FIND_FILE_CALLBACK  Callback
    119   )
    120 /*++
    121 
    122 Routine Description:
    123   Process a directory to find all files matching a given file mask
    124 
    125 Arguments:
    126   RootDirectory - base directory -- look in this directory and
    127                   all its subdirectories for files matching FileMask.
    128   FileMask      - file mask of files to find
    129   Callback      - function to call for each file found
    130 
    131 Returns:
    132 
    133 --*/
    134 {
    135   HANDLE          Handle;
    136   WIN32_FIND_DATA FindData;
    137   char            TempName[MAX_PATH];
    138 
    139   Handle = INVALID_HANDLE_VALUE;
    140   //
    141   // Concatenate the filemask to the directory to create the full
    142   // path\mask path name.
    143   //
    144   strcpy (TempName, RootDirectory);
    145   strcat (TempName, "\\");
    146   strcat (TempName, FileMask);
    147   memset (&FindData, 0, sizeof (FindData));
    148   Handle = FindFirstFile (TempName, &FindData);
    149   if (Handle != INVALID_HANDLE_VALUE) {
    150     do {
    151       if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
    152         strcpy (TempName, RootDirectory);
    153         strcat (TempName, "\\");
    154         strcat (TempName, FindData.cFileName);
    155         if (Callback (TempName) != 0) {
    156           goto Done;
    157         }
    158       }
    159     } while (FindNextFile (Handle, &FindData));
    160   }
    161 
    162   if (Handle != INVALID_HANDLE_VALUE) {
    163     FindClose (Handle);
    164     Handle = INVALID_HANDLE_VALUE;
    165   }
    166   //
    167   // Now create a *.* file mask to get all subdirectories and recursive call this
    168   // function to handle each one found.
    169   //
    170   strcpy (TempName, RootDirectory);
    171   strcat (TempName, "\\*.*");
    172   memset (&FindData, 0, sizeof (FindData));
    173   Handle = FindFirstFile (TempName, &FindData);
    174   //
    175   // Loop until no more files/directories
    176   //
    177   if (Handle != INVALID_HANDLE_VALUE) {
    178     do {
    179       if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
    180         //
    181         // Make sure it's not "." or ".."
    182         //
    183         if ((strcmp (FindData.cFileName, ".") != 0) && (strcmp (FindData.cFileName, "..") != 0)) {
    184           //
    185           // Found a valid directory. Put it all together and make a recursive call
    186           // to process it.
    187           //
    188           strcpy (TempName, RootDirectory);
    189           strcat (TempName, "\\");
    190           strcat (TempName, FindData.cFileName);
    191           if (ProcessDirectory (TempName, FileMask, Callback) != 0) {
    192             goto Done;
    193           }
    194         }
    195       }
    196     } while (FindNextFile (Handle, &FindData));
    197   }
    198 
    199 Done:
    200   //
    201   // Free the handle
    202   //
    203   if (Handle != INVALID_HANDLE_VALUE) {
    204     FindClose (Handle);
    205   }
    206 
    207   return 0;
    208 }
    209