user32/tests: Add test showing hotkeys change the async key state.
[wine] / dlls / msvcrt / data.c
1 /*
2  * msvcrt.dll dll data items
3  *
4  * Copyright 2000 Jon Griffiths
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <math.h>
25 #include "msvcrt.h"
26 #include "wine/library.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31
32 int MSVCRT___argc = 0;
33 unsigned int MSVCRT_basemajor = 0;/* FIXME: */
34 unsigned int MSVCRT_baseminor = 0;/* FIXME: */
35 unsigned int MSVCRT_baseversion = 0; /* FIXME: */
36 unsigned int MSVCRT__commode = 0;
37 unsigned int MSVCRT__fmode = 0;
38 unsigned int MSVCRT_osmajor = 0;/* FIXME: */
39 unsigned int MSVCRT_osminor = 0;/* FIXME: */
40 unsigned int MSVCRT_osmode = 0;/* FIXME: */
41 unsigned int MSVCRT__osver = 0;
42 unsigned int MSVCRT__osplatform = 0;
43 unsigned int MSVCRT_osversion = 0; /* FIXME: */
44 unsigned int MSVCRT__winmajor = 0;
45 unsigned int MSVCRT__winminor = 0;
46 unsigned int MSVCRT__winver = 0;
47 unsigned int MSVCRT___setlc_active = 0;
48 unsigned int MSVCRT___unguarded_readlc_active = 0;
49 double MSVCRT__HUGE = 0;
50 char **MSVCRT___argv = NULL;
51 MSVCRT_wchar_t **MSVCRT___wargv = NULL;
52 char *MSVCRT__acmdln = NULL;
53 MSVCRT_wchar_t *MSVCRT__wcmdln = NULL;
54 char **MSVCRT__environ = NULL;
55 MSVCRT_wchar_t **MSVCRT__wenviron = NULL;
56 char **MSVCRT___initenv = NULL;
57 MSVCRT_wchar_t **MSVCRT___winitenv = NULL;
58 int MSVCRT_app_type = 0;
59 char* MSVCRT__pgmptr = NULL;
60 WCHAR* MSVCRT__wpgmptr = NULL;
61
62 /* Get a snapshot of the current environment
63  * and construct the __p__environ array
64  *
65  * The pointer returned from GetEnvironmentStrings may get invalid when
66  * some other module cause a reallocation of the env-variable block
67  *
68  * blk is an array of pointers to environment strings, ending with a NULL
69  * and after that the actual copy of the environment strings, ending in a \0
70  */
71 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
72 {
73   char* environ_strings = GetEnvironmentStringsA();
74   int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
75   char *ptr;
76
77   for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
78   {
79     count++;
80     len += strlen(ptr) + 1;
81   }
82   if (blk)
83       blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
84   else
85     blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
86
87   if (blk)
88     {
89       if (count)
90         {
91           memcpy(&blk[count],environ_strings,len);
92           for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
93             {
94               blk[i++] = ptr;
95             }
96         }
97       blk[i] = NULL;
98     }
99   FreeEnvironmentStringsA(environ_strings);
100   return blk;
101 }
102
103 MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
104 {
105   MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
106   int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
107   MSVCRT_wchar_t *wptr;
108
109   for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
110   {
111     count++;
112     len += strlenW(wptr) + 1;
113   }
114   if (wblk)
115       wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
116   else
117     wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
118   if (wblk)
119     {
120       if (count)
121         {
122           memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
123           for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
124             {
125               wblk[i++] = wptr;
126             }
127         }
128       wblk[i] = NULL;
129     }
130   FreeEnvironmentStringsW(wenviron_strings);
131   return wblk;
132 }
133
134 typedef void (CDECL *_INITTERMFUN)(void);
135 typedef int (CDECL *_INITTERM_E_FN)(void);
136
137 /***********************************************************************
138  *              __p___argc (MSVCRT.@)
139  */
140 int* CDECL __p___argc(void) { return &MSVCRT___argc; }
141
142 /***********************************************************************
143  *              __p__commode (MSVCRT.@)
144  */
145 unsigned int* CDECL __p__commode(void) { return &MSVCRT__commode; }
146
147
148 /***********************************************************************
149  *              __p__pgmptr (MSVCRT.@)
150  */
151 char** CDECL __p__pgmptr(void) { return &MSVCRT__pgmptr; }
152
153 /***********************************************************************
154  *              __p__wpgmptr (MSVCRT.@)
155  */
156 WCHAR** CDECL __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }
157
158 /***********************************************************************
159  *              __p__fmode (MSVCRT.@)
160  */
161 unsigned int* CDECL __p__fmode(void) { return &MSVCRT__fmode; }
162
163 /***********************************************************************
164  *              __p__osver (MSVCRT.@)
165  */
166 unsigned int* CDECL __p__osver(void) { return &MSVCRT__osver; }
167
168 /***********************************************************************
169  *              __p__winmajor (MSVCRT.@)
170  */
171 unsigned int* CDECL __p__winmajor(void) { return &MSVCRT__winmajor; }
172
173 /***********************************************************************
174  *              __p__winminor (MSVCRT.@)
175  */
176 unsigned int* CDECL __p__winminor(void) { return &MSVCRT__winminor; }
177
178 /***********************************************************************
179  *              __p__winver (MSVCRT.@)
180  */
181 unsigned int* CDECL __p__winver(void) { return &MSVCRT__winver; }
182
183 /*********************************************************************
184  *              __p__acmdln (MSVCRT.@)
185  */
186 char** CDECL __p__acmdln(void) { return &MSVCRT__acmdln; }
187
188 /*********************************************************************
189  *              __p__wcmdln (MSVCRT.@)
190  */
191 MSVCRT_wchar_t** CDECL __p__wcmdln(void) { return &MSVCRT__wcmdln; }
192
193 /*********************************************************************
194  *              __p___argv (MSVCRT.@)
195  */
196 char*** CDECL __p___argv(void) { return &MSVCRT___argv; }
197
198 /*********************************************************************
199  *              __p___wargv (MSVCRT.@)
200  */
201 MSVCRT_wchar_t*** CDECL __p___wargv(void) { return &MSVCRT___wargv; }
202
203 /*********************************************************************
204  *              __p__environ (MSVCRT.@)
205  */
206 char*** CDECL MSVCRT___p__environ(void)
207 {
208   return &MSVCRT__environ;
209 }
210
211 /*********************************************************************
212  *              __p__wenviron (MSVCRT.@)
213  */
214 MSVCRT_wchar_t*** CDECL MSVCRT___p__wenviron(void)
215 {
216   return &MSVCRT__wenviron;
217 }
218
219 /*********************************************************************
220  *              __p___initenv (MSVCRT.@)
221  */
222 char*** CDECL __p___initenv(void) { return &MSVCRT___initenv; }
223
224 /*********************************************************************
225  *              __p___winitenv (MSVCRT.@)
226  */
227 MSVCRT_wchar_t*** CDECL __p___winitenv(void) { return &MSVCRT___winitenv; }
228
229 /*********************************************************************
230  *              _get_osplatform (MSVCRT.@)
231  */
232 int CDECL MSVCRT__get_osplatform(int *pValue)
233 {
234     if (!MSVCRT_CHECK_PMT(pValue != NULL)) {
235         *MSVCRT__errno() = MSVCRT_EINVAL;
236         return MSVCRT_EINVAL;
237     }
238
239     *pValue = MSVCRT__osplatform;
240     return 0;
241 }
242
243 /* INTERNAL: Create a wide string from an ascii string */
244 MSVCRT_wchar_t *msvcrt_wstrdupa(const char *str)
245 {
246   const unsigned int len = strlen(str) + 1 ;
247   MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
248   if (!wstr)
249     return NULL;
250    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
251   return wstr;
252 }
253
254 /*********************************************************************
255  *              ___unguarded_readlc_active_add_func (MSVCRT.@)
256  */
257 unsigned int * CDECL MSVCRT____unguarded_readlc_active_add_func(void)
258 {
259   return &MSVCRT___unguarded_readlc_active;
260 }
261
262 /*********************************************************************
263  *              ___setlc_active_func (MSVCRT.@)
264  */
265 unsigned int CDECL MSVCRT____setlc_active_func(void)
266 {
267   return MSVCRT___setlc_active;
268 }
269
270 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
271  * we initialise data values during DLL loading. When called by a native
272  * program we simply return the data we've already initialised. This also means
273  * you can call multiple times without leaking
274  */
275 void msvcrt_init_args(void)
276 {
277   OSVERSIONINFOW osvi;
278
279   MSVCRT__acmdln = MSVCRT__strdup( GetCommandLineA() );
280   MSVCRT__wcmdln = msvcrt_wstrdupa(MSVCRT__acmdln);
281   MSVCRT___argc = __wine_main_argc;
282   MSVCRT___argv = __wine_main_argv;
283   MSVCRT___wargv = __wine_main_wargv;
284
285   TRACE("got %s, wide = %s argc=%d\n", debugstr_a(MSVCRT__acmdln),
286         debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
287
288   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
289   GetVersionExW( &osvi );
290   MSVCRT__winver     = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
291   MSVCRT__winmajor   = osvi.dwMajorVersion;
292   MSVCRT__winminor   = osvi.dwMinorVersion;
293   MSVCRT__osver      = osvi.dwBuildNumber;
294   MSVCRT__osplatform = osvi.dwPlatformId;
295   MSVCRT_osversion   = MSVCRT__winver;
296   MSVCRT_osmajor     = MSVCRT__winmajor;
297   MSVCRT_osminor     = MSVCRT__winminor;
298   MSVCRT_baseversion = MSVCRT__osver;
299   MSVCRT_baseminor   = MSVCRT_baseversion & 0xFF;
300   MSVCRT_basemajor   = (MSVCRT_baseversion >> 8) & 0xFF;
301   TRACE( "winver %08x winmajor %08x winminor %08x osver%08x baseversion %08x basemajor %08x baseminor %08x\n",
302           MSVCRT__winver, MSVCRT__winmajor, MSVCRT__winminor, MSVCRT__osver, MSVCRT_baseversion,
303           MSVCRT_basemajor, MSVCRT_baseminor);
304   TRACE( "osversion %08x osmajor %08x osminor %08x\n", MSVCRT_osversion, MSVCRT_osmajor, MSVCRT_osminor);
305
306   MSVCRT__HUGE = HUGE_VAL;
307   MSVCRT___setlc_active = 0;
308   MSVCRT___unguarded_readlc_active = 0;
309   MSVCRT__fmode = MSVCRT__O_TEXT;
310
311   MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
312   MSVCRT___initenv = msvcrt_SnapshotOfEnvironmentA(NULL);
313   MSVCRT___winitenv = msvcrt_SnapshotOfEnvironmentW(NULL);
314
315   MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
316   if (MSVCRT__pgmptr)
317   {
318     if (!GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH))
319       MSVCRT__pgmptr[0] = '\0';
320     else
321       MSVCRT__pgmptr[MAX_PATH - 1] = '\0';
322   }
323
324   MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
325   if (MSVCRT__wpgmptr)
326   {
327     if (!GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH))
328       MSVCRT__wpgmptr[0] = '\0';
329     else
330       MSVCRT__wpgmptr[MAX_PATH - 1] = '\0';
331   }
332 }
333
334
335 /* INTERNAL: free memory used by args */
336 void msvcrt_free_args(void)
337 {
338   /* FIXME: more things to free */
339   HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
340   HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
341   HeapFree(GetProcessHeap(), 0, MSVCRT__environ);
342   HeapFree(GetProcessHeap(), 0, MSVCRT__wenviron);
343   HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
344   HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
345 }
346
347 /*********************************************************************
348  *              __getmainargs (MSVCRT.@)
349  */
350 void CDECL __getmainargs(int *argc, char** *argv, char** *envp,
351                          int expand_wildcards, int *new_mode)
352 {
353   TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
354   *argc = MSVCRT___argc;
355   *argv = MSVCRT___argv;
356   *envp = MSVCRT___initenv;
357   if (new_mode)
358     MSVCRT__set_new_mode( *new_mode );
359 }
360
361 /*********************************************************************
362  *              __wgetmainargs (MSVCRT.@)
363  */
364 void CDECL __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
365                           int expand_wildcards, int *new_mode)
366 {
367   TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
368
369   /* Initialize the _wenviron array if it's not already created. */
370   if (!MSVCRT__wenviron)
371     MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
372   *argc = MSVCRT___argc;
373   *wargv = MSVCRT___wargv;
374   *wenvp = MSVCRT___winitenv;
375   if (new_mode)
376     MSVCRT__set_new_mode( *new_mode );
377 }
378
379 /*********************************************************************
380  *              _initterm (MSVCRT.@)
381  */
382 void CDECL _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
383 {
384   _INITTERMFUN* current = start;
385
386   TRACE("(%p,%p)\n",start,end);
387   while (current<end)
388   {
389     if (*current)
390     {
391       TRACE("Call init function %p\n",*current);
392       (**current)();
393       TRACE("returned\n");
394     }
395     current++;
396   }
397 }
398
399 /*********************************************************************
400  *  _initterm_e (MSVCRT.@)
401  *
402  * call an array of application initialization functions and report the return value
403  */
404 int CDECL _initterm_e(_INITTERM_E_FN *table, _INITTERM_E_FN *end)
405 {
406     int res = 0;
407
408     TRACE("(%p, %p)\n", table, end);
409
410     while (!res && table < end) {
411         if (*table) {
412             TRACE("calling %p\n", **table);
413             res = (**table)();
414             if (res)
415                 TRACE("function %p failed: 0x%x\n", *table, res);
416
417         }
418         table++;
419     }
420     return res;
421 }
422
423 /*********************************************************************
424  *              __set_app_type (MSVCRT.@)
425  */
426 void CDECL MSVCRT___set_app_type(int app_type)
427 {
428   TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
429   MSVCRT_app_type = app_type;
430 }