+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-Module Name:\r
-\r
- FindFiles.c \r
- \r
-Abstract:\r
-\r
- OS-specific functions to assist in finding files in\r
- subdirectories.\r
- \r
---*/\r
-\r
-#include <windows.h>\r
-#include <direct.h>\r
-//\r
-// #include <io.h> // for _chmod()\r
-//\r
-#include <sys/stat.h>\r
-//\r
-// #include <errno.h> // for errno\r
-//\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#include <ctype.h>\r
-\r
-#include "HiiPack.h"\r
-\r
-extern\r
-void\r
-Error (\r
- char *Name,\r
- UINT32 LineNumber,\r
- UINT32 MessageCode,\r
- char *Text,\r
- char *MsgFmt,\r
- ...\r
- );\r
-\r
-static\r
-int\r
-ProcessDirectory (\r
- char *RootDirectory,\r
- char *FileMask,\r
- FIND_FILE_CALLBACK Callback\r
- );\r
-\r
-/*****************************************************************************/\r
-int\r
-FindFiles (\r
- char *RootDirectory,\r
- char *FileMask,\r
- FIND_FILE_CALLBACK Callback\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Find files of a given name under a root directory\r
-\r
-Arguments:\r
- RootDirectory - base directory -- look in this directory and\r
- all its subdirectories for files matching FileMask.\r
- FileMask - file mask of files to find\r
- Callback - function to call for each file found \r
-\r
-Returns:\r
-\r
---*/\r
-{\r
- char FullPath[MAX_PATH];\r
- //\r
- // If RootDirectory is relative, then append to cwd.\r
- //\r
- if (isalpha (RootDirectory[0]) && (RootDirectory[1] == ':')) {\r
- strcpy (FullPath, RootDirectory);\r
- } else {\r
- //\r
- // Get current working directory\r
- //\r
- if (_getcwd (FullPath, sizeof (FullPath)) == NULL) {\r
- Error (NULL, 0, 0, "failed to get current working directory", NULL);\r
- return 1;\r
- }\r
- //\r
- // Append the relative path they passed in\r
- //\r
- if (FullPath[strlen (FullPath) - 1] != '\\') {\r
- strcat (FullPath, "\\");\r
- }\r
-\r
- strcat (FullPath, RootDirectory);\r
- }\r
-\r
- if (FullPath[strlen (FullPath) - 1] == '\\') {\r
- FullPath[strlen (FullPath) - 1] = 0;\r
- }\r
- //\r
- // Process the directory\r
- //\r
- return ProcessDirectory (FullPath, FileMask, Callback);\r
-}\r
-\r
-static\r
-int\r
-ProcessDirectory (\r
- char *RootDirectory,\r
- char *FileMask,\r
- FIND_FILE_CALLBACK Callback\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Process a directory to find all files matching a given file mask\r
-\r
-Arguments:\r
- RootDirectory - base directory -- look in this directory and\r
- all its subdirectories for files matching FileMask.\r
- FileMask - file mask of files to find\r
- Callback - function to call for each file found \r
-\r
-Returns:\r
-\r
---*/\r
-{\r
- HANDLE Handle;\r
- WIN32_FIND_DATA FindData;\r
- char TempName[MAX_PATH];\r
-\r
- Handle = INVALID_HANDLE_VALUE;\r
- //\r
- // Concatenate the filemask to the directory to create the full\r
- // path\mask path name.\r
- //\r
- strcpy (TempName, RootDirectory);\r
- strcat (TempName, "\\");\r
- strcat (TempName, FileMask);\r
- memset (&FindData, 0, sizeof (FindData));\r
- Handle = FindFirstFile (TempName, &FindData);\r
- if (Handle != INVALID_HANDLE_VALUE) {\r
- do {\r
- if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {\r
- strcpy (TempName, RootDirectory);\r
- strcat (TempName, "\\");\r
- strcat (TempName, FindData.cFileName);\r
- if (Callback (TempName) != 0) {\r
- goto Done;\r
- }\r
- }\r
- } while (FindNextFile (Handle, &FindData));\r
- }\r
-\r
- if (Handle != INVALID_HANDLE_VALUE) {\r
- FindClose (Handle);\r
- Handle = INVALID_HANDLE_VALUE;\r
- }\r
- //\r
- // Now create a *.* file mask to get all subdirectories and recursive call this\r
- // function to handle each one found.\r
- //\r
- strcpy (TempName, RootDirectory);\r
- strcat (TempName, "\\*.*");\r
- memset (&FindData, 0, sizeof (FindData));\r
- Handle = FindFirstFile (TempName, &FindData);\r
- //\r
- // Loop until no more files/directories\r
- //\r
- if (Handle != INVALID_HANDLE_VALUE) {\r
- do {\r
- if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {\r
- //\r
- // Make sure it's not "." or ".."\r
- //\r
- if ((strcmp (FindData.cFileName, ".") != 0) && (strcmp (FindData.cFileName, "..") != 0)) {\r
- //\r
- // Found a valid directory. Put it all together and make a recursive call\r
- // to process it.\r
- //\r
- strcpy (TempName, RootDirectory);\r
- strcat (TempName, "\\");\r
- strcat (TempName, FindData.cFileName);\r
- if (ProcessDirectory (TempName, FileMask, Callback) != 0) {\r
- goto Done;\r
- }\r
- }\r
- }\r
- } while (FindNextFile (Handle, &FindData));\r
- }\r
-\r
-Done:\r
- //\r
- // Free the handle\r
- //\r
- if (Handle != INVALID_HANDLE_VALUE) {\r
- FindClose (Handle);\r
- }\r
-\r
- return 0;\r
-}\r