Added regedit unit test, a couple minor changes to regedit.
[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 #include "wine/library.h"
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 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
162  * we initialise data values during DLL loading. When called by a native
163  * program we simply return the data we've already initialised. This also means
164  * you can call multiple times without leaking
165  */
166 void msvcrt_init_args(void)
167 {
168   char *ptr;
169   WCHAR *wptr;
170   int count;
171   DWORD version;
172
173   MSVCRT__acmdln = _strdup( GetCommandLineA() );
174   MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
175   MSVCRT___argc = __wine_main_argc;
176   MSVCRT___argv = __wine_main_argv;
177   MSVCRT___wargv = __wine_main_wargv;
178
179   TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
180         debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
181
182   version = GetVersion();
183   MSVCRT__osver       = version >> 16;
184   MSVCRT__winminor    = version & 0xFF;
185   MSVCRT__winmajor    = (version>>8) & 0xFF;
186   MSVCRT_baseversion = version >> 16;
187   MSVCRT__winver     = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
188   MSVCRT_baseminor   = (version >> 16) & 0xFF;
189   MSVCRT_basemajor   = (version >> 24) & 0xFF;
190   MSVCRT_osversion   = version & 0xFFFF;
191   MSVCRT_osminor     = version & 0xFF;
192   MSVCRT_osmajor     = (version>>8) & 0xFF;
193   MSVCRT__sys_nerr   = 43;
194   MSVCRT__HUGE = HUGE_VAL;
195   MSVCRT___setlc_active = 0;
196   MSVCRT___unguarded_readlc_active = 0;
197   MSVCRT_timezone = 0;
198
199   /* FIXME: set app type for Winelib apps */
200
201   environ_strings = GetEnvironmentStringsA();
202   count = 1; /* for NULL sentinel */
203   for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
204   {
205     count++;
206   }
207   MSVCRT__environ = HeapAlloc(GetProcessHeap(), 0, count * sizeof(char*));
208   if (MSVCRT__environ)
209   {
210     count = 0;
211     for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
212     {
213       MSVCRT__environ[count++] = ptr;
214     }
215     MSVCRT__environ[count] = NULL;
216   }
217
218   MSVCRT___initenv = MSVCRT__environ;
219
220   wenviron_strings = GetEnvironmentStringsW();
221   count = 1; /* for NULL sentinel */
222   for (wptr = wenviron_strings; *wptr; wptr += lstrlenW(wptr) + 1)
223   {
224     count++;
225   }
226   MSVCRT__wenviron = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR*));
227   if (MSVCRT__wenviron)
228   {
229     count = 0;
230     for (wptr = wenviron_strings; *wptr; wptr += lstrlenW(wptr) + 1)
231     {
232       MSVCRT__wenviron[count++] = wptr;
233     }
234     MSVCRT__wenviron[count] = NULL;
235   }
236   MSVCRT___winitenv = MSVCRT__wenviron;
237 }
238
239
240 /* INTERNAL: free memory used by args */
241 void msvcrt_free_args(void)
242 {
243   /* FIXME: more things to free */
244   FreeEnvironmentStringsA(environ_strings);
245   FreeEnvironmentStringsW(wenviron_strings);
246 }
247
248 /*********************************************************************
249  *              __getmainargs (MSVCRT.@)
250  */
251 void __getmainargs(int *argc, char** *argv, char** *envp,
252                                   int expand_wildcards, int *new_mode)
253 {
254   TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
255   *argc = MSVCRT___argc;
256   *argv = MSVCRT___argv;
257   *envp = MSVCRT__environ;
258   if (new_mode)
259     MSVCRT__set_new_mode( *new_mode );
260 }
261
262 /*********************************************************************
263  *              __wgetmainargs (MSVCRT.@)
264  */
265 void __wgetmainargs(int *argc, WCHAR** *wargv, WCHAR** *wenvp,
266                                    int expand_wildcards, int *new_mode)
267 {
268   TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
269   *argc = MSVCRT___argc;
270   *wargv = MSVCRT___wargv;
271   *wenvp = MSVCRT__wenviron;
272   if (new_mode)
273     MSVCRT__set_new_mode( *new_mode );
274 }
275
276 /*********************************************************************
277  *              _initterm (MSVCRT.@)
278  */
279 unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
280 {
281   _INITTERMFUN* current = start;
282
283   TRACE("(%p,%p)\n",start,end);
284   while (current<end)
285   {
286     if (*current)
287     {
288       TRACE("Call init function %p\n",*current);
289       (**current)();
290       TRACE("returned\n");
291     }
292     current++;
293   }
294   return 0;
295 }
296
297 /*********************************************************************
298  *              __set_app_type (MSVCRT.@)
299  */
300 void MSVCRT___set_app_type(int app_type)
301 {
302   TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
303   MSVCRT_app_type = app_type;
304 }