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/msvcrtdbg.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
);
42 extern unsigned int _osver
;
43 extern unsigned int _winminor
;
44 extern unsigned int _winmajor
;
45 extern unsigned int _winver
;
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
;
56 extern char* _acmdln
; /* pointer to ascii command line */
57 extern wchar_t* _wcmdln
; /* pointer to wide character command line */
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 */
66 /* dev_t is a short in crtdll but an unsigned int in msvcrt */
67 typedef short crtdll_dev_t
;
73 unsigned short st_mode
;
84 /* convert struct _stat from crtdll format to msvcrt format */
85 static void convert_struct_stat( struct crtdll_stat
*dst
, const struct _stat
*src
)
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
;
101 extern void __getmainargs( int *argc
, char ***argv
, char ***envp
,
102 int expand_wildcards
, int *new_mode
);
104 /* LIBRARY GLOBAL VARIABLES ***************************************************/
106 HANDLE hHeap
= NULL
; /* handle for heap */
109 /* LIBRARY ENTRY POINT ********************************************************/
113 DllMain(PVOID hinstDll
, ULONG dwReason
, PVOID reserved
)
117 case DLL_PROCESS_ATTACH
://1
118 /* initialize version info */
119 DPRINT("Attach %d\n", nAttachCount
);
121 _osver
= GetVersion();
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);
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);
139 if (!__fileno_init())
142 /* create tls stuff */
143 if (!CreateThreadData())
146 if (BlockEnvToEnvironA() < 0)
149 if (BlockEnvToEnvironW() < 0)
151 FreeEnvironment((char**)_wenviron
);
155 _acmdln
= _strdup(GetCommandLineA());
156 _wcmdln
= _wcsdup(GetCommandLineW());
158 /* FIXME: more initializations... */
160 /* FIXME: Initialization of the WINE code */
161 msvcrt_init_mt_locks();
163 DPRINT("Attach done\n");
166 case DLL_THREAD_ATTACH
://2
169 case DLL_THREAD_DETACH
://4
170 FreeThreadData(NULL
);
173 case DLL_PROCESS_DETACH
://0
174 DPRINT("Detach %d\n", nAttachCount
);
175 /* FIXME: more cleanup... */
178 /* destroy tls stuff */
181 if (__winitenv
&& __winitenv
!= _wenviron
)
182 FreeEnvironment((char**)__winitenv
);
184 FreeEnvironment((char**)_wenviron
);
186 if (__initenv
&& __initenv
!= _environ
)
187 FreeEnvironment(__initenv
);
189 FreeEnvironment(_environ
);
194 DPRINT("Detach done\n");
204 /*********************************************************************
205 * __GetMainArgs (CRTDLL.@)
207 void __GetMainArgs( int *argc
, char ***argv
, char ***envp
, int expand_wildcards
)
210 __getmainargs( argc
, argv
, envp
, expand_wildcards
, &new_mode
);
214 /*********************************************************************
217 int CRTDLL__fstat(int fd
, struct crtdll_stat
* buf
)
219 extern int _fstat(int,struct _stat
*);
223 if (!(ret
= _fstat( fd
, &st
))) convert_struct_stat( buf
, &st
);
228 /*********************************************************************
231 int CRTDLL__stat(const char* path
, struct crtdll_stat
* buf
)
233 extern int _stat(const char*,struct _stat
*);
237 if (!(ret
= _stat( path
, &st
))) convert_struct_stat( buf
, &st
);
242 /*********************************************************************
245 char *_strdec(const char *str1
, const char *str2
)
247 return (char *)(str2
- 1);
251 /*********************************************************************
254 char *_strinc(const char *str
)
256 return (char *)(str
+ 1);
260 /*********************************************************************
261 * _strncnt (CRTDLL.@)
263 size_t _strncnt(const char *str
, size_t maxlen
)
265 size_t len
= strlen(str
);
266 return (len
> maxlen
) ? maxlen
: len
;
270 /*********************************************************************
271 * _strnextc (CRTDLL.@)
273 unsigned int _strnextc(const char *str
)
275 return (unsigned int)str
[0];
279 /*********************************************************************
280 * _strninc (CRTDLL.@)
282 char *_strninc(const char *str
, size_t len
)
284 return (char *)(str
+ len
);
288 /*********************************************************************
289 * _strspnp (CRTDLL.@)
291 char *_strspnp( const char *str1
, const char *str2
)
293 str1
+= strspn( str1
, str2
);
294 return *str1
? (char*)str1
: NULL
;