From ab94587a7d2cd89695f9190420daceae0d2b7446 Mon Sep 17 00:00:00 2001 From: jcarsey Date: Thu, 30 Jun 2011 22:58:53 +0000 Subject: This refactors 3 functions out of ShellCommandLib and puts them into a new library (but as 2 functions instead of 3). This allows for users outside of the shell itself to have access to these functions. 1) Remove the 3 functions out of the shell's internal library (ShellCommandLib) 2) Add a new library class (PathLib) 3) Add an instance of this class (BasePathLib) 4) Change all internal shell callers to use this new library class. signed-off-by: jcarsey reviewed-by: jljusten git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11936 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Library/BasePathLib/BasePathLib.c | 122 +++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 ShellPkg/Library/BasePathLib/BasePathLib.c (limited to 'ShellPkg/Library/BasePathLib/BasePathLib.c') diff --git a/ShellPkg/Library/BasePathLib/BasePathLib.c b/ShellPkg/Library/BasePathLib/BasePathLib.c new file mode 100644 index 0000000000..53a2a095b5 --- /dev/null +++ b/ShellPkg/Library/BasePathLib/BasePathLib.c @@ -0,0 +1,122 @@ +/** @file + Provides interface to path manipulation functions. + + Copyright (c) 2011, Intel Corporation. All rights reserved.
+ This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php. + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +**/ + +#include +#include +#include +#include + +/** + Removes the last directory or file entry in a path by changing the last + L'\' to a CHAR_NULL. + + @param[in,out] Path The pointer to the path to modify. + + @retval FALSE Nothing was found to remove. + @retval TRUE A directory or file was removed. +**/ +BOOLEAN +EFIAPI +PathRemoveLastItem( + IN OUT CHAR16 *Path + ) +{ + CHAR16 *Walker; + CHAR16 *LastSlash; + // + // get directory name from path... ('chop' off extra) + // + for ( Walker = Path, LastSlash = NULL + ; Walker != NULL && *Walker != CHAR_NULL + ; Walker++ + ){ + if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) { + LastSlash = Walker+1; + } + } + if (LastSlash != NULL) { + *LastSlash = CHAR_NULL; + return (TRUE); + } + return (FALSE); +} + +/** + Function to clean up paths. + + - Single periods in the path are removed. + - Double periods in the path are removed along with a single parent directory. + - Forward slashes L'/' are converted to backward slashes L'\'. + + This will be done inline and the existing buffer may be larger than required + upon completion. + + @param[in] Path The pointer to the string containing the path. + + @retval NULL An error occured. + @return Path in all other instances. +**/ +CHAR16* +EFIAPI +PathCleanUpDirectories( + IN CHAR16 *Path + ) +{ + CHAR16 *TempString; + UINTN TempSize; + if (Path==NULL) { + return(NULL); + } + + // + // Fix up the / vs \ + // + for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) { + if (*TempString == L'/') { + *TempString = L'\\'; + } + } + + // + // Fix up the .. + // + while ((TempString = StrStr(Path, L"\\..\\")) != NULL) { + *TempString = CHAR_NULL; + TempString += 4; + PathRemoveLastItem(Path); + TempSize = StrSize(TempString); + CopyMem(Path+StrLen(Path), TempString, TempSize); + } + if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) { + *TempString = CHAR_NULL; + PathRemoveLastItem(Path); + } + + // + // Fix up the . + // + while ((TempString = StrStr(Path, L"\\.\\")) != NULL) { + *TempString = CHAR_NULL; + TempString += 2; + TempSize = StrSize(TempString); + CopyMem(Path+StrLen(Path), TempString, TempSize); + } + if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) { + *TempString = CHAR_NULL; + } + + + + return (Path); +} + -- cgit v1.2.3