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