2011-07-01 00:58:53 +02:00
|
|
|
/** @file
|
2015-01-13 02:04:07 +01:00
|
|
|
Defines file-path manipulation functions.
|
2011-07-01 00:58:53 +02:00
|
|
|
|
2016-06-21 10:18:56 +02:00
|
|
|
Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
|
2011-07-01 00:58:53 +02:00
|
|
|
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.
|
|
|
|
**/
|
2015-01-13 02:04:07 +01:00
|
|
|
#include <Uefi/UefiBaseType.h>
|
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
#include <Protocol/SimpleTextIn.h>
|
2011-07-01 00:58:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Removes the last directory or file entry in a path by changing the last
|
2016-08-08 08:55:10 +02:00
|
|
|
L'\' to a CHAR_NULL. For a path which is like L"fs0:startup.nsh",
|
|
|
|
it's converted to L"fs0:".
|
2011-07-01 00:58:53 +02:00
|
|
|
|
2015-01-13 02:04:07 +01:00
|
|
|
@param[in,out] Path A pointer to the path to modify.
|
2011-07-01 00:58:53 +02:00
|
|
|
|
|
|
|
@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++
|
|
|
|
){
|
2016-08-08 08:55:10 +02:00
|
|
|
if ((*Walker == L'\\' || *Walker == L':') && *(Walker + 1) != CHAR_NULL) {
|
2011-07-01 00:58:53 +02:00
|
|
|
LastSlash = Walker+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (LastSlash != NULL) {
|
|
|
|
*LastSlash = CHAR_NULL;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-13 02:04:07 +01:00
|
|
|
Function to clean up paths.
|
|
|
|
|
2011-07-01 00:58:53 +02:00
|
|
|
- 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'\'.
|
|
|
|
|
2015-01-13 02:04:07 +01:00
|
|
|
This will be done inline and the existing buffer may be larger than required
|
2011-07-01 00:58:53 +02:00
|
|
|
upon completion.
|
|
|
|
|
|
|
|
@param[in] Path The pointer to the string containing the path.
|
|
|
|
|
2015-01-13 02:04:07 +01:00
|
|
|
@return Returns Path, otherwise returns NULL to indicate that an error has occured.
|
2011-07-01 00:58:53 +02:00
|
|
|
**/
|
|
|
|
CHAR16*
|
|
|
|
EFIAPI
|
|
|
|
PathCleanUpDirectories(
|
|
|
|
IN CHAR16 *Path
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CHAR16 *TempString;
|
|
|
|
UINTN TempSize;
|
2015-01-13 02:04:07 +01:00
|
|
|
|
2011-07-01 00:58:53 +02:00
|
|
|
if (Path==NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
//
|
2011-07-11 16:32:34 +02:00
|
|
|
// Fix up the '/' vs '\'
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
|
|
|
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;
|
2016-06-21 10:18:56 +02:00
|
|
|
if (!PathRemoveLastItem(Path)) {
|
|
|
|
*TempString = L'\\';
|
|
|
|
}
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// 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) {
|
2011-09-20 23:01:34 +02:00
|
|
|
*(TempString + 1) = CHAR_NULL;
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 21:15:02 +02:00
|
|
|
while ((TempString = StrStr(Path, L"\\\\")) != NULL) {
|
|
|
|
*TempString = CHAR_NULL;
|
|
|
|
TempString += 1;
|
|
|
|
TempSize = StrSize(TempString);
|
|
|
|
CopyMem(Path+StrLen(Path), TempString, TempSize);
|
|
|
|
}
|
|
|
|
if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {
|
|
|
|
*(TempString) = CHAR_NULL;
|
|
|
|
}
|
2011-07-01 00:58:53 +02:00
|
|
|
|
|
|
|
return (Path);
|
|
|
|
}
|
|
|
|
|