]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/LibC/Uefi/Devices/Utility/Path.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / Uefi / Devices / Utility / Path.c
index 92bb1a20b77ed936a89ec147dd44c549a0143f0f..96392e018daccb599d2d60b6fb7f9e9ea2d467f7 100644 (file)
@@ -248,10 +248,16 @@ PathAlias(
 \r
 /** Parse a path producing the target device, device instance, and file path.\r
 \r
+    It is the caller's responsibility to free() FullPath and MapPath when they\r
+    are no longer needed.\r
+\r
     @param[in]    path\r
     @param[out]   FullPath\r
     @param[out]   DevNode\r
     @param[out]   Which\r
+    @param[out]   MapPath       OPTIONAL.  If not NULL, it points to the place to save a pointer\r
+                                to the extracted map name.  If the path didn't have a map name,\r
+                                then *MapPath is set to NULL.\r
 \r
     @retval   RETURN_SUCCESS              The path was parsed successfully.\r
     @retval   RETURN_NOT_FOUND            The path does not map to a valid device.\r
@@ -266,7 +272,8 @@ ParsePath(
   IN    const char   *path,\r
   OUT   wchar_t     **FullPath,\r
   OUT   DeviceNode  **DevNode,\r
-  OUT   int          *Which\r
+  OUT   int          *Which,\r
+  OUT   wchar_t     **MapPath\r
   )\r
 {\r
   int                 MapLen;\r
@@ -301,7 +308,7 @@ reclassify:
           // Get the Map Name, including the trailing ':'. */\r
           MPath = calloc(MapLen+2, sizeof(wchar_t));\r
           if(MPath != NULL) {\r
-            wmemcpy(MPath, WPath, MapLen+2);\r
+            wmemcpy(MPath, WPath, MapLen+1);\r
           }\r
           else {\r
             errno = ENOMEM;\r
@@ -346,6 +353,12 @@ reclassify:
   if(!RETURN_ERROR(Status)) {\r
     *FullPath = WPath;\r
     *Which    = Instance;\r
+    if(MapPath != NULL) {\r
+      *MapPath  = MPath;\r
+    }\r
+    else if(MPath != NULL) {\r
+      free(MPath);    /* Caller doesn't want it so let MPath go free */\r
+    }\r
 \r
     /*  At this point, WPath is an absolute path,\r
         MPath is either NULL or points to the Map Name,\r
@@ -359,6 +372,9 @@ reclassify:
       if(Node != NULL) {\r
         Status = RETURN_SUCCESS;\r
       }\r
+      else {\r
+        Status = RETURN_NOT_FOUND;\r
+      }\r
     }\r
     else {\r
       /* This is a mapped path. */\r
@@ -375,8 +391,41 @@ reclassify:
       *DevNode = Node;\r
     }\r
   }\r
-  if(MPath != NULL) {\r
-    free(MPath);    // We don't need this any more.\r
-  }\r
   return Status;\r
 }\r
+\r
+/**\r
+  Parses a normalized wide character path and returns a pointer to the entry\r
+  following the last \.  If a \ is not found in the path the return value will\r
+  be the same as the input value.  All error conditions return NULL.\r
+\r
+  The behavior when passing in a path that has not been normalized is undefined.\r
+\r
+  @param  Path - A pointer to a wide character string containing a path to a\r
+                 directory or a file.\r
+\r
+  @return Pointer to the file name or terminal directory.  NULL if an error is\r
+          detected.\r
+**/\r
+wchar_t *\r
+EFIAPI\r
+GetFileNameFromPath (\r
+  const wchar_t   *Path\r
+  )\r
+{\r
+  wchar_t   *Tail;\r
+\r
+  if (Path == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  Tail = wcsrchr(Path, L'\\');\r
+  if(Tail == NULL) {\r
+    Tail = (wchar_t *) Path;\r
+  } else {\r
+    // Move to the next character after the '\\' to get the file name.\r
+    Tail++;\r
+  }\r
+\r
+  return Tail;\r
+}\r