Fixed some issues found by winapi_check.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <math.h>
25 #include "msvcrt.h"
26
27 #include "msvcrt/stdlib.h"
28 #include "msvcrt/string.h"
29
30 #include "wine/library.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
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;
50 unsigned int MSVCRT__sys_nerr; /* FIXME: not accessible from Winelib apps */
51 char**       MSVCRT__sys_errlist; /* FIXME: not accessible from Winelib apps */
52 unsigned int MSVCRT___setlc_active;
53 unsigned int MSVCRT___unguarded_readlc_active;
54 double MSVCRT__HUGE;
55 char **MSVCRT___argv;
56 WCHAR **MSVCRT___wargv;
57 char *MSVCRT__acmdln;
58 WCHAR *MSVCRT__wcmdln;
59 char **MSVCRT__environ = 0;
60 WCHAR **MSVCRT__wenviron = 0;
61 char **MSVCRT___initenv = 0;
62 WCHAR **MSVCRT___winitenv = 0;
63 int MSVCRT_timezone;
64 int MSVCRT_app_type;
65
66 /* Get a snapshot of the current environment
67  * and construct the __p__environ array
68  *
69  * The pointer returned from GetEnvironmentStrings may get invalid when
70  * some other module cause a reallocation of the env-variable block
71  *
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
74  */
75 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
76 {
77   char* environ_strings = GetEnvironmentStringsA();
78   int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
79   char *ptr;
80
81   for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
82   {
83     count++;
84     len += strlen(ptr) + 1;
85   }
86   if (blk)
87       blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
88   else
89     blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
90
91   if (blk)
92     {
93       if (count)
94         {
95           memcpy(&blk[count],environ_strings,len);
96           for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
97             {
98               blk[i++] = ptr;
99             }
100         }
101       blk[i] = NULL;
102     }
103   FreeEnvironmentStringsA(environ_strings);
104   return blk;
105 }
106
107 WCHAR ** msvcrt_SnapshotOfEnvironmentW(WCHAR **wblk)
108 {
109   WCHAR* wenviron_strings = GetEnvironmentStringsW();
110   int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
111   WCHAR *wptr;
112
113   for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
114   {
115     count++;
116     len += strlenW(wptr) + 1;
117   }
118   if (wblk)
119       wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(WCHAR*) + len * sizeof(WCHAR));
120   else
121     wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(WCHAR*) + len * sizeof(WCHAR));
122   if (wblk)
123     {
124       if (count)
125         {
126           memcpy(&wblk[count],wenviron_strings,len * sizeof(WCHAR));
127           for (wptr = (WCHAR*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
128             {
129               wblk[i++] = wptr;
130             }
131         }
132       wblk[i] = NULL;
133     }
134   FreeEnvironmentStringsW(wenviron_strings);
135   return wblk;
136 }
137
138 typedef void (*_INITTERMFUN)(void);
139
140 /***********************************************************************
141  *              __p___argc (MSVCRT.@)
142  */
143 int* __p___argc(void) { return &MSVCRT___argc; }
144
145 /***********************************************************************
146  *              __p__commode (MSVCRT.@)
147  */
148 unsigned int* __p__commode(void) { return &MSVCRT__commode; }
149
150 /***********************************************************************
151  *              __p__fmode (MSVCRT.@)
152  */
153 unsigned int* __p__fmode(void) { return &MSVCRT__fmode; }
154
155 /***********************************************************************
156  *              __p__osver (MSVCRT.@)
157  */
158 unsigned int* __p__osver(void) { return &MSVCRT__osver; }
159
160 /***********************************************************************
161  *              __p__winmajor (MSVCRT.@)
162  */
163 unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; }
164
165 /***********************************************************************
166  *              __p__winminor (MSVCRT.@)
167  */
168 unsigned int* __p__winminor(void) { return &MSVCRT__winminor; }
169
170 /***********************************************************************
171  *              __p__winver (MSVCRT.@)
172  */
173 unsigned int* __p__winver(void) { return &MSVCRT__winver; }
174
175 /*********************************************************************
176  *              __p__acmdln (MSVCRT.@)
177  */
178 char** __p__acmdln(void) { return &MSVCRT__acmdln; }
179
180 /*********************************************************************
181  *              __p__wcmdln (MSVCRT.@)
182  */
183 WCHAR** __p__wcmdln(void) { return &MSVCRT__wcmdln; }
184
185 /*********************************************************************
186  *              __p___argv (MSVCRT.@)
187  */
188 char*** __p___argv(void) { return &MSVCRT___argv; }
189
190 /*********************************************************************
191  *              __p___wargv (MSVCRT.@)
192  */
193 WCHAR*** __p___wargv(void) { return &MSVCRT___wargv; }
194
195 /*********************************************************************
196  *              __p__environ (MSVCRT.@)
197  */
198 char*** __p__environ(void)
199 {
200   if (!MSVCRT__environ)
201     MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
202   return &MSVCRT__environ;
203 }
204
205 /*********************************************************************
206  *              __p__wenviron (MSVCRT.@)
207  */
208 WCHAR*** __p__wenviron(void)
209 {
210   if (!MSVCRT__wenviron)
211     MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
212   return &MSVCRT__wenviron;
213 }
214
215 /*********************************************************************
216  *              __p___initenv (MSVCRT.@)
217  */
218 char*** __p___initenv(void) { return &MSVCRT___initenv; }
219
220 /*********************************************************************
221  *              __p___winitenv (MSVCRT.@)
222  */
223 WCHAR*** __p___winitenv(void) { return &MSVCRT___winitenv; }
224
225 /*********************************************************************
226  *              __p__timezone (MSVCRT.@)
227  */
228 int* __p__timezone(void) { return &MSVCRT_timezone; }
229
230 /* INTERNAL: Create a wide string from an ascii string */
231 static WCHAR *wstrdupa(const char *str)
232 {
233   const size_t len = strlen(str) + 1 ;
234   WCHAR *wstr = MSVCRT_malloc(len* sizeof (WCHAR));
235   if (!wstr)
236     return NULL;
237    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len*sizeof(char),wstr,len* sizeof (WCHAR));
238   return wstr;
239 }
240
241 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
242  * we initialise data values during DLL loading. When called by a native
243  * program we simply return the data we've already initialised. This also means
244  * you can call multiple times without leaking
245  */
246 void msvcrt_init_args(void)
247 {
248   DWORD version;
249
250   MSVCRT__acmdln = _strdup( GetCommandLineA() );
251   MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
252   MSVCRT___argc = __wine_main_argc;
253   MSVCRT___argv = __wine_main_argv;
254   MSVCRT___wargv = __wine_main_wargv;
255
256   TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
257         debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
258
259   version = GetVersion();
260   MSVCRT__osver       = version >> 16;
261   MSVCRT__winminor    = version & 0xFF;
262   MSVCRT__winmajor    = (version>>8) & 0xFF;
263   MSVCRT_baseversion = version >> 16;
264   MSVCRT__winver     = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
265   MSVCRT_baseminor   = (version >> 16) & 0xFF;
266   MSVCRT_basemajor   = (version >> 24) & 0xFF;
267   MSVCRT_osversion   = version & 0xFFFF;
268   MSVCRT_osminor     = version & 0xFF;
269   MSVCRT_osmajor     = (version>>8) & 0xFF;
270   MSVCRT__sys_nerr   = 43;
271   MSVCRT__HUGE = HUGE_VAL;
272   MSVCRT___setlc_active = 0;
273   MSVCRT___unguarded_readlc_active = 0;
274   MSVCRT_timezone = 0;
275
276   /* FIXME: set app type for Winelib apps */
277
278   MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
279   MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
280
281 }
282
283
284 /* INTERNAL: free memory used by args */
285 void msvcrt_free_args(void)
286 {
287   /* FIXME: more things to free */
288   if (MSVCRT___initenv) HeapFree(GetProcessHeap(), 0,MSVCRT___initenv);
289   if (MSVCRT___winitenv) HeapFree(GetProcessHeap(), 0,MSVCRT___winitenv);
290   if (MSVCRT__environ) HeapFree(GetProcessHeap(), 0,MSVCRT__environ);
291   if (MSVCRT__wenviron) HeapFree(GetProcessHeap(), 0,MSVCRT__wenviron);
292 }
293
294 /*********************************************************************
295  *              __getmainargs (MSVCRT.@)
296  */
297 void __getmainargs(int *argc, char** *argv, char** *envp,
298                                   int expand_wildcards, int *new_mode)
299 {
300   TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
301   *argc = MSVCRT___argc;
302   *argv = MSVCRT___argv;
303   *envp = MSVCRT___initenv;
304   if (new_mode)
305     MSVCRT__set_new_mode( *new_mode );
306 }
307
308 /*********************************************************************
309  *              __wgetmainargs (MSVCRT.@)
310  */
311 void __wgetmainargs(int *argc, WCHAR** *wargv, WCHAR** *wenvp,
312                                    int expand_wildcards, int *new_mode)
313 {
314   TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
315   *argc = MSVCRT___argc;
316   *wargv = MSVCRT___wargv;
317   *wenvp = MSVCRT___winitenv;
318   if (new_mode)
319     MSVCRT__set_new_mode( *new_mode );
320 }
321
322 /*********************************************************************
323  *              _initterm (MSVCRT.@)
324  */
325 unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
326 {
327   _INITTERMFUN* current = start;
328
329   TRACE("(%p,%p)\n",start,end);
330   while (current<end)
331   {
332     if (*current)
333     {
334       TRACE("Call init function %p\n",*current);
335       (**current)();
336       TRACE("returned\n");
337     }
338     current++;
339   }
340   return 0;
341 }
342
343 /*********************************************************************
344  *              __set_app_type (MSVCRT.@)
345  */
346 void MSVCRT___set_app_type(int app_type)
347 {
348   TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
349   MSVCRT_app_type = app_type;
350 }