]>
Commit | Line | Data |
---|---|---|
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 <Uefi/UefiBaseType.h>\r |
14 | #include <Library/BaseMemoryLib.h>\r | |
15 | #include <Library/BaseLib.h>\r | |
16 | #include <Protocol/SimpleTextIn.h>\r | |
ab94587a | 17 | \r |
18 | /**\r | |
19 | Removes the last directory or file entry in a path by changing the last\r | |
20 | L'\' to a CHAR_NULL.\r | |
21 | \r | |
ae591c14 | 22 | @param[in,out] Path A pointer to the path to modify.\r |
ab94587a | 23 | \r |
24 | @retval FALSE Nothing was found to remove.\r | |
25 | @retval TRUE A directory or file was removed.\r | |
26 | **/\r | |
27 | BOOLEAN\r | |
28 | EFIAPI\r | |
29 | PathRemoveLastItem(\r | |
30 | IN OUT CHAR16 *Path\r | |
31 | )\r | |
32 | {\r | |
33 | CHAR16 *Walker;\r | |
34 | CHAR16 *LastSlash;\r | |
35 | //\r | |
36 | // get directory name from path... ('chop' off extra)\r | |
37 | //\r | |
38 | for ( Walker = Path, LastSlash = NULL\r | |
39 | ; Walker != NULL && *Walker != CHAR_NULL\r | |
40 | ; Walker++\r | |
41 | ){\r | |
42 | if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r | |
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 |
67 | CHAR16*\r | |
68 | EFIAPI\r | |
69 | PathCleanUpDirectories(\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 |