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