- rewrote the file code so that streams manage low level file desc
[wine.git] / dlls / msvcrt / data.c
blobfa11ed2225da29cd66d4c01f759647d9108534ca
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/fcntl.h"
28 #include "msvcrt/stdlib.h"
29 #include "msvcrt/string.h"
31 #include "wine/library.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 unsigned int MSVCRT___argc;
38 unsigned int MSVCRT_basemajor;/* FIXME: */
39 unsigned int MSVCRT_baseminor;/* FIXME: */
40 unsigned int MSVCRT_baseversion; /* FIXME: */
41 unsigned int MSVCRT__commode;
42 unsigned int MSVCRT__fmode;
43 unsigned int MSVCRT_osmajor;/* FIXME: */
44 unsigned int MSVCRT_osminor;/* FIXME: */
45 unsigned int MSVCRT_osmode;/* FIXME: */
46 unsigned int MSVCRT__osver;
47 unsigned int MSVCRT_osversion; /* FIXME: */
48 unsigned int MSVCRT__winmajor;
49 unsigned int MSVCRT__winminor;
50 unsigned int MSVCRT__winver;
51 unsigned int MSVCRT__sys_nerr; /* FIXME: not accessible from Winelib apps */
52 char** MSVCRT__sys_errlist; /* FIXME: not accessible from Winelib apps */
53 unsigned int MSVCRT___setlc_active;
54 unsigned int MSVCRT___unguarded_readlc_active;
55 double MSVCRT__HUGE;
56 char **MSVCRT___argv;
57 MSVCRT_wchar_t **MSVCRT___wargv;
58 char *MSVCRT__acmdln;
59 MSVCRT_wchar_t *MSVCRT__wcmdln;
60 char **MSVCRT__environ = 0;
61 MSVCRT_wchar_t **MSVCRT__wenviron = 0;
62 char **MSVCRT___initenv = 0;
63 MSVCRT_wchar_t **MSVCRT___winitenv = 0;
64 int MSVCRT_timezone;
65 int MSVCRT_app_type;
66 char* MSVCRT__pgmptr = 0;
67 WCHAR* MSVCRT__wpgmptr = 0;
69 /* Get a snapshot of the current environment
70 * and construct the __p__environ array
72 * The pointer returned from GetEnvironmentStrings may get invalid when
73 * some other module cause a reallocation of the env-variable block
75 * blk is an array of pointers to environment strings, ending with a NULL
76 * and after that the actual copy of the environment strings, ending in a \0
78 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
80 char* environ_strings = GetEnvironmentStringsA();
81 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
82 char *ptr;
84 for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
86 count++;
87 len += strlen(ptr) + 1;
89 if (blk)
90 blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
91 else
92 blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
94 if (blk)
96 if (count)
98 memcpy(&blk[count],environ_strings,len);
99 for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
101 blk[i++] = ptr;
104 blk[i] = NULL;
106 FreeEnvironmentStringsA(environ_strings);
107 return blk;
110 MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
112 MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
113 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
114 MSVCRT_wchar_t *wptr;
116 for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
118 count++;
119 len += strlenW(wptr) + 1;
121 if (wblk)
122 wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
123 else
124 wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
125 if (wblk)
127 if (count)
129 memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
130 for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
132 wblk[i++] = wptr;
135 wblk[i] = NULL;
137 FreeEnvironmentStringsW(wenviron_strings);
138 return wblk;
141 typedef void (*_INITTERMFUN)(void);
143 /***********************************************************************
144 * __p___argc (MSVCRT.@)
146 int* __p___argc(void) { return &MSVCRT___argc; }
148 /***********************************************************************
149 * __p__commode (MSVCRT.@)
151 unsigned int* __p__commode(void) { return &MSVCRT__commode; }
154 /***********************************************************************
155 * __p__pgmptr (MSVCRT.@)
157 char** __p__pgmptr(void) { return &MSVCRT__pgmptr; }
159 /***********************************************************************
160 * __p__wpgmptr (MSVCRT.@)
162 WCHAR** __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }
164 /***********************************************************************
165 * __p__fmode (MSVCRT.@)
167 unsigned int* __p__fmode(void) { return &MSVCRT__fmode; }
169 /***********************************************************************
170 * __p__osver (MSVCRT.@)
172 unsigned int* __p__osver(void) { return &MSVCRT__osver; }
174 /***********************************************************************
175 * __p__winmajor (MSVCRT.@)
177 unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; }
179 /***********************************************************************
180 * __p__winminor (MSVCRT.@)
182 unsigned int* __p__winminor(void) { return &MSVCRT__winminor; }
184 /***********************************************************************
185 * __p__winver (MSVCRT.@)
187 unsigned int* __p__winver(void) { return &MSVCRT__winver; }
189 /*********************************************************************
190 * __p__acmdln (MSVCRT.@)
192 char** __p__acmdln(void) { return &MSVCRT__acmdln; }
194 /*********************************************************************
195 * __p__wcmdln (MSVCRT.@)
197 MSVCRT_wchar_t** __p__wcmdln(void) { return &MSVCRT__wcmdln; }
199 /*********************************************************************
200 * __p___argv (MSVCRT.@)
202 char*** __p___argv(void) { return &MSVCRT___argv; }
204 /*********************************************************************
205 * __p___wargv (MSVCRT.@)
207 MSVCRT_wchar_t*** __p___wargv(void) { return &MSVCRT___wargv; }
209 /*********************************************************************
210 * __p__environ (MSVCRT.@)
212 char*** __p__environ(void)
214 if (!MSVCRT__environ)
215 MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
216 return &MSVCRT__environ;
219 /*********************************************************************
220 * __p__wenviron (MSVCRT.@)
222 MSVCRT_wchar_t*** __p__wenviron(void)
224 if (!MSVCRT__wenviron)
225 MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
226 return &MSVCRT__wenviron;
229 /*********************************************************************
230 * __p___initenv (MSVCRT.@)
232 char*** __p___initenv(void) { return &MSVCRT___initenv; }
234 /*********************************************************************
235 * __p___winitenv (MSVCRT.@)
237 MSVCRT_wchar_t*** __p___winitenv(void) { return &MSVCRT___winitenv; }
239 /*********************************************************************
240 * __p__timezone (MSVCRT.@)
242 int* __p__timezone(void) { return &MSVCRT_timezone; }
244 /* INTERNAL: Create a wide string from an ascii string */
245 static MSVCRT_wchar_t *wstrdupa(const char *str)
247 const size_t len = strlen(str) + 1 ;
248 MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
249 if (!wstr)
250 return NULL;
251 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
252 return wstr;
255 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
256 * we initialise data values during DLL loading. When called by a native
257 * program we simply return the data we've already initialised. This also means
258 * you can call multiple times without leaking
260 void msvcrt_init_args(void)
262 DWORD version;
264 MSVCRT__acmdln = _strdup( GetCommandLineA() );
265 MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
266 MSVCRT___argc = __wine_main_argc;
267 MSVCRT___argv = __wine_main_argv;
268 MSVCRT___wargv = __wine_main_wargv;
270 TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
271 debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
273 version = GetVersion();
274 MSVCRT__osver = version >> 16;
275 MSVCRT__winminor = version & 0xFF;
276 MSVCRT__winmajor = (version>>8) & 0xFF;
277 MSVCRT_baseversion = version >> 16;
278 MSVCRT__winver = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
279 MSVCRT_baseminor = (version >> 16) & 0xFF;
280 MSVCRT_basemajor = (version >> 24) & 0xFF;
281 MSVCRT_osversion = version & 0xFFFF;
282 MSVCRT_osminor = version & 0xFF;
283 MSVCRT_osmajor = (version>>8) & 0xFF;
284 MSVCRT__sys_nerr = 43;
285 MSVCRT__HUGE = HUGE_VAL;
286 MSVCRT___setlc_active = 0;
287 MSVCRT___unguarded_readlc_active = 0;
288 MSVCRT_timezone = 0;
289 MSVCRT__fmode = _O_TEXT;
291 MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
292 MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
294 MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
295 if (MSVCRT__pgmptr)
296 GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH);
298 MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
299 if (MSVCRT__wpgmptr)
300 GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH);
304 /* INTERNAL: free memory used by args */
305 void msvcrt_free_args(void)
307 /* FIXME: more things to free */
308 if (MSVCRT___initenv) HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
309 if (MSVCRT___winitenv) HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
310 if (MSVCRT__environ) HeapFree(GetProcessHeap(), 0, MSVCRT__environ);
311 if (MSVCRT__wenviron) HeapFree(GetProcessHeap(), 0, MSVCRT__wenviron);
312 if (MSVCRT__pgmptr) HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
313 if (MSVCRT__wpgmptr) HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
316 /*********************************************************************
317 * __getmainargs (MSVCRT.@)
319 void __getmainargs(int *argc, char** *argv, char** *envp,
320 int expand_wildcards, int *new_mode)
322 TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
323 *argc = MSVCRT___argc;
324 *argv = MSVCRT___argv;
325 *envp = MSVCRT___initenv;
326 if (new_mode)
327 MSVCRT__set_new_mode( *new_mode );
330 /*********************************************************************
331 * __wgetmainargs (MSVCRT.@)
333 void __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
334 int expand_wildcards, int *new_mode)
336 TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
337 *argc = MSVCRT___argc;
338 *wargv = MSVCRT___wargv;
339 *wenvp = MSVCRT___winitenv;
340 if (new_mode)
341 MSVCRT__set_new_mode( *new_mode );
344 /*********************************************************************
345 * _initterm (MSVCRT.@)
347 unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
349 _INITTERMFUN* current = start;
351 TRACE("(%p,%p)\n",start,end);
352 while (current<end)
354 if (*current)
356 TRACE("Call init function %p\n",*current);
357 (**current)();
358 TRACE("returned\n");
360 current++;
362 return 0;
365 /*********************************************************************
366 * __set_app_type (MSVCRT.@)
368 void MSVCRT___set_app_type(int app_type)
370 TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
371 MSVCRT_app_type = app_type;