1 | /* $Id: rtPathRootSpecLen.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - rtPathRootSpecLen (internal).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/path.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/ctype.h>
|
---|
46 | #include "internal/path.h"
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Figures out the length of the root (or drive) specifier in @a pszPath.
|
---|
50 | *
|
---|
51 | * For UNC names, we consider the root specifier to include both the server and
|
---|
52 | * share names.
|
---|
53 | *
|
---|
54 | * @returns The length including all slashes. 0 if relative path.
|
---|
55 | *
|
---|
56 | * @param pszPath The path to examine.
|
---|
57 | */
|
---|
58 | DECLHIDDEN(size_t) rtPathRootSpecLen(const char *pszPath)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * If it's an absolute path, threat the root or volume specification as
|
---|
62 | * component 0. UNC is making this extra fun on OS/2 and Windows as usual.
|
---|
63 | */
|
---|
64 | size_t off = 0;
|
---|
65 | if (RTPATH_IS_SLASH(pszPath[0]))
|
---|
66 | {
|
---|
67 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
68 | if ( RTPATH_IS_SLASH(pszPath[1])
|
---|
69 | && !RTPATH_IS_SLASH(pszPath[2])
|
---|
70 | && pszPath[2])
|
---|
71 | {
|
---|
72 | /* UNC server name */
|
---|
73 | off = 2;
|
---|
74 | while (!RTPATH_IS_SLASH(pszPath[off]) && pszPath[off])
|
---|
75 | off++;
|
---|
76 | while (RTPATH_IS_SLASH(pszPath[off]))
|
---|
77 | off++;
|
---|
78 |
|
---|
79 | /* UNC share */
|
---|
80 | while (!RTPATH_IS_SLASH(pszPath[off]) && pszPath[off])
|
---|
81 | off++;
|
---|
82 | }
|
---|
83 | else
|
---|
84 | #endif
|
---|
85 | {
|
---|
86 | off = 1;
|
---|
87 | }
|
---|
88 | while (RTPATH_IS_SLASH(pszPath[off]))
|
---|
89 | off++;
|
---|
90 | }
|
---|
91 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
92 | else if (RT_C_IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
|
---|
93 | {
|
---|
94 | off = 2;
|
---|
95 | while (RTPATH_IS_SLASH(pszPath[off]))
|
---|
96 | off++;
|
---|
97 | }
|
---|
98 | #endif
|
---|
99 | Assert(!RTPATH_IS_SLASH(pszPath[off]));
|
---|
100 |
|
---|
101 | return off;
|
---|
102 | }
|
---|
103 |
|
---|