Changeset 1494 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Mar 15, 2007 1:18:15 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/posix/env-posix.cpp
r936 r1494 26 26 #include <iprt/env.h> 27 27 #include <iprt/string.h> 28 #include <iprt/alloca.h> 28 29 29 30 #include <stdlib.h> … … 31 32 32 33 33 /** 34 * Gets an environment variable (getenv). 35 * 36 * The caller is responsible for ensuring that nobody changes the environment 37 * while it's using the returned string pointer! 38 * 39 * @returns Pointer to read only string on success, NULL if the variable wasn't found. 40 * 41 * @param pszVar The environment variable name. 42 */ 34 RTDECL(bool) RTEnvExist(const char *pszVar) 35 { 36 return getenv(pszVar) != NULL; 37 } 38 39 43 40 RTDECL(const char *) RTEnvGet(const char *pszVar) 44 41 { … … 47 44 48 45 49 /**50 * Puts an variable=value string into the environment (putenv).51 *52 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.53 *54 * @param pszVarEqualValue The variable '=' value string. If the value and '=' is55 * omitted, the variable is removed from the environment.56 */57 46 RTDECL(int) RTEnvPut(const char *pszVarEqualValue) 58 47 { 59 /** @todo putenv is a memory leak. deal with this on a per system basis. */48 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */ 60 49 if (!putenv((char *)pszVarEqualValue)) 61 50 return 0; … … 63 52 } 64 53 54 RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue) 55 { 56 #if defined(_MSC_VER) 57 /* make a local copy and feed it to putenv. */ 58 const size_t cchVar = strlen(pszVar); 59 const size_t cchValue = strlen(pszValue); 60 char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue); 61 memcpy(pszTmp, pszVar, cchVar); 62 pszTmp[cchVar] = '='; 63 if (*pszValue) 64 memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1); 65 else 66 { 67 pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */ 68 pszTmp[cchVar + 2] = '\0'; 69 } 70 71 if (!putenv(pszTmp)) 72 return 0; 73 return RTErrConvertFromErrno(errno); 74 75 #else 76 if (!setenv(pszVar, pszValue, 1)) 77 return VINF_SUCCESS; 78 return RTErrConvertFromErrno(errno); 79 #endif 80 } 81
Note:
See TracChangeset
for help on using the changeset viewer.