]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/FilePaths.c
MdePkg BaseLib: API PathRemoveLastItem not handle root paths properly
[mirror_edk2.git] / MdePkg / Library / BaseLib / FilePaths.c
CommitLineData
ab94587a 1/** @file\r
ae591c14 2 Defines file-path manipulation functions.\r
ab94587a 3\r
85df6124 4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
ab94587a 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
ae591c14
DM
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/BaseLib.h>\r
ab94587a 15\r
16/**\r
6a623094
HW
17 Removes the last directory or file entry in a path. For a path which is\r
18 like L"fs0:startup.nsh", it's converted to L"fs0:".\r
ab94587a 19\r
ae591c14 20 @param[in,out] Path A pointer to the path to modify.\r
ab94587a 21\r
22 @retval FALSE Nothing was found to remove.\r
23 @retval TRUE A directory or file was removed.\r
24**/\r
25BOOLEAN\r
26EFIAPI\r
27PathRemoveLastItem(\r
28 IN OUT CHAR16 *Path\r
29 )\r
30{\r
31 CHAR16 *Walker;\r
32 CHAR16 *LastSlash;\r
33 //\r
34 // get directory name from path... ('chop' off extra)\r
35 //\r
36 for ( Walker = Path, LastSlash = NULL\r
37 ; Walker != NULL && *Walker != CHAR_NULL\r
38 ; Walker++\r
39 ){\r
6a623094
HW
40 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
41 LastSlash = Walker+1;\r
42 } else if (*Walker == L':' && *(Walker + 1) != L'\\' && *(Walker + 1) != CHAR_NULL) {\r
ab94587a 43 LastSlash = Walker+1;\r
44 }\r
45 }\r
46 if (LastSlash != NULL) {\r
47 *LastSlash = CHAR_NULL;\r
48 return (TRUE);\r
49 }\r
50 return (FALSE);\r
51}\r
52\r
53/**\r
ae591c14
DM
54 Function to clean up paths.\r
55\r
ab94587a 56 - Single periods in the path are removed.\r
57 - Double periods in the path are removed along with a single parent directory.\r
58 - Forward slashes L'/' are converted to backward slashes L'\'.\r
59\r
ae591c14 60 This will be done inline and the existing buffer may be larger than required\r
ab94587a 61 upon completion.\r
62\r
63 @param[in] Path The pointer to the string containing the path.\r
64\r
ae591c14 65 @return Returns Path, otherwise returns NULL to indicate that an error has occured.\r
ab94587a 66**/\r
67CHAR16*\r
68EFIAPI\r
69PathCleanUpDirectories(\r
70 IN CHAR16 *Path\r
71 )\r
72{\r
73 CHAR16 *TempString;\r
74 UINTN TempSize;\r
ae591c14 75\r
ab94587a 76 if (Path==NULL) {\r
77 return(NULL);\r
78 }\r
ab94587a 79 //\r
5ddca3b9 80 // Fix up the '/' vs '\'\r
ab94587a 81 //\r
82 for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {\r
83 if (*TempString == L'/') {\r
84 *TempString = L'\\';\r
85 }\r
86 }\r
ab94587a 87 //\r
88 // Fix up the ..\r
89 //\r
90 while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {\r
91 *TempString = CHAR_NULL;\r
92 TempString += 4;\r
93 PathRemoveLastItem(Path);\r
94 TempSize = StrSize(TempString);\r
95 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
96 }\r
97 if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {\r
98 *TempString = CHAR_NULL;\r
85df6124
QS
99 if (!PathRemoveLastItem(Path)) {\r
100 *TempString = L'\\';\r
101 }\r
ab94587a 102 }\r
ab94587a 103 //\r
104 // Fix up the .\r
105 //\r
106 while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {\r
107 *TempString = CHAR_NULL;\r
108 TempString += 2;\r
109 TempSize = StrSize(TempString);\r
110 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
111 }\r
112 if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {\r
1fc3749d 113 *(TempString + 1) = CHAR_NULL;\r
ab94587a 114 }\r
115\r
bbf904d1
JC
116 while ((TempString = StrStr(Path, L"\\\\")) != NULL) {\r
117 *TempString = CHAR_NULL;\r
118 TempString += 1;\r
119 TempSize = StrSize(TempString);\r
120 CopyMem(Path+StrLen(Path), TempString, TempSize);\r
121 }\r
122 if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {\r
123 *(TempString) = CHAR_NULL;\r
124 }\r
ab94587a 125\r
126 return (Path);\r
127}\r
128\r