]>
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.
30 // get directory name from path... ('chop' off extra)
32 for ( Walker
= Path
, LastSlash
= NULL
33 ; Walker
!= NULL
&& *Walker
!= CHAR_NULL
37 if ((*Walker
== L
'\\') && (*(Walker
+ 1) != CHAR_NULL
)) {
39 } else if ((*Walker
== L
':') && (*(Walker
+ 1) != L
'\\') && (*(Walker
+ 1) != CHAR_NULL
)) {
44 if (LastSlash
!= NULL
) {
45 *LastSlash
= CHAR_NULL
;
53 Function to clean up paths.
55 - Single periods in the path are removed.
56 - Double periods in the path are removed along with a single parent directory.
57 - Forward slashes L'/' are converted to backward slashes L'\'.
59 This will be done inline and the existing buffer may be larger than required
62 @param[in] Path The pointer to the string containing the path.
64 @return Returns Path, otherwise returns NULL to indicate that an error has occurred.
68 PathCleanUpDirectories (
79 // Replace the '/' with '\'
81 for (TempString
= Path
; *TempString
!= CHAR_NULL
; TempString
++) {
82 if (*TempString
== L
'/') {
88 // Replace the "\\" with "\"
90 while ((TempString
= StrStr (Path
, L
"\\\\")) != NULL
) {
91 CopyMem (TempString
, TempString
+ 1, StrSize (TempString
+ 1));
95 // Remove all the "\.". E.g.: fs0:\abc\.\def\.
97 while ((TempString
= StrStr (Path
, L
"\\.\\")) != NULL
) {
98 CopyMem (TempString
, TempString
+ 2, StrSize (TempString
+ 2));
101 if ((StrLen (Path
) >= 2) && (StrCmp (Path
+ StrLen (Path
) - 2, L
"\\.") == 0)) {
102 Path
[StrLen (Path
) - 1] = CHAR_NULL
;
106 // Remove all the "\..". E.g.: fs0:\abc\..\def\..
108 while (((TempString
= StrStr (Path
, L
"\\..")) != NULL
) &&
109 ((*(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));