1 | /* $Id: RTPathAppendEx.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathAppendEx
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.alldomusa.eu.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 <iprt/errcore.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Figures the length of the root part of the path.
|
---|
52 | *
|
---|
53 | * @returns length of the root specifier.
|
---|
54 | * @retval 0 if none.
|
---|
55 | *
|
---|
56 | * @param pszPath The path to investigate.
|
---|
57 | *
|
---|
58 | * @remarks Unnecessary root slashes will not be counted. The caller will have
|
---|
59 | * to deal with it where it matters. (Unlike rtPathRootSpecLen which
|
---|
60 | * counts them.)
|
---|
61 | */
|
---|
62 | static size_t rtPathRootSpecLen2(const char *pszPath)
|
---|
63 | {
|
---|
64 | /* fend of wildlife. */
|
---|
65 | if (!pszPath)
|
---|
66 | return 0;
|
---|
67 |
|
---|
68 | /* Root slash? */
|
---|
69 | if (RTPATH_IS_SLASH(pszPath[0]))
|
---|
70 | {
|
---|
71 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
72 | /* UNC? */
|
---|
73 | if ( RTPATH_IS_SLASH(pszPath[1])
|
---|
74 | && pszPath[2] != '\0'
|
---|
75 | && !RTPATH_IS_SLASH(pszPath[2]))
|
---|
76 | {
|
---|
77 | /* Find the end of the server name. */
|
---|
78 | const char *pszEnd = pszPath + 2;
|
---|
79 | pszEnd += 2;
|
---|
80 | while ( *pszEnd != '\0'
|
---|
81 | && !RTPATH_IS_SLASH(*pszEnd))
|
---|
82 | pszEnd++;
|
---|
83 | if (RTPATH_IS_SLASH(*pszEnd))
|
---|
84 | {
|
---|
85 | pszEnd++;
|
---|
86 | while (RTPATH_IS_SLASH(*pszEnd))
|
---|
87 | pszEnd++;
|
---|
88 |
|
---|
89 | /* Find the end of the share name */
|
---|
90 | while ( *pszEnd != '\0'
|
---|
91 | && !RTPATH_IS_SLASH(*pszEnd))
|
---|
92 | pszEnd++;
|
---|
93 | if (RTPATH_IS_SLASH(*pszEnd))
|
---|
94 | pszEnd++;
|
---|
95 | return pszPath - pszEnd;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | #endif
|
---|
99 | return 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
103 | /* Drive specifier? */
|
---|
104 | if ( pszPath[0] != '\0'
|
---|
105 | && pszPath[1] == ':'
|
---|
106 | && RT_C_IS_ALPHA(pszPath[0]))
|
---|
107 | {
|
---|
108 | if (RTPATH_IS_SLASH(pszPath[2]))
|
---|
109 | return 3;
|
---|
110 | return 2;
|
---|
111 | }
|
---|
112 | #endif
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(int) RTPathAppendEx(char *pszPath, size_t cbPathDst, const char *pszAppend, size_t cchAppendMax)
|
---|
118 | {
|
---|
119 | char *pszPathEnd = RTStrEnd(pszPath, cbPathDst);
|
---|
120 | AssertReturn(pszPathEnd, VERR_INVALID_PARAMETER);
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Special cases.
|
---|
124 | */
|
---|
125 | if (!pszAppend)
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | size_t cchAppend = RTStrNLen(pszAppend, cchAppendMax);
|
---|
128 | if (!cchAppend)
|
---|
129 | return VINF_SUCCESS;
|
---|
130 | if (pszPathEnd == pszPath)
|
---|
131 | {
|
---|
132 | if (cchAppend >= cbPathDst)
|
---|
133 | return VERR_BUFFER_OVERFLOW;
|
---|
134 | memcpy(pszPath, pszAppend, cchAppend);
|
---|
135 | pszPath[cchAppend] = '\0';
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Balance slashes and check for buffer overflow.
|
---|
141 | */
|
---|
142 | if (!RTPATH_IS_SLASH(pszPathEnd[-1]))
|
---|
143 | {
|
---|
144 | if (!RTPATH_IS_SLASH(pszAppend[0]))
|
---|
145 | {
|
---|
146 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
147 | if ( (size_t)(pszPathEnd - pszPath) == 2
|
---|
148 | && pszPath[1] == ':'
|
---|
149 | && RT_C_IS_ALPHA(pszPath[0]))
|
---|
150 | {
|
---|
151 | if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
|
---|
152 | return VERR_BUFFER_OVERFLOW;
|
---|
153 | }
|
---|
154 | else
|
---|
155 | #endif
|
---|
156 | {
|
---|
157 | if ((size_t)(pszPathEnd - pszPath) + 1 + cchAppend >= cbPathDst)
|
---|
158 | return VERR_BUFFER_OVERFLOW;
|
---|
159 | *pszPathEnd++ = RTPATH_SLASH;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | /* One slash is sufficient at this point. */
|
---|
165 | while (cchAppend > 1 && RTPATH_IS_SLASH(pszAppend[1]))
|
---|
166 | pszAppend++, cchAppend--;
|
---|
167 |
|
---|
168 | if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
|
---|
169 | return VERR_BUFFER_OVERFLOW;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | /* No slashes needed in the appended bit. */
|
---|
175 | while (cchAppend && RTPATH_IS_SLASH(*pszAppend))
|
---|
176 | pszAppend++, cchAppend--;
|
---|
177 |
|
---|
178 | /* In the leading path we can skip unnecessary trailing slashes, but
|
---|
179 | be sure to leave one. */
|
---|
180 | size_t const cchRoot = rtPathRootSpecLen2(pszPath);
|
---|
181 | while ( (size_t)(pszPathEnd - pszPath) > RT_MAX(1, cchRoot)
|
---|
182 | && RTPATH_IS_SLASH(pszPathEnd[-2]))
|
---|
183 | pszPathEnd--;
|
---|
184 |
|
---|
185 | if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
|
---|
186 | return VERR_BUFFER_OVERFLOW;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * What remains now is the just the copying.
|
---|
191 | */
|
---|
192 | memcpy(pszPathEnd, pszAppend, cchAppend);
|
---|
193 | pszPathEnd[cchAppend] = '\0';
|
---|
194 | return VINF_SUCCESS;
|
---|
195 | }
|
---|
196 |
|
---|