]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Ls.c
ShellPkg/Dp: Add null pointer check
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Ls.c
index 64ce4ae2c02b9e6a3130bfc8ad7681bb4e3d055a..7d2e15f5206b334ff79bedde2871f5f558335836 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Main file for ls shell level 2 function.\r
 \r
-  Copyright (c) 2013 Hewlett-Packard Development Company, L.P.\r
-  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
+  Copyright (c) 2009 - 2017, 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
 #include "UefiShellLevel2CommandsLib.h"\r
 #include <Guid/FileSystemInfo.h>\r
 \r
+UINTN     mDayOfMonth[] = {31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30};\r
+\r
 /**\r
   print out the standard format output volume entry.\r
 \r
   @param[in] TheList           a list of files from the volume.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 PrintSfoVolumeInfoTableEntry(\r
   IN CONST EFI_SHELL_FILE_INFO *TheList\r
   )\r
@@ -152,7 +153,6 @@ PrintSfoVolumeInfoTableEntry(
 \r
 **/\r
 VOID\r
-EFIAPI\r
 PrintFileInformation(\r
   IN CONST BOOLEAN              Sfo, \r
   IN CONST EFI_SHELL_FILE_INFO  *TheNode, \r
@@ -263,7 +263,6 @@ PrintFileInformation(
   @param[in] Path           String with starting path.\r
 **/\r
 VOID\r
-EFIAPI\r
 PrintNonSfoHeader(\r
   IN CONST CHAR16 *Path\r
   )\r
@@ -300,7 +299,6 @@ PrintNonSfoHeader(
   @param[in] Dirs             The number of directories.\r
 **/\r
 VOID\r
-EFIAPI\r
 PrintNonSfoFooter(\r
   IN UINT64                     Files, \r
   IN UINT64                     Size, \r
@@ -322,6 +320,96 @@ PrintNonSfoFooter(
    );\r
 }\r
 \r
+/**\r
+  Change the file time to local time based on the timezone.\r
+\r
+  @param[in] Time               The file time.\r
+  @param[in] LocalTimeZone      Local time zone.\r
+**/\r
+VOID\r
+FileTimeToLocalTime (\r
+  IN EFI_TIME             *Time,\r
+  IN INT16                LocalTimeZone\r
+  )\r
+{\r
+  INTN                    MinuteDiff;\r
+  INTN                    TempMinute;\r
+  INTN                    HourNumberOfTempMinute;\r
+  INTN                    TempHour;\r
+  INTN                    DayNumberOfTempHour;\r
+  INTN                    TempDay;\r
+  INTN                    MonthNumberOfTempDay;\r
+  INTN                    TempMonth;\r
+  INTN                    YearNumberOfTempMonth;\r
+  INTN                    MonthRecord;\r
+\r
+  ASSERT ((Time->TimeZone >= -1440) && (Time->TimeZone <=1440));\r
+  ASSERT ((LocalTimeZone >= -1440) && (LocalTimeZone <=1440));\r
+  ASSERT ((Time->Month >= 1) && (Time->Month <= 12));\r
+\r
+  if(Time->TimeZone == LocalTimeZone) {\r
+    //\r
+    //if the file timezone is equal to the local timezone, there is no need to adjust the file time.\r
+    //\r
+    return;\r
+  }\r
+\r
+  if((Time->Year % 4 == 0 && Time->Year / 100 != 0)||(Time->Year % 400 == 0)) {\r
+    //\r
+    // Day in February of leap year is 29.\r
+    //\r
+    mDayOfMonth[1] = 29;\r
+  }\r
+\r
+  MinuteDiff = Time->TimeZone - LocalTimeZone;\r
+  TempMinute = Time->Minute + MinuteDiff;\r
+\r
+  //\r
+  // Calculate Time->Minute\r
+  // TempHour will be used to calculate Time->Hour\r
+  //\r
+  HourNumberOfTempMinute = TempMinute / 60;\r
+  if(TempMinute < 0) {\r
+    HourNumberOfTempMinute --; \r
+  }\r
+  TempHour = Time->Hour + HourNumberOfTempMinute;\r
+  Time->Minute = (UINT8)(TempMinute - 60 * HourNumberOfTempMinute);\r
+\r
+  //\r
+  // Calculate Time->Hour\r
+  // TempDay will be used to calculate Time->Day\r
+  //\r
+  DayNumberOfTempHour = TempHour / 24 ;\r
+  if(TempHour < 0){\r
+    DayNumberOfTempHour--;\r
+  }\r
+  TempDay = Time->Day + DayNumberOfTempHour;\r
+  Time->Hour = (UINT8)(TempHour - 24 * DayNumberOfTempHour);\r
+\r
+  //\r
+  // Calculate Time->Day\r
+  // TempMonth will be used to calculate Time->Month\r
+  //\r
+  MonthNumberOfTempDay = (TempDay - 1) / (INTN)mDayOfMonth[Time->Month - 1];\r
+  MonthRecord = (INTN)(Time->Month) ;\r
+  if(TempDay - 1 < 0){\r
+    MonthNumberOfTempDay -- ;\r
+    MonthRecord -- ;\r
+  }\r
+  TempMonth = Time->Month + MonthNumberOfTempDay;\r
+  Time->Day = (UINT8)(TempDay - (INTN)mDayOfMonth[(MonthRecord - 1 + 12) % 12] * MonthNumberOfTempDay);\r
+\r
+  //\r
+  // Calculate Time->Month, Time->Year\r
+  //\r
+  YearNumberOfTempMonth = (TempMonth - 1) / 12;\r
+  if(TempMonth - 1 < 0){\r
+    YearNumberOfTempMonth --;\r
+  }\r
+  Time->Month = (UINT8)(TempMonth - 12 * (YearNumberOfTempMonth));\r
+  Time->Year = (UINT16)(Time->Year + YearNumberOfTempMonth);\r
+}\r
+\r
 /**\r
   print out the list of files and directories from the LS command\r
 \r
@@ -339,7 +427,6 @@ PrintNonSfoFooter(
   @retval SHELL_SUCCESS     the printing was sucessful.\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 PrintLsOutput(\r
   IN CONST BOOLEAN Rec,\r
   IN CONST UINT64  Attribs,\r
@@ -362,6 +449,7 @@ PrintLsOutput(
   CHAR16                *CorrectedPath;\r
   BOOLEAN               FoundOne;\r
   BOOLEAN               HeaderPrinted;\r
+  EFI_TIME              LocalTime;\r
 \r
   HeaderPrinted = FALSE;\r
   FileCount     = 0;\r
@@ -408,7 +496,34 @@ PrintLsOutput(
         ; !IsNull(&ListHead->Link, &Node->Link)\r
         ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&ListHead->Link, &Node->Link)\r
         ){\r
+      if (ShellGetExecutionBreakFlag ()) {\r
+        ShellStatus = SHELL_ABORTED;\r
+        break;\r
+      }\r
       ASSERT(Node != NULL);\r
+\r
+      //\r
+      // Change the file time to local time.\r
+      //\r
+      Status = gRT->GetTime(&LocalTime, NULL);\r
+      if (!EFI_ERROR (Status)) {\r
+        if ((Node->Info->CreateTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) &&\r
+            (Node->Info->CreateTime.Month >= 1 && Node->Info->CreateTime.Month <= 12)) {\r
+          //\r
+          // FileTimeToLocalTime () requires Month is in a valid range, other buffer out-of-band access happens.\r
+          //\r
+          FileTimeToLocalTime (&Node->Info->CreateTime, LocalTime.TimeZone);\r
+        }\r
+        if ((Node->Info->LastAccessTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) &&\r
+            (Node->Info->LastAccessTime.Month >= 1 && Node->Info->LastAccessTime.Month <= 12)) {\r
+          FileTimeToLocalTime (&Node->Info->LastAccessTime, LocalTime.TimeZone);\r
+        }\r
+        if ((Node->Info->ModificationTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) &&\r
+            (Node->Info->ModificationTime.Month >= 1 && Node->Info->ModificationTime.Month <= 12)) {\r
+          FileTimeToLocalTime (&Node->Info->ModificationTime, LocalTime.TimeZone);\r
+        }\r
+      }\r
+\r
       if (LongestPath < StrSize(Node->FullName)) {\r
         LongestPath = StrSize(Node->FullName);\r
       }\r
@@ -438,6 +553,7 @@ PrintLsOutput(
       }\r
 \r
       if (!Sfo && !HeaderPrinted) {\r
+        PathRemoveLastItem (CorrectedPath);\r
         PrintNonSfoHeader(CorrectedPath);\r
       }\r
       PrintFileInformation(Sfo, Node, &FileCount, &FileSize, &DirCount);\r
@@ -445,12 +561,12 @@ PrintLsOutput(
       HeaderPrinted = TRUE;\r
     }\r
 \r
-    if (!Sfo) {\r
+    if (!Sfo && ShellStatus != SHELL_ABORTED) {\r
       PrintNonSfoFooter(FileCount, FileSize, DirCount);\r
     }\r
   }\r
 \r
-  if (Rec) {\r
+  if (Rec && ShellStatus != SHELL_ABORTED) {\r
     //\r
     // Re-Open all the files under the starting path for directories that didnt necessarily match our file filter\r
     //\r
@@ -493,6 +609,13 @@ PrintLsOutput(
             &FoundOne,\r
             Count,\r
             TimeZone);\r
+            \r
+          //\r
+          // Since it's running recursively, we have to break immediately when returned SHELL_ABORTED\r
+          //\r
+          if (ShellStatus == SHELL_ABORTED) {\r
+            break;\r
+          }\r
         }\r
       }\r
     }\r
@@ -575,7 +698,7 @@ ShellCommandRunLs (
   Status = ShellCommandLineParse (LsParamList, &Package, &ProblemParam, TRUE);\r
   if (EFI_ERROR(Status)) {\r
     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"ls", ProblemParam);  \r
       FreePool(ProblemParam);\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
@@ -590,7 +713,7 @@ ShellCommandRunLs (
     }\r
 \r
     if (ShellCommandLineGetCount(Package) > 2) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"ls");  \r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
       //\r
@@ -628,7 +751,7 @@ ShellCommandRunLs (
               Count++;\r
               continue;\r
             default:\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ATTRIBUTE), gShellLevel2HiiHandle, ShellCommandLineGetValue(Package, L"-a"));\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ATTRIBUTE), gShellLevel2HiiHandle, L"ls", ShellCommandLineGetValue(Package, L"-a"));  \r
               ShellStatus = SHELL_INVALID_PARAMETER;\r
               break;\r
           } // switch\r
@@ -649,7 +772,7 @@ ShellCommandRunLs (
           CurDir = gEfiShellProtocol->GetCurDir(NULL);\r
           if (CurDir == NULL) {\r
             ShellStatus = SHELL_NOT_FOUND;\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"ls");  \r
           }\r
           //\r
           // Copy to the 2 strings for starting path and file search string\r
@@ -658,13 +781,15 @@ ShellCommandRunLs (
           ASSERT(FullPath == NULL);\r
           StrnCatGrow(&SearchString, NULL, L"*", 0);\r
           StrnCatGrow(&FullPath, NULL, CurDir, 0);\r
+          Size = FullPath != NULL? StrSize(FullPath) : 0;\r
+          StrnCatGrow(&FullPath, &Size, L"\\", 0);\r
         } else {\r
           if (StrStr(PathName, L":") == NULL && gEfiShellProtocol->GetCurDir(NULL) == NULL) {\r
             //\r
             // If we got something and it doesnt have a fully qualified path, then we needed to have a CWD.\r
             //\r
             ShellStatus = SHELL_NOT_FOUND;\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"ls");  \r
           } else {\r
             //\r
             // We got a valid fully qualified path or we have a CWD\r
@@ -676,6 +801,8 @@ ShellCommandRunLs (
                 ShellCommandLineFreeVarList (Package);\r
                 return SHELL_OUT_OF_RESOURCES;\r
               }\r
+              Size = FullPath != NULL? StrSize(FullPath) : 0;\r
+              StrnCatGrow(&FullPath, &Size, L"\\", 0);\r
             }\r
             StrnCatGrow(&FullPath, &Size, PathName, 0);\r
             if (FullPath == NULL) {\r
@@ -692,16 +819,20 @@ ShellCommandRunLs (
               //\r
               // must split off the search part that applies to files from the end of the directory part\r
               //\r
-              for (StrnCatGrow(&SearchString, NULL, PathName, 0)\r
-                ; SearchString != NULL && StrStr(SearchString, L"\\") != NULL\r
-                ; CopyMem(SearchString, StrStr(SearchString, L"\\") + 1, 1 + StrSize(StrStr(SearchString, L"\\") + 1))) ;\r
-              FullPath[StrLen(FullPath) - StrLen(SearchString)] = CHAR_NULL;\r
+              StrnCatGrow(&SearchString, NULL, FullPath, 0);\r
+              if (SearchString == NULL) {\r
+                FreePool (FullPath);\r
+                ShellCommandLineFreeVarList (Package);\r
+                return SHELL_OUT_OF_RESOURCES;\r
+              }\r
+              PathRemoveLastItem (FullPath);\r
+              CopyMem (SearchString, SearchString + StrLen (FullPath), StrSize (SearchString + StrLen (FullPath)));\r
             }\r
           }\r
         }\r
         Status = gRT->GetTime(&TheTime, NULL);\r
         if (EFI_ERROR(Status)) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"ls", L"gRT->GetTime", Status);  \r
           TheTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
         }\r
 \r
@@ -714,18 +845,18 @@ ShellCommandRunLs (
             SearchString,\r
             NULL,\r
             Count,\r
-            (INT16)(TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:TheTime.TimeZone)\r
+            TheTime.TimeZone\r
            );\r
           if (ShellStatus == SHELL_NOT_FOUND) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LS_FILE_NOT_FOUND), gShellLevel2HiiHandle);\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LS_FILE_NOT_FOUND), gShellLevel2HiiHandle, L"ls", FullPath);  \r
           } else if (ShellStatus == SHELL_INVALID_PARAMETER) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle);\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"ls", FullPath);  \r
           } else if (ShellStatus == SHELL_ABORTED) {\r
             //\r
             // Ignore aborting.\r
             //\r
           } else if (ShellStatus != SHELL_SUCCESS) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle);\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"ls", FullPath);  \r
           }\r
         }\r
       }\r