2 * msvcrt.dll dll data items
4 * Copyright 2000 Jon Griffiths
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32 int MSVCRT___argc = 0;
33 static int argc_expand;
34 static int wargc_expand;
35 unsigned int MSVCRT_basemajor = 0;/* FIXME: */
36 unsigned int MSVCRT_baseminor = 0;/* FIXME: */
37 unsigned int MSVCRT_baseversion = 0; /* FIXME: */
38 unsigned int MSVCRT__commode = 0;
39 unsigned int MSVCRT__fmode = 0;
40 unsigned int MSVCRT_osmajor = 0;/* FIXME: */
41 unsigned int MSVCRT_osminor = 0;/* FIXME: */
42 unsigned int MSVCRT_osmode = 0;/* FIXME: */
43 unsigned int MSVCRT__osver = 0;
44 unsigned int MSVCRT__osplatform = 0;
45 unsigned int MSVCRT_osversion = 0; /* FIXME: */
46 unsigned int MSVCRT__winmajor = 0;
47 unsigned int MSVCRT__winminor = 0;
48 unsigned int MSVCRT__winver = 0;
49 unsigned int MSVCRT___setlc_active = 0;
50 unsigned int MSVCRT___unguarded_readlc_active = 0;
51 double MSVCRT__HUGE = 0;
52 char **MSVCRT___argv = NULL;
53 static char **argv_expand;
54 MSVCRT_wchar_t **MSVCRT___wargv = NULL;
55 static MSVCRT_wchar_t **wargv_expand;
56 char *MSVCRT__acmdln = NULL;
57 MSVCRT_wchar_t *MSVCRT__wcmdln = NULL;
58 char **MSVCRT__environ = NULL;
59 MSVCRT_wchar_t **MSVCRT__wenviron = NULL;
60 char **MSVCRT___initenv = NULL;
61 MSVCRT_wchar_t **MSVCRT___winitenv = NULL;
62 int MSVCRT_app_type = 0;
63 char* MSVCRT__pgmptr = NULL;
64 WCHAR* MSVCRT__wpgmptr = NULL;
66 /* Get a snapshot of the current environment
67 * and construct the __p__environ array
69 * The pointer returned from GetEnvironmentStrings may get invalid when
70 * some other module cause a reallocation of the env-variable block
72 * blk is an array of pointers to environment strings, ending with a NULL
73 * and after that the actual copy of the environment strings, ending in a \0
75 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
77 char* environ_strings = GetEnvironmentStringsA();
78 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
81 for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
84 len += strlen(ptr) + 1;
87 blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
89 blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
95 memcpy(&blk[count],environ_strings,len);
96 for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
103 FreeEnvironmentStringsA(environ_strings);
107 MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
109 MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
110 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
111 MSVCRT_wchar_t *wptr;
113 for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
116 len += strlenW(wptr) + 1;
119 wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
121 wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
126 memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
127 for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
134 FreeEnvironmentStringsW(wenviron_strings);
138 typedef void (CDECL *_INITTERMFUN)(void);
139 typedef int (CDECL *_INITTERM_E_FN)(void);
141 /***********************************************************************
142 * __p___argc (MSVCRT.@)
144 int* CDECL __p___argc(void) { return &MSVCRT___argc; }
146 /***********************************************************************
147 * __p__commode (MSVCRT.@)
149 unsigned int* CDECL __p__commode(void) { return &MSVCRT__commode; }
152 /***********************************************************************
153 * __p__pgmptr (MSVCRT.@)
155 char** CDECL __p__pgmptr(void) { return &MSVCRT__pgmptr; }
157 /***********************************************************************
158 * __p__wpgmptr (MSVCRT.@)
160 WCHAR** CDECL __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }
162 /***********************************************************************
163 * _get_pgmptr (MSVCRT.@)
165 int CDECL _get_pgmptr(char** p)
167 if (!MSVCRT_CHECK_PMT(p)) return MSVCRT_EINVAL;
173 /***********************************************************************
174 * _get_wpgmptr (MSVCRT.@)
176 int CDECL _get_wpgmptr(WCHAR** p)
178 if (!MSVCRT_CHECK_PMT(p)) return MSVCRT_EINVAL;
179 *p = MSVCRT__wpgmptr;
183 /***********************************************************************
184 * __p__fmode (MSVCRT.@)
186 unsigned int* CDECL __p__fmode(void) { return &MSVCRT__fmode; }
188 /***********************************************************************
189 * __p__osver (MSVCRT.@)
191 unsigned int* CDECL __p__osver(void) { return &MSVCRT__osver; }
193 /***********************************************************************
194 * __p__winmajor (MSVCRT.@)
196 unsigned int* CDECL __p__winmajor(void) { return &MSVCRT__winmajor; }
198 /***********************************************************************
199 * __p__winminor (MSVCRT.@)
201 unsigned int* CDECL __p__winminor(void) { return &MSVCRT__winminor; }
203 /***********************************************************************
204 * __p__winver (MSVCRT.@)
206 unsigned int* CDECL __p__winver(void) { return &MSVCRT__winver; }
208 /*********************************************************************
209 * __p__acmdln (MSVCRT.@)
211 char** CDECL __p__acmdln(void) { return &MSVCRT__acmdln; }
213 /*********************************************************************
214 * __p__wcmdln (MSVCRT.@)
216 MSVCRT_wchar_t** CDECL __p__wcmdln(void) { return &MSVCRT__wcmdln; }
218 /*********************************************************************
219 * __p___argv (MSVCRT.@)
221 char*** CDECL __p___argv(void) { return &MSVCRT___argv; }
223 /*********************************************************************
224 * __p___wargv (MSVCRT.@)
226 MSVCRT_wchar_t*** CDECL __p___wargv(void) { return &MSVCRT___wargv; }
228 /*********************************************************************
229 * __p__environ (MSVCRT.@)
231 char*** CDECL MSVCRT___p__environ(void)
233 return &MSVCRT__environ;
236 /*********************************************************************
237 * __p__wenviron (MSVCRT.@)
239 MSVCRT_wchar_t*** CDECL MSVCRT___p__wenviron(void)
241 return &MSVCRT__wenviron;
244 /*********************************************************************
245 * __p___initenv (MSVCRT.@)
247 char*** CDECL __p___initenv(void) { return &MSVCRT___initenv; }
249 /*********************************************************************
250 * __p___winitenv (MSVCRT.@)
252 MSVCRT_wchar_t*** CDECL __p___winitenv(void) { return &MSVCRT___winitenv; }
254 /*********************************************************************
255 * _get_osplatform (MSVCRT.@)
257 int CDECL MSVCRT__get_osplatform(int *pValue)
259 if (!MSVCRT_CHECK_PMT(pValue != NULL)) return MSVCRT_EINVAL;
260 *pValue = MSVCRT__osplatform;
264 /* INTERNAL: Create a wide string from an ascii string */
265 MSVCRT_wchar_t *msvcrt_wstrdupa(const char *str)
267 const unsigned int len = strlen(str) + 1 ;
268 MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
271 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
275 /*********************************************************************
276 * ___unguarded_readlc_active_add_func (MSVCRT.@)
278 unsigned int * CDECL MSVCRT____unguarded_readlc_active_add_func(void)
280 return &MSVCRT___unguarded_readlc_active;
283 /*********************************************************************
284 * ___setlc_active_func (MSVCRT.@)
286 unsigned int CDECL MSVCRT____setlc_active_func(void)
288 return MSVCRT___setlc_active;
291 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
292 * we initialise data values during DLL loading. When called by a native
293 * program we simply return the data we've already initialised. This also means
294 * you can call multiple times without leaking
296 void msvcrt_init_args(void)
300 MSVCRT__acmdln = MSVCRT__strdup( GetCommandLineA() );
301 MSVCRT__wcmdln = msvcrt_wstrdupa(MSVCRT__acmdln);
302 MSVCRT___argc = __wine_main_argc;
303 MSVCRT___argv = __wine_main_argv;
304 MSVCRT___wargv = __wine_main_wargv;
306 TRACE("got %s, wide = %s argc=%d\n", debugstr_a(MSVCRT__acmdln),
307 debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
309 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
310 GetVersionExW( &osvi );
311 MSVCRT__winver = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
312 MSVCRT__winmajor = osvi.dwMajorVersion;
313 MSVCRT__winminor = osvi.dwMinorVersion;
314 MSVCRT__osver = osvi.dwBuildNumber;
315 MSVCRT__osplatform = osvi.dwPlatformId;
316 MSVCRT_osversion = MSVCRT__winver;
317 MSVCRT_osmajor = MSVCRT__winmajor;
318 MSVCRT_osminor = MSVCRT__winminor;
319 MSVCRT_baseversion = MSVCRT__osver;
320 MSVCRT_baseminor = MSVCRT_baseversion & 0xFF;
321 MSVCRT_basemajor = (MSVCRT_baseversion >> 8) & 0xFF;
322 TRACE( "winver %08x winmajor %08x winminor %08x osver%08x baseversion %08x basemajor %08x baseminor %08x\n",
323 MSVCRT__winver, MSVCRT__winmajor, MSVCRT__winminor, MSVCRT__osver, MSVCRT_baseversion,
324 MSVCRT_basemajor, MSVCRT_baseminor);
325 TRACE( "osversion %08x osmajor %08x osminor %08x\n", MSVCRT_osversion, MSVCRT_osmajor, MSVCRT_osminor);
327 MSVCRT__HUGE = HUGE_VAL;
328 MSVCRT___setlc_active = 0;
329 MSVCRT___unguarded_readlc_active = 0;
330 MSVCRT__fmode = MSVCRT__O_TEXT;
332 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
333 MSVCRT___initenv = msvcrt_SnapshotOfEnvironmentA(NULL);
334 MSVCRT___winitenv = msvcrt_SnapshotOfEnvironmentW(NULL);
336 MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
339 if (!GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH))
340 MSVCRT__pgmptr[0] = '\0';
342 MSVCRT__pgmptr[MAX_PATH - 1] = '\0';
345 MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
348 if (!GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH))
349 MSVCRT__wpgmptr[0] = '\0';
351 MSVCRT__wpgmptr[MAX_PATH - 1] = '\0';
355 /* INTERNAL: free memory used by args */
356 void msvcrt_free_args(void)
358 /* FIXME: more things to free */
359 HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
360 HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
361 HeapFree(GetProcessHeap(), 0, MSVCRT__environ);
362 HeapFree(GetProcessHeap(), 0, MSVCRT__wenviron);
363 HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
364 HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
365 HeapFree(GetProcessHeap(), 0, argv_expand);
366 HeapFree(GetProcessHeap(), 0, wargv_expand);
369 static int build_expanded_argv(int *argc, char **argv)
371 int i, size=0, args_no=0, path_len;
376 for(i=0; i<__wine_main_argc; i++) {
377 WIN32_FIND_DATAA data;
380 is_expandable = FALSE;
381 for(path_len = strlen(__wine_main_argv[i])-1; path_len>=0; path_len--) {
382 if(__wine_main_argv[i][path_len]=='*' || __wine_main_argv[i][path_len]=='?')
383 is_expandable = TRUE;
384 else if(__wine_main_argv[i][path_len]=='\\' || __wine_main_argv[i][path_len]=='/')
390 h = FindFirstFileA(__wine_main_argv[i], &data);
392 h = INVALID_HANDLE_VALUE;
394 if(h != INVALID_HANDLE_VALUE) {
396 if(data.cFileName[0]=='.' && (data.cFileName[1]=='\0' ||
397 (data.cFileName[1]=='.' && data.cFileName[2]=='\0')))
400 len = strlen(data.cFileName)+1;
402 argv[args_no] = (char*)(argv+*argc)+size;
403 memcpy(argv[args_no], __wine_main_argv[i], path_len*sizeof(char));
404 memcpy(argv[args_no]+path_len, data.cFileName, len*sizeof(char));
407 size += len+path_len;
408 }while(FindNextFileA(h, &data));
413 len = strlen(__wine_main_argv[i])+1;
415 argv[args_no] = (char*)(argv+*argc)+size;
416 memcpy(argv[args_no], __wine_main_argv[i], len*sizeof(char));
423 size += args_no*sizeof(char*);
428 /*********************************************************************
429 * __getmainargs (MSVCRT.@)
431 void CDECL __getmainargs(int *argc, char** *argv, char** *envp,
432 int expand_wildcards, int *new_mode)
434 TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
436 if (expand_wildcards) {
437 HeapFree(GetProcessHeap(), 0, argv_expand);
440 argv_expand = HeapAlloc(GetProcessHeap(), 0,
441 build_expanded_argv(&argc_expand, NULL));
443 build_expanded_argv(&argc_expand, argv_expand);
445 MSVCRT___argc = argc_expand;
446 MSVCRT___argv = argv_expand;
448 expand_wildcards = 0;
451 if (!expand_wildcards) {
452 MSVCRT___argc = __wine_main_argc;
453 MSVCRT___argv = __wine_main_argv;
456 *argc = MSVCRT___argc;
457 *argv = MSVCRT___argv;
458 *envp = MSVCRT___initenv;
461 MSVCRT__set_new_mode( *new_mode );
464 static int build_expanded_wargv(int *argc, MSVCRT_wchar_t **argv)
466 int i, size=0, args_no=0, path_len;
471 for(i=0; i<__wine_main_argc; i++) {
472 WIN32_FIND_DATAW data;
475 is_expandable = FALSE;
476 for(path_len = strlenW(__wine_main_wargv[i])-1; path_len>=0; path_len--) {
477 if(__wine_main_wargv[i][path_len]=='*' || __wine_main_wargv[i][path_len]=='?')
478 is_expandable = TRUE;
479 else if(__wine_main_wargv[i][path_len]=='\\' || __wine_main_wargv[i][path_len]=='/')
485 h = FindFirstFileW(__wine_main_wargv[i], &data);
487 h = INVALID_HANDLE_VALUE;
489 if(h != INVALID_HANDLE_VALUE) {
491 if(data.cFileName[0]=='.' && (data.cFileName[1]=='\0' ||
492 (data.cFileName[1]=='.' && data.cFileName[2]=='\0')))
495 len = strlenW(data.cFileName)+1;
497 argv[args_no] = (MSVCRT_wchar_t*)(argv+*argc)+size;
498 memcpy(argv[args_no], __wine_main_wargv[i], path_len*sizeof(MSVCRT_wchar_t));
499 memcpy(argv[args_no]+path_len, data.cFileName, len*sizeof(MSVCRT_wchar_t));
502 size += len+path_len;
503 }while(FindNextFileW(h, &data));
508 len = strlenW(__wine_main_wargv[i])+1;
510 argv[args_no] = (MSVCRT_wchar_t*)(argv+*argc)+size;
511 memcpy(argv[args_no], __wine_main_wargv[i], len*sizeof(MSVCRT_wchar_t));
518 size *= sizeof(MSVCRT_wchar_t);
519 size += args_no*sizeof(MSVCRT_wchar_t*);
524 /*********************************************************************
525 * __wgetmainargs (MSVCRT.@)
527 void CDECL __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
528 int expand_wildcards, int *new_mode)
530 TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
532 if (expand_wildcards) {
533 HeapFree(GetProcessHeap(), 0, wargv_expand);
536 wargv_expand = HeapAlloc(GetProcessHeap(), 0,
537 build_expanded_wargv(&wargc_expand, NULL));
539 build_expanded_wargv(&wargc_expand, wargv_expand);
541 MSVCRT___argc = wargc_expand;
542 MSVCRT___wargv = wargv_expand;
544 expand_wildcards = 0;
547 if (!expand_wildcards) {
548 MSVCRT___argc = __wine_main_argc;
549 MSVCRT___wargv = __wine_main_wargv;
552 /* Initialize the _wenviron array if it's not already created. */
553 if (!MSVCRT__wenviron)
554 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
555 *argc = MSVCRT___argc;
556 *wargv = MSVCRT___wargv;
557 *wenvp = MSVCRT___winitenv;
559 MSVCRT__set_new_mode( *new_mode );
562 /*********************************************************************
563 * _initterm (MSVCRT.@)
565 void CDECL _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
567 _INITTERMFUN* current = start;
569 TRACE("(%p,%p)\n",start,end);
574 TRACE("Call init function %p\n",*current);
582 /*********************************************************************
583 * _initterm_e (MSVCRT.@)
585 * call an array of application initialization functions and report the return value
587 int CDECL _initterm_e(_INITTERM_E_FN *table, _INITTERM_E_FN *end)
591 TRACE("(%p, %p)\n", table, end);
593 while (!res && table < end) {
595 TRACE("calling %p\n", **table);
598 TRACE("function %p failed: 0x%x\n", *table, res);
606 /*********************************************************************
607 * __set_app_type (MSVCRT.@)
609 void CDECL MSVCRT___set_app_type(int app_type)
611 TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
612 MSVCRT_app_type = app_type;