source: trunk/src/msvcrt/data.c@ 21395

Last change on this file since 21395 was 21395, checked in by dmik, 15 years ago

Attempted to resurrect building of msvcrt.dll.

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