Commit | Line | Data |
---|---|---|
1db20bfd JG |
1 | /* |
2 | * msvcrt.dll dll data items | |
3 | * | |
4 | * Copyright 2000 Jon Griffiths | |
0799c1a7 AJ |
5 | * |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
1db20bfd | 19 | */ |
5769d1de AJ |
20 | |
21 | #include "config.h" | |
22 | #include "wine/port.h" | |
23 | ||
1db20bfd JG |
24 | #include <math.h> |
25 | #include "msvcrt.h" | |
26 | ||
e7f75c5d FG |
27 | #include "msvcrt/stdlib.h" |
28 | #include "msvcrt/string.h" | |
29 | ||
702b158e | 30 | #include "wine/library.h" |
2f61c4a1 | 31 | #include "wine/unicode.h" |
bd1689ec AJ |
32 | #include "wine/debug.h" |
33 | ||
34 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); | |
1db20bfd JG |
35 | |
36 | unsigned int MSVCRT___argc; | |
37 | unsigned int MSVCRT_basemajor;/* FIXME: */ | |
38 | unsigned int MSVCRT_baseminor;/* FIXME: */ | |
39 | unsigned int MSVCRT_baseversion; /* FIXME: */ | |
40 | unsigned int MSVCRT__commode; | |
41 | unsigned int MSVCRT__fmode; | |
42 | unsigned int MSVCRT_osmajor;/* FIXME: */ | |
43 | unsigned int MSVCRT_osminor;/* FIXME: */ | |
44 | unsigned int MSVCRT_osmode;/* FIXME: */ | |
45 | unsigned int MSVCRT__osver; | |
46 | unsigned int MSVCRT_osversion; /* FIXME: */ | |
47 | unsigned int MSVCRT__winmajor; | |
48 | unsigned int MSVCRT__winminor; | |
49 | unsigned int MSVCRT__winver; | |
e7f75c5d FG |
50 | unsigned int MSVCRT__sys_nerr; /* FIXME: not accessible from Winelib apps */ |
51 | char** MSVCRT__sys_errlist; /* FIXME: not accessible from Winelib apps */ | |
1db20bfd JG |
52 | unsigned int MSVCRT___setlc_active; |
53 | unsigned int MSVCRT___unguarded_readlc_active; | |
54 | double MSVCRT__HUGE; | |
55 | char **MSVCRT___argv; | |
5f31b329 | 56 | MSVCRT_wchar_t **MSVCRT___wargv; |
1db20bfd | 57 | char *MSVCRT__acmdln; |
5f31b329 | 58 | MSVCRT_wchar_t *MSVCRT__wcmdln; |
2f61c4a1 | 59 | char **MSVCRT__environ = 0; |
5f31b329 | 60 | MSVCRT_wchar_t **MSVCRT__wenviron = 0; |
2f61c4a1 | 61 | char **MSVCRT___initenv = 0; |
5f31b329 | 62 | MSVCRT_wchar_t **MSVCRT___winitenv = 0; |
1db20bfd JG |
63 | int MSVCRT_timezone; |
64 | int MSVCRT_app_type; | |
57915f50 | 65 | char* MSVCRT__pgmptr = 0; |
622a343e | 66 | WCHAR* MSVCRT__wpgmptr = 0; |
1db20bfd | 67 | |
2f61c4a1 UB |
68 | /* Get a snapshot of the current environment |
69 | * and construct the __p__environ array | |
70 | * | |
71 | * The pointer returned from GetEnvironmentStrings may get invalid when | |
72 | * some other module cause a reallocation of the env-variable block | |
73 | * | |
74 | * blk is an array of pointers to environment strings, ending with a NULL | |
75 | * and after that the actual copy of the environment strings, ending in a \0 | |
76 | */ | |
77 | char ** msvcrt_SnapshotOfEnvironmentA(char **blk) | |
78 | { | |
79 | char* environ_strings = GetEnvironmentStringsA(); | |
80 | int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */ | |
81 | char *ptr; | |
82 | ||
83 | for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1) | |
84 | { | |
85 | count++; | |
86 | len += strlen(ptr) + 1; | |
87 | } | |
88 | if (blk) | |
89 | blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len ); | |
90 | else | |
91 | blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len ); | |
92 | ||
93 | if (blk) | |
94 | { | |
95 | if (count) | |
96 | { | |
97 | memcpy(&blk[count],environ_strings,len); | |
98 | for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1) | |
99 | { | |
100 | blk[i++] = ptr; | |
101 | } | |
102 | } | |
103 | blk[i] = NULL; | |
104 | } | |
105 | FreeEnvironmentStringsA(environ_strings); | |
106 | return blk; | |
107 | } | |
108 | ||
5f31b329 | 109 | MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk) |
2f61c4a1 | 110 | { |
5f31b329 | 111 | MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW(); |
2f61c4a1 | 112 | int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */ |
5f31b329 | 113 | MSVCRT_wchar_t *wptr; |
2f61c4a1 UB |
114 | |
115 | for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1) | |
116 | { | |
117 | count++; | |
118 | len += strlenW(wptr) + 1; | |
119 | } | |
120 | if (wblk) | |
5f31b329 | 121 | wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t)); |
2f61c4a1 | 122 | else |
5f31b329 | 123 | wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t)); |
2f61c4a1 UB |
124 | if (wblk) |
125 | { | |
126 | if (count) | |
127 | { | |
5f31b329 AJ |
128 | memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t)); |
129 | for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1) | |
2f61c4a1 UB |
130 | { |
131 | wblk[i++] = wptr; | |
132 | } | |
133 | } | |
134 | wblk[i] = NULL; | |
135 | } | |
136 | FreeEnvironmentStringsW(wenviron_strings); | |
137 | return wblk; | |
138 | } | |
30a3d18a | 139 | |
203a8f82 | 140 | typedef void (*_INITTERMFUN)(void); |
1db20bfd | 141 | |
4fb3aa5f PS |
142 | /*********************************************************************** |
143 | * __p___argc (MSVCRT.@) | |
144 | */ | |
e7f75c5d | 145 | int* __p___argc(void) { return &MSVCRT___argc; } |
4fb3aa5f PS |
146 | |
147 | /*********************************************************************** | |
148 | * __p__commode (MSVCRT.@) | |
149 | */ | |
203a8f82 | 150 | unsigned int* __p__commode(void) { return &MSVCRT__commode; } |
4fb3aa5f | 151 | |
57915f50 JG |
152 | |
153 | /*********************************************************************** | |
154 | * __p__pgmptr (MSVCRT.@) | |
155 | */ | |
156 | char** __p__pgmptr(void) { return &MSVCRT__pgmptr; } | |
157 | ||
622a343e JG |
158 | /*********************************************************************** |
159 | * __p__wpgmptr (MSVCRT.@) | |
160 | */ | |
161 | WCHAR** __p__wpgmptr(void) { return &MSVCRT__wpgmptr; } | |
162 | ||
4fb3aa5f PS |
163 | /*********************************************************************** |
164 | * __p__fmode (MSVCRT.@) | |
165 | */ | |
203a8f82 | 166 | unsigned int* __p__fmode(void) { return &MSVCRT__fmode; } |
4fb3aa5f PS |
167 | |
168 | /*********************************************************************** | |
169 | * __p__osver (MSVCRT.@) | |
170 | */ | |
203a8f82 | 171 | unsigned int* __p__osver(void) { return &MSVCRT__osver; } |
4fb3aa5f PS |
172 | |
173 | /*********************************************************************** | |
174 | * __p__winmajor (MSVCRT.@) | |
175 | */ | |
203a8f82 | 176 | unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; } |
4fb3aa5f PS |
177 | |
178 | /*********************************************************************** | |
179 | * __p__winminor (MSVCRT.@) | |
180 | */ | |
203a8f82 | 181 | unsigned int* __p__winminor(void) { return &MSVCRT__winminor; } |
4fb3aa5f PS |
182 | |
183 | /*********************************************************************** | |
184 | * __p__winver (MSVCRT.@) | |
185 | */ | |
203a8f82 | 186 | unsigned int* __p__winver(void) { return &MSVCRT__winver; } |
34c786b2 JG |
187 | |
188 | /********************************************************************* | |
189 | * __p__acmdln (MSVCRT.@) | |
190 | */ | |
203a8f82 | 191 | char** __p__acmdln(void) { return &MSVCRT__acmdln; } |
34c786b2 JG |
192 | |
193 | /********************************************************************* | |
194 | * __p__wcmdln (MSVCRT.@) | |
195 | */ | |
5f31b329 | 196 | MSVCRT_wchar_t** __p__wcmdln(void) { return &MSVCRT__wcmdln; } |
34c786b2 JG |
197 | |
198 | /********************************************************************* | |
199 | * __p___argv (MSVCRT.@) | |
200 | */ | |
203a8f82 | 201 | char*** __p___argv(void) { return &MSVCRT___argv; } |
34c786b2 JG |
202 | |
203 | /********************************************************************* | |
204 | * __p___wargv (MSVCRT.@) | |
205 | */ | |
5f31b329 | 206 | MSVCRT_wchar_t*** __p___wargv(void) { return &MSVCRT___wargv; } |
34c786b2 JG |
207 | |
208 | /********************************************************************* | |
209 | * __p__environ (MSVCRT.@) | |
210 | */ | |
2f61c4a1 UB |
211 | char*** __p__environ(void) |
212 | { | |
213 | if (!MSVCRT__environ) | |
214 | MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL); | |
215 | return &MSVCRT__environ; | |
216 | } | |
34c786b2 JG |
217 | |
218 | /********************************************************************* | |
219 | * __p__wenviron (MSVCRT.@) | |
220 | */ | |
5f31b329 | 221 | MSVCRT_wchar_t*** __p__wenviron(void) |
2f61c4a1 UB |
222 | { |
223 | if (!MSVCRT__wenviron) | |
224 | MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL); | |
225 | return &MSVCRT__wenviron; | |
226 | } | |
34c786b2 JG |
227 | |
228 | /********************************************************************* | |
229 | * __p___initenv (MSVCRT.@) | |
230 | */ | |
203a8f82 | 231 | char*** __p___initenv(void) { return &MSVCRT___initenv; } |
34c786b2 JG |
232 | |
233 | /********************************************************************* | |
234 | * __p___winitenv (MSVCRT.@) | |
235 | */ | |
5f31b329 | 236 | MSVCRT_wchar_t*** __p___winitenv(void) { return &MSVCRT___winitenv; } |
34c786b2 JG |
237 | |
238 | /********************************************************************* | |
239 | * __p__timezone (MSVCRT.@) | |
240 | */ | |
203a8f82 | 241 | int* __p__timezone(void) { return &MSVCRT_timezone; } |
1db20bfd JG |
242 | |
243 | /* INTERNAL: Create a wide string from an ascii string */ | |
5f31b329 | 244 | static MSVCRT_wchar_t *wstrdupa(const char *str) |
1db20bfd JG |
245 | { |
246 | const size_t len = strlen(str) + 1 ; | |
5f31b329 | 247 | MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t)); |
1db20bfd JG |
248 | if (!wstr) |
249 | return NULL; | |
5f31b329 | 250 | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len); |
1db20bfd JG |
251 | return wstr; |
252 | } | |
253 | ||
254 | /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs, | |
30a3d18a | 255 | * we initialise data values during DLL loading. When called by a native |
1db20bfd JG |
256 | * program we simply return the data we've already initialised. This also means |
257 | * you can call multiple times without leaking | |
258 | */ | |
203a8f82 | 259 | void msvcrt_init_args(void) |
1db20bfd | 260 | { |
1db20bfd JG |
261 | DWORD version; |
262 | ||
203a8f82 | 263 | MSVCRT__acmdln = _strdup( GetCommandLineA() ); |
20560f45 | 264 | MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln); |
702b158e AJ |
265 | MSVCRT___argc = __wine_main_argc; |
266 | MSVCRT___argv = __wine_main_argv; | |
267 | MSVCRT___wargv = __wine_main_wargv; | |
5ab5e1c5 | 268 | |
20560f45 FG |
269 | TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln, |
270 | debugstr_w(MSVCRT__wcmdln),MSVCRT___argc); | |
1db20bfd JG |
271 | |
272 | version = GetVersion(); | |
273 | MSVCRT__osver = version >> 16; | |
274 | MSVCRT__winminor = version & 0xFF; | |
275 | MSVCRT__winmajor = (version>>8) & 0xFF; | |
276 | MSVCRT_baseversion = version >> 16; | |
277 | MSVCRT__winver = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8); | |
278 | MSVCRT_baseminor = (version >> 16) & 0xFF; | |
279 | MSVCRT_basemajor = (version >> 24) & 0xFF; | |
280 | MSVCRT_osversion = version & 0xFFFF; | |
281 | MSVCRT_osminor = version & 0xFF; | |
282 | MSVCRT_osmajor = (version>>8) & 0xFF; | |
283 | MSVCRT__sys_nerr = 43; | |
284 | MSVCRT__HUGE = HUGE_VAL; | |
285 | MSVCRT___setlc_active = 0; | |
286 | MSVCRT___unguarded_readlc_active = 0; | |
287 | MSVCRT_timezone = 0; | |
288 | ||
2f61c4a1 UB |
289 | MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL); |
290 | MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL); | |
2f6744b3 | 291 | |
622a343e JG |
292 | MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH); |
293 | if (MSVCRT__pgmptr) | |
294 | GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH); | |
295 | ||
296 | MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR)); | |
297 | if (MSVCRT__wpgmptr) | |
298 | GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH); | |
1db20bfd JG |
299 | } |
300 | ||
301 | ||
302 | /* INTERNAL: free memory used by args */ | |
203a8f82 | 303 | void msvcrt_free_args(void) |
1db20bfd | 304 | { |
30a3d18a | 305 | /* FIXME: more things to free */ |
622a343e JG |
306 | if (MSVCRT___initenv) HeapFree(GetProcessHeap(), 0, MSVCRT___initenv); |
307 | if (MSVCRT___winitenv) HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv); | |
308 | if (MSVCRT__environ) HeapFree(GetProcessHeap(), 0, MSVCRT__environ); | |
309 | if (MSVCRT__wenviron) HeapFree(GetProcessHeap(), 0, MSVCRT__wenviron); | |
310 | if (MSVCRT__pgmptr) HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr); | |
311 | if (MSVCRT__wpgmptr) HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr); | |
1db20bfd JG |
312 | } |
313 | ||
314 | /********************************************************************* | |
315 | * __getmainargs (MSVCRT.@) | |
316 | */ | |
e7f75c5d | 317 | void __getmainargs(int *argc, char** *argv, char** *envp, |
3fe81b46 | 318 | int expand_wildcards, int *new_mode) |
1db20bfd | 319 | { |
e7f75c5d | 320 | TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode); |
1db20bfd JG |
321 | *argc = MSVCRT___argc; |
322 | *argv = MSVCRT___argv; | |
90423aa3 | 323 | *envp = MSVCRT___initenv; |
d82f35ed UB |
324 | if (new_mode) |
325 | MSVCRT__set_new_mode( *new_mode ); | |
1db20bfd JG |
326 | } |
327 | ||
328 | /********************************************************************* | |
329 | * __wgetmainargs (MSVCRT.@) | |
330 | */ | |
5f31b329 AJ |
331 | void __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp, |
332 | int expand_wildcards, int *new_mode) | |
1db20bfd | 333 | { |
e7f75c5d | 334 | TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode); |
1db20bfd JG |
335 | *argc = MSVCRT___argc; |
336 | *wargv = MSVCRT___wargv; | |
90423aa3 | 337 | *wenvp = MSVCRT___winitenv; |
d82f35ed UB |
338 | if (new_mode) |
339 | MSVCRT__set_new_mode( *new_mode ); | |
1db20bfd JG |
340 | } |
341 | ||
342 | /********************************************************************* | |
343 | * _initterm (MSVCRT.@) | |
344 | */ | |
203a8f82 | 345 | unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end) |
1db20bfd | 346 | { |
203a8f82 | 347 | _INITTERMFUN* current = start; |
1db20bfd JG |
348 | |
349 | TRACE("(%p,%p)\n",start,end); | |
350 | while (current<end) | |
351 | { | |
352 | if (*current) | |
353 | { | |
354 | TRACE("Call init function %p\n",*current); | |
355 | (**current)(); | |
356 | TRACE("returned\n"); | |
357 | } | |
358 | current++; | |
359 | } | |
360 | return 0; | |
361 | } | |
362 | ||
363 | /********************************************************************* | |
364 | * __set_app_type (MSVCRT.@) | |
365 | */ | |
203a8f82 | 366 | void MSVCRT___set_app_type(int app_type) |
1db20bfd JG |
367 | { |
368 | TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console"); | |
369 | MSVCRT_app_type = app_type; | |
370 | } |