Fixed warnings and errors so ReactOS can be compiled with GCC 3.2.
[reactos.git] / reactos / lib / msvcrt / misc / dllmain.c
1 /* $Id: dllmain.c,v 1.13 2002/05/05 14:57:41 chorns Exp $
2 *
3 * ReactOS MSVCRT.DLL Compatibility Library
4 */
5 #include <windows.h>
6
7 #include <msvcrt/internal/tls.h>
8 #include <msvcrt/stdlib.h>
9
10 #define NDEBUG
11 #include <msvcrt/msvcrtdbg.h>
12
13 static int nAttachCount = 0;
14
15 unsigned int _osver = 0;
16 unsigned int _winminor = 0;
17 unsigned int _winmajor = 0;
18 unsigned int _winver = 0;
19
20 char *_acmdln = NULL; /* pointer to ascii command line */
21 #undef _environ
22 char **_environ = NULL; /* pointer to environment block */
23 char ***_environ_dll = &_environ;/* pointer to environment block */
24
25 char **__initenv = NULL;
26
27 char *_pgmptr = NULL; /* pointer to program name */
28
29 int __app_type = 0; //_UNKNOWN_APP; /* application type */
30
31 int __mb_cur_max = 1;
32
33 HANDLE hHeap = NULL; /* handle for heap */
34
35
36 /* FUNCTIONS **************************************************************/
37
38 int BlockEnvToEnviron()
39 {
40 char * ptr, * ptr2;
41 int i, len;
42
43 DPRINT("BlockEnvToEnviron()\n");
44
45 if (_environ)
46 {
47 FreeEnvironmentStringsA(_environ[0]);
48 free(_environ);
49 _environ = NULL;
50 }
51 ptr2 = ptr = (char*)GetEnvironmentStringsA();
52 if (ptr == NULL)
53 {
54 DPRINT("GetEnvironmentStringsA() returnd NULL\n");
55 return -1;
56 }
57 len = 0;
58 while (*ptr2)
59 {
60 len++;
61 while (*ptr2++);
62 }
63 _environ = malloc((len + 1) * sizeof(char*));
64 if (_environ == NULL)
65 {
66 FreeEnvironmentStringsA(ptr);
67 return -1;
68 }
69 for (i = 0; i < len && *ptr; i++)
70 {
71 _environ[i] = ptr;
72 while (*ptr++);
73 }
74 _environ[i] = NULL;
75 return 0;
76 }
77
78 BOOL __stdcall
79 DllMain(PVOID hinstDll,
80 ULONG dwReason,
81 PVOID reserved)
82 {
83 switch (dwReason)
84 {
85 case DLL_PROCESS_ATTACH://1
86 /* initialize version info */
87 DPRINT("Attach %d\n", nAttachCount);
88 _osver = GetVersion();
89 _winmajor = (_osver >> 8) & 0xFF;
90 _winminor = _osver & 0xFF;
91 _winver = (_winmajor << 8) + _winminor;
92 _osver = (_osver >> 16) & 0xFFFF;
93
94 if (hHeap == NULL || hHeap == INVALID_HANDLE_VALUE)
95 {
96 hHeap = HeapCreate(0, 0, 0);
97 if (hHeap == NULL || hHeap == INVALID_HANDLE_VALUE)
98 {
99 return FALSE;
100 }
101 }
102
103 /* create tls stuff */
104 if (!CreateThreadData())
105 return FALSE;
106
107 _acmdln = (char *)GetCommandLineA();
108
109 /* FIXME: This crashes all applications */
110 if (BlockEnvToEnviron() < 0)
111 return FALSE;
112
113 /* FIXME: more initializations... */
114
115 nAttachCount++;
116 break;
117
118 case DLL_THREAD_ATTACH://2
119 break;
120
121 case DLL_THREAD_DETACH://4
122 FreeThreadData(NULL);
123 break;
124
125 case DLL_PROCESS_DETACH://0
126 DPRINT("Detach %d\n", nAttachCount);
127 if (nAttachCount > 0)
128 {
129 nAttachCount--;
130
131 /* FIXME: more cleanup... */
132 _fcloseall();
133
134 /* destroy tls stuff */
135 DestroyThreadData();
136
137 /* destroy heap */
138 if (nAttachCount == 0)
139 {
140
141 if (_environ)
142 {
143 FreeEnvironmentStringsA(_environ[0]);
144 free(_environ);
145 _environ = NULL;
146 }
147 #if 1
148 HeapDestroy(hHeap);
149 hHeap = NULL;
150 #endif
151 }
152 }
153 break;
154 }
155
156 return TRUE;
157 }
158
159
160
161 void __set_app_type(int app_type)
162 {
163 __app_type = app_type;
164 }
165
166
167 char **__p__acmdln(void)
168 {
169 return &_acmdln;
170 }
171
172 char ***__p__environ(void)
173 {
174 return _environ_dll;
175 }
176
177 char ***__p___initenv(void)
178 {
179 return &__initenv;
180 }
181
182 int *__p___mb_cur_max(void)
183 {
184 return &__mb_cur_max;
185 }
186
187 unsigned int *__p__osver(void)
188 {
189 return &_osver;
190 }
191
192 char **__p__pgmptr(void)
193 {
194 return &_pgmptr;
195 }
196
197 unsigned int *__p__winmajor(void)
198 {
199 return &_winmajor;
200 }
201
202 unsigned int *__p__winminor(void)
203 {
204 return &_winminor;
205 }
206
207 unsigned int *__p__winver(void)
208 {
209 return &_winver;
210 }
211
212
213
214 /* EOF */