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