Get rid of HEAP_strdupWtoA calls.
[wine/multimedia.git] / dlls / msvcrt / environ.c
blobe8630007fb94a7ef280053690346ce08a1a35907
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 *MSVCRT_getenv(const char *name)
34 char **environ;
35 unsigned int length=strlen(name);
37 for (environ = *__p__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 *_wgetenv(const MSVCRT_wchar_t *name)
55 MSVCRT_wchar_t **environ;
56 unsigned int length=strlenW(name);
58 for (environ = *__p__wenviron(); *environ; environ++)
60 MSVCRT_wchar_t *str = *environ;
61 MSVCRT_wchar_t *pos = strchrW(str,'=');
62 if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
64 TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
65 return pos + 1;
68 return NULL;
71 /*********************************************************************
72 * _putenv (MSVCRT.@)
74 int _putenv(const char *str)
76 char name[256], value[512];
77 char *dst = name;
78 int ret;
80 TRACE("%s\n", str);
82 if (!str)
83 return -1;
84 while (*str && *str != '=')
85 *dst++ = *str++;
86 if (!*str++)
87 return -1;
88 *dst = '\0';
89 dst = value;
90 while (*str)
91 *dst++ = *str++;
92 *dst = '\0';
94 ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
96 /* _putenv returns success on deletion of non-existent variable, unlike [Rtl]SetEnvironmentVariable */
97 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
99 /* Update the __p__environ array only when already initialized */
100 if (_environ)
101 _environ = msvcrt_SnapshotOfEnvironmentA(_environ);
102 if (_wenviron)
103 _wenviron = msvcrt_SnapshotOfEnvironmentW(_wenviron);
104 return ret;
107 /*********************************************************************
108 * _wputenv (MSVCRT.@)
110 int _wputenv(const MSVCRT_wchar_t *str)
112 MSVCRT_wchar_t name[256], value[512];
113 MSVCRT_wchar_t *dst = name;
114 int ret;
116 TRACE("%s\n", debugstr_w(str));
118 if (!str)
119 return -1;
120 while (*str && *str != '=')
121 *dst++ = *str++;
122 if (!*str++)
123 return -1;
124 *dst = 0;
125 dst = value;
126 while (*str)
127 *dst++ = *str++;
128 *dst = 0;
130 ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
132 /* _putenv returns success on deletion of non-existent variable, unlike [Rtl]SetEnvironmentVariable */
133 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
135 /* Update the __p__environ array only when already initialized */
136 if (_environ)
137 _environ = msvcrt_SnapshotOfEnvironmentA(_environ);
138 if (_wenviron)
139 _wenviron = msvcrt_SnapshotOfEnvironmentW(_wenviron);
140 return ret;