]>
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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/BaseLib.h>
18 Removes the last directory or file entry in a path. For a path which is
19 like L"fs0:startup.nsh", it's converted to L"fs0:".
21 @param[in,out] Path A pointer to the path to modify.
23 @retval FALSE Nothing was found to remove.
24 @retval TRUE A directory or file was removed.
35 // get directory name from path... ('chop' off extra)
37 for ( Walker
= Path
, LastSlash
= NULL
38 ; Walker
!= NULL
&& *Walker
!= CHAR_NULL
41 if (*Walker
== L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
43 } else if (*Walker
== L
':' && *(Walker
+ 1) != L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
47 if (LastSlash
!= NULL
) {
48 *LastSlash
= CHAR_NULL
;
55 Function to clean up paths.
57 - Single periods in the path are removed.
58 - Double periods in the path are removed along with a single parent directory.
59 - Forward slashes L'/' are converted to backward slashes L'\'.
61 This will be done inline and the existing buffer may be larger than required
64 @param[in] Path The pointer to the string containing the path.
66 @return Returns Path, otherwise returns NULL to indicate that an error has occured.
70 PathCleanUpDirectories(
81 // Replace the '/' with '\'
83 for (TempString
= Path
; *TempString
!= CHAR_NULL
; TempString
++) {
84 if (*TempString
== L
'/') {
90 // Replace the "\\" with "\"
92 while ((TempString
= StrStr (Path
, L
"\\\\")) != NULL
) {
93 CopyMem (TempString
, TempString
+ 1, StrSize (TempString
+ 1));
97 // Remove all the "\.". E.g.: fs0:\abc\.\def\.
99 while ((TempString
= StrStr (Path
, L
"\\.\\")) != NULL
) {
100 CopyMem (TempString
, TempString
+ 2, StrSize (TempString
+ 2));
102 if ((StrLen (Path
) >= 2) && (StrCmp (Path
+ StrLen (Path
) - 2, L
"\\.") == 0)) {
103 Path
[StrLen (Path
) - 1] = CHAR_NULL
;
107 // Remove all the "\..". E.g.: fs0:\abc\..\def\..
109 while (((TempString
= StrStr(Path
, L
"\\..")) != NULL
) &&
110 ((*(TempString
+ 3) == L
'\\') || (*(TempString
+ 3) == CHAR_NULL
))
112 *(TempString
+ 1) = CHAR_NULL
;
113 PathRemoveLastItem(Path
);
114 if (*(TempString
+ 3) != CHAR_NULL
) {
115 CopyMem (Path
+ StrLen (Path
), TempString
+ 4, StrSize (TempString
+ 4));