data.c 11 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll dll data items
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23

#include "config.h"
#include "wine/port.h"

24 25
#include <math.h>
#include "msvcrt.h"
26
#include "wine/library.h"
27
#include "wine/unicode.h"
28 29 30
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31

Mike McCormack's avatar
Mike McCormack committed
32
int MSVCRT___argc;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
unsigned int MSVCRT_basemajor;/* FIXME: */
unsigned int MSVCRT_baseminor;/* FIXME: */
unsigned int MSVCRT_baseversion; /* FIXME: */
unsigned int MSVCRT__commode;
unsigned int MSVCRT__fmode;
unsigned int MSVCRT_osmajor;/* FIXME: */
unsigned int MSVCRT_osminor;/* FIXME: */
unsigned int MSVCRT_osmode;/* FIXME: */
unsigned int MSVCRT__osver;
unsigned int MSVCRT_osversion; /* FIXME: */
unsigned int MSVCRT__winmajor;
unsigned int MSVCRT__winminor;
unsigned int MSVCRT__winver;
unsigned int MSVCRT___setlc_active;
unsigned int MSVCRT___unguarded_readlc_active;
double MSVCRT__HUGE;
char **MSVCRT___argv;
50
MSVCRT_wchar_t **MSVCRT___wargv;
51
char *MSVCRT__acmdln;
52
MSVCRT_wchar_t *MSVCRT__wcmdln;
53 54
char **_environ = 0;
MSVCRT_wchar_t **_wenviron = 0;
55
char **MSVCRT___initenv = 0;
56
MSVCRT_wchar_t **MSVCRT___winitenv = 0;
57
int MSVCRT_app_type;
58
char* MSVCRT__pgmptr = 0;
59
WCHAR* MSVCRT__wpgmptr = 0;
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/* Get a snapshot of the current environment
 * and construct the __p__environ array
 *
 * The pointer returned from GetEnvironmentStrings may get invalid when
 * some other module cause a reallocation of the env-variable block
 *
 * blk is an array of pointers to environment strings, ending with a NULL
 * and after that the actual copy of the environment strings, ending in a \0
 */
char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
{
  char* environ_strings = GetEnvironmentStringsA();
  int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
  char *ptr;

  for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
  {
    count++;
    len += strlen(ptr) + 1;
  }
  if (blk)
      blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
  else
    blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );

  if (blk)
    {
      if (count)
	{
	  memcpy(&blk[count],environ_strings,len);
	  for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
	    {
	      blk[i++] = ptr;
	    }
	}
      blk[i] = NULL;
    }
  FreeEnvironmentStringsA(environ_strings);
  return blk;
}

102
MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
103
{
104
  MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
105
  int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
106
  MSVCRT_wchar_t *wptr;
107 108 109 110 111 112 113

  for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
  {
    count++;
    len += strlenW(wptr) + 1;
  }
  if (wblk)
114
      wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
115
  else
116
    wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
117 118 119 120
  if (wblk)
    {
      if (count)
	{
121 122
	  memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
	  for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
123 124 125 126 127 128 129 130 131
	    {
	      wblk[i++] = wptr;
	    }
	}
      wblk[i] = NULL;
    }
  FreeEnvironmentStringsW(wenviron_strings);
  return wblk;
}
132

133
typedef void (*_INITTERMFUN)(void);
134

135 136 137
/***********************************************************************
 *		__p___argc (MSVCRT.@)
 */
138
int* CDECL __p___argc(void) { return &MSVCRT___argc; }
139 140 141 142

/***********************************************************************
 *		__p__commode (MSVCRT.@)
 */
143
unsigned int* CDECL __p__commode(void) { return &MSVCRT__commode; }
144

145 146 147 148

/***********************************************************************
 *              __p__pgmptr (MSVCRT.@)
 */
149
char** CDECL __p__pgmptr(void) { return &MSVCRT__pgmptr; }
150

151 152 153
/***********************************************************************
 *              __p__wpgmptr (MSVCRT.@)
 */
154
WCHAR** CDECL __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }
155

156 157 158
/***********************************************************************
 *		__p__fmode (MSVCRT.@)
 */
159
unsigned int* CDECL __p__fmode(void) { return &MSVCRT__fmode; }
160 161 162 163

/***********************************************************************
 *		__p__osver (MSVCRT.@)
 */
164
unsigned int* CDECL __p__osver(void) { return &MSVCRT__osver; }
165 166 167 168

/***********************************************************************
 *		__p__winmajor (MSVCRT.@)
 */
169
unsigned int* CDECL __p__winmajor(void) { return &MSVCRT__winmajor; }
170 171 172 173

/***********************************************************************
 *		__p__winminor (MSVCRT.@)
 */
174
unsigned int* CDECL __p__winminor(void) { return &MSVCRT__winminor; }
175 176 177 178

/***********************************************************************
 *		__p__winver (MSVCRT.@)
 */
179
unsigned int* CDECL __p__winver(void) { return &MSVCRT__winver; }
180 181 182 183

/*********************************************************************
 *		__p__acmdln (MSVCRT.@)
 */
184
char** CDECL __p__acmdln(void) { return &MSVCRT__acmdln; }
185 186 187 188

/*********************************************************************
 *		__p__wcmdln (MSVCRT.@)
 */
189
MSVCRT_wchar_t** CDECL __p__wcmdln(void) { return &MSVCRT__wcmdln; }
190 191 192 193

/*********************************************************************
 *		__p___argv (MSVCRT.@)
 */
194
char*** CDECL __p___argv(void) { return &MSVCRT___argv; }
195 196 197 198

/*********************************************************************
 *		__p___wargv (MSVCRT.@)
 */
199
MSVCRT_wchar_t*** CDECL __p___wargv(void) { return &MSVCRT___wargv; }
200 201 202 203

/*********************************************************************
 *		__p__environ (MSVCRT.@)
 */
204
char*** CDECL __p__environ(void)
205
{
206 207 208
  if (!_environ)
    _environ = msvcrt_SnapshotOfEnvironmentA(NULL);
  return &_environ;
209
}
210 211 212 213

/*********************************************************************
 *		__p__wenviron (MSVCRT.@)
 */
214
MSVCRT_wchar_t*** CDECL __p__wenviron(void)
215
{
216 217 218
  if (!_wenviron)
    _wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
  return &_wenviron;
219
}
220 221 222 223

/*********************************************************************
 *		__p___initenv (MSVCRT.@)
 */
224
char*** CDECL __p___initenv(void) { return &MSVCRT___initenv; }
225 226 227 228

/*********************************************************************
 *		__p___winitenv (MSVCRT.@)
 */
229
MSVCRT_wchar_t*** CDECL __p___winitenv(void) { return &MSVCRT___winitenv; }
230

231
/* INTERNAL: Create a wide string from an ascii string */
232
static MSVCRT_wchar_t *wstrdupa(const char *str)
233 234
{
  const size_t len = strlen(str) + 1 ;
235
  MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
236 237
  if (!wstr)
    return NULL;
238
   MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
239 240 241 242
  return wstr;
}

/* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
243
 * we initialise data values during DLL loading. When called by a native
244 245 246
 * program we simply return the data we've already initialised. This also means
 * you can call multiple times without leaking
 */
247
void msvcrt_init_args(void)
248 249 250
{
  DWORD version;

251
  MSVCRT__acmdln = _strdup( GetCommandLineA() );
252
  MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
253 254 255
  MSVCRT___argc = __wine_main_argc;
  MSVCRT___argv = __wine_main_argv;
  MSVCRT___wargv = __wine_main_wargv;
256

257
  TRACE("got %s, wide = %s argc=%d\n", debugstr_a(MSVCRT__acmdln),
258
        debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

  version = GetVersion();
  MSVCRT__osver       = version >> 16;
  MSVCRT__winminor    = version & 0xFF;
  MSVCRT__winmajor    = (version>>8) & 0xFF;
  MSVCRT_baseversion = version >> 16;
  MSVCRT__winver     = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
  MSVCRT_baseminor   = (version >> 16) & 0xFF;
  MSVCRT_basemajor   = (version >> 24) & 0xFF;
  MSVCRT_osversion   = version & 0xFFFF;
  MSVCRT_osminor     = version & 0xFF;
  MSVCRT_osmajor     = (version>>8) & 0xFF;
  MSVCRT__HUGE = HUGE_VAL;
  MSVCRT___setlc_active = 0;
  MSVCRT___unguarded_readlc_active = 0;
274
  MSVCRT__fmode = MSVCRT__O_TEXT;
275
  
276 277
  MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
  MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
278

279 280
  MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
  if (MSVCRT__pgmptr)
281 282 283 284 285 286
  {
    if (!GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH))
      MSVCRT__pgmptr[0] = '\0';
    else
      MSVCRT__pgmptr[MAX_PATH - 1] = '\0';
  }
287 288 289

  MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
  if (MSVCRT__wpgmptr)
290 291 292 293 294 295
  {
    if (!GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH))
      MSVCRT__wpgmptr[0] = '\0';
    else
      MSVCRT__wpgmptr[MAX_PATH - 1] = '\0';
  }
296 297 298 299
}


/* INTERNAL: free memory used by args */
300
void msvcrt_free_args(void)
301
{
302
  /* FIXME: more things to free */
303 304 305 306 307 308
  HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
  HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
  HeapFree(GetProcessHeap(), 0, _environ);
  HeapFree(GetProcessHeap(), 0, _wenviron);
  HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
  HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
309 310 311 312 313
}

/*********************************************************************
 *		__getmainargs (MSVCRT.@)
 */
314 315
void CDECL __getmainargs(int *argc, char** *argv, char** *envp,
                         int expand_wildcards, int *new_mode)
316
{
317
  TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
318 319
  *argc = MSVCRT___argc;
  *argv = MSVCRT___argv;
320
  *envp = MSVCRT___initenv;
321 322
  if (new_mode)
    MSVCRT__set_new_mode( *new_mode );
323 324 325 326 327
}

/*********************************************************************
 *		__wgetmainargs (MSVCRT.@)
 */
328 329
void CDECL __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
                          int expand_wildcards, int *new_mode)
330
{
331
  TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
332 333
  *argc = MSVCRT___argc;
  *wargv = MSVCRT___wargv;
334
  *wenvp = MSVCRT___winitenv;
335 336
  if (new_mode)
    MSVCRT__set_new_mode( *new_mode );
337 338 339 340 341
}

/*********************************************************************
 *		_initterm (MSVCRT.@)
 */
342
void CDECL _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
343
{
344
  _INITTERMFUN* current = start;
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

  TRACE("(%p,%p)\n",start,end);
  while (current<end)
  {
    if (*current)
    {
      TRACE("Call init function %p\n",*current);
      (**current)();
      TRACE("returned\n");
    }
    current++;
  }
}

/*********************************************************************
 *		__set_app_type (MSVCRT.@)
 */
362
void CDECL MSVCRT___set_app_type(int app_type)
363 364 365 366
{
  TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
  MSVCRT_app_type = app_type;
}