]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/BasePathLib/BasePathLib.c
301bd3b279a17e749828c026f96c75e5bfe57085
[mirror_edk2.git] / ShellPkg / Library / BasePathLib / BasePathLib.c
1 /** @file
2 Provides interface to path manipulation functions.
3
4 Copyright (c) 2011 - 2014, 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
14 #include <Base.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/PathLib.h>
17 #include <Library/BaseLib.h>
18 #include <Protocol/SimpleTextIn.h>
19
20 /**
21 Removes the last directory or file entry in a path by changing the last
22 L'\' to a CHAR_NULL.
23
24 @param[in, out] Path The pointer to the path to modify.
25
26 @retval FALSE Nothing was found to remove.
27 @retval TRUE A directory or file was removed.
28 **/
29 BOOLEAN
30 EFIAPI
31 PathRemoveLastItem(
32 IN OUT CHAR16 *Path
33 )
34 {
35 CHAR16 *Walker;
36 CHAR16 *LastSlash;
37 //
38 // get directory name from path... ('chop' off extra)
39 //
40 for ( Walker = Path, LastSlash = NULL
41 ; Walker != NULL && *Walker != CHAR_NULL
42 ; Walker++
43 ){
44 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {
45 LastSlash = Walker+1;
46 }
47 }
48 if (LastSlash != NULL) {
49 *LastSlash = CHAR_NULL;
50 return (TRUE);
51 }
52 return (FALSE);
53 }
54
55 /**
56 Function to clean up paths.
57
58 - Single periods in the path are removed.
59 - Double periods in the path are removed along with a single parent directory.
60 - Forward slashes L'/' are converted to backward slashes L'\'.
61
62 This will be done inline and the existing buffer may be larger than required
63 upon completion.
64
65 @param[in] Path The pointer to the string containing the path.
66
67 @retval NULL An error occured.
68 @return Path in all other instances.
69 **/
70 CHAR16*
71 EFIAPI
72 PathCleanUpDirectories(
73 IN CHAR16 *Path
74 )
75 {
76 CHAR16 *TempString;
77 UINTN TempSize;
78 if (Path==NULL) {
79 return(NULL);
80 }
81
82 //
83 // Fix up the '/' vs '\'
84 //
85 for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {
86 if (*TempString == L'/') {
87 *TempString = L'\\';
88 }
89 }
90
91 //
92 // Fix up the ..
93 //
94 while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {
95 *TempString = CHAR_NULL;
96 TempString += 4;
97 PathRemoveLastItem(Path);
98 TempSize = StrSize(TempString);
99 CopyMem(Path+StrLen(Path), TempString, TempSize);
100 }
101 if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {
102 *TempString = CHAR_NULL;
103 PathRemoveLastItem(Path);
104 }
105
106 //
107 // Fix up the .
108 //
109 while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {
110 *TempString = CHAR_NULL;
111 TempString += 2;
112 TempSize = StrSize(TempString);
113 CopyMem(Path+StrLen(Path), TempString, TempSize);
114 }
115 if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {
116 *(TempString + 1) = CHAR_NULL;
117 }
118
119 while ((TempString = StrStr(Path, L"\\\\")) != NULL) {
120 *TempString = CHAR_NULL;
121 TempString += 1;
122 TempSize = StrSize(TempString);
123 CopyMem(Path+StrLen(Path), TempString, TempSize);
124 }
125 if ((TempString = StrStr(Path, L"\\\\")) != NULL && *(TempString + 1) == CHAR_NULL) {
126 *(TempString) = CHAR_NULL;
127 }
128
129 return (Path);
130 }
131