vbox的更動 50408 路徑 trunk/src/VBox/Runtime/r3/win/env-win.cpp
- 時間撮記:
- 2014-2-11 上午02:21:39 (11 年 以前)
- 檔案:
-
- 複製 1 筆資料
圖例:
- 未更動
- 新增
- 刪除
-
trunk/src/VBox/Runtime/r3/win/env-win.cpp
r50388 r50408 5 5 6 6 /* 7 * Copyright (C) 2006-201 0Oracle Corporation7 * Copyright (C) 2006-2014 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 29 29 * Header Files * 30 30 *******************************************************************************/ 31 #ifdef RT_OS_DARWIN32 /* pick the correct prototype for unsetenv. */33 # define _POSIX_C_SOURCE 134 #endif35 31 #include <iprt/env.h> 36 #include <iprt/string.h> 32 37 33 #include <iprt/alloca.h> 38 34 #include <iprt/assert.h> 39 #if defined(DEBUG) && defined(RT_OS_LINUX) 40 # include <iprt/asm.h> 41 #endif 35 #include <iprt/string.h> 36 #include <iprt/mem.h> 42 37 43 38 #include <stdlib.h> 44 39 #include <errno.h> 45 40 46 #include "internal/alignmentchecks.h" 41 42 RTDECL(bool) RTEnvExistsBad(const char *pszVar) 43 { 44 return RTEnvGetBad(pszVar) != NULL; 45 } 47 46 48 47 49 48 RTDECL(bool) RTEnvExist(const char *pszVar) 50 49 { 51 return RTEnvGet(pszVar) != NULL; 50 return RTEnvExistsBad(pszVar); 51 } 52 53 54 RTDECL(bool) RTEnvExistsUtf8(const char *pszVar) 55 { 56 PRTUTF16 pwszVar; 57 int rc = RTStrToUtf16(pszVar, &pwszVar); 58 AssertRCReturn(rc, false); 59 bool fRet = _wgetenv(pwszVar) != NULL; 60 RTUtf16Free(pwszVar); 61 return fRet; 62 } 63 64 65 RTDECL(const char *) RTEnvGetBad(const char *pszVar) 66 { 67 return getenv(pszVar); 52 68 } 53 69 … … 55 71 RTDECL(const char *) RTEnvGet(const char *pszVar) 56 72 { 57 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */ 58 const char *pszValue = getenv(pszVar); 59 IPRT_ALIGNMENT_CHECKS_ENABLE(); 60 return pszValue; 61 } 62 63 64 RTDECL(int) RTEnvPut(const char *pszVarEqualValue) 73 return RTEnvGetBad(pszVar); 74 } 75 76 RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual) 77 { 78 AssertPtrReturn(pszVar, VERR_INVALID_POINTER); 79 AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER); 80 AssertReturn(pszValue || !cbValue, VERR_INVALID_PARAMETER); 81 AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER); 82 AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER); 83 84 if (pcchActual) 85 *pcchActual = 0; 86 87 PRTUTF16 pwszVar; 88 int rc = RTStrToUtf16(pszVar, &pwszVar); 89 AssertRCReturn(rc, false); 90 91 /** @todo Consider _wgetenv_s or GetEnvironmentVariableW here to avoid the 92 * potential race with a concurrent _wputenv/_putenv. */ 93 PCRTUTF16 pwszValue = _wgetenv(pwszVar); 94 RTUtf16Free(pwszVar); 95 if (pwszValue) 96 { 97 if (cbValue) 98 rc = RTUtf16ToUtf8Ex(pwszValue, RTSTR_MAX, &pszValue, cbValue, pcchActual); 99 else 100 rc = RTUtf16CalcUtf8LenEx(pwszValue, RTSTR_MAX, pcchActual); 101 } 102 else 103 rc = VERR_ENV_VAR_NOT_FOUND; 104 return rc; 105 } 106 107 108 RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue) 65 109 { 66 110 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */ … … 70 114 } 71 115 72 RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue) 73 { 74 #if defined(_MSC_VER) 116 117 RTDECL(int) RTEnvPut(const char *pszVarEqualValue) 118 { 119 return RTEnvPutBad(pszVarEqualValue); 120 } 121 122 123 RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue) 124 { 125 PRTUTF16 pwszVarEqualValue; 126 int rc = RTStrToUtf16(pszVarEqualValue, &pwszVarEqualValue); 127 if (RT_SUCCESS(rc)) 128 { 129 if (!_wputenv(pwszVarEqualValue)) 130 rc = VINF_SUCCESS; 131 else 132 rc = RTErrConvertFromErrno(errno); 133 RTUtf16Free(pwszVarEqualValue); 134 } 135 return rc; 136 } 137 138 139 140 RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue) 141 { 75 142 /* make a local copy and feed it to putenv. */ 76 143 const size_t cchVar = strlen(pszVar); … … 90 157 return 0; 91 158 return RTErrConvertFromErrno(errno); 92 93 #else 94 if (!setenv(pszVar, pszValue, 1)) 95 return VINF_SUCCESS; 96 return RTErrConvertFromErrno(errno); 97 #endif 98 } 99 100 101 RTDECL(int) RTEnvUnset(const char *pszVar) 159 } 160 161 162 RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue) 163 { 164 return RTEnvSetBad(pszVar, pszValue); 165 } 166 167 RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue) 168 { 169 size_t cwcVar; 170 int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar); 171 if (RT_SUCCESS(rc)) 172 { 173 size_t cwcValue; 174 rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcValue); 175 if (RT_SUCCESS(rc)) 176 { 177 PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + cwcValue + 1) * sizeof(RTUTF16)); 178 if (pwszTmp) 179 { 180 rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL); 181 if (RT_SUCCESS(rc)) 182 { 183 PRTUTF16 pwszTmpValue = &pwszTmp[cwcVar]; 184 *pwszTmpValue++ = '='; 185 rc = RTStrToUtf16Ex(pszValue, RTSTR_MAX, &pwszTmpValue, cwcValue + 1, NULL); 186 if (RT_SUCCESS(rc)) 187 { 188 if (!_wputenv(pwszTmp)) 189 rc = VINF_SUCCESS; 190 else 191 rc = RTErrConvertFromErrno(errno); 192 } 193 } 194 RTMemTmpFree(pwszTmp); 195 } 196 else 197 rc = VERR_NO_TMP_MEMORY; 198 } 199 } 200 return rc; 201 } 202 203 204 RTDECL(int) RTEnvUnsetBad(const char *pszVar) 102 205 { 103 206 AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER); … … 132 235 } 133 236 237 238 RTDECL(int) RTEnvUnset(const char *pszVar) 239 { 240 return RTEnvUnsetBad(pszVar); 241 } 242 243 244 RTDECL(int) RTEnvUnsetUtf8(const char *pszVar) 245 { 246 size_t cwcVar; 247 int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar); 248 if (RT_SUCCESS(rc)) 249 { 250 PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + 1) * sizeof(RTUTF16)); 251 if (pwszTmp) 252 { 253 rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL); 254 if (RT_SUCCESS(rc)) 255 { 256 pwszTmp[cwcVar] = '='; 257 pwszTmp[cwcVar + 1] = '\0'; 258 if (!_wputenv(pwszTmp)) 259 rc = VINF_SUCCESS; 260 else 261 rc = RTErrConvertFromErrno(errno); 262 } 263 RTMemTmpFree(pwszTmp); 264 } 265 } 266 return rc; 267 } 268
注意:
瀏覽 TracChangeset
來幫助您使用更動檢視器