Fixed potential memory corruption.
[wine/multimedia.git] / dlls / msvcrt / data.c
blob826020c88987e2be0690b3072e9aea24fb5fab21
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <math.h>
25 #include "msvcrt.h"
27 #include "msvcrt/stdlib.h"
28 #include "msvcrt/string.h"
30 #include "wine/library.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
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;
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 */
79 char *ptr;
81 for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
83 count++;
84 len += strlen(ptr) + 1;
86 if (blk)
87 blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
88 else
89 blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
91 if (blk)
93 if (count)
95 memcpy(&blk[count],environ_strings,len);
96 for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
98 blk[i++] = ptr;
101 blk[i] = NULL;
103 FreeEnvironmentStringsA(environ_strings);
104 return blk;
107 WCHAR ** msvcrt_SnapshotOfEnvironmentW(WCHAR **wblk)
109 WCHAR* wenviron_strings = GetEnvironmentStringsW();
110 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
111 WCHAR *wptr;
113 for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
115 count++;
116 len += strlenW(wptr) + 1;
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)
124 if (count)
126 memcpy(&wblk[count],wenviron_strings,len * sizeof(WCHAR));
127 for (wptr = (WCHAR*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
129 wblk[i++] = wptr;
132 wblk[i] = NULL;
134 FreeEnvironmentStringsW(wenviron_strings);
135 return wblk;
138 typedef void (*_INITTERMFUN)(void);
140 /***********************************************************************
141 * __p___argc (MSVCRT.@)
143 int* __p___argc(void) { return &MSVCRT___argc; }
145 /***********************************************************************
146 * __p__commode (MSVCRT.@)
148 unsigned int* __p__commode(void) { return &MSVCRT__commode; }
150 /***********************************************************************
151 * __p__fmode (MSVCRT.@)
153 unsigned int* __p__fmode(void) { return &MSVCRT__fmode; }
155 /***********************************************************************
156 * __p__osver (MSVCRT.@)
158 unsigned int* __p__osver(void) { return &MSVCRT__osver; }
160 /***********************************************************************
161 * __p__winmajor (MSVCRT.@)
163 unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; }
165 /***********************************************************************
166 * __p__winminor (MSVCRT.@)
168 unsigned int* __p__winminor(void) { return &MSVCRT__winminor; }
170 /***********************************************************************
171 * __p__winver (MSVCRT.@)
173 unsigned int* __p__winver(void) { return &MSVCRT__winver; }
175 /*********************************************************************
176 * __p__acmdln (MSVCRT.@)
178 char** __p__acmdln(void) { return &MSVCRT__acmdln; }
180 /*********************************************************************
181 * __p__wcmdln (MSVCRT.@)
183 WCHAR** __p__wcmdln(void) { return &MSVCRT__wcmdln; }
185 /*********************************************************************
186 * __p___argv (MSVCRT.@)
188 char*** __p___argv(void) { return &MSVCRT___argv; }
190 /*********************************************************************
191 * __p___wargv (MSVCRT.@)
193 WCHAR*** __p___wargv(void) { return &MSVCRT___wargv; }
195 /*********************************************************************
196 * __p__environ (MSVCRT.@)
198 char*** __p__environ(void)
200 if (!MSVCRT__environ)
201 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
202 return &MSVCRT__environ;
205 /*********************************************************************
206 * __p__wenviron (MSVCRT.@)
208 WCHAR*** __p__wenviron(void)
210 if (!MSVCRT__wenviron)
211 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
212 return &MSVCRT__wenviron;
215 /*********************************************************************
216 * __p___initenv (MSVCRT.@)
218 char*** __p___initenv(void) { return &MSVCRT___initenv; }
220 /*********************************************************************
221 * __p___winitenv (MSVCRT.@)
223 WCHAR*** __p___winitenv(void) { return &MSVCRT___winitenv; }
225 /*********************************************************************
226 * __p__timezone (MSVCRT.@)
228 int* __p__timezone(void) { return &MSVCRT_timezone; }
230 /* INTERNAL: Create a wide string from an ascii string */
231 static WCHAR *wstrdupa(const char *str)
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;
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
246 void msvcrt_init_args(void)
248 DWORD version;
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;
256 TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
257 debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
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;
276 /* FIXME: set app type for Winelib apps */
278 MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
279 MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
284 /* INTERNAL: free memory used by args */
285 void msvcrt_free_args(void)
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);
294 /*********************************************************************
295 * __getmainargs (MSVCRT.@)
297 void __getmainargs(int *argc, char** *argv, char** *envp,
298 int expand_wildcards, int *new_mode)
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 );
308 /*********************************************************************
309 * __wgetmainargs (MSVCRT.@)
311 void __wgetmainargs(int *argc, WCHAR** *wargv, WCHAR** *wenvp,
312 int expand_wildcards, int *new_mode)
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 );
322 /*********************************************************************
323 * _initterm (MSVCRT.@)
325 unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
327 _INITTERMFUN* current = start;
329 TRACE("(%p,%p)\n",start,end);
330 while (current<end)
332 if (*current)
334 TRACE("Call init function %p\n",*current);
335 (**current)();
336 TRACE("returned\n");
338 current++;
340 return 0;
343 /*********************************************************************
344 * __set_app_type (MSVCRT.@)
346 void MSVCRT___set_app_type(int app_type)
348 TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
349 MSVCRT_app_type = app_type;