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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#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.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char *MSVCRT_getenv(const char *name)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-09-24 20:57:28 +02:00
|
|
|
char **environ;
|
|
|
|
unsigned int length=strlen(name);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2003-09-24 20:57:28 +02:00
|
|
|
for (environ = *__p__environ(); *environ; environ++)
|
|
|
|
{
|
|
|
|
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-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wgetenv (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t *_wgetenv(const MSVCRT_wchar_t *name)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-09-24 20:57:28 +02:00
|
|
|
MSVCRT_wchar_t **environ;
|
|
|
|
unsigned int length=strlenW(name);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2003-09-24 20:57:28 +02:00
|
|
|
for (environ = *__p__wenviron(); *environ; environ++)
|
|
|
|
{
|
|
|
|
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-11 00:59:25 +01:00
|
|
|
}
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putenv (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _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;
|
|
|
|
|
2002-07-23 22:59:12 +02:00
|
|
|
/* Update the __p__environ array only when already initialized */
|
2004-06-25 03:19:15 +02:00
|
|
|
if (_environ)
|
|
|
|
_environ = msvcrt_SnapshotOfEnvironmentA(_environ);
|
|
|
|
if (_wenviron)
|
|
|
|
_wenviron = msvcrt_SnapshotOfEnvironmentW(_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.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wputenv(const MSVCRT_wchar_t *str)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2006-04-03 09:31:12 +02:00
|
|
|
MSVCRT_wchar_t *name, *value;
|
|
|
|
MSVCRT_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;
|
2006-04-03 09:31:12 +02:00
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t));
|
|
|
|
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;
|
|
|
|
|
2002-07-23 22:59:12 +02:00
|
|
|
/* Update the __p__environ array only when already initialized */
|
2004-06-25 03:19:15 +02:00
|
|
|
if (_environ)
|
|
|
|
_environ = msvcrt_SnapshotOfEnvironmentA(_environ);
|
|
|
|
if (_wenviron)
|
|
|
|
_wenviron = msvcrt_SnapshotOfEnvironmentW(_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
|
|
|
}
|