]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
ShellPkg: Follow spec to remove the last '\' char in return name of GetCurDir().
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cd.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for attrib shell level 2 function.\r
3\r
c011b6c9 4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
fbd2dfad 5 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
a405b86d 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "UefiShellLevel2CommandsLib.h"\r
17\r
18/**\r
19 Function for 'cd' command.\r
20\r
21 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
22 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
23**/\r
24SHELL_STATUS\r
25EFIAPI\r
26ShellCommandRunCd (\r
27 IN EFI_HANDLE ImageHandle,\r
28 IN EFI_SYSTEM_TABLE *SystemTable\r
29 )\r
30{\r
31 EFI_STATUS Status;\r
32 LIST_ENTRY *Package;\r
33 CONST CHAR16 *Directory;\r
fbd2dfad 34 CHAR16 *Cwd;\r
a405b86d 35 CHAR16 *Path;\r
36 CHAR16 *Drive;\r
fbd2dfad 37 UINTN CwdSize;\r
a405b86d 38 UINTN DriveSize;\r
39 CHAR16 *ProblemParam;\r
40 SHELL_STATUS ShellStatus;\r
41 SHELL_FILE_HANDLE Handle;\r
42 CONST CHAR16 *Param1;\r
1fc3749d 43 CHAR16 *Param1Copy;\r
0960ba17 44 CHAR16* Walker;\r
a405b86d 45\r
46 ProblemParam = NULL;\r
47 ShellStatus = SHELL_SUCCESS;\r
48 Drive = NULL;\r
49 DriveSize = 0;\r
50\r
51 Status = CommandInit();\r
52 ASSERT_EFI_ERROR(Status);\r
53\r
54 //\r
55 // initialize the shell lib (we must be in non-auto-init...)\r
56 //\r
57 Status = ShellInitialize();\r
58 ASSERT_EFI_ERROR(Status);\r
59\r
60 //\r
61 // parse the command line\r
62 //\r
63 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r
64 if (EFI_ERROR(Status)) {\r
65 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
099e8ff5 66 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cd", ProblemParam); \r
a405b86d 67 FreePool(ProblemParam);\r
68 ShellStatus = SHELL_INVALID_PARAMETER;\r
69 } else {\r
70 ASSERT(FALSE);\r
71 }\r
72 }\r
73\r
74 //\r
75 // check for "-?"\r
76 //\r
77 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
78 ASSERT(FALSE);\r
79 } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {\r
099e8ff5 80 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 81 ShellStatus = SHELL_INVALID_PARAMETER;\r
82 } else {\r
83 //\r
84 // remember that param 0 is the command name\r
85 // If there are 0 value parameters, then print the current directory\r
86 // else If there are 2 value parameters, then print the error message\r
87 // else If there is 1 value paramerer , then change the directory\r
88 //\r
89 Param1 = ShellCommandLineGetRawValue(Package, 1);\r
90 if (Param1 == NULL) {\r
91 //\r
92 // display the current directory\r
93 //\r
94 Directory = ShellGetCurrentDir(NULL);\r
95 if (Directory != NULL) {\r
96 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Directory);\r
97 } else {\r
099e8ff5 98 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 99 ShellStatus = SHELL_NOT_FOUND;\r
100 }\r
101 } else {\r
1fc3749d 102 Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r
0960ba17
QS
103 for (Walker = Param1Copy; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r
104 if (*Walker == L'\"') {\r
105 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r
106 }\r
107 }\r
108 \r
beab0fc5 109 if (Param1Copy != NULL) {\r
110 Param1Copy = PathCleanUpDirectories(Param1Copy);\r
111 }\r
112 if (Param1Copy != NULL) {\r
113 if (StrCmp(Param1Copy, L".") == 0) {\r
a405b86d 114 //\r
beab0fc5 115 // nothing to do... change to current directory\r
a405b86d 116 //\r
beab0fc5 117 } else if (StrCmp(Param1Copy, L"..") == 0) {\r
a405b86d 118 //\r
beab0fc5 119 // Change up one directory...\r
a405b86d 120 //\r
beab0fc5 121 Directory = ShellGetCurrentDir(NULL);\r
122 if (Directory == NULL) {\r
099e8ff5 123 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 124 ShellStatus = SHELL_NOT_FOUND;\r
a405b86d 125 } else {\r
fbd2dfad
QS
126 CwdSize = StrSize(Directory) + sizeof(CHAR16);\r
127 Cwd = AllocateZeroPool(CwdSize);\r
128 ASSERT(Cwd!=NULL);\r
129 StrCpyS(Cwd, StrSize(Directory)/sizeof(CHAR16)+1, Directory);\r
130 StrCatS(Cwd, StrSize(Directory)/sizeof(CHAR16)+1, L"\\");\r
131 Drive = GetFullyQualifiedPath(Cwd);\r
beab0fc5 132 PathRemoveLastItem(Drive);\r
fbd2dfad 133 FreePool(Cwd);\r
a405b86d 134 }\r
beab0fc5 135 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
136 //\r
137 // change directory on current drive letter\r
138 //\r
139 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
140 if (Status == EFI_NOT_FOUND) {\r
099e8ff5 141 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 142 ShellStatus = SHELL_NOT_FOUND;\r
143 }\r
144 }\r
145 } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
a405b86d 146 //\r
beab0fc5 147 // Move to root of current drive\r
a405b86d 148 //\r
beab0fc5 149 Directory = ShellGetCurrentDir(NULL);\r
150 if (Directory == NULL) {\r
099e8ff5 151 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 152 ShellStatus = SHELL_NOT_FOUND;\r
beab0fc5 153 } else {\r
fbd2dfad
QS
154 CwdSize = StrSize(Directory) + sizeof(CHAR16);\r
155 Cwd = AllocateZeroPool(CwdSize);\r
156 ASSERT(Cwd!=NULL);\r
157 StrCpyS(Cwd, StrSize(Directory)/sizeof(CHAR16)+1, Directory);\r
158 StrCatS(Cwd, StrSize(Directory)/sizeof(CHAR16)+1, L"\\");\r
159 Drive = GetFullyQualifiedPath(Cwd);\r
160 while (PathRemoveLastItem(Drive));\r
161 FreePool(Cwd);\r
a405b86d 162 }\r
163 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
164 //\r
165 // change directory on current drive letter\r
166 //\r
167 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
168 if (Status == EFI_NOT_FOUND) {\r
099e8ff5 169 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 170 ShellStatus = SHELL_NOT_FOUND;\r
171 }\r
172 }\r
beab0fc5 173 } else if (StrStr(Param1Copy, L":") == NULL) {\r
668a5781 174 //\r
175 // change directory without a drive identifier\r
176 //\r
beab0fc5 177 if (ShellGetCurrentDir(NULL) == NULL) {\r
099e8ff5 178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 179 ShellStatus = SHELL_NOT_FOUND;\r
180 } else {\r
181 ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
182 Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
fbd2dfad 183 Drive = StrnCatGrow(&Drive, &DriveSize, L"\\", 0);\r
beab0fc5 184 if (*Param1Copy == L'\\') {\r
185 while (PathRemoveLastItem(Drive)) ;\r
186 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
187 } else {\r
188 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
189 }\r
190 //\r
191 // Verify that this is a valid directory\r
192 //\r
193 Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
194 if (EFI_ERROR(Status)) {\r
099e8ff5 195 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Drive); \r
beab0fc5 196 ShellStatus = SHELL_NOT_FOUND;\r
197 } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
099e8ff5 198 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Drive); \r
beab0fc5 199 ShellStatus = SHELL_NOT_FOUND;\r
200 }\r
201 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
202 //\r
203 // change directory on current drive letter\r
204 //\r
205 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
206 if (Status == EFI_NOT_FOUND) {\r
099e8ff5 207 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 208 ShellStatus = SHELL_NOT_FOUND;\r
209 }\r
210 }\r
211 if (Handle != NULL) {\r
212 gEfiShellProtocol->CloseFile(Handle);\r
213 DEBUG_CODE(Handle = NULL;);\r
214 }\r
a405b86d 215 }\r
9ea69f8a 216 } else {\r
beab0fc5 217 //\r
668a5781 218 // change directory with a drive letter\r
beab0fc5 219 //\r
cea5e3b9 220 Drive = AllocateCopyPool(StrSize(Param1Copy), Param1Copy);\r
beab0fc5 221 if (Drive == NULL) {\r
099e8ff5 222 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 223 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
9ea69f8a 224 } else {\r
beab0fc5 225 Path = StrStr(Drive, L":");\r
226 ASSERT(Path != NULL);\r
668a5781 227 if (EFI_ERROR(ShellIsDirectory(Param1Copy))) {\r
099e8ff5 228 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Param1Copy); \r
668a5781 229 ShellStatus = SHELL_NOT_FOUND;\r
230 } else if (*(Path+1) == CHAR_NULL) {\r
099e8ff5 231 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
b54fd049 232 ShellStatus = SHELL_NOT_FOUND;\r
233 } else {\r
beab0fc5 234 *(Path+1) = CHAR_NULL;\r
235 if (Path == Drive + StrLen(Drive)) {\r
099e8ff5 236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 237 ShellStatus = SHELL_NOT_FOUND;\r
238 } else {\r
239 Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
240 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
241 }\r
242 }\r
243 if (Status == EFI_NOT_FOUND) {\r
099e8ff5 244 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 245 Status = SHELL_NOT_FOUND;\r
246 } else if (EFI_ERROR(Status)) {\r
099e8ff5 247 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Param1Copy); \r
beab0fc5 248 Status = SHELL_NOT_FOUND;\r
b54fd049 249 }\r
9ea69f8a 250 }\r
a405b86d 251 }\r
252 }\r
1fc3749d 253 FreePool(Param1Copy);\r
a405b86d 254 }\r
255 }\r
256\r
257 if (Drive != NULL) {\r
258 FreePool(Drive);\r
259 }\r
260 //\r
261 // free the command line package\r
262 //\r
263 ShellCommandLineFreeVarList (Package);\r
264\r
265 //\r
266 // return the status\r
267 //\r
268 return (ShellStatus);\r
269}\r
270\r