GitLab repository storage has been migrated to hashed layout. Please contact Infrastructure team if you notice any issues with repositories or hooks.

gwin32.c 15.6 KB
Newer Older
1 2 3 4 5
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 1998-1999  Tor Lillqvist
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 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
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
22
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27 28 29 30 31
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

/* 
 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
 */

32
#include "config.h"
33 34 35 36 37 38

#include "glibconfig.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
39
#include <wchar.h>
40 41 42 43
#include <errno.h>

#define STRICT			/* Strict typing, please */
#include <windows.h>
44 45
#undef STRICT
#ifndef G_WITH_CYGWIN
46
#include <direct.h>
47
#endif
48 49
#include <errno.h>
#include <ctype.h>
50
#if defined(_MSC_VER) || defined(__DMC__)
51
#  include <io.h>
52
#endif /* _MSC_VER || __DMC__ */
53 54

#include "glib.h"
55
#include "galias.h"
56

57 58 59 60 61 62 63
#ifdef G_WITH_CYGWIN
#include <sys/cygwin.h>
#endif

#ifndef G_WITH_CYGWIN

gint
64 65 66
g_win32_ftruncate (gint  fd,
		   guint size)
{
67
  return _chsize (fd, size);
68 69
}

70
#endif
71

72 73 74
/**
 * g_win32_getlocale:
 *
75 76 77 78 79 80 81
 * The setlocale() function in the Microsoft C library uses locale
 * names of the form "English_United States.1252" etc. We want the
 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
 * current thread locale from Windows - without any encoding info -
 * and returns it as a string of the above form for use in forming
 * file names etc. The returned string should be deallocated with
 * g_free().
Havoc Pennington's avatar
docs  
Havoc Pennington committed
82
 *
83
 * Returns: newly-allocated locale name.
84
 **/
85

86 87
#ifndef SUBLANG_SERBIAN_LATIN_BA
#define SUBLANG_SERBIAN_LATIN_BA 0x06
88 89
#endif

90 91 92
gchar *
g_win32_getlocale (void)
{
93
  LCID lcid;
94
  LANGID langid;
95
  gchar *ev;
96
  gint primary, sub;
97 98 99
  char iso639[10];
  char iso3166[10];
  const gchar *script = NULL;
100

101
  /* Let the user override the system settings through environment
102 103 104 105
   * variables, as on POSIX systems. Note that in GTK+ applications
   * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
   * Win32 locale and C library locale through code in gtkmain.c.
   */
106 107 108
  if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
      || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
      || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
109 110 111
    return g_strdup (ev);

  lcid = GetThreadLocale ();
112

113 114 115 116
  if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
      !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
    return g_strdup ("C");
  
117 118 119 120 121 122
  /* Strip off the sorting rules, keep only the language part.  */
  langid = LANGIDFROMLCID (lcid);

  /* Split into language and territory part.  */
  primary = PRIMARYLANGID (langid);
  sub = SUBLANGID (langid);
123 124

  /* Handle special cases */
125 126
  switch (primary)
    {
127
    case LANG_AZERI:
128 129
      switch (sub)
	{
130 131 132 133 134 135
	case SUBLANG_AZERI_LATIN:
	  script = "@Latn";
	  break;
	case SUBLANG_AZERI_CYRILLIC:
	  script = "@Cyrl";
	  break;
136 137
	}
      break;
138
    case LANG_SERBIAN:		/* LANG_CROATIAN == LANG_SERBIAN */
139 140
      switch (sub)
	{
141 142 143 144
	case SUBLANG_SERBIAN_LATIN:
	case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
	  script = "@Latn";
	  break;
145 146 147 148 149
	}
      break;
    case LANG_UZBEK:
      switch (sub)
	{
150 151 152 153 154 155
	case SUBLANG_UZBEK_LATIN:
	  script = "@Latn";
	  break;
	case SUBLANG_UZBEK_CYRILLIC:
	  script = "@Cyrl";
	  break;
156 157 158
	}
      break;
    }
159
  return g_strconcat (iso639, "_", iso3166, script, NULL);
160
}
161

162 163
/**
 * g_win32_error_message:
164
 * @error: error code.
165 166 167 168
 *
 * Translate a Win32 error code (as returned by GetLastError()) into
 * the corresponding message. The message is either language neutral,
 * or in the thread's language, or the user's language, the system's
169 170 171
 * language, or US English (see docs for FormatMessage()). The
 * returned string is in UTF-8. It should be deallocated with
 * g_free().
Havoc Pennington's avatar
docs  
Havoc Pennington committed
172
 *
173
 * Returns: newly-allocated error message
174
 **/
175 176 177 178
gchar *
g_win32_error_message (gint error)
{
  gchar *retval;
179 180 181 182 183 184 185 186 187
  wchar_t *msg = NULL;
  int nchars;

  FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
		  |FORMAT_MESSAGE_IGNORE_INSERTS
		  |FORMAT_MESSAGE_FROM_SYSTEM,
		  NULL, error, 0,
		  (LPWSTR) &msg, 0, NULL);
  if (msg != NULL)
188
    {
189 190 191 192 193 194 195 196
      nchars = wcslen (msg);
      
      if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
	msg[nchars-2] = '\0';
      
      retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
      
      LocalFree (msg);
197 198
    }
  else
199
    retval = g_strdup ("");
200 201 202

  return retval;
}
203

204
static gchar *
205
get_package_directory_from_module (const gchar *module_name)
206
{
207
  static GHashTable *module_dirs = NULL;
208
  G_LOCK_DEFINE_STATIC (module_dirs);
209
  HMODULE hmodule = NULL;
210
  gchar *fn;
211
  gchar *p;
212
  gchar *result;
213
  wchar_t wc_fn[MAX_PATH];
214

215 216
  G_LOCK (module_dirs);

217 218 219 220 221 222
  if (module_dirs == NULL)
    module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
  
  result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
      
  if (result)
223 224 225 226
    {
      G_UNLOCK (module_dirs);
      return g_strdup (result);
    }
227 228 229

  if (module_name)
    {
230 231 232 233
      wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
      hmodule = GetModuleHandleW (wc_module_name);
      g_free (wc_module_name);

234 235 236 237
      if (!hmodule)
	return NULL;
    }

238
  if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
239
    {
240 241
      G_UNLOCK (module_dirs);
      return NULL;
242
    }
243
  fn = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
244

245 246 247 248 249 250 251 252
  if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
    *p = '\0';

  p = strrchr (fn, G_DIR_SEPARATOR);
  if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
	    g_ascii_strcasecmp (p + 1, "lib") == 0))
    *p = '\0';

253 254 255 256 257 258 259 260 261 262 263
#ifdef G_WITH_CYGWIN
  /* In Cygwin we need to have POSIX paths */
  {
    gchar tmp[MAX_PATH];

    cygwin_conv_to_posix_path(fn, tmp);
    g_free(fn);
    fn = g_strdup(tmp);
  }
#endif

264
  g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
265

266 267
  G_UNLOCK (module_dirs);

268 269 270 271 272
  return g_strdup (fn);
}

/**
 * g_win32_get_package_installation_directory:
273
 * @package: An identifier for a software package, or %NULL, in UTF-8
274
 * @dll_name: The name of a DLL that a package provides, or %NULL, in UTF-8
275 276 277 278 279
 *
 * Try to determine the installation directory for a software package.
 * Typically used by GNU software packages.
 *
 * @package should be a short identifier for the package. Typically it
280 281 282
 * is the same identifier as used for
 * <literal>GETTEXT_PACKAGE</literal> in software configured according
 * to GNU standards. The function first looks in the Windows Registry
283 284
 * for the value <literal>&num;InstallationDirectory</literal> in the key
 * <literal>&num;HKLM\Software\@package</literal>, and if that value
285
 * exists and is a string, returns that.
286
 *
287 288
 * If @package is %NULL, or the above value isn't found in the
 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
289 290 291 292 293 294
 * into the current process. Typically that would be the name of the
 * DLL calling this function, looking for its installation
 * directory. The function then asks Windows what directory that DLL
 * was loaded from. If that directory's last component is "bin" or
 * "lib", the parent directory is returned, otherwise the directory
 * itself. If that DLL isn't loaded, the function proceeds as if
295
 * @dll_name was %NULL.
296
 *
Matthias Clasen's avatar
Matthias Clasen committed
297
 * If both @package and @dll_name are %NULL, the directory from where
298
 * the main executable of the process was loaded is used instead in
299 300
 * the same way as above.
 *
301 302 303 304
 * Returns: a string containing the installation directory for
 * @package. The string is in the GLib file name encoding, i.e. UTF-8
 * on Windows. The return value should be freed with g_free() when not
 * needed any longer.
305 306
 **/

307
gchar *
308 309
g_win32_get_package_installation_directory (const gchar *package,
					    const gchar *dll_name)
310 311
{
  static GHashTable *package_dirs = NULL;
312
  G_LOCK_DEFINE_STATIC (package_dirs);
313
  gchar *result = NULL;
314
  gchar *key;
315
  wchar_t *wc_key;
316 317 318 319
  HKEY reg_key = NULL;
  DWORD type;
  DWORD nbytes;

320
  if (package != NULL)
321
    {
322 323
      G_LOCK (package_dirs);
      
324 325 326 327 328 329
      if (package_dirs == NULL)
	package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
      
      result = g_hash_table_lookup (package_dirs, package);
      
      if (result && result[0])
330 331 332 333
	{
	  G_UNLOCK (package_dirs);
	  return g_strdup (result);
	}
334 335 336 337
      
      key = g_strconcat ("Software\\", package, NULL);
      
      nbytes = 0;
338 339 340 341 342 343 344 345 346 347 348 349

      wc_key = g_utf8_to_utf16 (key, -1, NULL, NULL, NULL);
      if (((RegOpenKeyExW (HKEY_CURRENT_USER, wc_key, 0,
			   KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
	    && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
				 &type, NULL, &nbytes) == ERROR_SUCCESS)
	   ||
	   (RegOpenKeyExW (HKEY_LOCAL_MACHINE, wc_key, 0,
			   KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
	    && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
				 &type, NULL, &nbytes) == ERROR_SUCCESS))
	  && type == REG_SZ)
350
	{
351 352 353 354 355 356
	  wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
	  RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
			    &type, (LPBYTE) wc_temp, &nbytes);
	  wc_temp[nbytes/2] = '\0';
	  result = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
	  g_free (wc_temp);
357
	}
358
      g_free (wc_key);
359 360 361 362 363

      if (reg_key != NULL)
	RegCloseKey (reg_key);
      
      g_free (key);
364 365 366

      if (result)
	{
367
	  g_hash_table_insert (package_dirs, g_strdup (package), result);
368
	  G_UNLOCK (package_dirs);
369
	  return g_strdup (result);
370 371
	}
      G_UNLOCK (package_dirs);
372 373
    }

374 375
  if (dll_name != NULL)
    result = get_package_directory_from_module (dll_name);
376

377 378
  if (result == NULL)
    result = get_package_directory_from_module (NULL);
379 380 381

  return result;
}
382

383 384 385 386 387
#undef g_win32_get_package_installation_directory

/* DLL ABI binary compatibility version that uses system codepage file names */

gchar *
388 389
g_win32_get_package_installation_directory (const gchar *package,
					    const gchar *dll_name)
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
{
  gchar *utf8_package = NULL, *utf8_dll_name = NULL;
  gchar *utf8_retval, *retval;

  if (package != NULL)
    utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);

  if (dll_name != NULL)
    utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);

  utf8_retval =
    g_win32_get_package_installation_directory_utf8 (utf8_package,
						     utf8_dll_name);

  retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);

  g_free (utf8_package);
  g_free (utf8_dll_name);
  g_free (utf8_retval);

  return retval;
}

413 414
/**
 * g_win32_get_package_installation_subdirectory:
415 416 417
 * @package: An identifier for a software package, in UTF-8, or %NULL
 * @dll_name: The name of a DLL that a package provides, in UTF-8, or %NULL
 * @subdir: A subdirectory of the package installation directory, also in UTF-8
418
 *
Matthias Clasen's avatar
Matthias Clasen committed
419
 * Returns a newly-allocated string containing the path of the
420
 * subdirectory @subdir in the return value from calling
421
 * g_win32_get_package_installation_directory() with the @package and
Matthias Clasen's avatar
Matthias Clasen committed
422 423
 * @dll_name parameters. 
 *
424
 * Returns: a string containing the complete path to @subdir inside
425 426 427
 * the installation directory of @package. The returned string is in
 * the GLib file name encoding, i.e. UTF-8 on Windows. The return
 * value should be freed with g_free() when no longer needed.
428
 **/
429

430
gchar *
431 432 433
g_win32_get_package_installation_subdirectory (const gchar *package,
					       const gchar *dll_name,
					       const gchar *subdir)
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
{
  gchar *prefix;
  gchar *dirname;

  prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);

  dirname = g_build_filename (prefix, subdir, NULL);
  g_free (prefix);

  return dirname;
}

#undef g_win32_get_package_installation_subdirectory

/* DLL ABI binary compatibility version that uses system codepage file names */

450
gchar *
451 452 453
g_win32_get_package_installation_subdirectory (const gchar *package,
					       const gchar *dll_name,
					       const gchar *subdir)
454 455
{
  gchar *prefix;
456
  gchar *dirname;
457 458 459

  prefix = g_win32_get_package_installation_directory (package, dll_name);

460 461 462 463
  dirname = g_build_filename (prefix, subdir, NULL);
  g_free (prefix);

  return dirname;
464
}
465

466 467 468 469
static guint windows_version;

static void 
g_win32_windows_version_init (void)
470 471 472 473 474
{
  static gboolean beenhere = FALSE;

  if (!beenhere)
    {
475
      beenhere = TRUE;
476 477 478 479
      windows_version = GetVersion ();

      if (windows_version & 0x80000000)
	g_error ("This version of GLib requires NT-based Windows.");
480
    }
481 482 483 484 485 486 487 488
}

void 
_g_win32_thread_init (void)
{
  g_win32_windows_version_init ();
}

489 490 491 492 493 494
/**
 * g_win32_get_windows_version:
 *
 * Returns version information for the Windows operating system the
 * code is running on. See MSDN documentation for the GetVersion()
 * function. To summarize, the most significant bit is one on Win9x,
495 496 497 498 499
 * and zero on NT-based systems. Since version 2.14, GLib works only
 * on NT-based systems, so checking whether your are running on Win9x
 * in your own software is moot. The least significant byte is 4 on
 * Windows NT 4, and 5 on Windows XP. Software that needs really
 * detailled version and feature information should use Win32 API like
500 501 502
 * GetVersionEx() and VerifyVersionInfo().
 *
 * Returns: The version information.
503 504
 * 
 * Since: 2.6
505
 **/
506 507 508 509 510 511
guint
g_win32_get_windows_version (void)
{
  g_win32_windows_version_init ();
  
  return windows_version;
512
}
513

514 515 516 517
/**
 * g_win32_locale_filename_from_utf8:
 * @utf8filename: a UTF-8 encoded filename.
 *
Matthias Clasen's avatar
Matthias Clasen committed
518
 * Converts a filename from UTF-8 to the system codepage.
519 520 521 522 523 524 525 526 527 528 529
 *
 * On NT-based Windows, on NTFS file systems, file names are in
 * Unicode. It is quite possible that Unicode file names contain
 * characters not representable in the system codepage. (For instance,
 * Greek or Cyrillic characters on Western European or US Windows
 * installations, or various less common CJK characters on CJK Windows
 * installations.)
 *
 * In such a case, and if the filename refers to an existing file, and
 * the file system stores alternate short (8.3) names for directory
 * entries, the short form of the filename is returned. Note that the
530 531 532 533
 * "short" name might in fact be longer than the Unicode name if the
 * Unicode name has very short pathname components containing
 * non-ASCII characters. If no system codepage name for the file is
 * possible, %NULL is returned.
534
 *
535 536
 * The return value is dynamically allocated and should be freed with
 * g_free() when no longer needed.
537 538 539 540
 *
 * Return value: The converted filename, or %NULL on conversion
 * failure and lack of short names.
 *
Matthias Clasen's avatar
Matthias Clasen committed
541
 * Since: 2.8
542 543 544 545 546 547
 */
gchar *
g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
{
  gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);

548
  if (retval == NULL)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    {
      /* Conversion failed, so convert to wide chars, check if there
       * is a 8.3 version, and use that.
       */
      wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
      if (wname != NULL)
	{
	  wchar_t wshortname[MAX_PATH + 1];
	  if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
	    {
	      gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
	      retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
	      g_free (tem);
	    }
	  g_free (wname);
	}
    }
  return retval;
}

569 570
#define __G_WIN32_C__
#include "galiasdef.c"