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
= *__p__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 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));
71 /*********************************************************************
74 int CDECL
_putenv(const char *str
)
80 TRACE("%s\n", debugstr_a(str
));
85 name
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
89 while (*str
&& *str
!= '=')
102 ret
= SetEnvironmentVariableA(name
, value
[0] ? value
: NULL
) ? 0 : -1;
104 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
105 if ((ret
== -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND
)) ret
= 0;
107 MSVCRT__environ
= msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ
);
108 /* Update the __p__wenviron array only when already initialized */
109 if (MSVCRT__wenviron
)
110 MSVCRT__wenviron
= msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron
);
113 HeapFree(GetProcessHeap(), 0, name
);
117 /*********************************************************************
118 * _wputenv (MSVCRT.@)
120 int CDECL
_wputenv(const MSVCRT_wchar_t
*str
)
122 MSVCRT_wchar_t
*name
, *value
;
126 TRACE("%s\n", debugstr_w(str
));
130 name
= HeapAlloc(GetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(MSVCRT_wchar_t
));
134 while (*str
&& *str
!= '=')
147 ret
= SetEnvironmentVariableW(name
, value
[0] ? value
: NULL
) ? 0 : -1;
149 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
150 if ((ret
== -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND
)) ret
= 0;
152 MSVCRT__environ
= msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ
);
153 MSVCRT__wenviron
= msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron
);
156 HeapFree(GetProcessHeap(), 0, name
);