Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / msvcrt / environ.c
blob1370fbbb70fb55dc0b52fca4bc6a7e4e75b4232f
1 /*
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"
24 #include "msvcrt.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
29 /*********************************************************************
30 * getenv (MSVCRT.@)
32 char * CDECL MSVCRT_getenv(const char *name)
34 char **environ;
35 unsigned int length=strlen(name);
37 for (environ = MSVCRT__environ; *environ; environ++)
39 char *str = *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));
44 return pos + 1;
47 return NULL;
50 /*********************************************************************
51 * _wgetenv (MSVCRT.@)
53 MSVCRT_wchar_t * CDECL MSVCRT__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));
69 return pos + 1;
72 return NULL;
75 /*********************************************************************
76 * _putenv (MSVCRT.@)
78 int CDECL _putenv(const char *str)
80 char *name, *value;
81 char *dst;
82 int ret;
84 TRACE("%s\n", debugstr_a(str));
86 if (!str)
87 return -1;
89 name = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
90 if (!name)
91 return -1;
92 dst = name;
93 while (*str && *str != '=')
94 *dst++ = *str++;
95 if (!*str++)
97 ret = -1;
98 goto finish;
100 *dst++ = '\0';
101 value = dst;
102 while (*str)
103 *dst++ = *str++;
104 *dst = '\0';
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);
116 finish:
117 HeapFree(GetProcessHeap(), 0, name);
118 return ret;
121 /*********************************************************************
122 * _wputenv (MSVCRT.@)
124 int CDECL _wputenv(const MSVCRT_wchar_t *str)
126 MSVCRT_wchar_t *name, *value;
127 MSVCRT_wchar_t *dst;
128 int ret;
130 TRACE("%s\n", debugstr_w(str));
132 if (!str)
133 return -1;
134 name = HeapAlloc(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t));
135 if (!name)
136 return -1;
137 dst = name;
138 while (*str && *str != '=')
139 *dst++ = *str++;
140 if (!*str++)
142 ret = -1;
143 goto finish;
145 *dst++ = 0;
146 value = dst;
147 while (*str)
148 *dst++ = *str++;
149 *dst = 0;
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);
159 finish:
160 HeapFree(GetProcessHeap(), 0, name);
161 return ret;
164 /*********************************************************************
165 * _putenv_s (MSVCRT.@)
167 int CDECL _putenv_s(const char *name, const char *value)
169 int ret;
171 TRACE("%s %s\n", debugstr_a(name), debugstr_a(value));
173 if (!MSVCRT_CHECK_PMT(name != NULL)) return -1;
174 if (!MSVCRT_CHECK_PMT(value != NULL)) return -1;
176 ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
178 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
179 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
181 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
182 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
184 return ret;
187 /*********************************************************************
188 * _wputenv_s (MSVCRT.@)
190 int CDECL _wputenv_s(const MSVCRT_wchar_t *name, const MSVCRT_wchar_t *value)
192 int ret;
194 TRACE("%s %s\n", debugstr_w(name), debugstr_w(value));
196 if (!MSVCRT_CHECK_PMT(name != NULL)) return -1;
197 if (!MSVCRT_CHECK_PMT(value != NULL)) return -1;
199 ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
201 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
202 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
204 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
205 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
207 return ret;
210 /******************************************************************
211 * _dupenv_s (MSVCRT.@)
213 int CDECL _dupenv_s(char **buffer, MSVCRT_size_t *numberOfElements, const char *varname)
215 char* e;
216 MSVCRT_size_t sz;
218 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
219 if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
221 if (!(e = MSVCRT_getenv(varname))) return *MSVCRT__errno() = MSVCRT_EINVAL;
223 sz = strlen(e) + 1;
224 if (!(*buffer = MSVCRT_malloc(sz)))
226 if (numberOfElements) *numberOfElements = 0;
227 return *MSVCRT__errno() = MSVCRT_ENOMEM;
229 strcpy(*buffer, e);
230 if (numberOfElements) *numberOfElements = sz;
231 return 0;
234 /******************************************************************
235 * _wdupenv_s (MSVCRT.@)
237 int CDECL _wdupenv_s(MSVCRT_wchar_t **buffer, MSVCRT_size_t *numberOfElements,
238 const MSVCRT_wchar_t *varname)
240 MSVCRT_wchar_t* e;
241 MSVCRT_size_t sz;
243 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
244 if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
246 if (!(e = MSVCRT__wgetenv(varname))) return *MSVCRT__errno() = MSVCRT_EINVAL;
248 sz = strlenW(e) + 1;
249 if (!(*buffer = MSVCRT_malloc(sz * sizeof(MSVCRT_wchar_t))))
251 if (numberOfElements) *numberOfElements = 0;
252 return *MSVCRT__errno() = MSVCRT_ENOMEM;
254 strcpyW(*buffer, e);
255 if (numberOfElements) *numberOfElements = sz;
256 return 0;
259 /******************************************************************
260 * getenv_s (MSVCRT.@)
262 int CDECL getenv_s(MSVCRT_size_t *pReturnValue, char* buffer, MSVCRT_size_t numberOfElements, const char *varname)
264 char* e;
266 if (!MSVCRT_CHECK_PMT(pReturnValue != NULL)) return MSVCRT_EINVAL;
267 if (!MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0))) return MSVCRT_EINVAL;
268 if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
270 if (!(e = MSVCRT_getenv(varname)))
272 *pReturnValue = 0;
273 return *MSVCRT__errno() = MSVCRT_EINVAL;
275 *pReturnValue = strlen(e) + 1;
276 if (numberOfElements < *pReturnValue)
278 return *MSVCRT__errno() = MSVCRT_ERANGE;
280 strcpy(buffer, e);
281 return 0;
284 /******************************************************************
285 * _wgetenv_s (MSVCRT.@)
287 int CDECL _wgetenv_s(MSVCRT_size_t *pReturnValue, MSVCRT_wchar_t *buffer, MSVCRT_size_t numberOfElements,
288 const MSVCRT_wchar_t *varname)
290 MSVCRT_wchar_t* e;
292 if (!MSVCRT_CHECK_PMT(pReturnValue != NULL)) return MSVCRT_EINVAL;
293 if (!MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0))) return MSVCRT_EINVAL;
294 if (!MSVCRT_CHECK_PMT(varname != NULL)) return MSVCRT_EINVAL;
296 if (!(e = MSVCRT__wgetenv(varname)))
298 *pReturnValue = 0;
299 return *MSVCRT__errno() = MSVCRT_EINVAL;
301 *pReturnValue = strlenW(e) + 1;
302 if (numberOfElements < *pReturnValue)
304 return *MSVCRT__errno() = MSVCRT_ERANGE;
306 strcpyW(buffer, e);
307 return 0;