2 * msvcrt.dll environment functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/unicode.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
29 /*********************************************************************
32 char * CDECL MSVCRT_getenv(const char *name)
35 unsigned int length=strlen(name);
37 for (environ = MSVCRT__environ; *environ; environ++)
40 char *pos = strchr(str,'=');
41 if (pos && ((pos - str) == length) && !strncasecmp(str,name,length))
43 TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
50 /*********************************************************************
53 MSVCRT_wchar_t * CDECL _wgetenv(const MSVCRT_wchar_t *name)
55 MSVCRT_wchar_t **environ;
56 unsigned int length=strlenW(name);
58 /* Initialize the _wenviron array if it's not already created. */
59 if (!MSVCRT__wenviron)
60 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
62 for (environ = MSVCRT__wenviron; *environ; environ++)
64 MSVCRT_wchar_t *str = *environ;
65 MSVCRT_wchar_t *pos = strchrW(str,'=');
66 if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
68 TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
75 /*********************************************************************
78 int CDECL _putenv(const char *str)
84 TRACE("%s\n", debugstr_a(str));
89 name = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
93 while (*str && *str != '=')
106 ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
108 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
109 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
111 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
112 /* Update the __p__wenviron array only when already initialized */
113 if (MSVCRT__wenviron)
114 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
117 HeapFree(GetProcessHeap(), 0, name);
121 /*********************************************************************
122 * _wputenv (MSVCRT.@)
124 int CDECL _wputenv(const MSVCRT_wchar_t *str)
126 MSVCRT_wchar_t *name, *value;
130 TRACE("%s\n", debugstr_w(str));
134 name = HeapAlloc(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t));
138 while (*str && *str != '=')
151 ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
153 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
154 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
156 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
157 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
160 HeapFree(GetProcessHeap(), 0, name);
164 /*********************************************************************
165 * _putenv_s (MSVCRT.@)
167 int CDECL _putenv_s(const char *name, const char *value)
171 TRACE("%s %s\n", debugstr_a(name), debugstr_a(value));
173 if (!MSVCRT_CHECK_PMT(name != NULL) || !MSVCRT_CHECK_PMT(value != NULL))
175 *MSVCRT__errno() = MSVCRT_EINVAL;
179 ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
181 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
182 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
184 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
185 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
190 /*********************************************************************
191 * _wputenv_s (MSVCRT.@)
193 int CDECL _wputenv_s(const MSVCRT_wchar_t *name, const MSVCRT_wchar_t *value)
197 TRACE("%s %s\n", debugstr_w(name), debugstr_w(value));
199 if (!MSVCRT_CHECK_PMT(name != NULL) || !MSVCRT_CHECK_PMT(value != NULL))
201 *MSVCRT__errno() = MSVCRT_EINVAL;
205 ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
207 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
208 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
210 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
211 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
216 /******************************************************************
217 * _dupenv_s (MSVCRT.@)
219 int _dupenv_s(char **buffer, MSVCRT_size_t *numberOfElements, const char *varname)
224 if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(varname) ||
225 !(e = MSVCRT_getenv(varname)))
227 return *MSVCRT__errno() = MSVCRT_EINVAL;
230 if (!(*buffer = MSVCRT_malloc(sz)))
232 if (numberOfElements) *numberOfElements = 0;
233 return *MSVCRT__errno() = MSVCRT_ENOMEM;
236 if (numberOfElements) *numberOfElements = sz;
240 /******************************************************************
241 * _wdupenv_s (MSVCRT.@)
243 int _wdupenv_s(MSVCRT_wchar_t **buffer, MSVCRT_size_t *numberOfElements,
244 const MSVCRT_wchar_t *varname)
249 if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(varname) ||
250 !(e = _wgetenv(varname)))
252 return *MSVCRT__errno() = MSVCRT_EINVAL;
255 if (!(*buffer = MSVCRT_malloc(sz * sizeof(MSVCRT_wchar_t))))
257 if (numberOfElements) *numberOfElements = 0;
258 return *MSVCRT__errno() = MSVCRT_ENOMEM;
261 if (numberOfElements) *numberOfElements = sz;
265 /******************************************************************
266 * getenv_s (MSVCRT.@)
268 int getenv_s(MSVCRT_size_t *pReturnValue, char* buffer, MSVCRT_size_t numberOfElements, const char *varname)
272 if (!MSVCRT_CHECK_PMT(pReturnValue != NULL) ||
273 !MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0)) ||
274 !MSVCRT_CHECK_PMT(varname != NULL))
276 return *MSVCRT__errno() = MSVCRT_EINVAL;
278 if (!(e = MSVCRT_getenv(varname)))
281 return *MSVCRT__errno() = MSVCRT_EINVAL;
283 *pReturnValue = strlen(e) + 1;
284 if (numberOfElements < *pReturnValue)
286 return *MSVCRT__errno() = MSVCRT_ERANGE;
292 /******************************************************************
293 * _wgetenv_s (MSVCRT.@)
295 int _wgetenv_s(MSVCRT_size_t *pReturnValue, MSVCRT_wchar_t *buffer, MSVCRT_size_t numberOfElements,
296 const MSVCRT_wchar_t *varname)
300 if (!MSVCRT_CHECK_PMT(pReturnValue != NULL) ||
301 !MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0)) ||
302 !MSVCRT_CHECK_PMT(varname != NULL))
304 return *MSVCRT__errno() = MSVCRT_EINVAL;
306 if (!(e = _wgetenv(varname)))
309 return *MSVCRT__errno() = MSVCRT_EINVAL;
311 *pReturnValue = strlenW(e) + 1;
312 if (numberOfElements < *pReturnValue)
314 return *MSVCRT__errno() = MSVCRT_ERANGE;