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