data.c 11.3 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 18
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20 21 22 23

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

24 25 26
#include <math.h>
#include "msvcrt.h"

27 28 29
#include "msvcrt/stdlib.h"
#include "msvcrt/string.h"

30
#include "wine/library.h"
31
#include "wine/unicode.h"
32 33 34
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

unsigned int MSVCRT___argc;
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;
50 51
unsigned int MSVCRT__sys_nerr; /* FIXME: not accessible from Winelib apps */
char**       MSVCRT__sys_errlist; /* FIXME: not accessible from Winelib apps */
52 53 54 55
unsigned int MSVCRT___setlc_active;
unsigned int MSVCRT___unguarded_readlc_active;
double MSVCRT__HUGE;
char **MSVCRT___argv;
56
MSVCRT_wchar_t **MSVCRT___wargv;
57
char *MSVCRT__acmdln;
58
MSVCRT_wchar_t *MSVCRT__wcmdln;
59
char **MSVCRT__environ = 0;
60
MSVCRT_wchar_t **MSVCRT__wenviron = 0;
61
char **MSVCRT___initenv = 0;
62
MSVCRT_wchar_t **MSVCRT___winitenv = 0;
63 64
int MSVCRT_timezone;
int MSVCRT_app_type;
65
char* MSVCRT__pgmptr = 0;
66
WCHAR* MSVCRT__wpgmptr = 0;
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 102 103 104 105 106 107 108
/* 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;
}

109
MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
110
{
111
  MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
112
  int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
113
  MSVCRT_wchar_t *wptr;
114 115 116 117 118 119 120

  for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
  {
    count++;
    len += strlenW(wptr) + 1;
  }
  if (wblk)
121
      wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
122
  else
123
    wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
124 125 126 127
  if (wblk)
    {
      if (count)
	{
128 129
	  memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
	  for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
130 131 132 133 134 135 136 137 138
	    {
	      wblk[i++] = wptr;
	    }
	}
      wblk[i] = NULL;
    }
  FreeEnvironmentStringsW(wenviron_strings);
  return wblk;
}
139

140
typedef void (*_INITTERMFUN)(void);
141

142 143 144
/***********************************************************************
 *		__p___argc (MSVCRT.@)
 */
145
int* __p___argc(void) { return &MSVCRT___argc; }
146 147 148 149

/***********************************************************************
 *		__p__commode (MSVCRT.@)
 */
150
unsigned int* __p__commode(void) { return &MSVCRT__commode; }
151

152 153 154 155 156 157

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

158 159 160 161 162
/***********************************************************************
 *              __p__wpgmptr (MSVCRT.@)
 */
WCHAR** __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }

163 164 165
/***********************************************************************
 *		__p__fmode (MSVCRT.@)
 */
166
unsigned int* __p__fmode(void) { return &MSVCRT__fmode; }
167 168 169 170

/***********************************************************************
 *		__p__osver (MSVCRT.@)
 */
171
unsigned int* __p__osver(void) { return &MSVCRT__osver; }
172 173 174 175

/***********************************************************************
 *		__p__winmajor (MSVCRT.@)
 */
176
unsigned int* __p__winmajor(void) { return &MSVCRT__winmajor; }
177 178 179 180

/***********************************************************************
 *		__p__winminor (MSVCRT.@)
 */
181
unsigned int* __p__winminor(void) { return &MSVCRT__winminor; }
182 183 184 185

/***********************************************************************
 *		__p__winver (MSVCRT.@)
 */
186
unsigned int* __p__winver(void) { return &MSVCRT__winver; }
187 188 189 190

/*********************************************************************
 *		__p__acmdln (MSVCRT.@)
 */
191
char** __p__acmdln(void) { return &MSVCRT__acmdln; }
192 193 194 195

/*********************************************************************
 *		__p__wcmdln (MSVCRT.@)
 */
196
MSVCRT_wchar_t** __p__wcmdln(void) { return &MSVCRT__wcmdln; }
197 198 199 200

/*********************************************************************
 *		__p___argv (MSVCRT.@)
 */
201
char*** __p___argv(void) { return &MSVCRT___argv; }
202 203 204 205

/*********************************************************************
 *		__p___wargv (MSVCRT.@)
 */
206
MSVCRT_wchar_t*** __p___wargv(void) { return &MSVCRT___wargv; }
207 208 209 210

/*********************************************************************
 *		__p__environ (MSVCRT.@)
 */
211 212 213 214 215 216
char*** __p__environ(void)
{
  if (!MSVCRT__environ)
    MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(NULL);
  return &MSVCRT__environ;
}
217 218 219 220

/*********************************************************************
 *		__p__wenviron (MSVCRT.@)
 */
221
MSVCRT_wchar_t*** __p__wenviron(void)
222 223 224 225 226
{
  if (!MSVCRT__wenviron)
    MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
  return &MSVCRT__wenviron;
}
227 228 229 230

/*********************************************************************
 *		__p___initenv (MSVCRT.@)
 */
231
char*** __p___initenv(void) { return &MSVCRT___initenv; }
232 233 234 235

/*********************************************************************
 *		__p___winitenv (MSVCRT.@)
 */
236
MSVCRT_wchar_t*** __p___winitenv(void) { return &MSVCRT___winitenv; }
237 238 239 240

/*********************************************************************
 *		__p__timezone (MSVCRT.@)
 */
241
int* __p__timezone(void) { return &MSVCRT_timezone; }
242 243

/* INTERNAL: Create a wide string from an ascii string */
244
static MSVCRT_wchar_t *wstrdupa(const char *str)
245 246
{
  const size_t len = strlen(str) + 1 ;
247
  MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
248 249
  if (!wstr)
    return NULL;
250
   MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
251 252 253 254
  return wstr;
}

/* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
255
 * we initialise data values during DLL loading. When called by a native
256 257 258
 * program we simply return the data we've already initialised. This also means
 * you can call multiple times without leaking
 */
259
void msvcrt_init_args(void)
260 261 262
{
  DWORD version;

263
  MSVCRT__acmdln = _strdup( GetCommandLineA() );
264
  MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
265 266 267
  MSVCRT___argc = __wine_main_argc;
  MSVCRT___argv = __wine_main_argv;
  MSVCRT___wargv = __wine_main_wargv;
268

269 270
  TRACE("got '%s', wide = %s argc=%d\n", MSVCRT__acmdln,
        debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  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__sys_nerr   = 43;
  MSVCRT__HUGE = HUGE_VAL;
  MSVCRT___setlc_active = 0;
  MSVCRT___unguarded_readlc_active = 0;
  MSVCRT_timezone = 0;

289 290
  MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
  MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
291

292 293 294 295 296 297 298
  MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
  if (MSVCRT__pgmptr)
    GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH);

  MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
  if (MSVCRT__wpgmptr)
    GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH);
299 300 301 302
}


/* INTERNAL: free memory used by args */
303
void msvcrt_free_args(void)
304
{
305
  /* FIXME: more things to free */
306 307 308 309 310 311
  if (MSVCRT___initenv) HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
  if (MSVCRT___winitenv) HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
  if (MSVCRT__environ) HeapFree(GetProcessHeap(), 0, MSVCRT__environ);
  if (MSVCRT__wenviron) HeapFree(GetProcessHeap(), 0, MSVCRT__wenviron);
  if (MSVCRT__pgmptr) HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
  if (MSVCRT__wpgmptr) HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
312 313 314 315 316
}

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

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

/*********************************************************************
 *		_initterm (MSVCRT.@)
 */
345
unsigned int _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
346
{
347
  _INITTERMFUN* current = start;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

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

/*********************************************************************
 *		__set_app_type (MSVCRT.@)
 */
366
void MSVCRT___set_app_type(int app_type)
367 368 369 370
{
  TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
  MSVCRT_app_type = app_type;
}