/** @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
\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