RTPathAbsEx.cpp revision d21a3a052d34d3a0269e5b53108273ddad6987ea
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync/* $Id$ */
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync/** @file
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * IPRT - RTPathAbsEx
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync */
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync/*
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync *
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * available from http://www.virtualbox.org. This file is free software;
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * you can redistribute it and/or modify it under the terms of the GNU
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * General Public License (GPL) as published by the Free Software
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync *
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * The contents of this file may alternatively be used under the terms
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * of the Common Development and Distribution License Version 1.0
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * VirtualBox OSE distribution, in which case the provisions of the
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * CDDL are applicable instead of those of the GPL.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync *
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * You may elect to license modified versions of this file under the
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * terms and conditions of either the GPL or the CDDL or both.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync *
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * additional information or have any questions.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync */
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync/*******************************************************************************
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync* Header Files *
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync*******************************************************************************/
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync#include "internal/iprt.h"
fabda9ef896faeeadf670e09a9b11b711eecdcbfvboxsync#include <iprt/path.h>
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync#include <iprt/err.h>
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync#include <iprt/param.h>
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync#include <iprt/string.h>
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync#include "internal/path.h"
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync/**
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * Get the absolute path (no symlinks, no . or .. components), assuming the
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * given base path as the current directory. The resulting path doesn't have
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * to exist.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync *
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * @returns iprt status code.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * @param pszBase The base path to act like a current directory.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * When NULL, the actual cwd is used (i.e. the call
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * is equivalent to RTPathAbs(pszPath, ...).
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * @param pszPath The path to resolve.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync * @param pszAbsPath Where to store the absolute path.
fc85d12482b4d79dd2dfd0989b9730433c7a45cbvboxsync * @param cchAbsPath Size of the buffer.
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync */
412ad5bac323727b4073056113e1d8e0faf60db3vboxsyncRTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cchAbsPath)
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync{
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync if ( pszBase
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync && pszPath
fc85d12482b4d79dd2dfd0989b9730433c7a45cbvboxsync && !rtPathVolumeSpecLen(pszPath)
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync )
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync {
412ad5bac323727b4073056113e1d8e0faf60db3vboxsync#if defined(RT_OS_WINDOWS)
/* The format for very long paths is not supported. */
if ( RTPATH_IS_SLASH(pszBase[0])
&& RTPATH_IS_SLASH(pszBase[1])
&& pszBase[2] == '?'
&& RTPATH_IS_SLASH(pszBase[3])
)
return VERR_INVALID_NAME;
#endif
/** @todo there are a couple of things which isn't 100% correct, although the
* current code will have to do for now, no time to fix.
*
* 1) On Windows & OS/2 we confuse '/' with an abspath spec and will
* not necessarily resolve it on the right drive.
* 2) A trailing slash in the base might cause UNC names to be created.
*/
size_t const cchPath = strlen(pszPath);
char szTmpPath[RTPATH_MAX];
if (RTPATH_IS_SLASH(pszPath[0]))
{
/* join the disk name from base and the path (DOS systems only) */
size_t const cchVolSpec = rtPathVolumeSpecLen(pszBase);
if (cchVolSpec + cchPath + 1 > sizeof(szTmpPath))
return VERR_FILENAME_TOO_LONG;
memcpy(szTmpPath, pszBase, cchVolSpec);
memcpy(&szTmpPath[cchVolSpec], pszPath, cchPath + 1);
}
else
{
/* join the base path and the path */
size_t const cchBase = strlen(pszBase);
if (cchBase + 1 + cchPath + 1 > sizeof(szTmpPath))
return VERR_FILENAME_TOO_LONG;
memcpy(szTmpPath, pszBase, cchBase);
szTmpPath[cchBase] = RTPATH_DELIMITER;
memcpy(&szTmpPath[cchBase + 1], pszPath, cchPath + 1);
}
return RTPathAbs(szTmpPath, pszAbsPath, cchAbsPath);
}
/* Fallback to the non *Ex version */
return RTPathAbs(pszPath, pszAbsPath, cchAbsPath);
}