]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
Add code to check whether the pointer 'CorrectedPath' and 'FullPath' are NULL before...
[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
668a5781 4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
a405b86d 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
13**/\r
14\r
15#include "UefiShellLevel2CommandsLib.h"\r
16\r
17/**\r
18 Function for 'cd' command.\r
19\r
20 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
21 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
22**/\r
23SHELL_STATUS\r
24EFIAPI\r
25ShellCommandRunCd (\r
26 IN EFI_HANDLE ImageHandle,\r
27 IN EFI_SYSTEM_TABLE *SystemTable\r
28 )\r
29{\r
30 EFI_STATUS Status;\r
31 LIST_ENTRY *Package;\r
32 CONST CHAR16 *Directory;\r
33 CHAR16 *Path;\r
34 CHAR16 *Drive;\r
35 UINTN DriveSize;\r
36 CHAR16 *ProblemParam;\r
37 SHELL_STATUS ShellStatus;\r
38 SHELL_FILE_HANDLE Handle;\r
39 CONST CHAR16 *Param1;\r
1fc3749d 40 CHAR16 *Param1Copy;\r
a405b86d 41\r
42 ProblemParam = NULL;\r
43 ShellStatus = SHELL_SUCCESS;\r
44 Drive = NULL;\r
45 DriveSize = 0;\r
46\r
47 Status = CommandInit();\r
48 ASSERT_EFI_ERROR(Status);\r
49\r
50 //\r
51 // initialize the shell lib (we must be in non-auto-init...)\r
52 //\r
53 Status = ShellInitialize();\r
54 ASSERT_EFI_ERROR(Status);\r
55\r
56 //\r
57 // parse the command line\r
58 //\r
59 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r
60 if (EFI_ERROR(Status)) {\r
61 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
62 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
63 FreePool(ProblemParam);\r
64 ShellStatus = SHELL_INVALID_PARAMETER;\r
65 } else {\r
66 ASSERT(FALSE);\r
67 }\r
68 }\r
69\r
70 //\r
71 // check for "-?"\r
72 //\r
73 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
74 ASSERT(FALSE);\r
75 } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {\r
76 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);\r
77 ShellStatus = SHELL_INVALID_PARAMETER;\r
78 } else {\r
79 //\r
80 // remember that param 0 is the command name\r
81 // If there are 0 value parameters, then print the current directory\r
82 // else If there are 2 value parameters, then print the error message\r
83 // else If there is 1 value paramerer , then change the directory\r
84 //\r
85 Param1 = ShellCommandLineGetRawValue(Package, 1);\r
86 if (Param1 == NULL) {\r
87 //\r
88 // display the current directory\r
89 //\r
90 Directory = ShellGetCurrentDir(NULL);\r
91 if (Directory != NULL) {\r
92 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Directory);\r
93 } else {\r
94 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
95 ShellStatus = SHELL_NOT_FOUND;\r
96 }\r
97 } else {\r
1fc3749d 98 Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r
beab0fc5 99 if (Param1Copy != NULL) {\r
100 Param1Copy = PathCleanUpDirectories(Param1Copy);\r
101 }\r
102 if (Param1Copy != NULL) {\r
103 if (StrCmp(Param1Copy, L".") == 0) {\r
a405b86d 104 //\r
beab0fc5 105 // nothing to do... change to current directory\r
a405b86d 106 //\r
beab0fc5 107 } else if (StrCmp(Param1Copy, L"..") == 0) {\r
a405b86d 108 //\r
beab0fc5 109 // Change up one directory...\r
a405b86d 110 //\r
beab0fc5 111 Directory = ShellGetCurrentDir(NULL);\r
112 if (Directory == NULL) {\r
113 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
a405b86d 114 ShellStatus = SHELL_NOT_FOUND;\r
a405b86d 115 } else {\r
beab0fc5 116 Drive = GetFullyQualifiedPath(Directory);\r
117 PathRemoveLastItem(Drive);\r
a405b86d 118 }\r
beab0fc5 119 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
120 //\r
121 // change directory on current drive letter\r
122 //\r
123 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
124 if (Status == EFI_NOT_FOUND) {\r
125 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
126 ShellStatus = SHELL_NOT_FOUND;\r
127 }\r
128 }\r
129 } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
a405b86d 130 //\r
beab0fc5 131 // Move to root of current drive\r
a405b86d 132 //\r
beab0fc5 133 Directory = ShellGetCurrentDir(NULL);\r
134 if (Directory == NULL) {\r
135 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
a405b86d 136 ShellStatus = SHELL_NOT_FOUND;\r
beab0fc5 137 } else {\r
138 Drive = GetFullyQualifiedPath(Directory);\r
139 while (PathRemoveLastItem(Drive)) ;\r
a405b86d 140 }\r
141 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
142 //\r
143 // change directory on current drive letter\r
144 //\r
145 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
146 if (Status == EFI_NOT_FOUND) {\r
147 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
148 ShellStatus = SHELL_NOT_FOUND;\r
149 }\r
150 }\r
beab0fc5 151 } else if (StrStr(Param1Copy, L":") == NULL) {\r
668a5781 152 //\r
153 // change directory without a drive identifier\r
154 //\r
beab0fc5 155 if (ShellGetCurrentDir(NULL) == NULL) {\r
156 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
157 ShellStatus = SHELL_NOT_FOUND;\r
158 } else {\r
159 ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
160 Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
161 if (*Param1Copy == L'\\') {\r
162 while (PathRemoveLastItem(Drive)) ;\r
163 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
164 } else {\r
165 Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
166 }\r
167 //\r
168 // Verify that this is a valid directory\r
169 //\r
170 Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
171 if (EFI_ERROR(Status)) {\r
172 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
173 ShellStatus = SHELL_NOT_FOUND;\r
174 } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
175 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
176 ShellStatus = SHELL_NOT_FOUND;\r
177 }\r
178 if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
179 //\r
180 // change directory on current drive letter\r
181 //\r
182 Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
183 if (Status == EFI_NOT_FOUND) {\r
184 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
185 ShellStatus = SHELL_NOT_FOUND;\r
186 }\r
187 }\r
188 if (Handle != NULL) {\r
189 gEfiShellProtocol->CloseFile(Handle);\r
190 DEBUG_CODE(Handle = NULL;);\r
191 }\r
a405b86d 192 }\r
9ea69f8a 193 } else {\r
beab0fc5 194 //\r
668a5781 195 // change directory with a drive letter\r
beab0fc5 196 //\r
197 Drive = AllocateZeroPool(StrSize(Param1Copy));\r
198 if (Drive == NULL) {\r
199 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
200 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
9ea69f8a 201 } else {\r
beab0fc5 202 Drive = StrCpy(Drive, Param1Copy);\r
203 Path = StrStr(Drive, L":");\r
204 ASSERT(Path != NULL);\r
668a5781 205 if (EFI_ERROR(ShellIsDirectory(Param1Copy))) {\r
206 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Param1Copy);\r
207 ShellStatus = SHELL_NOT_FOUND;\r
208 } else if (*(Path+1) == CHAR_NULL) {\r
b54fd049 209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
210 ShellStatus = SHELL_NOT_FOUND;\r
211 } else {\r
beab0fc5 212 *(Path+1) = CHAR_NULL;\r
213 if (Path == Drive + StrLen(Drive)) {\r
214 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
215 ShellStatus = SHELL_NOT_FOUND;\r
216 } else {\r
217 Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
218 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
219 }\r
220 }\r
221 if (Status == EFI_NOT_FOUND) {\r
222 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
223 Status = SHELL_NOT_FOUND;\r
224 } else if (EFI_ERROR(Status)) {\r
225 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1Copy);\r
226 Status = SHELL_NOT_FOUND;\r
b54fd049 227 }\r
9ea69f8a 228 }\r
a405b86d 229 }\r
230 }\r
1fc3749d 231 FreePool(Param1Copy);\r
a405b86d 232 }\r
233 }\r
234\r
235 if (Drive != NULL) {\r
236 FreePool(Drive);\r
237 }\r
238 //\r
239 // free the command line package\r
240 //\r
241 ShellCommandLineFreeVarList (Package);\r
242\r
243 //\r
244 // return the status\r
245 //\r
246 return (ShellStatus);\r
247}\r
248\r