VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/pathhost-posix.cpp@ 37563

Last change on this file since 37563 was 37563, checked in by vboxsync, 13 years ago

Runtime/pathhost-posix.cpp: missing include

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1/* $Id: pathhost-posix.cpp 37563 2011-06-20 19:47:47Z vboxsync $ */
2/** @file
3 * IPRT - Path Conversions, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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#define LOG_GROUP RTLOGGROUP_PATH
32#include "internal/iprt.h"
33#include "internal/path.h"
34#include "internal/string.h"
35#include "internal/thread.h"
36
37#include <iprt/env.h>
38#include <iprt/string.h>
39#include <iprt/once.h>
40
41
42/*******************************************************************************
43* Global Variables *
44*******************************************************************************/
45/** Initialize once object. */
46static RTONCE g_OnceInitPathConv = RTONCE_INITIALIZER;
47/** If set, then we can pass UTF-8 thru directly. */
48static bool g_fPassthruUtf8 = false;
49/** The UTF-8 to FS iconv cache entry. */
50static RTSTRICONV g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_LOCALE;
51/** The FS to UTF-8 iconv cache entry. */
52static RTSTRICONV g_enmFsToUtf8Idx = RTSTRICONV_LOCALE_TO_UTF8;
53/** The codeset we're using. */
54static char g_szFsCodeset[32];
55
56
57/**
58 * Do a case insensitive compare where the 2nd string is known and can be case
59 * folded when writing the code.
60 *
61 * @returns see strcmp.
62 * @param pszStr1 The string to compare against pszLower and
63 * pszUpper.
64 * @param pszUpper The upper case edition of the 2nd string.
65 * @param pszLower The lower case edition of the 2nd string.
66 */
67static int rtPathStrICmp(const char *pszStr1, const char *pszUpper, const char *pszLower)
68{
69 Assert(strlen(pszLower) == strlen(pszUpper));
70 for (;;)
71 {
72 char ch1 = *pszStr1++;
73 char ch2Upper = *pszUpper++;
74 char ch2Lower = *pszLower++;
75 if ( ch1 != ch2Upper
76 && ch1 != ch2Lower)
77 return ch1 < ch2Upper ? -1 : 1;
78 if (!ch1)
79 return 0;
80 }
81}
82
83/**
84 * Is the specified codeset something we can treat as UTF-8.
85 *
86 * @returns true if we can do UTF-8 passthru, false if not.
87 * @param pszCodeset The codeset in question.
88 */
89static bool rtPathConvInitIsUtf8(const char *pszCodeset)
90{
91 /* Paranoia. */
92 if (!pszCodeset)
93 return false;
94
95 /*
96 * Avoid RTStrICmp at this point.
97 */
98 static struct
99 {
100 const char *pszUpper;
101 const char *pszLower;
102 } const s_aUtf8Compatible[] =
103 {
104 /* The default locale. */
105 { "C" , "c" },
106 { "POSIX" , "posix" },
107 /* 7-bit ASCII. */
108 { "ANSI_X3.4-1968" , "ansi_x3.4-1968" },
109 { "ANSI_X3.4-1986" , "ansi_x3.4-1986" },
110 { "US-ASCII" , "us-ascii" },
111 { "ISO646-US" , "iso646-us" },
112 { "ISO_646.IRV:1991" , "iso_646.irv:1991" },
113 { "ISO-IR-6" , "iso-ir-6" },
114 { "IBM367" , "ibm367" },
115 /* UTF-8 */
116 { "UTF-8" , "utf-8" },
117 { "UTF8" , "utf8" },
118 { "ISO-10646/UTF-8" , "iso-10646/utf-8" },
119 { "ISO-10646/UTF8" , "iso-10646/utf8" }
120 };
121
122 for (size_t i = 0; i < RT_ELEMENTS(s_aUtf8Compatible); i++)
123 if (!rtPathStrICmp(pszCodeset, s_aUtf8Compatible[i].pszUpper, s_aUtf8Compatible[i].pszLower))
124 return true;
125
126 return false;
127}
128
129
130/**
131 * Init once for the path conversion code.
132 *
133 * @returns IPRT status code.
134 * @param pvUser1 Unused.
135 * @param pvUser2 Unused.
136 */
137static DECLCALLBACK(int32_t) rtPathConvInitOnce(void *pvUser1, void *pvUser2)
138{
139 /*
140 * Read the environment variable, no mercy on misconfigs here except that
141 * empty values are quietly ignored. (We use a temp buffer for stripping.)
142 */
143 char *pszEnvValue = NULL;
144 char szEnvValue[sizeof(g_szFsCodeset)];
145 int rc = RTEnvGetEx(RTENV_DEFAULT, RTPATH_CODESET_ENV_VAR, szEnvValue, sizeof(szEnvValue), NULL);
146 if (rc != VERR_ENV_VAR_NOT_FOUND && RT_FAILURE(rc))
147 return rc;
148 if (RT_SUCCESS(rc))
149 pszEnvValue = RTStrStrip(szEnvValue);
150
151 if (pszEnvValue && *pszEnvValue)
152 {
153 g_fPassthruUtf8 = rtPathConvInitIsUtf8(pszEnvValue);
154 g_enmFsToUtf8Idx = RTSTRICONV_FS_TO_UTF8;
155 g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_FS;
156 strcpy(g_szFsCodeset, pszEnvValue);
157 }
158 else
159 {
160 const char *pszCodeset = rtStrGetLocaleCodeset();
161 size_t cchCodeset = pszCodeset ? strlen(pszCodeset) : sizeof(g_szFsCodeset);
162 if (cchCodeset >= sizeof(g_szFsCodeset))
163 /* This shouldn't happen, but we'll manage. */
164 g_szFsCodeset[0] = '\0';
165 else
166 {
167 memcpy(g_szFsCodeset, pszCodeset, cchCodeset + 1);
168 pszCodeset = g_szFsCodeset;
169 }
170 g_fPassthruUtf8 = rtPathConvInitIsUtf8(pszCodeset);
171 g_enmFsToUtf8Idx = RTSTRICONV_LOCALE_TO_UTF8;
172 g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_LOCALE;
173 }
174
175 NOREF(pvUser1); NOREF(pvUser2);
176 return VINF_SUCCESS;
177}
178
179
180int rtPathToNative(char const **ppszNativePath, const char *pszPath, const char *pszBasePath)
181{
182 *ppszNativePath = NULL;
183
184 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
185 if (RT_SUCCESS(rc))
186 {
187 if (g_fPassthruUtf8 || !*pszPath)
188 *ppszNativePath = pszPath;
189 else
190 rc = rtStrConvert(pszPath, strlen(pszPath), "UTF-8",
191 (char **)ppszNativePath, 0, g_szFsCodeset,
192 2, g_enmUtf8ToFsIdx);
193 }
194 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
195 return rc;
196}
197
198
199void rtPathFreeNative(char const *pszNativePath, const char *pszPath)
200{
201 if ( pszNativePath != pszPath
202 && pszNativePath)
203 RTStrFree((char *)pszNativePath);
204}
205
206
207int rtPathFromNative(const char **ppszPath, const char *pszNativePath, const char *pszBasePath)
208{
209 *ppszPath = NULL;
210
211 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
212 if (RT_SUCCESS(rc))
213 {
214 if (g_fPassthruUtf8 || !*pszNativePath)
215 {
216 size_t cCpsIgnored;
217 size_t cchNativePath;
218 rc = rtUtf8Length(pszNativePath, RTSTR_MAX, &cCpsIgnored, &cchNativePath);
219 if (RT_SUCCESS(rc))
220 {
221 char *pszPath;
222 *ppszPath = pszPath = RTStrAlloc(cchNativePath + 1);
223 if (pszPath)
224 memcpy(pszPath, pszNativePath, cchNativePath + 1);
225 else
226 rc = VERR_NO_STR_MEMORY;
227 }
228 }
229 else
230 rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
231 (char **)ppszPath, 0, "UTF-8",
232 2, g_enmFsToUtf8Idx);
233 }
234 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
235 return rc;
236}
237
238
239void rtPathFreeIprt(const char *pszPath, const char *pszNativePath)
240{
241 if ( pszPath != pszNativePath
242 && pszPath)
243 RTStrFree((char *)pszPath);
244}
245
246
247int rtPathFromNativeCopy(char *pszPath, size_t cbPath, const char *pszNativePath, const char *pszBasePath)
248{
249 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
250 if (RT_SUCCESS(rc))
251 {
252 if (g_fPassthruUtf8 || !*pszNativePath)
253 rc = RTStrCopy(pszPath, cbPath, pszNativePath);
254 else if (cbPath)
255 rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
256 &pszPath, cbPath, "UTF-8",
257 2, g_enmFsToUtf8Idx);
258 else
259 rc = VERR_BUFFER_OVERFLOW;
260 }
261
262 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
263 return rc;
264}
265
266
267int rtPathFromNativeDup(char **ppszPath, const char *pszNativePath, const char *pszBasePath)
268{
269 int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL, NULL);
270 if (RT_SUCCESS(rc))
271 {
272 if (g_fPassthruUtf8 || !*pszNativePath)
273 rc = RTStrDupEx(ppszPath, pszNativePath);
274 else
275 rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
276 ppszPath, 0, "UTF-8",
277 2, g_enmFsToUtf8Idx);
278 }
279
280 NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
281 return rc;
282}
283
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette