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