RTPathAppend.cpp revision fe02cc356f7bd01a7ffbcd6ceb8ca09bb1c35330
/* $Id$ */
/** @file
* IPRT - RTPathAppend
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/**
* Figures the length of the root part of the path.
*
* @returns length of the root specifier.
* @retval 0 if none.
*
* @param pszPath The path to investigate.
*
* @remarks Unnecessary root slashes will not be counted. The caller will have
* to deal with it where it matters.
*/
{
/* fend of wildlife. */
if (!pszPath)
return 0;
/* Root slash? */
if (RTPATH_IS_SLASH(pszPath[0]))
{
#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
/* UNC? */
{
/* Find the end of the server name. */
pszEnd += 2;
while ( *pszEnd != '\0'
&& !RTPATH_IS_SLASH(*pszEnd))
pszEnd++;
if (RTPATH_IS_SLASH(*pszEnd))
{
pszEnd++;
while (RTPATH_IS_SLASH(*pszEnd))
pszEnd++;
/* Find the end of the share name */
while ( *pszEnd != '\0'
&& !RTPATH_IS_SLASH(*pszEnd))
pszEnd++;
if (RTPATH_IS_SLASH(*pszEnd))
pszEnd++;
}
}
#endif
return 1;
}
#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
/* Drive specifier? */
if ( pszPath[0] != '\0'
&& RT_C_IS_ALPHA(pszPath[0]))
{
return 3;
return 2;
}
#endif
return 0;
}
{
/*
* Special cases.
*/
if (!pszAppend)
return VINF_SUCCESS;
if (!cchAppend)
return VINF_SUCCESS;
if (pszPathEnd == pszPath)
{
return VERR_BUFFER_OVERFLOW;
return VINF_SUCCESS;
}
/*
* Balance slashes and check for buffer overflow.
*/
bool fAddSlash = false;
{
if (!RTPATH_IS_SLASH(pszAppend[0]))
{
#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
&& RT_C_IS_ALPHA(pszPath[0]))
{
return VERR_BUFFER_OVERFLOW;
}
else
#endif
{
return VERR_BUFFER_OVERFLOW;
*pszPathEnd++ = '/';
}
}
else
{
/* One slash is sufficient at this point. */
return VERR_BUFFER_OVERFLOW;
}
}
else
{
/* No slashes needed in the appended bit. */
while (RTPATH_IS_SLASH(*pszAppend))
/* In the leading path we can skip unnecessary trailing slashes, but
be sure to leave one. */
pszPathEnd--;
return VERR_BUFFER_OVERFLOW;
}
/*
* What remains now is the just the copying.
*/
return VINF_SUCCESS;
}