1 | /* $Id: RTPathAppendEx.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathAppendEx
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/path.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/ctype.h>
|
---|
36 | #include <iprt/errcore.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Figures the length of the root part of the path.
|
---|
42 | *
|
---|
43 | * @returns length of the root specifier.
|
---|
44 | * @retval 0 if none.
|
---|
45 | *
|
---|
46 | * @param pszPath The path to investigate.
|
---|
47 | *
|
---|
48 | * @remarks Unnecessary root slashes will not be counted. The caller will have
|
---|
49 | * to deal with it where it matters. (Unlike rtPathRootSpecLen which
|
---|
50 | * counts them.)
|
---|
51 | */
|
---|
52 | static size_t rtPathRootSpecLen2(const char *pszPath)
|
---|
53 | {
|
---|
54 | /* fend of wildlife. */
|
---|
55 | if (!pszPath)
|
---|
56 | return 0;
|
---|
57 |
|
---|
58 | /* Root slash? */
|
---|
59 | if (RTPATH_IS_SLASH(pszPath[0]))
|
---|
60 | {
|
---|
61 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
62 | /* UNC? */
|
---|
63 | if ( RTPATH_IS_SLASH(pszPath[1])
|
---|
64 | && pszPath[2] != '\0'
|
---|
65 | && !RTPATH_IS_SLASH(pszPath[2]))
|
---|
66 | {
|
---|
67 | /* Find the end of the server name. */
|
---|
68 | const char *pszEnd = pszPath + 2;
|
---|
69 | pszEnd += 2;
|
---|
70 | while ( *pszEnd != '\0'
|
---|
71 | && !RTPATH_IS_SLASH(*pszEnd))
|
---|
72 | pszEnd++;
|
---|
73 | if (RTPATH_IS_SLASH(*pszEnd))
|
---|
74 | {
|
---|
75 | pszEnd++;
|
---|
76 | while (RTPATH_IS_SLASH(*pszEnd))
|
---|
77 | pszEnd++;
|
---|
78 |
|
---|
79 | /* Find the end of the share name */
|
---|
80 | while ( *pszEnd != '\0'
|
---|
81 | && !RTPATH_IS_SLASH(*pszEnd))
|
---|
82 | pszEnd++;
|
---|
83 | if (RTPATH_IS_SLASH(*pszEnd))
|
---|
84 | pszEnd++;
|
---|
85 | return pszPath - pszEnd;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | #endif
|
---|
89 | return 1;
|
---|
90 | }
|
---|
91 |
|
---|
92 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
93 | /* Drive specifier? */
|
---|
94 | if ( pszPath[0] != '\0'
|
---|
95 | && pszPath[1] == ':'
|
---|
96 | && RT_C_IS_ALPHA(pszPath[0]))
|
---|
97 | {
|
---|
98 | if (RTPATH_IS_SLASH(pszPath[2]))
|
---|
99 | return 3;
|
---|
100 | return 2;
|
---|
101 | }
|
---|
102 | #endif
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(int) RTPathAppendEx(char *pszPath, size_t cbPathDst, const char *pszAppend, size_t cchAppendMax)
|
---|
108 | {
|
---|
109 | char *pszPathEnd = RTStrEnd(pszPath, cbPathDst);
|
---|
110 | AssertReturn(pszPathEnd, VERR_INVALID_PARAMETER);
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Special cases.
|
---|
114 | */
|
---|
115 | if (!pszAppend)
|
---|
116 | return VINF_SUCCESS;
|
---|
117 | size_t cchAppend = RTStrNLen(pszAppend, cchAppendMax);
|
---|
118 | if (!cchAppend)
|
---|
119 | return VINF_SUCCESS;
|
---|
120 | if (pszPathEnd == pszPath)
|
---|
121 | {
|
---|
122 | if (cchAppend >= cbPathDst)
|
---|
123 | return VERR_BUFFER_OVERFLOW;
|
---|
124 | memcpy(pszPath, pszAppend, cchAppend);
|
---|
125 | pszPath[cchAppend] = '\0';
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Balance slashes and check for buffer overflow.
|
---|
131 | */
|
---|
132 | if (!RTPATH_IS_SLASH(pszPathEnd[-1]))
|
---|
133 | {
|
---|
134 | if (!RTPATH_IS_SLASH(pszAppend[0]))
|
---|
135 | {
|
---|
136 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
137 | if ( (size_t)(pszPathEnd - pszPath) == 2
|
---|
138 | && pszPath[1] == ':'
|
---|
139 | && RT_C_IS_ALPHA(pszPath[0]))
|
---|
140 | {
|
---|
141 | if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
|
---|
142 | return VERR_BUFFER_OVERFLOW;
|
---|
143 | }
|
---|
144 | else
|
---|
145 | #endif
|
---|
146 | {
|
---|
147 | if ((size_t)(pszPathEnd - pszPath) + 1 + cchAppend >= cbPathDst)
|
---|
148 | return VERR_BUFFER_OVERFLOW;
|
---|
149 | *pszPathEnd++ = RTPATH_SLASH;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | /* One slash is sufficient at this point. */
|
---|
155 | while (cchAppend > 1 && RTPATH_IS_SLASH(pszAppend[1]))
|
---|
156 | pszAppend++, cchAppend--;
|
---|
157 |
|
---|
158 | if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
|
---|
159 | return VERR_BUFFER_OVERFLOW;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | /* No slashes needed in the appended bit. */
|
---|
165 | while (cchAppend && RTPATH_IS_SLASH(*pszAppend))
|
---|
166 | pszAppend++, cchAppend--;
|
---|
167 |
|
---|
168 | /* In the leading path we can skip unnecessary trailing slashes, but
|
---|
169 | be sure to leave one. */
|
---|
170 | size_t const cchRoot = rtPathRootSpecLen2(pszPath);
|
---|
171 | while ( (size_t)(pszPathEnd - pszPath) > RT_MAX(1, cchRoot)
|
---|
172 | && RTPATH_IS_SLASH(pszPathEnd[-2]))
|
---|
173 | pszPathEnd--;
|
---|
174 |
|
---|
175 | if ((size_t)(pszPathEnd - pszPath) + cchAppend >= cbPathDst)
|
---|
176 | return VERR_BUFFER_OVERFLOW;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * What remains now is the just the copying.
|
---|
181 | */
|
---|
182 | memcpy(pszPathEnd, pszAppend, cchAppend);
|
---|
183 | pszPathEnd[cchAppend] = '\0';
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|