2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll environment functions
|
|
|
|
*
|
|
|
|
* Copyright 1996,1998 Marcus Meissner
|
|
|
|
* Copyright 1996 Jukka Iivonen
|
|
|
|
* Copyright 1997,2000 Uwe Bonnes
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
|
|
|
#include "msvcrt.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getenv (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:53:33 +01:00
|
|
|
char * CDECL getenv(const char *name)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
char **env;
|
2003-09-24 20:57:28 +02:00
|
|
|
unsigned int length=strlen(name);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2020-12-01 13:51:23 +01:00
|
|
|
for (env = MSVCRT__environ; *env; env++)
|
2003-09-24 20:57:28 +02:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
char *str = *env;
|
2003-09-24 20:57:28 +02:00
|
|
|
char *pos = strchr(str,'=');
|
2020-12-02 00:54:09 +01:00
|
|
|
if (pos && ((pos - str) == length) && !_strnicmp(str,name,length))
|
2003-09-24 20:57:28 +02:00
|
|
|
{
|
|
|
|
TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wgetenv (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:53:33 +01:00
|
|
|
wchar_t * CDECL _wgetenv(const wchar_t *name)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
wchar_t **env;
|
2020-12-03 19:10:54 +01:00
|
|
|
unsigned int length=wcslen(name);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2010-10-07 04:59:57 +02:00
|
|
|
/* Initialize the _wenviron array if it's not already created. */
|
|
|
|
if (!MSVCRT__wenviron)
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
|
|
|
|
|
2020-12-01 13:51:23 +01:00
|
|
|
for (env = MSVCRT__wenviron; *env; env++)
|
2003-09-24 20:57:28 +02:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
wchar_t *str = *env;
|
2020-12-03 19:10:54 +01:00
|
|
|
wchar_t *pos = wcschr(str,'=');
|
|
|
|
if (pos && ((pos - str) == length) && !_wcsnicmp(str,name,length))
|
2003-09-24 20:57:28 +02:00
|
|
|
{
|
|
|
|
TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putenv (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _putenv(const char *str)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2006-04-03 09:31:12 +02:00
|
|
|
char *name, *value;
|
|
|
|
char *dst;
|
2002-07-23 22:59:12 +02:00
|
|
|
int ret;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2006-04-05 03:21:26 +02:00
|
|
|
TRACE("%s\n", debugstr_a(str));
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
2006-04-03 09:31:12 +02:00
|
|
|
|
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
|
|
|
|
if (!name)
|
|
|
|
return -1;
|
|
|
|
dst = name;
|
2001-01-22 03:21:54 +01:00
|
|
|
while (*str && *str != '=')
|
|
|
|
*dst++ = *str++;
|
|
|
|
if (!*str++)
|
2006-04-03 09:31:12 +02:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
*dst++ = '\0';
|
|
|
|
value = dst;
|
2001-01-22 03:21:54 +01:00
|
|
|
while (*str)
|
|
|
|
*dst++ = *str++;
|
|
|
|
*dst = '\0';
|
|
|
|
|
2004-07-19 23:23:02 +02:00
|
|
|
ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
|
|
|
|
|
2005-02-10 20:19:35 +01:00
|
|
|
/* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
|
2004-07-19 23:23:02 +02:00
|
|
|
if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
|
|
|
|
|
2010-09-30 08:24:13 +02:00
|
|
|
MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
|
|
|
/* Update the __p__wenviron array only when already initialized */
|
2009-10-05 14:55:09 +02:00
|
|
|
if (MSVCRT__wenviron)
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
2006-04-03 09:31:12 +02:00
|
|
|
|
|
|
|
finish:
|
|
|
|
HeapFree(GetProcessHeap(), 0, name);
|
2002-07-23 22:59:12 +02:00
|
|
|
return ret;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wputenv (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:16:49 +01:00
|
|
|
int CDECL _wputenv(const wchar_t *str)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *name, *value;
|
|
|
|
wchar_t *dst;
|
2002-07-23 22:59:12 +02:00
|
|
|
int ret;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(str));
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
2020-12-03 19:10:54 +01:00
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, (wcslen(str) + 1) * sizeof(wchar_t));
|
2006-04-03 09:31:12 +02:00
|
|
|
if (!name)
|
|
|
|
return -1;
|
|
|
|
dst = name;
|
2002-12-19 05:21:30 +01:00
|
|
|
while (*str && *str != '=')
|
2001-01-22 03:21:54 +01:00
|
|
|
*dst++ = *str++;
|
|
|
|
if (!*str++)
|
2006-04-03 09:31:12 +02:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
*dst++ = 0;
|
|
|
|
value = dst;
|
2001-01-22 03:21:54 +01:00
|
|
|
while (*str)
|
|
|
|
*dst++ = *str++;
|
2002-12-19 05:21:30 +01:00
|
|
|
*dst = 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2004-07-19 23:23:02 +02:00
|
|
|
ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
|
|
|
|
|
2005-02-10 20:19:35 +01:00
|
|
|
/* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
|
2004-07-19 23:23:02 +02:00
|
|
|
if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
|
|
|
|
|
2010-09-30 08:24:13 +02:00
|
|
|
MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
2006-04-03 09:31:12 +02:00
|
|
|
|
|
|
|
finish:
|
|
|
|
HeapFree(GetProcessHeap(), 0, name);
|
2002-07-23 22:59:12 +02:00
|
|
|
return ret;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
2010-11-02 22:02:59 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putenv_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL _putenv_s(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
TRACE("%s %s\n", debugstr_a(name), debugstr_a(value));
|
|
|
|
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(name != NULL)) return -1;
|
|
|
|
if (!MSVCRT_CHECK_PMT(value != NULL)) return -1;
|
2010-11-02 22:02:59 +01:00
|
|
|
|
|
|
|
ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
|
|
|
|
|
|
|
|
/* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
|
|
|
|
if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
|
|
|
|
|
|
|
|
MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wputenv_s (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:16:49 +01:00
|
|
|
int CDECL _wputenv_s(const wchar_t *name, const wchar_t *value)
|
2010-11-02 22:02:59 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
TRACE("%s %s\n", debugstr_w(name), debugstr_w(value));
|
|
|
|
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(name != NULL)) return -1;
|
|
|
|
if (!MSVCRT_CHECK_PMT(value != NULL)) return -1;
|
2010-11-02 22:02:59 +01:00
|
|
|
|
|
|
|
ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
|
|
|
|
|
|
|
|
/* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
|
|
|
|
if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
|
|
|
|
|
|
|
|
MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-11-02 22:03:05 +01:00
|
|
|
|
2018-01-03 09:11:44 +01:00
|
|
|
#if _MSVCR_VER>=80
|
|
|
|
|
2010-11-02 22:03:05 +01:00
|
|
|
/******************************************************************
|
2018-01-03 09:11:44 +01:00
|
|
|
* _dupenv_s (MSVCR80.@)
|
2010-11-02 22:03:05 +01:00
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL _dupenv_s(char **buffer, size_t *numberOfElements, const char *varname)
|
2010-11-02 22:03:05 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
char *e;
|
|
|
|
size_t sz;
|
2010-11-02 22:03:05 +01:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(buffer != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2020-12-02 00:53:33 +01:00
|
|
|
if (!(e = getenv(varname))) return *_errno() = EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2010-11-02 22:03:05 +01:00
|
|
|
sz = strlen(e) + 1;
|
2020-12-01 13:51:32 +01:00
|
|
|
if (!(*buffer = malloc(sz)))
|
2010-11-02 22:03:05 +01:00
|
|
|
{
|
|
|
|
if (numberOfElements) *numberOfElements = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno() = ENOMEM;
|
2010-11-02 22:03:05 +01:00
|
|
|
}
|
|
|
|
strcpy(*buffer, e);
|
|
|
|
if (numberOfElements) *numberOfElements = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
2018-01-03 09:11:44 +01:00
|
|
|
* _wdupenv_s (MSVCR80.@)
|
2010-11-02 22:03:05 +01:00
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL _wdupenv_s(wchar_t **buffer, size_t *numberOfElements,
|
2020-11-27 14:16:49 +01:00
|
|
|
const wchar_t *varname)
|
2010-11-02 22:03:05 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wchar_t *e;
|
|
|
|
size_t sz;
|
2010-11-02 22:03:05 +01:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(buffer != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2020-12-02 00:53:33 +01:00
|
|
|
if (!(e = _wgetenv(varname))) return *_errno() = EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2020-12-03 19:10:54 +01:00
|
|
|
sz = wcslen(e) + 1;
|
2020-12-01 13:51:32 +01:00
|
|
|
if (!(*buffer = malloc(sz * sizeof(wchar_t))))
|
2010-11-02 22:03:05 +01:00
|
|
|
{
|
|
|
|
if (numberOfElements) *numberOfElements = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno() = ENOMEM;
|
2010-11-02 22:03:05 +01:00
|
|
|
}
|
2020-12-03 19:10:54 +01:00
|
|
|
wcscpy(*buffer, e);
|
2010-11-02 22:03:05 +01:00
|
|
|
if (numberOfElements) *numberOfElements = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-07 19:11:26 +01:00
|
|
|
|
2018-01-03 09:11:44 +01:00
|
|
|
#endif /* _MSVCR_VER>=80 */
|
|
|
|
|
2010-11-07 19:11:26 +01:00
|
|
|
/******************************************************************
|
|
|
|
* getenv_s (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL getenv_s(size_t *pReturnValue, char* buffer, size_t numberOfElements, const char *varname)
|
2010-11-07 19:11:26 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
char *e;
|
2010-11-07 19:11:26 +01:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(pReturnValue != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0))) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2020-12-02 00:53:33 +01:00
|
|
|
if (!(e = getenv(varname)))
|
2010-11-07 19:11:26 +01:00
|
|
|
{
|
|
|
|
*pReturnValue = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno() = EINVAL;
|
2010-11-07 19:11:26 +01:00
|
|
|
}
|
|
|
|
*pReturnValue = strlen(e) + 1;
|
|
|
|
if (numberOfElements < *pReturnValue)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno() = ERANGE;
|
2010-11-07 19:11:26 +01:00
|
|
|
}
|
|
|
|
strcpy(buffer, e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* _wgetenv_s (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL _wgetenv_s(size_t *pReturnValue, wchar_t *buffer, size_t numberOfElements,
|
2020-11-27 14:16:49 +01:00
|
|
|
const wchar_t *varname)
|
2010-11-07 19:11:26 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wchar_t *e;
|
2010-11-07 19:11:26 +01:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(pReturnValue != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0))) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2020-12-02 00:53:33 +01:00
|
|
|
if (!(e = _wgetenv(varname)))
|
2010-11-07 19:11:26 +01:00
|
|
|
{
|
|
|
|
*pReturnValue = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno() = EINVAL;
|
2010-11-07 19:11:26 +01:00
|
|
|
}
|
2020-12-03 19:10:54 +01:00
|
|
|
*pReturnValue = wcslen(e) + 1;
|
2010-11-07 19:11:26 +01:00
|
|
|
if (numberOfElements < *pReturnValue)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno() = ERANGE;
|
2010-11-07 19:11:26 +01:00
|
|
|
}
|
2020-12-03 19:10:54 +01:00
|
|
|
wcscpy(buffer, e);
|
2010-11-07 19:11:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2019-04-10 14:51:55 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _get_environ (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:53:33 +01:00
|
|
|
void CDECL _get_environ(char ***ptr)
|
2019-04-10 14:51:55 +02:00
|
|
|
{
|
|
|
|
*ptr = MSVCRT__environ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _get_wenviron (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:53:33 +01:00
|
|
|
void CDECL _get_wenviron(wchar_t ***ptr)
|
2019-04-10 14:51:55 +02:00
|
|
|
{
|
|
|
|
*ptr = MSVCRT__wenviron;
|
|
|
|
}
|