]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/FilePaths.c
2 Defines file-path manipulation functions.
4 Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2018, Dell Technologies. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include <Library/BaseMemoryLib.h>
9 #include <Library/BaseLib.h>
12 Removes the last directory or file entry in a path. For a path which is
13 like L"fs0:startup.nsh", it's converted to L"fs0:".
15 @param[in,out] Path A pointer to the path to modify.
17 @retval FALSE Nothing was found to remove.
18 @retval TRUE A directory or file was removed.
29 // get directory name from path... ('chop' off extra)
31 for ( Walker
= Path
, LastSlash
= NULL
32 ; Walker
!= NULL
&& *Walker
!= CHAR_NULL
35 if (*Walker
== L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
37 } else if (*Walker
== L
':' && *(Walker
+ 1) != L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
41 if (LastSlash
!= NULL
) {
42 *LastSlash
= CHAR_NULL
;
49 Function to clean up paths.
51 - Single periods in the path are removed.
52 - Double periods in the path are removed along with a single parent directory.
53 - Forward slashes L'/' are converted to backward slashes L'\'.
55 This will be done inline and the existing buffer may be larger than required
58 @param[in] Path The pointer to the string containing the path.
60 @return Returns Path, otherwise returns NULL to indicate that an error has occured.
64 PathCleanUpDirectories(
75 // Replace the '/' with '\'
77 for (TempString
= Path
; *TempString
!= CHAR_NULL
; TempString
++) {
78 if (*TempString
== L
'/') {
84 // Replace the "\\" with "\"
86 while ((TempString
= StrStr (Path
, L
"\\\\")) != NULL
) {
87 CopyMem (TempString
, TempString
+ 1, StrSize (TempString
+ 1));
91 // Remove all the "\.". E.g.: fs0:\abc\.\def\.
93 while ((TempString
= StrStr (Path
, L
"\\.\\")) != NULL
) {
94 CopyMem (TempString
, TempString
+ 2, StrSize (TempString
+ 2));
96 if ((StrLen (Path
) >= 2) && (StrCmp (Path
+ StrLen (Path
) - 2, L
"\\.") == 0)) {
97 Path
[StrLen (Path
) - 1] = CHAR_NULL
;
101 // Remove all the "\..". E.g.: fs0:\abc\..\def\..
103 while (((TempString
= StrStr(Path
, L
"\\..")) != NULL
) &&
104 ((*(TempString
+ 3) == L
'\\') || (*(TempString
+ 3) == CHAR_NULL
))
106 *(TempString
+ 1) = CHAR_NULL
;
107 PathRemoveLastItem(Path
);
108 if (*(TempString
+ 3) != CHAR_NULL
) {
109 CopyMem (Path
+ StrLen (Path
), TempString
+ 4, StrSize (TempString
+ 4));