[MSVCRT][CRT]: Improvements/fixes over popen(), from Wine code and ported by Andreas...
[reactos.git] / reactos / dll / win32 / msvcrt40 / msvcrt40.c
1 /*
2 * msvcrt40 main file
3 *
4 * Copyright (C) 2007 Louis Lenders
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22
23 #include <stdarg.h>
24 //#include <stdio.h>
25 #define _CRT_PRECOMP_H
26 #include <internal/tls.h>
27 //#include <stdlib.h>
28 //#include <windows.h>
29 #include <internal/wine/msvcrt.h>
30 #include <internal/locale.h>
31 //#include <locale.h>
32 //#include <mbctype.h>
33
34 #include <wine/debug.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36
37 /* EXTERNAL PROTOTYPES ********************************************************/
38
39 extern int BlockEnvToEnvironA(void);
40 extern int BlockEnvToEnvironW(void);
41 extern void FreeEnvironment(char **environment);
42
43 extern unsigned int _osplatform;
44 extern unsigned int _osver;
45 extern unsigned int _winminor;
46 extern unsigned int _winmajor;
47 extern unsigned int _winver;
48
49 extern char* _acmdln; /* pointer to ascii command line */
50 extern wchar_t* _wcmdln; /* pointer to wide character command line */
51 #undef _environ
52 extern char** _environ; /* pointer to environment block */
53 extern char** __initenv; /* pointer to initial environment block */
54 extern wchar_t** _wenviron; /* pointer to environment block */
55 extern wchar_t** __winitenv; /* pointer to initial environment block */
56
57 /* LIBRARY ENTRY POINT ********************************************************/
58
59 BOOL
60 WINAPI
61 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
62 {
63 OSVERSIONINFOW osvi;
64 switch (dwReason)
65 {
66 case DLL_PROCESS_ATTACH:
67 /* initialize version info */
68 TRACE("Process Attach\n");
69 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
70 GetVersionExW( &osvi );
71 _winver = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
72 _winmajor = osvi.dwMajorVersion;
73 _winminor = osvi.dwMinorVersion;
74 _osplatform = osvi.dwPlatformId;
75 _osver = osvi.dwBuildNumber;
76
77 /* create tls stuff */
78 if (!msvcrt_init_tls())
79 return FALSE;
80
81 if (BlockEnvToEnvironA() < 0)
82 return FALSE;
83
84 if (BlockEnvToEnvironW() < 0)
85 {
86 FreeEnvironment(_environ);
87 return FALSE;
88 }
89
90 _acmdln = _strdup(GetCommandLineA());
91 _wcmdln = _wcsdup(GetCommandLineW());
92
93 /* Initialization of the WINE code */
94 msvcrt_init_mt_locks();
95 //msvcrt_init_math();
96 msvcrt_init_io();
97 //msvcrt_init_console();
98 //msvcrt_init_args();
99 //msvcrt_init_signals();
100 TRACE("Attach done\n");
101 break;
102
103 case DLL_THREAD_ATTACH:
104 //msvcrt_get_thread_data creates data when first called
105 break;
106
107 case DLL_THREAD_DETACH:
108 msvcrt_free_tls_mem();
109 break;
110
111 case DLL_PROCESS_DETACH:
112 TRACE("Detach\n");
113 /* Deinit of the WINE code */
114 msvcrt_free_io();
115 if (reserved) break;
116 msvcrt_free_popen_data();
117 msvcrt_free_mt_locks();
118 //msvcrt_free_console();
119 //msvcrt_free_args();
120 //msvcrt_free_signals();
121 msvcrt_free_tls_mem();
122 if (!msvcrt_free_tls())
123 return FALSE;
124 if(global_locale)
125 MSVCRT__free_locale(global_locale);
126
127 if (__winitenv && __winitenv != _wenviron)
128 FreeEnvironment((char**)__winitenv);
129 if (_wenviron)
130 FreeEnvironment((char**)_wenviron);
131
132 if (__initenv && __initenv != _environ)
133 FreeEnvironment(__initenv);
134 if (_environ)
135 FreeEnvironment(_environ);
136
137 TRACE("Detach done\n");
138 break;
139 }
140
141 return TRUE;
142 }
143
144 /* EOF */