Release 20030408.
[wine/gsoc-2012-control.git] / dlls / msvcrt / data.c
blobaaeb4fd4e1e54d2df12e2e398a0c0f6cc23b42a2
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 MSVCRT_wchar_t **MSVCRT___wargv;
57 char *MSVCRT__acmdln;
58 MSVCRT_wchar_t *MSVCRT__wcmdln;
59 char **MSVCRT__environ = 0;
60 MSVCRT_wchar_t **MSVCRT__wenviron = 0;
61 char **MSVCRT___initenv = 0;
62 MSVCRT_wchar_t **MSVCRT___winitenv = 0;
63 int MSVCRT_timezone;
64 int MSVCRT_app_type;
65 char MSVCRT_pgm[MAX_PATH];
66 char* MSVCRT__pgmptr = 0;
68 /* Get a snapshot of the current environment
69 * and construct the __p__environ array
71 * The pointer returned from GetEnvironmentStrings may get invalid when
72 * some other module cause a reallocation of the env-variable block
74 * blk is an array of pointers to environment strings, ending with a NULL
75 * and after that the actual copy of the environment strings, ending in a \0
77 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
79 char* environ_strings = GetEnvironmentStringsA();
80 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
81 char *ptr;
83 for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
85 count++;
86 len += strlen(ptr) + 1;
88 if (blk)
89 blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
90 else
91 blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
93 if (blk)
95 if (count)
97 memcpy(&blk[count],environ_strings,len);
98 for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
100 blk[i++] = ptr;
103 blk[i] = NULL;
105 FreeEnvironmentStringsA(environ_strings);
106 return blk;
109 MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
111 MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
112 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
113 MSVCRT_wchar_t *wptr;
115 for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
117 count++;
118 len += strlenW(wptr) + 1;
120 if (wblk)
121 wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
122 else
123 wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
124 if (wblk)
126 if (count)
128 memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
129 for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
131 wblk[i++] = wptr;
134 wblk[i] = NULL;
136 FreeEnvironmentStringsW(wenviron_strings);
137 return wblk;
140 typedef void (*_INITTERMFUN)(void);
142 /***********************************************************************
143 * __p___argc (MSVCRT.@)
145 int* __p___argc(void) { return &MSVCRT___argc; }
147 /***********************************************************************
148 * __p__commode (MSVCRT.@)
150 unsigned int* __p__commode(void) { return &MSVCRT__commode; }
153 /***********************************************************************
154 * __p__pgmptr (MSVCRT.@)
156 char** __p__pgmptr(void) { return &MSVCRT__pgmptr; }
158 /***********************************************************************
159 * __p__fmode (MSVCRT.@)
161 unsigned int* __p__fmode(void) { return &MSVCRT__fmode; }
163 /***********************************************************************
164 * __p__osver (MSVCRT.@)
166 unsigned int* __p__osver(void) { return &MSVCRT__osver; }
168 /***********************************************************************
169 * __p__winmajor (MSVCRT.@)
171 unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; }
173 /***********************************************************************
174 * __p__winminor (MSVCRT.@)
176 unsigned int* __p__winminor(void) { return &MSVCRT__winminor; }
178 /***********************************************************************
179 * __p__winver (MSVCRT.@)
181 unsigned int* __p__winver(void) { return &MSVCRT__winver; }
183 /*********************************************************************
184 * __p__acmdln (MSVCRT.@)
186 char** __p__acmdln(void) { return &MSVCRT__acmdln; }
188 /*********************************************************************
189 * __p__wcmdln (MSVCRT.@)
191 MSVCRT_wchar_t** __p__wcmdln(void) { return &MSVCRT__wcmdln; }
193 /*********************************************************************
194 * __p___argv (MSVCRT.@)
196 char*** __p___argv(void) { return &MSVCRT___argv; }
198 /*********************************************************************
199 * __p___wargv (MSVCRT.@)
201 MSVCRT_wchar_t*** __p___wargv(void) { return &MSVCRT___wargv; }
203 /*********************************************************************
204 * __p__environ (MSVCRT.@)
206 char*** __p__environ(void)
208 if (!MSVCRT__environ)
209 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
210 return &MSVCRT__environ;
213 /*********************************************************************
214 * __p__wenviron (MSVCRT.@)
216 MSVCRT_wchar_t*** __p__wenviron(void)
218 if (!MSVCRT__wenviron)
219 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
220 return &MSVCRT__wenviron;
223 /*********************************************************************
224 * __p___initenv (MSVCRT.@)
226 char*** __p___initenv(void) { return &MSVCRT___initenv; }
228 /*********************************************************************
229 * __p___winitenv (MSVCRT.@)
231 MSVCRT_wchar_t*** __p___winitenv(void) { return &MSVCRT___winitenv; }
233 /*********************************************************************
234 * __p__timezone (MSVCRT.@)
236 int* __p__timezone(void) { return &MSVCRT_timezone; }
238 /* INTERNAL: Create a wide string from an ascii string */
239 static MSVCRT_wchar_t *wstrdupa(const char *str)
241 const size_t len = strlen(str) + 1 ;
242 MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
243 if (!wstr)
244 return NULL;
245 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
246 return wstr;
249 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
250 * we initialise data values during DLL loading. When called by a native
251 * program we simply return the data we've already initialised. This also means
252 * you can call multiple times without leaking
254 void msvcrt_init_args(void)
256 DWORD version;
258 MSVCRT__acmdln = _strdup( GetCommandLineA() );
259 MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
260 MSVCRT___argc = __wine_main_argc;
261 MSVCRT___argv = __wine_main_argv;
262 MSVCRT___wargv = __wine_main_wargv;
264 TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
265 debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
267 version = GetVersion();
268 MSVCRT__osver = version >> 16;
269 MSVCRT__winminor = version & 0xFF;
270 MSVCRT__winmajor = (version>>8) & 0xFF;
271 MSVCRT_baseversion = version >> 16;
272 MSVCRT__winver = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
273 MSVCRT_baseminor = (version >> 16) & 0xFF;
274 MSVCRT_basemajor = (version >> 24) & 0xFF;
275 MSVCRT_osversion = version & 0xFFFF;
276 MSVCRT_osminor = version & 0xFF;
277 MSVCRT_osmajor = (version>>8) & 0xFF;
278 MSVCRT__sys_nerr = 43;
279 MSVCRT__HUGE = HUGE_VAL;
280 MSVCRT___setlc_active = 0;
281 MSVCRT___unguarded_readlc_active = 0;
282 MSVCRT_timezone = 0;
284 MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
285 MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
287 MSVCRT_pgm[0] = '\0';
288 GetModuleFileNameA(0, MSVCRT_pgm, sizeof(MSVCRT_pgm)/sizeof(MSVCRT_pgm[0]));
289 MSVCRT__pgmptr = MSVCRT_pgm;
293 /* INTERNAL: free memory used by args */
294 void msvcrt_free_args(void)
296 /* FIXME: more things to free */
297 if (MSVCRT___initenv) HeapFree(GetProcessHeap(), 0,MSVCRT___initenv);
298 if (MSVCRT___winitenv) HeapFree(GetProcessHeap(), 0,MSVCRT___winitenv);
299 if (MSVCRT__environ) HeapFree(GetProcessHeap(), 0,MSVCRT__environ);
300 if (MSVCRT__wenviron) HeapFree(GetProcessHeap(), 0,MSVCRT__wenviron);
303 /*********************************************************************
304 * __getmainargs (MSVCRT.@)
306 void __getmainargs(int *argc, char** *argv, char** *envp,
307 int expand_wildcards, int *new_mode)
309 TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
310 *argc = MSVCRT___argc;
311 *argv = MSVCRT___argv;
312 *envp = MSVCRT___initenv;
313 if (new_mode)
314 MSVCRT__set_new_mode( *new_mode );
317 /*********************************************************************
318 * __wgetmainargs (MSVCRT.@)
320 void __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
321 int expand_wildcards, int *new_mode)
323 TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
324 *argc = MSVCRT___argc;
325 *wargv = MSVCRT___wargv;
326 *wenvp = MSVCRT___winitenv;
327 if (new_mode)
328 MSVCRT__set_new_mode( *new_mode );
331 /*********************************************************************
332 * _initterm (MSVCRT.@)
334 unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
336 _INITTERMFUN* current = start;
338 TRACE("(%p,%p)\n",start,end);
339 while (current<end)
341 if (*current)
343 TRACE("Call init function %p\n",*current);
344 (**current)();
345 TRACE("returned\n");
347 current++;
349 return 0;
352 /*********************************************************************
353 * __set_app_type (MSVCRT.@)
355 void MSVCRT___set_app_type(int app_type)
357 TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
358 MSVCRT_app_type = app_type;