xref: /reactos/dll/win32/crtdll/dllmain.c (revision b5218987)
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>
24 WINE_DEFAULT_DEBUG_CHANNEL(crtdll);
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 
36 extern unsigned int _osver;
37 extern unsigned int _winminor;
38 extern unsigned int _winmajor;
39 extern unsigned int _winver;
40 
41 unsigned int CRTDLL__basemajor_dll = 0;
42 unsigned int CRTDLL__baseminor_dll = 0;
43 unsigned int CRTDLL__baseversion_dll = 0;
44 unsigned int CRTDLL__cpumode_dll = 0;
45 unsigned int CRTDLL__osmajor_dll = 0;
46 unsigned int CRTDLL__osminor_dll = 0;
47 unsigned int CRTDLL__osmode_dll = 0;
48 unsigned int CRTDLL__osversion_dll = 0;
49 int _fileinfo_dll;
50 
51 extern char* _acmdln;        /* pointer to ascii command line */
52 extern wchar_t* _wcmdln;     /* pointer to wide character command line */
53 #undef _environ
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 */
58 
59 /* dev_t is a short in crtdll but an unsigned int in msvcrt */
60 typedef short crtdll_dev_t;
61 
62 struct crtdll_stat
63 {
64   crtdll_dev_t   st_dev;
65   _ino_t         st_ino;
66   unsigned short st_mode;
67   short          st_nlink;
68   short          st_uid;
69   short          st_gid;
70   crtdll_dev_t   st_rdev;
71   _off_t         st_size;
72   time_t         st_atime;
73   time_t         st_mtime;
74   time_t         st_ctime;
75 };
76 
77 /* convert struct _stat from crtdll format to msvcrt format */
78 static void convert_struct_stat( struct crtdll_stat *dst, const struct _stat *src )
79 {
80     dst->st_dev   = src->st_dev;
81     dst->st_ino   = src->st_ino;
82     dst->st_mode  = src->st_mode;
83     dst->st_nlink = src->st_nlink;
84     dst->st_uid   = src->st_uid;
85     dst->st_gid   = src->st_gid;
86     dst->st_rdev  = src->st_rdev;
87     dst->st_size  = src->st_size;
88     dst->st_atime = src->st_atime;
89     dst->st_mtime = src->st_mtime;
90     dst->st_ctime = src->st_ctime;
91 }
92 
93 /* LIBRARY ENTRY POINT ********************************************************/
94 
95 BOOL
96 WINAPI
97 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
98 {
99     DWORD version;
100     switch (dwReason)
101     {
102     case DLL_PROCESS_ATTACH:
103         version = GetVersion();
104 
105         /* initialize version info */
106         CRTDLL__basemajor_dll   = (version >> 24) & 0xFF;
107         CRTDLL__baseminor_dll   = (version >> 16) & 0xFF;
108         CRTDLL__baseversion_dll = (version >> 16);
109         CRTDLL__cpumode_dll     = 1; /* FIXME */
110         CRTDLL__osmajor_dll     = (version >>8) & 0xFF;
111         CRTDLL__osminor_dll     = (version & 0xFF);
112         CRTDLL__osmode_dll      = 1; /* FIXME */
113         CRTDLL__osversion_dll   = (version & 0xFFFF);
114 
115         _winmajor = (_osver >> 8) & 0xFF;
116         _winminor = _osver & 0xFF;
117         _winver = (_winmajor << 8) + _winminor;
118         _osver = (_osver >> 16) & 0xFFFF;
119 
120         /* create tls stuff */
121         if (!msvcrt_init_tls())
122           return FALSE;
123 
124         if (BlockEnvToEnvironA() < 0)
125             return FALSE;
126 
127         if (BlockEnvToEnvironW() < 0)
128         {
129             FreeEnvironment(_environ);
130             return FALSE;
131         }
132 
133         _acmdln = _strdup(GetCommandLineA());
134         _wcmdln = _wcsdup(GetCommandLineW());
135 
136         /* Initialization of the WINE code */
137         msvcrt_init_mt_locks();
138         //if(!msvcrt_init_locale()) {
139             //msvcrt_free_mt_locks();
140            // msvcrt_free_tls_mem();
141             //return FALSE;
142         //}
143         //msvcrt_init_math();
144         msvcrt_init_io();
145         //msvcrt_init_console();
146         //msvcrt_init_args();
147         //msvcrt_init_signals();
148         _setmbcp(_MB_CP_LOCALE);
149         TRACE("Attach done\n");
150         break;
151     case DLL_THREAD_ATTACH:
152         break;
153 
154     case DLL_THREAD_DETACH:
155         msvcrt_free_tls_mem();
156         break;
157 
158     case DLL_PROCESS_DETACH:
159         TRACE("Detach\n");
160         /* Deinit of the WINE code */
161         msvcrt_free_io();
162         if (reserved) break;
163         msvcrt_free_popen_data();
164         msvcrt_free_mt_locks();
165         //msvcrt_free_console();
166         //msvcrt_free_args();
167         //msvcrt_free_signals();
168         msvcrt_free_tls_mem();
169         if (!msvcrt_free_tls())
170           return FALSE;
171         //MSVCRT__free_locale(MSVCRT_locale);
172 
173         if (__winitenv && __winitenv != _wenviron)
174             FreeEnvironment((char**)__winitenv);
175         if (_wenviron)
176             FreeEnvironment((char**)_wenviron);
177 
178         if (__initenv && __initenv != _environ)
179             FreeEnvironment(__initenv);
180         if (_environ)
181             FreeEnvironment(_environ);
182 
183         TRACE("Detach done\n");
184         break;
185     }
186 
187     return TRUE;
188 }
189 
190 
191 /*********************************************************************
192  *                  __GetMainArgs  (CRTDLL.@)
193  */
194 void __GetMainArgs( int *argc, char ***argv, char ***envp, int expand_wildcards )
195 {
196     int new_mode = 0;
197     __getmainargs( argc, argv, envp, expand_wildcards, &new_mode );
198 }
199 
200 
201 /*********************************************************************
202  *    _fstat (CRTDLL.@)
203  */
204 int CRTDLL__fstat(int fd, struct crtdll_stat* buf)
205 {
206     extern int _fstat(int,struct _stat*);
207     struct _stat st;
208     int ret;
209 
210     if (!(ret = _fstat( fd, &st ))) convert_struct_stat( buf, &st );
211     return ret;
212 }
213 
214 
215 /*********************************************************************
216  *    _stat (CRTDLL.@)
217  */
218 int CRTDLL__stat(const char* path, struct crtdll_stat * buf)
219 {
220     extern int _stat(const char*,struct _stat*);
221     struct _stat st;
222     int ret;
223 
224     if (!(ret = _stat( path, &st ))) convert_struct_stat( buf, &st );
225     return ret;
226 }
227 
228 
229 /*********************************************************************
230  *    _strdec (CRTDLL.@)
231  */
232 char *_strdec(const char *str1, const char *str2)
233 {
234     return (char *)(str2 - 1);
235 }
236 
237 
238 /*********************************************************************
239  *    _strinc (CRTDLL.@)
240  */
241 char *_strinc(const char *str)
242 {
243     return (char *)(str + 1);
244 }
245 
246 
247 /*********************************************************************
248  *    _strncnt (CRTDLL.@)
249  */
250 size_t _strncnt(const char *str, size_t maxlen)
251 {
252     size_t len = strlen(str);
253     return (len > maxlen) ? maxlen : len;
254 }
255 
256 
257 /*********************************************************************
258  *    _strnextc (CRTDLL.@)
259  */
260 unsigned int _strnextc(const char *str)
261 {
262     return (unsigned int)str[0];
263 }
264 
265 
266 /*********************************************************************
267  *    _strninc (CRTDLL.@)
268  */
269 char *_strninc(const char *str, size_t len)
270 {
271     return (char *)(str + len);
272 }
273 
274 
275 /*********************************************************************
276  *    _strspnp (CRTDLL.@)
277  */
278 char *_strspnp( const char *str1, const char *str2)
279 {
280     str1 += strspn( str1, str2 );
281     return *str1 ? (char*)str1 : NULL;
282 }
283 
284 /* EOF */
285