2001-01-10 23:59:25 +00: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-09 23:29:33 +00: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 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-10 23:59:25 +00:00
|
|
|
*/
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "msvcrt.h"
|
2002-01-22 00:57:16 +00:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-10 23:59:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getenv (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
char * CDECL MSVCRT_getenv(const char *name)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
2003-09-24 18:57:28 +00:00
|
|
|
char **environ;
|
|
|
|
unsigned int length=strlen(name);
|
2001-01-10 23:59:25 +00:00
|
|
|
|
2010-10-07 02:59:57 +00:00
|
|
|
for (environ = MSVCRT__environ; *environ; environ++)
|
2003-09-24 18:57:28 +00:00
|
|
|
{
|
|
|
|
char *str = *environ;
|
|
|
|
char *pos = strchr(str,'=');
|
|
|
|
if (pos && ((pos - str) == length) && !strncasecmp(str,name,length))
|
|
|
|
{
|
|
|
|
TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-01-10 23:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wgetenv (MSVCRT.@)
|
|
|
|
*/
|
2011-06-03 13:28:32 +00:00
|
|
|
MSVCRT_wchar_t * CDECL MSVCRT__wgetenv(const MSVCRT_wchar_t *name)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
2003-09-24 18:57:28 +00:00
|
|
|
MSVCRT_wchar_t **environ;
|
|
|
|
unsigned int length=strlenW(name);
|
2001-01-10 23:59:25 +00:00
|
|
|
|
2010-10-07 02:59:57 +00:00
|
|
|
/* Initialize the _wenviron array if it's not already created. */
|
|
|
|
if (!MSVCRT__wenviron)
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
|
|
|
|
|
|
|
|
for (environ = MSVCRT__wenviron; *environ; environ++)
|
2003-09-24 18:57:28 +00:00
|
|
|
{
|
|
|
|
MSVCRT_wchar_t *str = *environ;
|
|
|
|
MSVCRT_wchar_t *pos = strchrW(str,'=');
|
|
|
|
if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
|
|
|
|
{
|
|
|
|
TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-01-10 23:59:25 +00:00
|
|
|
}
|
2001-01-22 02:21:54 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putenv (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
int CDECL _putenv(const char *str)
|
2001-01-22 02:21:54 +00:00
|
|
|
{
|
2006-04-03 07:31:12 +00:00
|
|
|
char *name, *value;
|
|
|
|
char *dst;
|
2002-07-23 20:59:12 +00:00
|
|
|
int ret;
|
2001-01-22 02:21:54 +00:00
|
|
|
|
2006-04-05 01:21:26 +00:00
|
|
|
TRACE("%s\n", debugstr_a(str));
|
2001-01-22 02:21:54 +00:00
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
2006-04-03 07:31:12 +00:00
|
|
|
|
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
|
|
|
|
if (!name)
|
|
|
|
return -1;
|
|
|
|
dst = name;
|
2001-01-22 02:21:54 +00:00
|
|
|
while (*str && *str != '=')
|
|
|
|
*dst++ = *str++;
|
|
|
|
if (!*str++)
|
2006-04-03 07:31:12 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
*dst++ = '\0';
|
|
|
|
value = dst;
|
2001-01-22 02:21:54 +00:00
|
|
|
while (*str)
|
|
|
|
*dst++ = *str++;
|
|
|
|
*dst = '\0';
|
|
|
|
|
2004-07-19 21:23:02 +00:00
|
|
|
ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
|
|
|
|
|
2005-02-10 19:19:35 +00:00
|
|
|
/* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
|
2004-07-19 21:23:02 +00:00
|
|
|
if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
|
|
|
|
|
2010-09-30 06:24:13 +00:00
|
|
|
MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
|
|
|
/* Update the __p__wenviron array only when already initialized */
|
2009-10-05 12:55:09 +00:00
|
|
|
if (MSVCRT__wenviron)
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
2006-04-03 07:31:12 +00:00
|
|
|
|
|
|
|
finish:
|
|
|
|
HeapFree(GetProcessHeap(), 0, name);
|
2002-07-23 20:59:12 +00:00
|
|
|
return ret;
|
2001-01-22 02:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wputenv (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
int CDECL _wputenv(const MSVCRT_wchar_t *str)
|
2001-01-22 02:21:54 +00:00
|
|
|
{
|
2006-04-03 07:31:12 +00:00
|
|
|
MSVCRT_wchar_t *name, *value;
|
|
|
|
MSVCRT_wchar_t *dst;
|
2002-07-23 20:59:12 +00:00
|
|
|
int ret;
|
2001-01-22 02:21:54 +00:00
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(str));
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
2006-04-03 07:31:12 +00:00
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t));
|
|
|
|
if (!name)
|
|
|
|
return -1;
|
|
|
|
dst = name;
|
2002-12-19 04:21:30 +00:00
|
|
|
while (*str && *str != '=')
|
2001-01-22 02:21:54 +00:00
|
|
|
*dst++ = *str++;
|
|
|
|
if (!*str++)
|
2006-04-03 07:31:12 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
*dst++ = 0;
|
|
|
|
value = dst;
|
2001-01-22 02:21:54 +00:00
|
|
|
while (*str)
|
|
|
|
*dst++ = *str++;
|
2002-12-19 04:21:30 +00:00
|
|
|
*dst = 0;
|
2001-01-22 02:21:54 +00:00
|
|
|
|
2004-07-19 21:23:02 +00:00
|
|
|
ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
|
|
|
|
|
2005-02-10 19:19:35 +00:00
|
|
|
/* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
|
2004-07-19 21:23:02 +00:00
|
|
|
if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
|
|
|
|
|
2010-09-30 06:24:13 +00:00
|
|
|
MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
|
|
|
MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
2006-04-03 07:31:12 +00:00
|
|
|
|
|
|
|
finish:
|
|
|
|
HeapFree(GetProcessHeap(), 0, name);
|
2002-07-23 20:59:12 +00:00
|
|
|
return ret;
|
2001-01-22 02:21:54 +00:00
|
|
|
}
|
2010-11-02 21:02:59 +00: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 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(name != NULL)) return -1;
|
|
|
|
if (!MSVCRT_CHECK_PMT(value != NULL)) return -1;
|
2010-11-02 21:02:59 +00: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.@)
|
|
|
|
*/
|
|
|
|
int CDECL _wputenv_s(const MSVCRT_wchar_t *name, const MSVCRT_wchar_t *value)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
TRACE("%s %s\n", debugstr_w(name), debugstr_w(value));
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(name != NULL)) return -1;
|
|
|
|
if (!MSVCRT_CHECK_PMT(value != NULL)) return -1;
|
2010-11-02 21:02:59 +00: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 21:03:05 +00:00
|
|
|
|
2018-01-03 08:11:44 +00:00
|
|
|
#if _MSVCR_VER>=80
|
|
|
|
|
2010-11-02 21:03:05 +00:00
|
|
|
/******************************************************************
|
2018-01-03 08:11:44 +00:00
|
|
|
* _dupenv_s (MSVCR80.@)
|
2010-11-02 21:03:05 +00:00
|
|
|
*/
|
2010-11-08 15:43:31 +00:00
|
|
|
int CDECL _dupenv_s(char **buffer, MSVCRT_size_t *numberOfElements, const char *varname)
|
2010-11-02 21:03:05 +00:00
|
|
|
{
|
|
|
|
char* e;
|
|
|
|
MSVCRT_size_t sz;
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
|
|
|
|
|
|
|
|
if (!(e = MSVCRT_getenv(varname))) return *MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
|
2010-11-02 21:03:05 +00:00
|
|
|
sz = strlen(e) + 1;
|
|
|
|
if (!(*buffer = MSVCRT_malloc(sz)))
|
|
|
|
{
|
|
|
|
if (numberOfElements) *numberOfElements = 0;
|
|
|
|
return *MSVCRT__errno() = MSVCRT_ENOMEM;
|
|
|
|
}
|
|
|
|
strcpy(*buffer, e);
|
|
|
|
if (numberOfElements) *numberOfElements = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
2018-01-03 08:11:44 +00:00
|
|
|
* _wdupenv_s (MSVCR80.@)
|
2010-11-02 21:03:05 +00:00
|
|
|
*/
|
2010-11-08 15:43:31 +00:00
|
|
|
int CDECL _wdupenv_s(MSVCRT_wchar_t **buffer, MSVCRT_size_t *numberOfElements,
|
|
|
|
const MSVCRT_wchar_t *varname)
|
2010-11-02 21:03:05 +00:00
|
|
|
{
|
|
|
|
MSVCRT_wchar_t* e;
|
|
|
|
MSVCRT_size_t sz;
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
|
|
|
|
|
|
|
|
if (!(e = MSVCRT__wgetenv(varname))) return *MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
|
2010-11-02 21:03:05 +00:00
|
|
|
sz = strlenW(e) + 1;
|
|
|
|
if (!(*buffer = MSVCRT_malloc(sz * sizeof(MSVCRT_wchar_t))))
|
|
|
|
{
|
|
|
|
if (numberOfElements) *numberOfElements = 0;
|
|
|
|
return *MSVCRT__errno() = MSVCRT_ENOMEM;
|
|
|
|
}
|
|
|
|
strcpyW(*buffer, e);
|
|
|
|
if (numberOfElements) *numberOfElements = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-07 18:11:26 +00:00
|
|
|
|
2018-01-03 08:11:44 +00:00
|
|
|
#endif /* _MSVCR_VER>=80 */
|
|
|
|
|
2010-11-07 18:11:26 +00:00
|
|
|
/******************************************************************
|
|
|
|
* getenv_s (MSVCRT.@)
|
|
|
|
*/
|
2010-11-08 15:43:31 +00:00
|
|
|
int CDECL getenv_s(MSVCRT_size_t *pReturnValue, char* buffer, MSVCRT_size_t numberOfElements, const char *varname)
|
2010-11-07 18:11:26 +00:00
|
|
|
{
|
|
|
|
char* e;
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(pReturnValue != NULL)) return MSVCRT_EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0))) return MSVCRT_EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
|
|
|
|
|
2010-11-07 18:11:26 +00:00
|
|
|
if (!(e = MSVCRT_getenv(varname)))
|
|
|
|
{
|
|
|
|
*pReturnValue = 0;
|
|
|
|
return *MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
*pReturnValue = strlen(e) + 1;
|
|
|
|
if (numberOfElements < *pReturnValue)
|
|
|
|
{
|
|
|
|
return *MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
}
|
|
|
|
strcpy(buffer, e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* _wgetenv_s (MSVCRT.@)
|
|
|
|
*/
|
2010-11-08 15:43:31 +00:00
|
|
|
int CDECL _wgetenv_s(MSVCRT_size_t *pReturnValue, MSVCRT_wchar_t *buffer, MSVCRT_size_t numberOfElements,
|
|
|
|
const MSVCRT_wchar_t *varname)
|
2010-11-07 18:11:26 +00:00
|
|
|
{
|
|
|
|
MSVCRT_wchar_t* e;
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(pReturnValue != NULL)) return MSVCRT_EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0))) return MSVCRT_EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
|
|
|
|
|
2011-06-03 13:28:32 +00:00
|
|
|
if (!(e = MSVCRT__wgetenv(varname)))
|
2010-11-07 18:11:26 +00:00
|
|
|
{
|
|
|
|
*pReturnValue = 0;
|
|
|
|
return *MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
*pReturnValue = strlenW(e) + 1;
|
|
|
|
if (numberOfElements < *pReturnValue)
|
|
|
|
{
|
|
|
|
return *MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
}
|
|
|
|
strcpyW(buffer, e);
|
|
|
|
return 0;
|
|
|
|
}
|