Better implementation of GetCalendarInfo{A,W}, not perfect.
[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
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34
35 unsigned int MSVCRT___argc;
36 unsigned int MSVCRT_basemajor;/* FIXME: */
37 unsigned int MSVCRT_baseminor;/* FIXME: */
38 unsigned int MSVCRT_baseversion; /* FIXME: */
39 unsigned int MSVCRT__commode;
40 unsigned int MSVCRT__fmode;
41 unsigned int MSVCRT_osmajor;/* FIXME: */
42 unsigned int MSVCRT_osminor;/* FIXME: */
43 unsigned int MSVCRT_osmode;/* FIXME: */
44 unsigned int MSVCRT__osver;
45 unsigned int MSVCRT_osversion; /* FIXME: */
46 unsigned int MSVCRT__winmajor;
47 unsigned int MSVCRT__winminor;
48 unsigned int MSVCRT__winver;
49 unsigned int MSVCRT__sys_nerr; /* FIXME: not accessible from Winelib apps */
50 char**       MSVCRT__sys_errlist; /* FIXME: not accessible from Winelib apps */
51 unsigned int MSVCRT___setlc_active;
52 unsigned int MSVCRT___unguarded_readlc_active;
53 double MSVCRT__HUGE;
54 char **MSVCRT___argv;
55 WCHAR **MSVCRT___wargv;
56 char *MSVCRT__acmdln;
57 WCHAR *MSVCRT__wcmdln;
58 char **MSVCRT__environ;
59 WCHAR **MSVCRT__wenviron;
60 char **MSVCRT___initenv;
61 WCHAR **MSVCRT___winitenv;
62 int MSVCRT_timezone;
63 int MSVCRT_app_type;
64
65 static char* environ_strings;
66 static WCHAR* wenviron_strings;
67
68 typedef void (*_INITTERMFUN)(void);
69
70 /***********************************************************************
71  *              __p___argc (MSVCRT.@)
72  */
73 int* __p___argc(void) { return &MSVCRT___argc; }
74
75 /***********************************************************************
76  *              __p__commode (MSVCRT.@)
77  */
78 unsigned int* __p__commode(void) { return &MSVCRT__commode; }
79
80 /***********************************************************************
81  *              __p__fmode (MSVCRT.@)
82  */
83 unsigned int* __p__fmode(void) { return &MSVCRT__fmode; }
84
85 /***********************************************************************
86  *              __p__osver (MSVCRT.@)
87  */
88 unsigned int* __p__osver(void) { return &MSVCRT__osver; }
89
90 /***********************************************************************
91  *              __p__winmajor (MSVCRT.@)
92  */
93 unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; }
94
95 /***********************************************************************
96  *              __p__winminor (MSVCRT.@)
97  */
98 unsigned int* __p__winminor(void) { return &MSVCRT__winminor; }
99
100 /***********************************************************************
101  *              __p__winver (MSVCRT.@)
102  */
103 unsigned int* __p__winver(void) { return &MSVCRT__winver; }
104
105 /*********************************************************************
106  *              __p__acmdln (MSVCRT.@)
107  */
108 char** __p__acmdln(void) { return &MSVCRT__acmdln; }
109
110 /*********************************************************************
111  *              __p__wcmdln (MSVCRT.@)
112  */
113 WCHAR** __p__wcmdln(void) { return &MSVCRT__wcmdln; }
114
115 /*********************************************************************
116  *              __p___argv (MSVCRT.@)
117  */
118 char*** __p___argv(void) { return &MSVCRT___argv; }
119
120 /*********************************************************************
121  *              __p___wargv (MSVCRT.@)
122  */
123 WCHAR*** __p___wargv(void) { return &MSVCRT___wargv; }
124
125 /*********************************************************************
126  *              __p__environ (MSVCRT.@)
127  */
128 char*** __p__environ(void) { return &MSVCRT__environ; }
129
130 /*********************************************************************
131  *              __p__wenviron (MSVCRT.@)
132  */
133 WCHAR*** __p__wenviron(void) { return &MSVCRT__wenviron; }
134
135 /*********************************************************************
136  *              __p___initenv (MSVCRT.@)
137  */
138 char*** __p___initenv(void) { return &MSVCRT___initenv; }
139
140 /*********************************************************************
141  *              __p___winitenv (MSVCRT.@)
142  */
143 WCHAR*** __p___winitenv(void) { return &MSVCRT___winitenv; }
144
145 /*********************************************************************
146  *              __p__timezone (MSVCRT.@)
147  */
148 int* __p__timezone(void) { return &MSVCRT_timezone; }
149
150 /* INTERNAL: Create a wide string from an ascii string */
151 static WCHAR *wstrdupa(const char *str)
152 {
153   const size_t len = strlen(str) + 1 ;
154   WCHAR *wstr = MSVCRT_malloc(len* sizeof (WCHAR));
155   if (!wstr)
156     return NULL;
157    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len*sizeof(char),wstr,len* sizeof (WCHAR));
158   return wstr;
159 }
160
161 extern int __wine_get_main_args(char*** argv);
162 extern int __wine_get_wmain_args(WCHAR*** argv);
163
164 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
165  * we initialise data values during DLL loading. When called by a native
166  * program we simply return the data we've already initialised. This also means
167  * you can call multiple times without leaking
168  */
169 void msvcrt_init_args(void)
170 {
171   char *ptr;
172   WCHAR *wptr;
173   int count;
174   DWORD version;
175
176   MSVCRT__acmdln = _strdup( GetCommandLineA() );
177   MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
178   MSVCRT___argc = __wine_get_main_args(&MSVCRT___argv);
179   __wine_get_wmain_args(&MSVCRT___wargv);
180
181   TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
182         debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
183
184   version = GetVersion();
185   MSVCRT__osver       = version >> 16;
186   MSVCRT__winminor    = version & 0xFF;
187   MSVCRT__winmajor    = (version>>8) & 0xFF;
188   MSVCRT_baseversion = version >> 16;
189   MSVCRT__winver     = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
190   MSVCRT_baseminor   = (version >> 16) & 0xFF;
191   MSVCRT_basemajor   = (version >> 24) & 0xFF;
192   MSVCRT_osversion   = version & 0xFFFF;
193   MSVCRT_osminor     = version & 0xFF;
194   MSVCRT_osmajor     = (version>>8) & 0xFF;
195   MSVCRT__sys_nerr   = 43;
196   MSVCRT__HUGE = HUGE_VAL;
197   MSVCRT___setlc_active = 0;
198   MSVCRT___unguarded_readlc_active = 0;
199   MSVCRT_timezone = 0;
200
201   /* FIXME: set app type for Winelib apps */
202
203   environ_strings = GetEnvironmentStringsA();
204   count = 1; /* for NULL sentinel */
205   for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
206   {
207     count++;
208   }
209   MSVCRT__environ = HeapAlloc(GetProcessHeap(), 0, count * sizeof(char*));
210   if (MSVCRT__environ)
211   {
212     count = 0;
213     for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
214     {
215       MSVCRT__environ[count++] = ptr;
216     }
217     MSVCRT__environ[count] = NULL;
218   }
219
220   MSVCRT___initenv = MSVCRT__environ;
221
222   wenviron_strings = GetEnvironmentStringsW();
223   count = 1; /* for NULL sentinel */
224   for (wptr = wenviron_strings; *wptr; wptr += lstrlenW(wptr) + 1)
225   {
226     count++;
227   }
228   MSVCRT__wenviron = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR*));
229   if (MSVCRT__wenviron)
230   {
231     count = 0;
232     for (wptr = wenviron_strings; *wptr; wptr += lstrlenW(wptr) + 1)
233     {
234       MSVCRT__wenviron[count++] = wptr;
235     }
236     MSVCRT__wenviron[count] = NULL;
237   }
238   MSVCRT___winitenv = MSVCRT__wenviron;
239 }
240
241
242 /* INTERNAL: free memory used by args */
243 void msvcrt_free_args(void)
244 {
245   /* FIXME: more things to free */
246   FreeEnvironmentStringsA(environ_strings);
247   FreeEnvironmentStringsW(wenviron_strings);
248 }
249
250 /*********************************************************************
251  *              __getmainargs (MSVCRT.@)
252  */
253 void __getmainargs(int *argc, char** *argv, char** *envp,
254                                   int expand_wildcards, int *new_mode)
255 {
256   TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
257   *argc = MSVCRT___argc;
258   *argv = MSVCRT___argv;
259   *envp = MSVCRT__environ;
260   if (new_mode)
261     MSVCRT__set_new_mode( *new_mode );
262 }
263
264 /*********************************************************************
265  *              __wgetmainargs (MSVCRT.@)
266  */
267 void __wgetmainargs(int *argc, WCHAR** *wargv, WCHAR** *wenvp,
268                                    int expand_wildcards, int *new_mode)
269 {
270   TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
271   *argc = MSVCRT___argc;
272   *wargv = MSVCRT___wargv;
273   *wenvp = MSVCRT__wenviron;
274   if (new_mode)
275     MSVCRT__set_new_mode( *new_mode );
276 }
277
278 /*********************************************************************
279  *              _initterm (MSVCRT.@)
280  */
281 unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
282 {
283   _INITTERMFUN* current = start;
284
285   TRACE("(%p,%p)\n",start,end);
286   while (current<end)
287   {
288     if (*current)
289     {
290       TRACE("Call init function %p\n",*current);
291       (**current)();
292       TRACE("returned\n");
293     }
294     current++;
295   }
296   return 0;
297 }
298
299 /*********************************************************************
300  *              __set_app_type (MSVCRT.@)
301  */
302 void MSVCRT___set_app_type(int app_type)
303 {
304   TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
305   MSVCRT_app_type = app_type;
306 }