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