RTPathAppendEx.cpp revision c9a2ef54a0b1991dcd349bf214a622de1356fca3
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * IPRT - RTPathAppendEx
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * Copyright (C) 2009-2010 Oracle Corporation
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * available from http://www.virtualbox.org. This file is free software;
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * General Public License (GPL) as published by the Free Software
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * The contents of this file may alternatively be used under the terms
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * of the Common Development and Distribution License Version 1.0
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * VirtualBox OSE distribution, in which case the provisions of the
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * CDDL are applicable instead of those of the GPL.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * You may elect to license modified versions of this file under the
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * terms and conditions of either the GPL or the CDDL or both.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync/*******************************************************************************
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync* Header Files *
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync*******************************************************************************/
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * Figures the length of the root part of the path.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * @returns length of the root specifier.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * @retval 0 if none.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * @param pszPath The path to investigate.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * @remarks Unnecessary root slashes will not be counted. The caller will have
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * to deal with it where it matters. (Unlike rtPathRootSpecLen which
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * counts them.)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsyncstatic size_t rtPathRootSpecLen2(const char *pszPath)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* fend of wildlife. */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* Root slash? */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* Find the end of the server name. */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* Find the end of the share name */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* Drive specifier? */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsyncRTDECL(int) RTPathAppendEx(char *pszPath, size_t cbPathDst, const char *pszAppend, size_t cchAppendMax)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * Special cases.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync size_t cchAppend = RTStrNLen(pszAppend, cchAppendMax);
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * Balance slashes and check for buffer overflow.
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync bool fAddSlash = false;
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync if ((size_t)(pszPathEnd - pszPath) + 1 + cchAppend >= cbPathDst)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* One slash is sufficient at this point. */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync while (cchAppend > 1 && RTPATH_IS_SLASH(pszAppend[1]))
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* No slashes needed in the appended bit. */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync /* In the leading path we can skip unnecessary trailing slashes, but
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync be sure to leave one. */
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync size_t const cchRoot = rtPathRootSpecLen2(pszPath);
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync while ( (size_t)(pszPathEnd - pszPath) > RT_MAX(1, cchRoot)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
c9a2ef54a0b1991dcd349bf214a622de1356fca3vboxsync * What remains now is the just the copying.