msvcrt: Support expand_wildcards parameter in __getmainargs function.
[wine.git] / dlls / msvcrt / data.c
blob2dc393caf8a50e64bbc09b4101297f6b8f8bec3a
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <math.h>
25 #include "msvcrt.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 unsigned int MSVCRT_basemajor = 0;/* FIXME: */
35 unsigned int MSVCRT_baseminor = 0;/* FIXME: */
36 unsigned int MSVCRT_baseversion = 0; /* FIXME: */
37 unsigned int MSVCRT__commode = 0;
38 unsigned int MSVCRT__fmode = 0;
39 unsigned int MSVCRT_osmajor = 0;/* FIXME: */
40 unsigned int MSVCRT_osminor = 0;/* FIXME: */
41 unsigned int MSVCRT_osmode = 0;/* FIXME: */
42 unsigned int MSVCRT__osver = 0;
43 unsigned int MSVCRT__osplatform = 0;
44 unsigned int MSVCRT_osversion = 0; /* FIXME: */
45 unsigned int MSVCRT__winmajor = 0;
46 unsigned int MSVCRT__winminor = 0;
47 unsigned int MSVCRT__winver = 0;
48 unsigned int MSVCRT___setlc_active = 0;
49 unsigned int MSVCRT___unguarded_readlc_active = 0;
50 double MSVCRT__HUGE = 0;
51 char **MSVCRT___argv = NULL;
52 static char **argv_expand;
53 MSVCRT_wchar_t **MSVCRT___wargv = NULL;
54 char *MSVCRT__acmdln = NULL;
55 MSVCRT_wchar_t *MSVCRT__wcmdln = NULL;
56 char **MSVCRT__environ = NULL;
57 MSVCRT_wchar_t **MSVCRT__wenviron = NULL;
58 char **MSVCRT___initenv = NULL;
59 MSVCRT_wchar_t **MSVCRT___winitenv = NULL;
60 int MSVCRT_app_type = 0;
61 char* MSVCRT__pgmptr = NULL;
62 WCHAR* MSVCRT__wpgmptr = NULL;
64 /* Get a snapshot of the current environment
65 * and construct the __p__environ array
67 * The pointer returned from GetEnvironmentStrings may get invalid when
68 * some other module cause a reallocation of the env-variable block
70 * blk is an array of pointers to environment strings, ending with a NULL
71 * and after that the actual copy of the environment strings, ending in a \0
73 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
75 char* environ_strings = GetEnvironmentStringsA();
76 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
77 char *ptr;
79 for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
81 count++;
82 len += strlen(ptr) + 1;
84 if (blk)
85 blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
86 else
87 blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
89 if (blk)
91 if (count)
93 memcpy(&blk[count],environ_strings,len);
94 for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
96 blk[i++] = ptr;
99 blk[i] = NULL;
101 FreeEnvironmentStringsA(environ_strings);
102 return blk;
105 MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
107 MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
108 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
109 MSVCRT_wchar_t *wptr;
111 for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
113 count++;
114 len += strlenW(wptr) + 1;
116 if (wblk)
117 wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
118 else
119 wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
120 if (wblk)
122 if (count)
124 memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
125 for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
127 wblk[i++] = wptr;
130 wblk[i] = NULL;
132 FreeEnvironmentStringsW(wenviron_strings);
133 return wblk;
136 typedef void (CDECL *_INITTERMFUN)(void);
137 typedef int (CDECL *_INITTERM_E_FN)(void);
139 /***********************************************************************
140 * __p___argc (MSVCRT.@)
142 int* CDECL __p___argc(void) { return &MSVCRT___argc; }
144 /***********************************************************************
145 * __p__commode (MSVCRT.@)
147 unsigned int* CDECL __p__commode(void) { return &MSVCRT__commode; }
150 /***********************************************************************
151 * __p__pgmptr (MSVCRT.@)
153 char** CDECL __p__pgmptr(void) { return &MSVCRT__pgmptr; }
155 /***********************************************************************
156 * __p__wpgmptr (MSVCRT.@)
158 WCHAR** CDECL __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }
160 /***********************************************************************
161 * _get_pgmptr (MSVCRT.@)
163 int CDECL _get_pgmptr(char** p)
165 if (!MSVCRT_CHECK_PMT(p)) return MSVCRT_EINVAL;
167 *p = MSVCRT__pgmptr;
168 return 0;
171 /***********************************************************************
172 * _get_wpgmptr (MSVCRT.@)
174 int CDECL _get_wpgmptr(WCHAR** p)
176 if (!MSVCRT_CHECK_PMT(p)) return MSVCRT_EINVAL;
177 *p = MSVCRT__wpgmptr;
178 return 0;
181 /***********************************************************************
182 * __p__fmode (MSVCRT.@)
184 unsigned int* CDECL __p__fmode(void) { return &MSVCRT__fmode; }
186 /***********************************************************************
187 * __p__osver (MSVCRT.@)
189 unsigned int* CDECL __p__osver(void) { return &MSVCRT__osver; }
191 /***********************************************************************
192 * __p__winmajor (MSVCRT.@)
194 unsigned int* CDECL __p__winmajor(void) { return &MSVCRT__winmajor; }
196 /***********************************************************************
197 * __p__winminor (MSVCRT.@)
199 unsigned int* CDECL __p__winminor(void) { return &MSVCRT__winminor; }
201 /***********************************************************************
202 * __p__winver (MSVCRT.@)
204 unsigned int* CDECL __p__winver(void) { return &MSVCRT__winver; }
206 /*********************************************************************
207 * __p__acmdln (MSVCRT.@)
209 char** CDECL __p__acmdln(void) { return &MSVCRT__acmdln; }
211 /*********************************************************************
212 * __p__wcmdln (MSVCRT.@)
214 MSVCRT_wchar_t** CDECL __p__wcmdln(void) { return &MSVCRT__wcmdln; }
216 /*********************************************************************
217 * __p___argv (MSVCRT.@)
219 char*** CDECL __p___argv(void) { return &MSVCRT___argv; }
221 /*********************************************************************
222 * __p___wargv (MSVCRT.@)
224 MSVCRT_wchar_t*** CDECL __p___wargv(void) { return &MSVCRT___wargv; }
226 /*********************************************************************
227 * __p__environ (MSVCRT.@)
229 char*** CDECL MSVCRT___p__environ(void)
231 return &MSVCRT__environ;
234 /*********************************************************************
235 * __p__wenviron (MSVCRT.@)
237 MSVCRT_wchar_t*** CDECL MSVCRT___p__wenviron(void)
239 return &MSVCRT__wenviron;
242 /*********************************************************************
243 * __p___initenv (MSVCRT.@)
245 char*** CDECL __p___initenv(void) { return &MSVCRT___initenv; }
247 /*********************************************************************
248 * __p___winitenv (MSVCRT.@)
250 MSVCRT_wchar_t*** CDECL __p___winitenv(void) { return &MSVCRT___winitenv; }
252 /*********************************************************************
253 * _get_osplatform (MSVCRT.@)
255 int CDECL MSVCRT__get_osplatform(int *pValue)
257 if (!MSVCRT_CHECK_PMT(pValue != NULL)) return MSVCRT_EINVAL;
258 *pValue = MSVCRT__osplatform;
259 return 0;
262 /* INTERNAL: Create a wide string from an ascii string */
263 MSVCRT_wchar_t *msvcrt_wstrdupa(const char *str)
265 const unsigned int len = strlen(str) + 1 ;
266 MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
267 if (!wstr)
268 return NULL;
269 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
270 return wstr;
273 /*********************************************************************
274 * ___unguarded_readlc_active_add_func (MSVCRT.@)
276 unsigned int * CDECL MSVCRT____unguarded_readlc_active_add_func(void)
278 return &MSVCRT___unguarded_readlc_active;
281 /*********************************************************************
282 * ___setlc_active_func (MSVCRT.@)
284 unsigned int CDECL MSVCRT____setlc_active_func(void)
286 return MSVCRT___setlc_active;
289 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
290 * we initialise data values during DLL loading. When called by a native
291 * program we simply return the data we've already initialised. This also means
292 * you can call multiple times without leaking
294 void msvcrt_init_args(void)
296 OSVERSIONINFOW osvi;
298 MSVCRT__acmdln = MSVCRT__strdup( GetCommandLineA() );
299 MSVCRT__wcmdln = msvcrt_wstrdupa(MSVCRT__acmdln);
300 MSVCRT___argc = __wine_main_argc;
301 MSVCRT___argv = __wine_main_argv;
302 MSVCRT___wargv = __wine_main_wargv;
304 TRACE("got %s, wide = %s argc=%d\n", debugstr_a(MSVCRT__acmdln),
305 debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
307 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
308 GetVersionExW( &osvi );
309 MSVCRT__winver = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
310 MSVCRT__winmajor = osvi.dwMajorVersion;
311 MSVCRT__winminor = osvi.dwMinorVersion;
312 MSVCRT__osver = osvi.dwBuildNumber;
313 MSVCRT__osplatform = osvi.dwPlatformId;
314 MSVCRT_osversion = MSVCRT__winver;
315 MSVCRT_osmajor = MSVCRT__winmajor;
316 MSVCRT_osminor = MSVCRT__winminor;
317 MSVCRT_baseversion = MSVCRT__osver;
318 MSVCRT_baseminor = MSVCRT_baseversion & 0xFF;
319 MSVCRT_basemajor = (MSVCRT_baseversion >> 8) & 0xFF;
320 TRACE( "winver %08x winmajor %08x winminor %08x osver%08x baseversion %08x basemajor %08x baseminor %08x\n",
321 MSVCRT__winver, MSVCRT__winmajor, MSVCRT__winminor, MSVCRT__osver, MSVCRT_baseversion,
322 MSVCRT_basemajor, MSVCRT_baseminor);
323 TRACE( "osversion %08x osmajor %08x osminor %08x\n", MSVCRT_osversion, MSVCRT_osmajor, MSVCRT_osminor);
325 MSVCRT__HUGE = HUGE_VAL;
326 MSVCRT___setlc_active = 0;
327 MSVCRT___unguarded_readlc_active = 0;
328 MSVCRT__fmode = MSVCRT__O_TEXT;
330 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
331 MSVCRT___initenv = msvcrt_SnapshotOfEnvironmentA(NULL);
332 MSVCRT___winitenv = msvcrt_SnapshotOfEnvironmentW(NULL);
334 MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
335 if (MSVCRT__pgmptr)
337 if (!GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH))
338 MSVCRT__pgmptr[0] = '\0';
339 else
340 MSVCRT__pgmptr[MAX_PATH - 1] = '\0';
343 MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
344 if (MSVCRT__wpgmptr)
346 if (!GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH))
347 MSVCRT__wpgmptr[0] = '\0';
348 else
349 MSVCRT__wpgmptr[MAX_PATH - 1] = '\0';
353 /* INTERNAL: free memory used by args */
354 void msvcrt_free_args(void)
356 /* FIXME: more things to free */
357 HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
358 HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
359 HeapFree(GetProcessHeap(), 0, MSVCRT__environ);
360 HeapFree(GetProcessHeap(), 0, MSVCRT__wenviron);
361 HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
362 HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
363 HeapFree(GetProcessHeap(), 0, argv_expand);
366 static int build_expanded_argv(int *argc, char **argv)
368 int i, size=0, args_no=0;
369 HANDLE h;
371 args_no = 0;
372 for(i=0; i<__wine_main_argc; i++) {
373 WIN32_FIND_DATAA data;
374 int len = 0;
376 h = FindFirstFileA(__wine_main_argv[i], &data);
377 if(h != INVALID_HANDLE_VALUE) {
378 do {
379 if(data.cFileName[0]=='.' && (data.cFileName[1]=='\0' ||
380 (data.cFileName[1]=='.' && data.cFileName[2]=='\0')))
381 continue;
383 len = strlen(data.cFileName)+1;
384 if(argv) {
385 argv[args_no] = (char*)(argv+*argc)+size;
386 memcpy(argv[args_no], data.cFileName, len*sizeof(char));
388 args_no++;
389 size += len;
390 }while(FindNextFileA(h, &data));
391 CloseHandle(h);
394 if(!len) {
395 len = strlen(__wine_main_argv[i])+1;
396 if(argv) {
397 argv[args_no] = (char*)(argv+*argc)+size;
398 memcpy(argv[args_no], __wine_main_argv[i], len*sizeof(char));
400 args_no++;
401 size += len;
405 size += args_no*sizeof(char*);
406 *argc = args_no;
407 return size;
410 /*********************************************************************
411 * __getmainargs (MSVCRT.@)
413 void CDECL __getmainargs(int *argc, char** *argv, char** *envp,
414 int expand_wildcards, int *new_mode)
416 TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
418 if (expand_wildcards) {
419 HeapFree(GetProcessHeap(), 0, argv_expand);
420 argv_expand = NULL;
422 argv_expand = HeapAlloc(GetProcessHeap(), 0,
423 build_expanded_argv(&argc_expand, NULL));
424 if (argv_expand) {
425 build_expanded_argv(&argc_expand, argv_expand);
427 MSVCRT___argc = argc_expand;
428 MSVCRT___argv = argv_expand;
429 }else {
430 expand_wildcards = 0;
433 if (!expand_wildcards) {
434 MSVCRT___argc = __wine_main_argc;
435 MSVCRT___argv = __wine_main_argv;
438 *argc = MSVCRT___argc;
439 *argv = MSVCRT___argv;
440 *envp = MSVCRT___initenv;
442 if (new_mode)
443 MSVCRT__set_new_mode( *new_mode );
446 /*********************************************************************
447 * __wgetmainargs (MSVCRT.@)
449 void CDECL __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
450 int expand_wildcards, int *new_mode)
452 TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
454 /* Initialize the _wenviron array if it's not already created. */
455 if (!MSVCRT__wenviron)
456 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
457 *argc = MSVCRT___argc;
458 *wargv = MSVCRT___wargv;
459 *wenvp = MSVCRT___winitenv;
460 if (new_mode)
461 MSVCRT__set_new_mode( *new_mode );
464 /*********************************************************************
465 * _initterm (MSVCRT.@)
467 void CDECL _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
469 _INITTERMFUN* current = start;
471 TRACE("(%p,%p)\n",start,end);
472 while (current<end)
474 if (*current)
476 TRACE("Call init function %p\n",*current);
477 (**current)();
478 TRACE("returned\n");
480 current++;
484 /*********************************************************************
485 * _initterm_e (MSVCRT.@)
487 * call an array of application initialization functions and report the return value
489 int CDECL _initterm_e(_INITTERM_E_FN *table, _INITTERM_E_FN *end)
491 int res = 0;
493 TRACE("(%p, %p)\n", table, end);
495 while (!res && table < end) {
496 if (*table) {
497 TRACE("calling %p\n", **table);
498 res = (**table)();
499 if (res)
500 TRACE("function %p failed: 0x%x\n", *table, res);
503 table++;
505 return res;
508 /*********************************************************************
509 * __set_app_type (MSVCRT.@)
511 void CDECL MSVCRT___set_app_type(int app_type)
513 TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
514 MSVCRT_app_type = app_type;