* Sync to recent trunk (r52563).
[reactos.git] / dll / win32 / crtdll / dllmain.c
1 /* $Id: dllmain.c 12852 2005-01-06 13:58:04Z mf $
2 *
3 * dllmain.c
4 *
5 * ReactOS CRTDLL.DLL Compatibility Library
6 *
7 * THIS SOFTWARE IS NOT COPYRIGHTED
8 *
9 * This source code is offered for use in the public domain. You may
10 * use, modify or distribute it freely.
11 *
12 * This code is distributed in the hope that it will be useful but
13 * WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
14 * DISCLAMED. This includes but is not limited to warrenties of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 */
18
19 #include <precomp.h>
20 #include <internal/wine/msvcrt.h>
21 #include <sys/stat.h>
22 #include <locale.h>
23 #include <mbctype.h>
24
25 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(crtdll);
27
28
29 /* EXTERNAL PROTOTYPES ********************************************************/
30
31 extern int BlockEnvToEnvironA(void);
32 extern int BlockEnvToEnvironW(void);
33 extern void FreeEnvironment(char **environment);
34 extern void _atexit_cleanup(void);
35
36 extern unsigned int _osver;
37 extern unsigned int _winminor;
38 extern unsigned int _winmajor;
39 extern unsigned int _winver;
40
41 unsigned int CRTDLL__basemajor_dll;
42 unsigned int CRTDLL__baseminor_dll;
43 unsigned int CRTDLL__baseversion_dll;
44 unsigned int CRTDLL__cpumode_dll;
45 unsigned int CRTDLL__osmajor_dll;
46 unsigned int CRTDLL__osminor_dll;
47 unsigned int CRTDLL__osmode_dll;
48 unsigned int CRTDLL__osversion_dll;
49 int _fileinfo_dll;
50
51 extern char* _acmdln; /* pointer to ascii command line */
52 extern wchar_t* _wcmdln; /* pointer to wide character command line */
53 #undef _environ
54 extern char** _environ; /* pointer to environment block */
55 extern char** __initenv; /* pointer to initial environment block */
56 extern wchar_t** _wenviron; /* pointer to environment block */
57 extern wchar_t** __winitenv; /* pointer to initial environment block */
58
59
60
61 /* dev_t is a short in crtdll but an unsigned int in msvcrt */
62 typedef short crtdll_dev_t;
63
64 struct crtdll_stat
65 {
66 crtdll_dev_t st_dev;
67 _ino_t st_ino;
68 unsigned short st_mode;
69 short st_nlink;
70 short st_uid;
71 short st_gid;
72 crtdll_dev_t st_rdev;
73 _off_t st_size;
74 time_t st_atime;
75 time_t st_mtime;
76 time_t st_ctime;
77 };
78
79 /* convert struct _stat from crtdll format to msvcrt format */
80 static void convert_struct_stat( struct crtdll_stat *dst, const struct _stat *src )
81 {
82 dst->st_dev = src->st_dev;
83 dst->st_ino = src->st_ino;
84 dst->st_mode = src->st_mode;
85 dst->st_nlink = src->st_nlink;
86 dst->st_uid = src->st_uid;
87 dst->st_gid = src->st_gid;
88 dst->st_rdev = src->st_rdev;
89 dst->st_size = src->st_size;
90 dst->st_atime = src->st_atime;
91 dst->st_mtime = src->st_mtime;
92 dst->st_ctime = src->st_ctime;
93 }
94
95 /* from msvcrt */
96 extern void __getmainargs( int *argc, char ***argv, char ***envp,
97 int expand_wildcards, int *new_mode );
98
99 /* LIBRARY GLOBAL VARIABLES ***************************************************/
100
101 HANDLE hHeap = NULL; /* handle for heap */
102
103
104 /* LIBRARY ENTRY POINT ********************************************************/
105
106 BOOL
107 WINAPI
108 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
109 {
110 switch (dwReason)
111 {
112 case DLL_PROCESS_ATTACH://1
113 /* initialize version info */
114 //TRACE("Attach %d\n", nAttachCount);
115
116 _osver = GetVersion();
117
118 CRTDLL__basemajor_dll = (_osver >> 24) & 0xFF;
119 CRTDLL__baseminor_dll = (_osver >> 16) & 0xFF;
120 CRTDLL__baseversion_dll = (_osver >> 16);
121 CRTDLL__cpumode_dll = 1; /* FIXME */
122 CRTDLL__osmajor_dll = (_osver >>8) & 0xFF;
123 CRTDLL__osminor_dll = (_osver & 0xFF);
124 CRTDLL__osmode_dll = 1; /* FIXME */
125 CRTDLL__osversion_dll = (_osver & 0xFFFF);
126
127 _winmajor = (_osver >> 8) & 0xFF;
128 _winminor = _osver & 0xFF;
129 _winver = (_winmajor << 8) + _winminor;
130 _osver = (_osver >> 16) & 0xFFFF;
131 hHeap = HeapCreate(0, 100000, 0);
132 if (hHeap == NULL)
133 return FALSE;
134
135 /* create tls stuff */
136 if (!CreateThreadData())
137 return FALSE;
138
139 if (BlockEnvToEnvironA() < 0)
140 return FALSE;
141
142 if (BlockEnvToEnvironW() < 0)
143 {
144 FreeEnvironment((char**)_wenviron);
145 return FALSE;
146 }
147
148 _acmdln = _strdup(GetCommandLineA());
149 _wcmdln = _wcsdup(GetCommandLineW());
150
151 /* FIXME: more initializations... */
152
153 /* FIXME: Initialization of the WINE code */
154 msvcrt_init_mt_locks();
155 msvcrt_init_io();
156 setlocale(0, "C");
157 //_setmbcp(_MB_CP_LOCALE);
158
159 TRACE("Attach done\n");
160 break;
161
162 case DLL_THREAD_ATTACH://2
163 break;
164
165 case DLL_THREAD_DETACH://4
166 FreeThreadData(NULL);
167 break;
168
169 case DLL_PROCESS_DETACH://0
170 //TRACE("Detach %d\n", nAttachCount);
171 /* Deinit of the WINE code */
172 msvcrt_free_io();
173 msvcrt_free_mt_locks();
174 _atexit_cleanup();
175
176 /* destroy tls stuff */
177 DestroyThreadData();
178
179 if (__winitenv && __winitenv != _wenviron)
180 FreeEnvironment((char**)__winitenv);
181 if (_wenviron)
182 FreeEnvironment((char**)_wenviron);
183
184 if (__initenv && __initenv != _environ)
185 FreeEnvironment(__initenv);
186 if (_environ)
187 FreeEnvironment(_environ);
188
189 /* destroy heap */
190 HeapDestroy(hHeap);
191
192 TRACE("Detach done\n");
193 break;
194 }
195
196 return TRUE;
197 }
198
199
200
201
202 /*********************************************************************
203 * __GetMainArgs (CRTDLL.@)
204 */
205 void __GetMainArgs( int *argc, char ***argv, char ***envp, int expand_wildcards )
206 {
207 int new_mode = 0;
208 __getmainargs( argc, argv, envp, expand_wildcards, &new_mode );
209 }
210
211
212 /*********************************************************************
213 * _fstat (CRTDLL.@)
214 */
215 int CRTDLL__fstat(int fd, struct crtdll_stat* buf)
216 {
217 extern int _fstat(int,struct _stat*);
218 struct _stat st;
219 int ret;
220
221 if (!(ret = _fstat( fd, &st ))) convert_struct_stat( buf, &st );
222 return ret;
223 }
224
225
226 /*********************************************************************
227 * _stat (CRTDLL.@)
228 */
229 int CRTDLL__stat(const char* path, struct crtdll_stat * buf)
230 {
231 extern int _stat(const char*,struct _stat*);
232 struct _stat st;
233 int ret;
234
235 if (!(ret = _stat( path, &st ))) convert_struct_stat( buf, &st );
236 return ret;
237 }
238
239
240 /*********************************************************************
241 * _strdec (CRTDLL.@)
242 */
243 char *_strdec(const char *str1, const char *str2)
244 {
245 return (char *)(str2 - 1);
246 }
247
248
249 /*********************************************************************
250 * _strinc (CRTDLL.@)
251 */
252 char *_strinc(const char *str)
253 {
254 return (char *)(str + 1);
255 }
256
257
258 /*********************************************************************
259 * _strncnt (CRTDLL.@)
260 */
261 size_t _strncnt(const char *str, size_t maxlen)
262 {
263 size_t len = strlen(str);
264 return (len > maxlen) ? maxlen : len;
265 }
266
267
268 /*********************************************************************
269 * _strnextc (CRTDLL.@)
270 */
271 unsigned int _strnextc(const char *str)
272 {
273 return (unsigned int)str[0];
274 }
275
276
277 /*********************************************************************
278 * _strninc (CRTDLL.@)
279 */
280 char *_strninc(const char *str, size_t len)
281 {
282 return (char *)(str + len);
283 }
284
285
286 /*********************************************************************
287 * _strspnp (CRTDLL.@)
288 */
289 char *_strspnp( const char *str1, const char *str2)
290 {
291 str1 += strspn( str1, str2 );
292 return *str1 ? (char*)str1 : NULL;
293 }
294
295 /* EOF */