ReactOS  0.4.14-dev-368-gfa26425
dllmain.c
Go to the documentation of this file.
1 /*
2  * dllmain.c
3  *
4  * ReactOS CRTDLL.DLL Compatibility Library
5  *
6  * THIS SOFTWARE IS NOT COPYRIGHTED
7  *
8  * This source code is offered for use in the public domain. You may
9  * use, modify or distribute it freely.
10  *
11  * This code is distributed in the hope that it will be useful but
12  * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
13  * DISCLAMED. This includes but is not limited to warranties of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  */
17 
18 #include "precomp.h"
19 #include <mbctype.h>
20 #include <sys/stat.h>
21 #include <internal/wine/msvcrt.h>
22 
23 #include <wine/debug.h>
25 
26 /* from msvcrt */
27 extern void __getmainargs( int *argc, char ***argv, char ***envp,
28  int expand_wildcards, int *new_mode );
29 
30 /* EXTERNAL PROTOTYPES ********************************************************/
31 
32 extern int BlockEnvToEnvironA(void);
33 extern int BlockEnvToEnvironW(void);
34 extern void FreeEnvironment(char **environment);
35 extern void _atexit_cleanup(void);
36 
37 extern unsigned int _osver;
38 extern unsigned int _winminor;
39 extern unsigned int _winmajor;
40 extern unsigned int _winver;
41 
42 unsigned int CRTDLL__basemajor_dll = 0;
43 unsigned int CRTDLL__baseminor_dll = 0;
44 unsigned int CRTDLL__baseversion_dll = 0;
45 unsigned int CRTDLL__cpumode_dll = 0;
46 unsigned int CRTDLL__osmajor_dll = 0;
47 unsigned int CRTDLL__osminor_dll = 0;
48 unsigned int CRTDLL__osmode_dll = 0;
49 unsigned int CRTDLL__osversion_dll = 0;
51 
52 extern char* _acmdln; /* pointer to ascii command line */
53 extern wchar_t* _wcmdln; /* pointer to wide character command line */
54 #undef _environ
55 extern char** _environ; /* pointer to environment block */
56 extern char** __initenv; /* pointer to initial environment block */
57 extern wchar_t** _wenviron; /* pointer to environment block */
58 extern wchar_t** __winitenv; /* pointer to initial environment block */
59 
60 /* dev_t is a short in crtdll but an unsigned int in msvcrt */
61 typedef short crtdll_dev_t;
62 
64 {
67  unsigned short st_mode;
68  short st_nlink;
69  short st_uid;
70  short st_gid;
76 };
77 
78 /* convert struct _stat from crtdll format to msvcrt format */
79 static void convert_struct_stat( struct crtdll_stat *dst, const struct _stat *src )
80 {
81  dst->st_dev = src->st_dev;
82  dst->st_ino = src->st_ino;
83  dst->st_mode = src->st_mode;
84  dst->st_nlink = src->st_nlink;
85  dst->st_uid = src->st_uid;
86  dst->st_gid = src->st_gid;
87  dst->st_rdev = src->st_rdev;
88  dst->st_size = src->st_size;
89  dst->st_atime = src->st_atime;
90  dst->st_mtime = src->st_mtime;
91  dst->st_ctime = src->st_ctime;
92 }
93 
94 /* LIBRARY ENTRY POINT ********************************************************/
95 
96 BOOL
97 WINAPI
99 {
100  DWORD version;
101  switch (dwReason)
102  {
103  case DLL_PROCESS_ATTACH:
104  version = GetVersion();
105 
106  /* initialize version info */
107  CRTDLL__basemajor_dll = (version >> 24) & 0xFF;
108  CRTDLL__baseminor_dll = (version >> 16) & 0xFF;
110  CRTDLL__cpumode_dll = 1; /* FIXME */
111  CRTDLL__osmajor_dll = (version >>8) & 0xFF;
112  CRTDLL__osminor_dll = (version & 0xFF);
113  CRTDLL__osmode_dll = 1; /* FIXME */
114  CRTDLL__osversion_dll = (version & 0xFFFF);
115 
116  _winmajor = (_osver >> 8) & 0xFF;
117  _winminor = _osver & 0xFF;
118  _winver = (_winmajor << 8) + _winminor;
119  _osver = (_osver >> 16) & 0xFFFF;
120 
121  /* create tls stuff */
122  if (!msvcrt_init_tls())
123  return FALSE;
124 
125  if (BlockEnvToEnvironA() < 0)
126  return FALSE;
127 
128  if (BlockEnvToEnvironW() < 0)
129  {
131  return FALSE;
132  }
133 
136 
137  /* Initialization of the WINE code */
139  //if(!msvcrt_init_locale()) {
140  //msvcrt_free_mt_locks();
141  // msvcrt_free_tls_mem();
142  //return FALSE;
143  //}
144  //msvcrt_init_math();
145  msvcrt_init_io();
146  //msvcrt_init_console();
147  //msvcrt_init_args();
148  //msvcrt_init_signals();
150  TRACE("Attach done\n");
151  break;
152  case DLL_THREAD_ATTACH:
153  break;
154 
155  case DLL_THREAD_DETACH:
157  break;
158 
159  case DLL_PROCESS_DETACH:
160  TRACE("Detach\n");
161  /* Deinit of the WINE code */
162  msvcrt_free_io();
163  if (reserved) break;
166  //msvcrt_free_console();
167  //msvcrt_free_args();
168  //msvcrt_free_signals();
170  if (!msvcrt_free_tls())
171  return FALSE;
172  //MSVCRT__free_locale(MSVCRT_locale);
173 
174  if (__winitenv && __winitenv != _wenviron)
175  FreeEnvironment((char**)__winitenv);
176  if (_wenviron)
177  FreeEnvironment((char**)_wenviron);
178 
179  if (__initenv && __initenv != _environ)
181  if (_environ)
183 
184  TRACE("Detach done\n");
185  break;
186  }
187 
188  return TRUE;
189 }
190 
191 
192 /*********************************************************************
193  * __GetMainArgs (CRTDLL.@)
194  */
195 void __GetMainArgs( int *argc, char ***argv, char ***envp, int expand_wildcards )
196 {
197  int new_mode = 0;
198  __getmainargs( argc, argv, envp, expand_wildcards, &new_mode );
199 }
200 
201 
202 /*********************************************************************
203  * _fstat (CRTDLL.@)
204  */
205 int CRTDLL__fstat(int fd, struct crtdll_stat* buf)
206 {
207  extern int _fstat(int,struct _stat*);
208  struct _stat st;
209  int ret;
210 
211  if (!(ret = _fstat( fd, &st ))) convert_struct_stat( buf, &st );
212  return ret;
213 }
214 
215 
216 /*********************************************************************
217  * _stat (CRTDLL.@)
218  */
219 int CRTDLL__stat(const char* path, struct crtdll_stat * buf)
220 {
221  extern int _stat(const char*,struct _stat*);
222  struct _stat st;
223  int ret;
224 
225  if (!(ret = _stat( path, &st ))) convert_struct_stat( buf, &st );
226  return ret;
227 }
228 
229 
230 /*********************************************************************
231  * _strdec (CRTDLL.@)
232  */
233 char *_strdec(const char *str1, const char *str2)
234 {
235  return (char *)(str2 - 1);
236 }
237 
238 
239 /*********************************************************************
240  * _strinc (CRTDLL.@)
241  */
242 char *_strinc(const char *str)
243 {
244  return (char *)(str + 1);
245 }
246 
247 
248 /*********************************************************************
249  * _strncnt (CRTDLL.@)
250  */
251 size_t _strncnt(const char *str, size_t maxlen)
252 {
253  size_t len = strlen(str);
254  return (len > maxlen) ? maxlen : len;
255 }
256 
257 
258 /*********************************************************************
259  * _strnextc (CRTDLL.@)
260  */
261 unsigned int _strnextc(const char *str)
262 {
263  return (unsigned int)str[0];
264 }
265 
266 
267 /*********************************************************************
268  * _strninc (CRTDLL.@)
269  */
270 char *_strninc(const char *str, size_t len)
271 {
272  return (char *)(str + len);
273 }
274 
275 
276 /*********************************************************************
277  * _strspnp (CRTDLL.@)
278  */
279 char *_strspnp( const char *str1, const char *str2)
280 {
281  str1 += strspn( str1, str2 );
282  return *str1 ? (char*)str1 : NULL;
283 }
284 
285 /* EOF */
short st_uid
Definition: dllmain.c:69
static int argc
Definition: ServiceArgs.c:12
time_t st_mtime
Definition: dllmain.c:74
unsigned int CRTDLL__osversion_dll
Definition: dllmain.c:49
unsigned int _strnextc(const char *str)
Definition: dllmain.c:261
int CRTDLL__stat(const char *path, struct crtdll_stat *buf)
Definition: dllmain.c:219
unsigned int CRTDLL__cpumode_dll
Definition: dllmain.c:45
#define TRUE
Definition: types.h:120
void msvcrt_free_io(void)
Definition: file.c:1100
wchar_t ** _wenviron
Definition: environ.c:23
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define DLL_THREAD_ATTACH
Definition: compat.h:121
unsigned int CRTDLL__basemajor_dll
Definition: dllmain.c:42
void msvcrt_free_tls_mem(void)
Definition: tls.c:51
crtdll_dev_t st_rdev
Definition: dllmain.c:71
Definition: stat.h:40
unsigned int CRTDLL__osmode_dll
Definition: dllmain.c:48
_Check_return_ _CRTIMP size_t __cdecl strspn(_In_z_ const char *_Str, _In_z_ const char *_Control)
static int fd
Definition: io.c:51
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
void msvcrt_init_io(void)
Definition: file.c:457
BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
Definition: dllmain.c:52
#define argv
Definition: mplay32.c:18
#define _strdup
Definition: debug_ros.c:7
char * _strinc(const char *str)
Definition: dllmain.c:242
#define DLL_THREAD_DETACH
Definition: compat.h:122
DWORD WINAPI GetVersion(VOID)
Definition: version.c:22
unsigned int _winminor
Definition: environ.c:12
wchar_t ** __winitenv
Definition: environ.c:25
void msvcrt_free_popen_data(void)
Definition: popen.c:29
unsigned int CRTDLL__baseminor_dll
Definition: dllmain.c:43
BOOL msvcrt_free_tls(void)
Definition: tls.c:21
DWORD dwReason
Definition: misc.cpp:154
char ** _environ
Definition: environ.c:22
unsigned short st_mode
Definition: dllmain.c:67
unsigned int BOOL
Definition: ntddk_ex.h:94
LPWSTR WINAPI GetCommandLineW(VOID)
Definition: proc.c:2013
r reserved
Definition: btrfs.c:2865
unsigned short _ino_t
Definition: types.h:17
const WCHAR * str
short crtdll_dev_t
Definition: dllmain.c:61
smooth NULL
Definition: ftsmooth.c:416
static const WCHAR version[]
Definition: asmname.c:66
char * _acmdln
Definition: environ.c:18
unsigned int _osver
Definition: environ.c:11
#define DLL_PROCESS_DETACH
Definition: compat.h:119
void msvcrt_free_mt_locks(void)
Definition: lock.c:77
#define TRACE(s)
Definition: solgame.cpp:4
_ino_t st_ino
Definition: dllmain.c:66
char * _strninc(const char *str, size_t len)
Definition: dllmain.c:270
unsigned int _winmajor
Definition: environ.c:13
static void convert_struct_stat(struct crtdll_stat *dst, const struct _stat *src)
Definition: dllmain.c:79
void __getmainargs(int *argc, char ***argv, char ***envp, int expand_wildcards, int *new_mode)
Definition: getargs.c:182
int __cdecl _setmbcp(int)
Definition: _setmbcp.c:218
#define WINAPI
Definition: msvc.h:8
size_t _strncnt(const char *str, size_t maxlen)
Definition: dllmain.c:251
unsigned long DWORD
Definition: ntddk_ex.h:95
time_t st_atime
Definition: dllmain.c:73
BOOL msvcrt_init_tls(void)
Definition: tls.c:9
unsigned int _winver
Definition: environ.c:14
LPSTR WINAPI GetCommandLineA(VOID)
Definition: proc.c:2003
int ret
#define _MB_CP_LOCALE
Definition: msvcrt.h:831
char ** __initenv
Definition: environ.c:24
time_t st_ctime
Definition: dllmain.c:75
GLenum GLsizei len
Definition: glext.h:6722
short st_nlink
Definition: dllmain.c:68
char * _strdec(const char *str1, const char *str2)
Definition: dllmain.c:233
GLenum src
Definition: glext.h:6340
_CRTIMP int __cdecl _stat(_In_z_ const char *_Name, _Out_ struct _stat *_Stat)
wchar_t * _wcmdln
Definition: environ.c:19
void FreeEnvironment(char **environment)
Definition: environ.c:190
_CRTIMP int __cdecl _fstat(_In_ int _FileDes, _Out_ struct _stat *_Stat)
void __GetMainArgs(int *argc, char ***argv, char ***envp, int expand_wildcards)
Definition: dllmain.c:195
char * _strspnp(const char *str1, const char *str2)
Definition: dllmain.c:279
__kernel_time_t time_t
Definition: linux.h:252
_off_t st_size
Definition: dllmain.c:72
WINE_DEFAULT_DEBUG_CHANNEL(crtdll)
Definition: services.c:325
GLenum GLenum dst
Definition: glext.h:6340
unsigned int CRTDLL__osmajor_dll
Definition: dllmain.c:46
int BlockEnvToEnvironW(void)
Definition: environ.c:86
unsigned int CRTDLL__baseversion_dll
Definition: dllmain.c:44
int _fileinfo_dll
Definition: dllmain.c:50
long _off_t
Definition: stdio.h:81
short st_gid
Definition: dllmain.c:70
unsigned int ULONG
Definition: retypes.h:1
int CRTDLL__fstat(int fd, struct crtdll_stat *buf)
Definition: dllmain.c:205
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)
int BlockEnvToEnvironA(void)
Definition: environ.c:34
unsigned int CRTDLL__osminor_dll
Definition: dllmain.c:47
void _atexit_cleanup(void)
void msvcrt_init_mt_locks(void)
Definition: lock.c:54
crtdll_dev_t st_dev
Definition: dllmain.c:65