]> git.proxmox.com Git - mirror_edk2.git/blobdiff - 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
index bee4b0ae43989d8486fa5b3e8acced18b6a9034f..c0d3c920f492fb70f332c59a26a14b2022c6b37f 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Main file for attrib shell level 2 function.\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -37,6 +37,7 @@ ShellCommandRunCd (
   SHELL_STATUS      ShellStatus;\r
   SHELL_FILE_HANDLE Handle;\r
   CONST CHAR16      *Param1;\r
+  CHAR16            *Param1Copy;\r
 \r
   ProblemParam = NULL;\r
   ShellStatus = SHELL_SUCCESS;\r
@@ -94,77 +95,48 @@ ShellCommandRunCd (
         ShellStatus = SHELL_NOT_FOUND;\r
       }\r
     } else {\r
-      if (StrCmp(Param1, L".") == 0) {\r
-        //\r
-        // nothing to do... change to current directory\r
-        //\r
-      } else if (StrCmp(Param1, L"..") == 0) {\r
-        //\r
-        // Change up one directory...\r
-        //\r
-        Directory = ShellGetCurrentDir(NULL);\r
-        if (Directory == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_NOT_FOUND;\r
-        } else {\r
-          Drive = GetFullyQualifiedPath(Directory);\r
-          ChopLastSlash(Drive);\r
-        }\r
-        if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+      Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r
+      if (Param1Copy != NULL) {\r
+        Param1Copy = PathCleanUpDirectories(Param1Copy);\r
+      }\r
+      if (Param1Copy != NULL) {\r
+        if (StrCmp(Param1Copy, L".") == 0) {\r
           //\r
-          // change directory on current drive letter\r
+          // nothing to do... change to current directory\r
           //\r
-          Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
-          if (Status == EFI_NOT_FOUND) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
-            ShellStatus = SHELL_NOT_FOUND;\r
-          }\r
-        }\r
-      } else if (StrCmp(Param1, L"\\") == 0) {\r
-        //\r
-        // Move to root of current drive\r
-        //\r
-        Directory = ShellGetCurrentDir(NULL);\r
-        if (Directory == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_NOT_FOUND;\r
-        } else {\r
-          Drive = GetFullyQualifiedPath(Directory);\r
-          while (ChopLastSlash(Drive)) ;\r
-        }\r
-        if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+        } else if (StrCmp(Param1Copy, L"..") == 0) {\r
           //\r
-          // change directory on current drive letter\r
+          // Change up one directory...\r
           //\r
-          Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
-          if (Status == EFI_NOT_FOUND) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+          Directory = ShellGetCurrentDir(NULL);\r
+          if (Directory == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
             ShellStatus = SHELL_NOT_FOUND;\r
-          }\r
-        }\r
-      } else if (StrStr(Param1, L":") == NULL) {\r
-        if (ShellGetCurrentDir(NULL) == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_NOT_FOUND;\r
-        } else {\r
-          ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
-          Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
-          if (*Param1 == L'\\') {\r
-            while (ChopLastSlash(Drive)) ;\r
-            Drive = StrnCatGrow(&Drive, &DriveSize, Param1+1, 0);\r
           } else {\r
-            Drive = StrnCatGrow(&Drive, &DriveSize, Param1, 0);\r
+            Drive = GetFullyQualifiedPath(Directory);\r
+            PathRemoveLastItem(Drive);\r
           }\r
+          if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+            //\r
+            // change directory on current drive letter\r
+            //\r
+            Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
+            if (Status == EFI_NOT_FOUND) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            }\r
+          }\r
+        } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
           //\r
-          // Verify that this is a valid directory\r
+          // Move to root of current drive\r
           //\r
-          Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
-          if (EFI_ERROR(Status)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
-            ShellStatus = SHELL_NOT_FOUND;\r
-          } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
+          Directory = ShellGetCurrentDir(NULL);\r
+          if (Directory == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
             ShellStatus = SHELL_NOT_FOUND;\r
+          } else {\r
+            Drive = GetFullyQualifiedPath(Directory);\r
+            while (PathRemoveLastItem(Drive)) ;\r
           }\r
           if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
             //\r
@@ -176,45 +148,87 @@ ShellCommandRunCd (
               ShellStatus = SHELL_NOT_FOUND;\r
             }\r
           }\r
-          if (Handle != NULL) {\r
-            gEfiShellProtocol->CloseFile(Handle);\r
-            DEBUG_CODE(Handle = NULL;);\r
+        } else if (StrStr(Param1Copy, L":") == NULL) {\r
+          //\r
+          // change directory without a drive identifier\r
+          //\r
+          if (ShellGetCurrentDir(NULL) == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
+            ShellStatus = SHELL_NOT_FOUND;\r
+          } else {\r
+            ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
+            Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
+            if (*Param1Copy == L'\\') {\r
+              while (PathRemoveLastItem(Drive)) ;\r
+              Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
+            } else {\r
+              Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
+            }\r
+            //\r
+            // Verify that this is a valid directory\r
+            //\r
+            Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
+            if (EFI_ERROR(Status)) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            }\r
+            if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+              //\r
+              // change directory on current drive letter\r
+              //\r
+              Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
+              if (Status == EFI_NOT_FOUND) {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+                ShellStatus = SHELL_NOT_FOUND;\r
+              }\r
+            }\r
+            if (Handle != NULL) {\r
+              gEfiShellProtocol->CloseFile(Handle);\r
+              DEBUG_CODE(Handle = NULL;);\r
+            }\r
           }\r
-        }\r
-      } else {\r
-        //\r
-        // change directory on other drive letter\r
-        //\r
-        Drive = AllocateZeroPool(StrSize(Param1));\r
-        if (Drive == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_OUT_OF_RESOURCES;\r
         } else {\r
-          Drive = StrCpy(Drive, Param1);\r
-          Path = StrStr(Drive, L":");\r
-          ASSERT(Path != NULL);\r
-          if (*(Path+1) == CHAR_NULL) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
-            ShellStatus = SHELL_NOT_FOUND;\r
+          //\r
+          // change directory with a drive letter\r
+          //\r
+          Drive = AllocateZeroPool(StrSize(Param1Copy));\r
+          if (Drive == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
+            ShellStatus = SHELL_OUT_OF_RESOURCES;\r
           } else {\r
-            *(Path+1) = CHAR_NULL;\r
-            if (Path == Drive + StrLen(Drive)) {\r
+            Drive = StrCpy(Drive, Param1Copy);\r
+            Path = StrStr(Drive, L":");\r
+            ASSERT(Path != NULL);\r
+            if (EFI_ERROR(ShellIsDirectory(Param1Copy))) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Param1Copy);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            } else if (*(Path+1) == CHAR_NULL) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
               ShellStatus = SHELL_NOT_FOUND;\r
             } else {\r
-              Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
+              *(Path+1) = CHAR_NULL;\r
+              if (Path == Drive + StrLen(Drive)) {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+                ShellStatus = SHELL_NOT_FOUND;\r
+              } else {\r
+                Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
+              }\r
+            }\r
+            if (Status == EFI_NOT_FOUND) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+              Status = SHELL_NOT_FOUND;\r
+            } else if (EFI_ERROR(Status)) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1Copy);\r
+              Status = SHELL_NOT_FOUND;\r
             }\r
-          }\r
-          if (Status == EFI_NOT_FOUND) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
-            Status = SHELL_NOT_FOUND;\r
-          } else if (EFI_ERROR(Status)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1);\r
-            Status = SHELL_NOT_FOUND;\r
           }\r
         }\r
       }\r
+      FreePool(Param1Copy);\r
     }\r
   }\r
 \r