]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/PosixLib/Gen/dirname.c
StdLib: Add isDirSep character classification macro and function. Implement several...
[mirror_edk2.git] / StdLib / PosixLib / Gen / dirname.c
index 7fb4d39a0f947c00463da62082b0f37009e65383..eb924d435c26c86f8bca942cbde80dd0cbdc1497 100644 (file)
@@ -1,5 +1,14 @@
 /** @file\r
 \r
+  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials are licensed and made available under\r
+  the terms and conditions of the BSD License that accompanies this distribution.\r
+  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
  * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.\r
  * All rights reserved.\r
  *\r
     NetBSD: dirname.c,v 1.10 2008/05/10 22:39:40 christos Exp\r
  */\r
 #include  <LibConfig.h>\r
-\r
 #include <sys/cdefs.h>\r
 \r
-//#include "namespace.h"\r
-//#include <libgen.h>\r
 #include <limits.h>\r
+#include  <ctype.h>\r
 #include <string.h>\r
 \r
 #ifdef __weak_alias\r
 __weak_alias(dirname,_dirname)\r
 #endif\r
 \r
-#if !HAVE_DIRNAME\r
+#ifdef HAVE_DIRNAME\r
 char *\r
 dirname(char *path)\r
 {\r
@@ -60,24 +67,34 @@ dirname(char *path)
 \r
   /* Strip trailing slashes, if any. */\r
   lastp = path + strlen(path) - 1;\r
-  while (lastp != path && *lastp == '/')\r
+  while (lastp != path && isDirSep(*lastp))\r
     lastp--;\r
 \r
   /* Terminate path at the last occurence of '/'. */\r
   do {\r
-    if (*lastp == '/') {\r
+    if (isDirSep(*lastp)) {\r
       /* Strip trailing slashes, if any. */\r
-      while (lastp != path && *lastp == '/')\r
+      while (lastp != path && isDirSep(*lastp))\r
         lastp--;\r
 \r
-      /* ...and copy the result into the result buffer. */\r
+      /* ...and copy the result into the result buffer.\r
+        We make sure that there will be room for the terminating NUL\r
+        and for a final '/', if necessary.\r
+      */\r
       len = (lastp - path) + 1 /* last char */;\r
-      if (len > (PATH_MAX - 1))\r
-        len = PATH_MAX - 1;\r
+      if (len > (PATH_MAX - 2))\r
+        len = PATH_MAX - 2;\r
 \r
       memcpy(result, path, len);\r
+      if(*lastp == ':') {   /* Have we stripped off all except the Volume name? */\r
+        if(isDirSep(lastp[1])) { /* Was ...":/"... so we want the root of the volume. */\r
+          result[len++] = '/';\r
+        }\r
+        else {\r
+          result[len++] = '.';\r
+        }\r
+      }\r
       result[len] = '\0';\r
-\r
       return (result);\r
     }\r
   } while (--lastp >= path);\r