b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync/* $Id$ */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync/** @file
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * IPRT - rtPathRootSpecLen (internal).
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2010 Oracle Corporation
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * available from http://www.virtualbox.org. This file is free software;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * General Public License (GPL) as published by the Free Software
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * The contents of this file may alternatively be used under the terms
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * of the Common Development and Distribution License Version 1.0
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * CDDL are applicable instead of those of the GPL.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * You may elect to license modified versions of this file under the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * terms and conditions of either the GPL or the CDDL or both.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync/*******************************************************************************
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync* Header Files *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync*******************************************************************************/
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#include "internal/iprt.h"
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#include <iprt/path.h>
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#include <iprt/assert.h>
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#include <iprt/ctype.h>
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#include "internal/path.h"
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync/**
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Figures out the length of the root (or drive) specifier in @a pszPath.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * For UNC names, we consider the root specifier to include both the server and
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * share names.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @returns The length including all slashes. 0 if relative path.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param pszPath The path to examine.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsyncDECLHIDDEN(size_t) rtPathRootSpecLen(const char *pszPath)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync{
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /*
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * If it's an absolute path, threat the root or volume specification as
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * component 0. UNC is making this extra fun on OS/2 and Windows as usual.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync size_t off = 0;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync if (RTPATH_IS_SLASH(pszPath[0]))
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync {
889f8d29a8325ed741d1466074c5371910a1fd63vboxsync#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync if ( RTPATH_IS_SLASH(pszPath[1])
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync && !RTPATH_IS_SLASH(pszPath[2])
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync && pszPath[2])
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync {
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /* UNC server name */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off = 2;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync while (!RTPATH_IS_SLASH(pszPath[off]) && pszPath[off])
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off++;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync while (RTPATH_IS_SLASH(pszPath[off]))
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off++;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /* UNC share */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync while (!RTPATH_IS_SLASH(pszPath[off]) && pszPath[off])
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off++;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync }
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync else
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#endif
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync {
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off = 1;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync }
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync while (RTPATH_IS_SLASH(pszPath[off]))
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off++;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync }
889f8d29a8325ed741d1466074c5371910a1fd63vboxsync#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync else if (RT_C_IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync {
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off = 2;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync while (RTPATH_IS_SLASH(pszPath[off]))
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync off++;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync }
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync#endif
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync Assert(!RTPATH_IS_SLASH(pszPath[off]));
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync return off;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync}
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync