~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/msvcrt/environ.c

Version: ~ [ wine-1.5.30 ] ~ [ wine-1.5.29 ] ~ [ wine-1.5.28 ] ~ [ wine-1.5.27 ] ~ [ wine-1.5.26 ] ~ [ wine-1.5.25 ] ~ [ wine-1.5.24 ] ~ [ wine-1.5.23 ] ~ [ wine-1.5.22 ] ~ [ wine-1.5.21 ] ~ [ wine-1.5.20 ] ~ [ wine-1.5.19 ] ~ [ wine-1.5.18 ] ~ [ wine-1.5.17 ] ~ [ wine-1.5.16 ] ~ [ wine-1.5.15 ] ~ [ wine-1.5.14 ] ~ [ wine-1.5.13 ] ~ [ wine-1.5.12 ] ~ [ wine-1.5.11 ] ~ [ wine-1.5.10 ] ~ [ wine-1.5.9 ] ~ [ wine-1.5.8 ] ~ [ wine-1.5.7 ] ~ [ wine-1.4.1 ] ~ [ wine-1.5.6 ] ~ [ wine-1.5.5 ] ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * msvcrt.dll environment functions
  3  *
  4  * Copyright 1996,1998 Marcus Meissner
  5  * Copyright 1996 Jukka Iivonen
  6  * Copyright 1997,2000 Uwe Bonnes
  7  * Copyright 2000 Jon Griffiths
  8  *
  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.
 13  *
 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.
 18  *
 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
 22  */
 23 #include "wine/unicode.h"
 24 #include "msvcrt.h"
 25 #include "wine/debug.h"
 26 
 27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
 28 
 29 /*********************************************************************
 30  *              getenv (MSVCRT.@)
 31  */
 32 char * CDECL MSVCRT_getenv(const char *name)
 33 {
 34     char **environ;
 35     unsigned int length=strlen(name);
 36 
 37     for (environ = MSVCRT__environ; *environ; environ++)
 38     {
 39         char *str = *environ;
 40         char *pos = strchr(str,'=');
 41         if (pos && ((pos - str) == length) && !strncasecmp(str,name,length))
 42         {
 43             TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
 44             return pos + 1;
 45         }
 46     }
 47     return NULL;
 48 }
 49 
 50 /*********************************************************************
 51  *              _wgetenv (MSVCRT.@)
 52  */
 53 MSVCRT_wchar_t * CDECL MSVCRT__wgetenv(const MSVCRT_wchar_t *name)
 54 {
 55     MSVCRT_wchar_t **environ;
 56     unsigned int length=strlenW(name);
 57 
 58     /* Initialize the _wenviron array if it's not already created. */
 59     if (!MSVCRT__wenviron)
 60         MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
 61 
 62     for (environ = MSVCRT__wenviron; *environ; environ++)
 63     {
 64         MSVCRT_wchar_t *str = *environ;
 65         MSVCRT_wchar_t *pos = strchrW(str,'=');
 66         if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
 67         {
 68             TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
 69             return pos + 1;
 70         }
 71     }
 72     return NULL;
 73 }
 74 
 75 /*********************************************************************
 76  *              _putenv (MSVCRT.@)
 77  */
 78 int CDECL _putenv(const char *str)
 79 {
 80  char *name, *value;
 81  char *dst;
 82  int ret;
 83 
 84  TRACE("%s\n", debugstr_a(str));
 85 
 86  if (!str)
 87    return -1;
 88    
 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++)
 96  {
 97    ret = -1;
 98    goto finish;
 99  }
100  *dst++ = '\0';
101  value = dst;
102  while (*str)
103   *dst++ = *str++;
104  *dst = '\0';
105 
106  ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
107 
108  /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
109  if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
110 
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);
115    
116 finish:
117  HeapFree(GetProcessHeap(), 0, name);
118  return ret;
119 }
120 
121 /*********************************************************************
122  *              _wputenv (MSVCRT.@)
123  */
124 int CDECL _wputenv(const MSVCRT_wchar_t *str)
125 {
126  MSVCRT_wchar_t *name, *value;
127  MSVCRT_wchar_t *dst;
128  int ret;
129 
130  TRACE("%s\n", debugstr_w(str));
131 
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++)
141  {
142    ret = -1;
143    goto finish;
144  }
145  *dst++ = 0;
146  value = dst;
147  while (*str)
148   *dst++ = *str++;
149  *dst = 0;
150 
151  ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
152 
153  /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
154  if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
155 
156  MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
157  MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
158 
159 finish:
160  HeapFree(GetProcessHeap(), 0, name);
161  return ret;
162 }
163 
164 /*********************************************************************
165  *              _putenv_s (MSVCRT.@)
166  */
167 int CDECL _putenv_s(const char *name, const char *value)
168 {
169     int ret;
170 
171     TRACE("%s %s\n", debugstr_a(name), debugstr_a(value));
172 
173     if (!MSVCRT_CHECK_PMT(name != NULL) || !MSVCRT_CHECK_PMT(value != NULL))
174     {
175         *MSVCRT__errno() = MSVCRT_EINVAL;
176         return -1;
177     }
178 
179     ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
180 
181     /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
182     if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
183 
184     MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
185     MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
186 
187     return ret;
188 }
189 
190 /*********************************************************************
191  *              _wputenv_s (MSVCRT.@)
192  */
193 int CDECL _wputenv_s(const MSVCRT_wchar_t *name, const MSVCRT_wchar_t *value)
194 {
195     int ret;
196 
197     TRACE("%s %s\n", debugstr_w(name), debugstr_w(value));
198 
199     if (!MSVCRT_CHECK_PMT(name != NULL) || !MSVCRT_CHECK_PMT(value != NULL))
200     {
201         *MSVCRT__errno() = MSVCRT_EINVAL;
202         return -1;
203     }
204 
205     ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
206 
207     /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
208     if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
209 
210     MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
211     MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
212 
213     return ret;
214 }
215 
216 /******************************************************************
217  *              _dupenv_s (MSVCRT.@)
218  */
219 int CDECL _dupenv_s(char **buffer, MSVCRT_size_t *numberOfElements, const char *varname)
220 {
221     char*               e;
222     MSVCRT_size_t       sz;
223 
224     if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(varname) ||
225         !(e = MSVCRT_getenv(varname)))
226     {
227         return *MSVCRT__errno() = MSVCRT_EINVAL;
228     }
229     sz = strlen(e) + 1;
230     if (!(*buffer = MSVCRT_malloc(sz)))
231     {
232         if (numberOfElements) *numberOfElements = 0;
233         return *MSVCRT__errno() = MSVCRT_ENOMEM;
234     }
235     strcpy(*buffer, e);
236     if (numberOfElements) *numberOfElements = sz;
237     return 0;
238 }
239 
240 /******************************************************************
241  *              _wdupenv_s (MSVCRT.@)
242  */
243 int CDECL _wdupenv_s(MSVCRT_wchar_t **buffer, MSVCRT_size_t *numberOfElements,
244                      const MSVCRT_wchar_t *varname)
245 {
246     MSVCRT_wchar_t*     e;
247     MSVCRT_size_t       sz;
248 
249     if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(varname) ||
250         !(e = MSVCRT__wgetenv(varname)))
251     {
252         return *MSVCRT__errno() = MSVCRT_EINVAL;
253     }
254     sz = strlenW(e) + 1;
255     if (!(*buffer = MSVCRT_malloc(sz * sizeof(MSVCRT_wchar_t))))
256     {
257         if (numberOfElements) *numberOfElements = 0;
258         return *MSVCRT__errno() = MSVCRT_ENOMEM;
259     }
260     strcpyW(*buffer, e);
261     if (numberOfElements) *numberOfElements = sz;
262     return 0;
263 }
264 
265 /******************************************************************
266  *              getenv_s (MSVCRT.@)
267  */
268 int CDECL getenv_s(MSVCRT_size_t *pReturnValue, char* buffer, MSVCRT_size_t numberOfElements, const char *varname)
269 {
270     char*       e;
271 
272     if (!MSVCRT_CHECK_PMT(pReturnValue != NULL) ||
273         !MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0)) ||
274         !MSVCRT_CHECK_PMT(varname != NULL))
275     {
276         return *MSVCRT__errno() = MSVCRT_EINVAL;
277     }
278     if (!(e = MSVCRT_getenv(varname)))
279     {
280         *pReturnValue = 0;
281         return *MSVCRT__errno() = MSVCRT_EINVAL;
282     }
283     *pReturnValue = strlen(e) + 1;
284     if (numberOfElements < *pReturnValue)
285     {
286         return *MSVCRT__errno() = MSVCRT_ERANGE;
287     }
288     strcpy(buffer, e);
289     return 0;
290 }
291 
292 /******************************************************************
293  *              _wgetenv_s (MSVCRT.@)
294  */
295 int CDECL _wgetenv_s(MSVCRT_size_t *pReturnValue, MSVCRT_wchar_t *buffer, MSVCRT_size_t numberOfElements,
296                      const MSVCRT_wchar_t *varname)
297 {
298     MSVCRT_wchar_t*     e;
299 
300     if (!MSVCRT_CHECK_PMT(pReturnValue != NULL) ||
301         !MSVCRT_CHECK_PMT(!(buffer == NULL && numberOfElements > 0)) ||
302         !MSVCRT_CHECK_PMT(varname != NULL))
303     {
304         return *MSVCRT__errno() = MSVCRT_EINVAL;
305     }
306     if (!(e = MSVCRT__wgetenv(varname)))
307     {
308         *pReturnValue = 0;
309         return *MSVCRT__errno() = MSVCRT_EINVAL;
310     }
311     *pReturnValue = strlenW(e) + 1;
312     if (numberOfElements < *pReturnValue)
313     {
314         return *MSVCRT__errno() = MSVCRT_ERANGE;
315     }
316     strcpyW(buffer, e);
317     return 0;
318 }
319 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.