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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
22 | */
|
---|
23 | #ifdef __WIN32OS2__
|
---|
24 | #include <winbase.h>
|
---|
25 | #include <string.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #ifndef __MINIVCRT__
|
---|
29 |
|
---|
30 | #include "wine/unicode.h"
|
---|
31 | #include "msvcrt.h"
|
---|
32 |
|
---|
33 | #include "msvcrt/stddef.h"
|
---|
34 | #include "msvcrt/stdlib.h"
|
---|
35 |
|
---|
36 | #include "wine/debug.h"
|
---|
37 |
|
---|
38 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
---|
39 |
|
---|
40 | #else /* !__MINIVCRT__ */
|
---|
41 |
|
---|
42 | #include "minivcrt.h"
|
---|
43 | #include "minivcrt_internal.h"
|
---|
44 |
|
---|
45 | #include "winternl.h"
|
---|
46 | #include "wine/unicode.h"
|
---|
47 |
|
---|
48 | #endif /* !__MINIVCRT__ */
|
---|
49 |
|
---|
50 | #ifndef __MINIVCRT__
|
---|
51 |
|
---|
52 | /*********************************************************************
|
---|
53 | * getenv (MSVCRT.@)
|
---|
54 | */
|
---|
55 | char *MSVCRT_getenv(const char *name)
|
---|
56 | {
|
---|
57 | char *environ = GetEnvironmentStringsA();
|
---|
58 | char *pp,*pos = NULL;
|
---|
59 | unsigned int length=strlen(name);
|
---|
60 |
|
---|
61 | dprintf(("MSVCRT: %s",name));
|
---|
62 |
|
---|
63 | for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
|
---|
64 | {
|
---|
65 | pos =strchr(pp,'=');
|
---|
66 | if ((pos) && ((pos - pp) == length))
|
---|
67 | {
|
---|
68 | if (!_strnicmp(pp,name,length)) break;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | if ((*pp)&& (pos))
|
---|
72 | {
|
---|
73 | pp = pos+1;
|
---|
74 | TRACE("got %s\n",pp);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | pp = 0;
|
---|
78 | FreeEnvironmentStringsA( environ );
|
---|
79 | return pp;
|
---|
80 | }
|
---|
81 |
|
---|
82 | #endif /* !__MINIVCRT__ */
|
---|
83 |
|
---|
84 | /*********************************************************************
|
---|
85 | * _wgetenv (MSVCRT.@)
|
---|
86 | */
|
---|
87 | MSVCRT_wchar_t *_wgetenv(const MSVCRT_wchar_t *name)
|
---|
88 | {
|
---|
89 | MSVCRT_wchar_t* environ = GetEnvironmentStringsW();
|
---|
90 | MSVCRT_wchar_t* pp,*pos = NULL;
|
---|
91 | unsigned int length=strlenW(name);
|
---|
92 |
|
---|
93 | dprintf(("MSVCRT: _wgetenv %s",debugstr_w(name)));
|
---|
94 |
|
---|
95 | for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
|
---|
96 | {
|
---|
97 | pos = strchrW(pp,'=');
|
---|
98 | if ((pos) && ((pos - pp) == length))
|
---|
99 | {
|
---|
100 | if (!strncmpiW(pp,name,length))
|
---|
101 | {
|
---|
102 | pp = pos+1;
|
---|
103 | TRACE("got %s\n",debugstr_w(pp));
|
---|
104 | /* can't free pointer since we are returning it */
|
---|
105 | /* should probably use MSVCRT_wenviron instead */
|
---|
106 | FIXME( "memory leak\n" );
|
---|
107 | return pp;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | FreeEnvironmentStringsW( environ );
|
---|
112 | return NULL;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #ifndef __MINIVCRT__
|
---|
116 |
|
---|
117 | /*********************************************************************
|
---|
118 | * _putenv (MSVCRT.@)
|
---|
119 | */
|
---|
120 | int MSVCRT__putenv(const char *str)
|
---|
121 | {
|
---|
122 | char name[256], value[512];
|
---|
123 | char *dst = name;
|
---|
124 | int ret;
|
---|
125 |
|
---|
126 | TRACE("%s\n", str);
|
---|
127 |
|
---|
128 | if (!str)
|
---|
129 | return -1;
|
---|
130 | while (*str && *str != '=')
|
---|
131 | *dst++ = *str++;
|
---|
132 | if (!*str++)
|
---|
133 | return -1;
|
---|
134 | *dst = '\0';
|
---|
135 | dst = value;
|
---|
136 | while (*str)
|
---|
137 | *dst++ = *str++;
|
---|
138 | *dst = '\0';
|
---|
139 |
|
---|
140 | ret = !SetEnvironmentVariableA(name, value[0] ? value : NULL);
|
---|
141 | /* Update the __p__environ array only when already initialized */
|
---|
142 | if (MSVCRT__environ)
|
---|
143 | MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
---|
144 | if (MSVCRT__wenviron)
|
---|
145 | MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
---|
146 | return ret;
|
---|
147 | }
|
---|
148 |
|
---|
149 | #endif /* !__MINIVCRT__ */
|
---|
150 |
|
---|
151 | /*********************************************************************
|
---|
152 | * _wputenv (MSVCRT.@)
|
---|
153 | */
|
---|
154 | int _wputenv(const MSVCRT_wchar_t *str)
|
---|
155 | {
|
---|
156 | MSVCRT_wchar_t name[256], value[512];
|
---|
157 | MSVCRT_wchar_t *dst = name;
|
---|
158 | int ret;
|
---|
159 |
|
---|
160 | TRACE("%s\n", debugstr_w(str));
|
---|
161 |
|
---|
162 | if (!str)
|
---|
163 | return -1;
|
---|
164 | while (*str && *str != '=')
|
---|
165 | *dst++ = *str++;
|
---|
166 | if (!*str++)
|
---|
167 | return -1;
|
---|
168 | *dst = 0;
|
---|
169 | dst = value;
|
---|
170 | while (*str)
|
---|
171 | *dst++ = *str++;
|
---|
172 | *dst = 0;
|
---|
173 |
|
---|
174 | ret = !SetEnvironmentVariableW(name, value[0] ? value : NULL);
|
---|
175 | #ifndef __MINIVCRT__
|
---|
176 | /* Update the __p__environ array only when already initialized */
|
---|
177 | if (MSVCRT__environ)
|
---|
178 | MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
---|
179 | if (MSVCRT__wenviron)
|
---|
180 | MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
---|
181 | #endif /* !__MINIVCRT__ */
|
---|
182 | return ret;
|
---|
183 | }
|
---|