1 /* $Id: dllmain.c 12852 2005-01-06 13:58:04Z mf $
5 * ReactOS CRTDLL.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.
20 #include <internal/wine/msvcrt.h>
25 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(crtdll
);
29 /* EXTERNAL PROTOTYPES ********************************************************/
31 extern int BlockEnvToEnvironA(void);
32 extern int BlockEnvToEnvironW(void);
33 extern void FreeEnvironment(char **environment
);
34 extern void _atexit_cleanup(void);
36 extern unsigned int _osver
;
37 extern unsigned int _winminor
;
38 extern unsigned int _winmajor
;
39 extern unsigned int _winver
;
41 unsigned int CRTDLL__basemajor_dll
;
42 unsigned int CRTDLL__baseminor_dll
;
43 unsigned int CRTDLL__baseversion_dll
;
44 unsigned int CRTDLL__cpumode_dll
;
45 unsigned int CRTDLL__osmajor_dll
;
46 unsigned int CRTDLL__osminor_dll
;
47 unsigned int CRTDLL__osmode_dll
;
48 unsigned int CRTDLL__osversion_dll
;
51 extern char* _acmdln
; /* pointer to ascii command line */
52 extern wchar_t* _wcmdln
; /* pointer to wide character command line */
54 extern char** _environ
; /* pointer to environment block */
55 extern char** __initenv
; /* pointer to initial environment block */
56 extern wchar_t** _wenviron
; /* pointer to environment block */
57 extern wchar_t** __winitenv
; /* pointer to initial environment block */
61 /* dev_t is a short in crtdll but an unsigned int in msvcrt */
62 typedef short crtdll_dev_t
;
68 unsigned short st_mode
;
79 /* convert struct _stat from crtdll format to msvcrt format */
80 static void convert_struct_stat( struct crtdll_stat
*dst
, const struct _stat
*src
)
82 dst
->st_dev
= src
->st_dev
;
83 dst
->st_ino
= src
->st_ino
;
84 dst
->st_mode
= src
->st_mode
;
85 dst
->st_nlink
= src
->st_nlink
;
86 dst
->st_uid
= src
->st_uid
;
87 dst
->st_gid
= src
->st_gid
;
88 dst
->st_rdev
= src
->st_rdev
;
89 dst
->st_size
= src
->st_size
;
90 dst
->st_atime
= src
->st_atime
;
91 dst
->st_mtime
= src
->st_mtime
;
92 dst
->st_ctime
= src
->st_ctime
;
96 extern void __getmainargs( int *argc
, char ***argv
, char ***envp
,
97 int expand_wildcards
, int *new_mode
);
99 /* LIBRARY GLOBAL VARIABLES ***************************************************/
101 HANDLE hHeap
= NULL
; /* handle for heap */
104 /* LIBRARY ENTRY POINT ********************************************************/
108 DllMain(PVOID hinstDll
, ULONG dwReason
, PVOID reserved
)
112 case DLL_PROCESS_ATTACH
://1
113 /* initialize version info */
114 //TRACE("Attach %d\n", nAttachCount);
116 _osver
= GetVersion();
118 CRTDLL__basemajor_dll
= (_osver
>> 24) & 0xFF;
119 CRTDLL__baseminor_dll
= (_osver
>> 16) & 0xFF;
120 CRTDLL__baseversion_dll
= (_osver
>> 16);
121 CRTDLL__cpumode_dll
= 1; /* FIXME */
122 CRTDLL__osmajor_dll
= (_osver
>>8) & 0xFF;
123 CRTDLL__osminor_dll
= (_osver
& 0xFF);
124 CRTDLL__osmode_dll
= 1; /* FIXME */
125 CRTDLL__osversion_dll
= (_osver
& 0xFFFF);
127 _winmajor
= (_osver
>> 8) & 0xFF;
128 _winminor
= _osver
& 0xFF;
129 _winver
= (_winmajor
<< 8) + _winminor
;
130 _osver
= (_osver
>> 16) & 0xFFFF;
131 hHeap
= HeapCreate(0, 100000, 0);
135 /* create tls stuff */
136 if (!CreateThreadData())
139 if (BlockEnvToEnvironA() < 0)
142 if (BlockEnvToEnvironW() < 0)
144 FreeEnvironment((char**)_wenviron
);
148 _acmdln
= _strdup(GetCommandLineA());
149 _wcmdln
= _wcsdup(GetCommandLineW());
151 /* FIXME: more initializations... */
153 /* FIXME: Initialization of the WINE code */
154 msvcrt_init_mt_locks();
157 //_setmbcp(_MB_CP_LOCALE);
159 TRACE("Attach done\n");
162 case DLL_THREAD_ATTACH
://2
165 case DLL_THREAD_DETACH
://4
166 FreeThreadData(NULL
);
169 case DLL_PROCESS_DETACH
://0
170 //TRACE("Detach %d\n", nAttachCount);
171 /* Deinit of the WINE code */
173 msvcrt_free_mt_locks();
176 /* destroy tls stuff */
179 if (__winitenv
&& __winitenv
!= _wenviron
)
180 FreeEnvironment((char**)__winitenv
);
182 FreeEnvironment((char**)_wenviron
);
184 if (__initenv
&& __initenv
!= _environ
)
185 FreeEnvironment(__initenv
);
187 FreeEnvironment(_environ
);
192 TRACE("Detach done\n");
202 /*********************************************************************
203 * __GetMainArgs (CRTDLL.@)
205 void __GetMainArgs( int *argc
, char ***argv
, char ***envp
, int expand_wildcards
)
208 __getmainargs( argc
, argv
, envp
, expand_wildcards
, &new_mode
);
212 /*********************************************************************
215 int CRTDLL__fstat(int fd
, struct crtdll_stat
* buf
)
217 extern int _fstat(int,struct _stat
*);
221 if (!(ret
= _fstat( fd
, &st
))) convert_struct_stat( buf
, &st
);
226 /*********************************************************************
229 int CRTDLL__stat(const char* path
, struct crtdll_stat
* buf
)
231 extern int _stat(const char*,struct _stat
*);
235 if (!(ret
= _stat( path
, &st
))) convert_struct_stat( buf
, &st
);
240 /*********************************************************************
243 char *_strdec(const char *str1
, const char *str2
)
245 return (char *)(str2
- 1);
249 /*********************************************************************
252 char *_strinc(const char *str
)
254 return (char *)(str
+ 1);
258 /*********************************************************************
259 * _strncnt (CRTDLL.@)
261 size_t _strncnt(const char *str
, size_t maxlen
)
263 size_t len
= strlen(str
);
264 return (len
> maxlen
) ? maxlen
: len
;
268 /*********************************************************************
269 * _strnextc (CRTDLL.@)
271 unsigned int _strnextc(const char *str
)
273 return (unsigned int)str
[0];
277 /*********************************************************************
278 * _strninc (CRTDLL.@)
280 char *_strninc(const char *str
, size_t len
)
282 return (char *)(str
+ len
);
286 /*********************************************************************
287 * _strspnp (CRTDLL.@)
289 char *_strspnp( const char *str1
, const char *str2
)
291 str1
+= strspn( str1
, str2
);
292 return *str1
? (char*)str1
: NULL
;