1 /* $Id: dllmain.c 12852 2005-01-06 13:58:04Z mf $
5 * ReactOS MSVCRT.DLL Compatibility Library
7 * THIS SOFTWARE IS NOT COPYRIGHTED
9 * This source code is offered for use in the public domain. You may
10 * use, modify or distribute it freely.
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.
19 * $Date: 2005-01-06 14:58:04 +0100 (Thu, 06 Jan 2005) $
25 #include <internal/tls.h>
27 #include <internal/wine/msvcrt.h>
31 #include <internal/debug.h>
34 /* EXTERNAL PROTOTYPES ********************************************************/
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 extern void _atexit_cleanup(void);
43 extern unsigned int _osver
;
44 extern unsigned int _winminor
;
45 extern unsigned int _winmajor
;
46 extern unsigned int _winver
;
48 unsigned int CRTDLL__basemajor_dll
;
49 unsigned int CRTDLL__baseminor_dll
;
50 unsigned int CRTDLL__baseversion_dll
;
51 unsigned int CRTDLL__cpumode_dll
;
52 unsigned int CRTDLL__osmajor_dll
;
53 unsigned int CRTDLL__osminor_dll
;
54 unsigned int CRTDLL__osmode_dll
;
55 unsigned int CRTDLL__osversion_dll
;
57 extern char* _acmdln
; /* pointer to ascii command line */
58 extern wchar_t* _wcmdln
; /* pointer to wide character command line */
60 extern char** _environ
; /* pointer to environment block */
61 extern char** __initenv
; /* pointer to initial environment block */
62 extern wchar_t** _wenviron
; /* pointer to environment block */
63 extern wchar_t** __winitenv
; /* pointer to initial environment block */
67 /* dev_t is a short in crtdll but an unsigned int in msvcrt */
68 typedef short crtdll_dev_t
;
74 unsigned short st_mode
;
85 /* convert struct _stat from crtdll format to msvcrt format */
86 static void convert_struct_stat( struct crtdll_stat
*dst
, const struct _stat
*src
)
88 dst
->st_dev
= src
->st_dev
;
89 dst
->st_ino
= src
->st_ino
;
90 dst
->st_mode
= src
->st_mode
;
91 dst
->st_nlink
= src
->st_nlink
;
92 dst
->st_uid
= src
->st_uid
;
93 dst
->st_gid
= src
->st_gid
;
94 dst
->st_rdev
= src
->st_rdev
;
95 dst
->st_size
= src
->st_size
;
96 dst
->st_atime
= src
->st_atime
;
97 dst
->st_mtime
= src
->st_mtime
;
98 dst
->st_ctime
= src
->st_ctime
;
102 extern void __getmainargs( int *argc
, char ***argv
, char ***envp
,
103 int expand_wildcards
, int *new_mode
);
105 /* LIBRARY GLOBAL VARIABLES ***************************************************/
107 HANDLE hHeap
= NULL
; /* handle for heap */
110 /* LIBRARY ENTRY POINT ********************************************************/
114 DllMain(PVOID hinstDll
, ULONG dwReason
, PVOID reserved
)
118 case DLL_PROCESS_ATTACH
://1
119 /* initialize version info */
120 DPRINT("Attach %d\n", nAttachCount
);
122 _osver
= GetVersion();
124 CRTDLL__basemajor_dll
= (_osver
>> 24) & 0xFF;
125 CRTDLL__baseminor_dll
= (_osver
>> 16) & 0xFF;
126 CRTDLL__baseversion_dll
= (_osver
>> 16);
127 CRTDLL__cpumode_dll
= 1; /* FIXME */
128 CRTDLL__osmajor_dll
= (_osver
>>8) & 0xFF;
129 CRTDLL__osminor_dll
= (_osver
& 0xFF);
130 CRTDLL__osmode_dll
= 1; /* FIXME */
131 CRTDLL__osversion_dll
= (_osver
& 0xFFFF);
133 _winmajor
= (_osver
>> 8) & 0xFF;
134 _winminor
= _osver
& 0xFF;
135 _winver
= (_winmajor
<< 8) + _winminor
;
136 _osver
= (_osver
>> 16) & 0xFFFF;
137 hHeap
= HeapCreate(0, 100000, 0);
140 if (!__fileno_init())
143 /* create tls stuff */
144 if (!CreateThreadData())
147 if (BlockEnvToEnvironA() < 0)
150 if (BlockEnvToEnvironW() < 0)
152 FreeEnvironment((char**)_wenviron
);
156 _acmdln
= _strdup(GetCommandLineA());
157 _wcmdln
= _wcsdup(GetCommandLineW());
159 /* FIXME: more initializations... */
161 /* FIXME: Initialization of the WINE code */
162 msvcrt_init_mt_locks();
164 DPRINT("Attach done\n");
167 case DLL_THREAD_ATTACH
://2
170 case DLL_THREAD_DETACH
://4
171 FreeThreadData(NULL
);
174 case DLL_PROCESS_DETACH
://0
175 DPRINT("Detach %d\n", nAttachCount
);
176 /* FIXME: more cleanup... */
180 /* destroy tls stuff */
183 if (__winitenv
&& __winitenv
!= _wenviron
)
184 FreeEnvironment((char**)__winitenv
);
186 FreeEnvironment((char**)_wenviron
);
188 if (__initenv
&& __initenv
!= _environ
)
189 FreeEnvironment(__initenv
);
191 FreeEnvironment(_environ
);
196 DPRINT("Detach done\n");
206 /*********************************************************************
207 * __GetMainArgs (CRTDLL.@)
209 void __GetMainArgs( int *argc
, char ***argv
, char ***envp
, int expand_wildcards
)
212 __getmainargs( argc
, argv
, envp
, expand_wildcards
, &new_mode
);
216 /*********************************************************************
219 int CRTDLL__fstat(int fd
, struct crtdll_stat
* buf
)
221 extern int _fstat(int,struct _stat
*);
225 if (!(ret
= _fstat( fd
, &st
))) convert_struct_stat( buf
, &st
);
230 /*********************************************************************
233 int CRTDLL__stat(const char* path
, struct crtdll_stat
* buf
)
235 extern int _stat(const char*,struct _stat
*);
239 if (!(ret
= _stat( path
, &st
))) convert_struct_stat( buf
, &st
);
244 /*********************************************************************
247 char *_strdec(const char *str1
, const char *str2
)
249 return (char *)(str2
- 1);
253 /*********************************************************************
256 char *_strinc(const char *str
)
258 return (char *)(str
+ 1);
262 /*********************************************************************
263 * _strncnt (CRTDLL.@)
265 size_t _strncnt(const char *str
, size_t maxlen
)
267 size_t len
= strlen(str
);
268 return (len
> maxlen
) ? maxlen
: len
;
272 /*********************************************************************
273 * _strnextc (CRTDLL.@)
275 unsigned int _strnextc(const char *str
)
277 return (unsigned int)str
[0];
281 /*********************************************************************
282 * _strninc (CRTDLL.@)
284 char *_strninc(const char *str
, size_t len
)
286 return (char *)(str
+ len
);
290 /*********************************************************************
291 * _strspnp (CRTDLL.@)
293 char *_strspnp( const char *str1
, const char *str2
)
295 str1
+= strspn( str1
, str2
);
296 return *str1
? (char*)str1
: NULL
;