diff options
Diffstat (limited to 'src/corelib/global')
-rw-r--r-- | src/corelib/global/qglobal.cpp | 37 | ||||
-rw-r--r-- | src/corelib/global/qglobal.h | 1 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 8bbc44978d..9cd19ccae9 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -2171,6 +2171,10 @@ bool qEnvironmentVariableIsSet(const char *varName) Q_DECL_NOEXCEPT \a varName. It will create the variable if it does not exist. It returns 0 if the variable could not be set. + Calling qputenv with an empty value removes the environment variable on + Windows, and makes it set (but empty) on Unix. Prefer using qunsetenv() + for fully portable behavior. + \note qputenv() was introduced because putenv() from the standard C library was deprecated in VC2005 (and later versions). qputenv() uses the replacement function in VC, and calls the standard C @@ -2197,6 +2201,39 @@ bool qputenv(const char *varName, const QByteArray& value) #endif } +/*! + \relates <QtGlobal> + + This function deletes the variable \a varName from the environment. + + Returns true on success. + + \since 5.1 + + \sa qputenv(), qgetenv() +*/ +bool qunsetenv(const char *varName) +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + return _putenv_s(varName, "") == 0; +#elif (defined(_POSIX_VERSION) && (_POSIX_VERSION-0) >= 200112L) || defined(Q_OS_BSD4) + // POSIX.1-2001 and BSD have unsetenv + return unsetenv(varName) == 0; +#elif defined(Q_CC_MINGW) + // On mingw, putenv("var=") removes "var" from the environment + QByteArray buffer(varName); + buffer += '='; + return putenv(buffer.constData()) == 0; +#else + // Fallback to putenv("var=") which will insert an empty var into the + // environment and leak it + QByteArray buffer(varName); + buffer += '='; + char *envVar = qstrdup(buffer.constData()); + return putenv(envVar) == 0; +#endif +} + #if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) # if defined(Q_OS_INTEGRITY) && defined(__GHS_VERSION_NUMBER) && (__GHS_VERSION_NUMBER < 500) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 4ee91d821c..73e849cb3e 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -934,6 +934,7 @@ Q_CORE_EXPORT QString qtTrId(const char *id, int n = -1); class QByteArray; Q_CORE_EXPORT QByteArray qgetenv(const char *varName); Q_CORE_EXPORT bool qputenv(const char *varName, const QByteArray& value); +Q_CORE_EXPORT bool qunsetenv(const char *varName); Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) Q_DECL_NOEXCEPT; Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) Q_DECL_NOEXCEPT; |