:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / msvcrt / misc / dllmain.c
1 /* $Id$
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                         if (nAttachCount==0)
103                         {
104                                 __fileno_init();
105                         }
106
107                         /* create tls stuff */
108                         if (!CreateThreadData())
109                                 return FALSE;
110
111                         _acmdln = (char *)GetCommandLineA();
112
113                         /* FIXME: This crashes all applications */
114                         if (BlockEnvToEnviron() < 0)
115                           return FALSE;
116
117                         /* FIXME: more initializations... */
118
119                         nAttachCount++;
120                         DPRINT("Attach done\n");
121                         break;
122
123                 case DLL_THREAD_ATTACH://2
124                         break;
125
126                 case DLL_THREAD_DETACH://4
127                         FreeThreadData(NULL);
128                         break;
129
130                 case DLL_PROCESS_DETACH://0
131                         DPRINT("Detach %d\n", nAttachCount);
132                         if (nAttachCount > 0)
133                         {
134                                 nAttachCount--;
135
136                                 /* FIXME: more cleanup... */
137                                 _fcloseall();
138
139                                 /* destroy tls stuff */
140                                 DestroyThreadData();
141
142                                 /* destroy heap */
143                                 if (nAttachCount == 0)
144                                 {
145
146                                         if (_environ)
147                                         {
148                                                 FreeEnvironmentStringsA(_environ[0]);
149                                                 free(_environ);
150                                                 _environ = NULL;
151                                         }
152 #if 1
153                                         HeapDestroy(hHeap);
154                                         hHeap = NULL;
155 #endif
156                                 }
157                         DPRINT("Detach done\n");
158                         }
159                         break;
160         }
161
162         return TRUE;
163 }
164
165
166
167 void __set_app_type(int app_type)
168 {
169    __app_type = app_type;
170 }
171
172
173 char **__p__acmdln(void)
174 {
175    return &_acmdln;
176 }
177
178 char ***__p__environ(void)
179 {
180    return _environ_dll;
181 }
182
183 char ***__p___initenv(void)
184 {
185    return &__initenv;
186 }
187
188 int *__p___mb_cur_max(void)
189 {
190    return &__mb_cur_max;
191 }
192
193 unsigned int *__p__osver(void)
194 {
195    return &_osver;
196 }
197
198 char **__p__pgmptr(void)
199 {
200    return &_pgmptr;
201 }
202
203 unsigned int *__p__winmajor(void)
204 {
205    return &_winmajor;
206 }
207
208 unsigned int *__p__winminor(void)
209 {
210    return &_winminor;
211 }
212
213 unsigned int *__p__winver(void)
214 {
215    return &_winver;
216 }
217
218
219
220 /* EOF */