gutils.c 80.7 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1
/* GLIB - Library of useful routines for C programming
2
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
Owen Taylor's avatar
Owen Taylor committed
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
Owen Taylor's avatar
Owen Taylor committed
8 9 10 11
 *
 * 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
12
 * Lesser General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Owen Taylor's avatar
Owen Taylor committed
16
 */
17

18
/*
19
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * 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/. 
 */

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

29
#include "config.h"
30 31

#include "gutils.h"
32
#include "gutilsprivate.h"
33

Owen Taylor's avatar
Owen Taylor committed
34 35
#include <stdarg.h>
#include <stdlib.h>
36
#include <stdio.h>
37
#include <locale.h>
Owen Taylor's avatar
Owen Taylor committed
38
#include <string.h>
39
#include <ctype.h>		/* For tolower() */
Manish Singh's avatar
Manish Singh committed
40
#include <errno.h>
41 42
#include <sys/types.h>
#include <sys/stat.h>
43
#ifdef G_OS_UNIX
44
#include <pwd.h>
45
#include <unistd.h>
46
#endif
47
#include <sys/types.h>
48
#ifdef HAVE_SYS_PARAM_H
49
#include <sys/param.h>
50
#endif
51
#ifdef HAVE_CRT_EXTERNS_H 
52
#include <crt_externs.h> /* for _NSGetEnviron */
53
#endif
54 55 56
#ifdef HAVE_SYS_AUXV_H
#include <sys/auxv.h>
#endif
57

58
#include "glib-init.h"
59
#include "glib-private.h"
60
#include "genviron.h"
61
#include "gfileutils.h"
62
#include "ggettext.h"
63 64 65 66 67
#include "ghash.h"
#include "gthread.h"
#include "gtestutils.h"
#include "gunicode.h"
#include "gstrfuncs.h"
68
#include "garray.h"
69
#include "glibintl.h"
70
#include "gstdio.h"
Owen Taylor's avatar
Owen Taylor committed
71

72 73 74 75 76
#ifdef G_PLATFORM_WIN32
#include "gconvert.h"
#include "gwin32.h"
#endif

77 78 79 80 81 82 83 84 85

/**
 * SECTION:misc_utils
 * @title: Miscellaneous Utility Functions
 * @short_description: a selection of portable utility functions
 *
 * These are portable utility functions.
 */

86
#ifdef G_PLATFORM_WIN32
87
#  include <windows.h>
88 89 90 91
#  ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
#    define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
#    define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
#  endif
92
#  include <lmcons.h>		/* For UNLEN */
93 94 95
#endif /* G_PLATFORM_WIN32 */

#ifdef G_OS_WIN32
96
#  include <direct.h>
97
#  include <shlobj.h>
98
   /* older SDK (e.g. msvc 5.0) does not have these*/
99 100 101 102 103 104
#  ifndef CSIDL_MYMUSIC
#    define CSIDL_MYMUSIC 13
#  endif
#  ifndef CSIDL_MYVIDEO
#    define CSIDL_MYVIDEO 14
#  endif
105 106 107 108 109 110
#  ifndef CSIDL_INTERNET_CACHE
#    define CSIDL_INTERNET_CACHE 32
#  endif
#  ifndef CSIDL_COMMON_APPDATA
#    define CSIDL_COMMON_APPDATA 35
#  endif
111 112 113
#  ifndef CSIDL_MYPICTURES
#    define CSIDL_MYPICTURES 0x27
#  endif
114 115 116 117 118 119
#  ifndef CSIDL_COMMON_DOCUMENTS
#    define CSIDL_COMMON_DOCUMENTS 46
#  endif
#  ifndef CSIDL_PROFILE
#    define CSIDL_PROFILE 40
#  endif
120
#  include <process.h>
121
#endif
122

123 124 125 126
#ifdef HAVE_CARBON
#include <CoreServices/CoreServices.h>
#endif

127 128 129 130
#ifdef HAVE_CODESET
#include <langinfo.h>
#endif

131 132
#ifdef G_PLATFORM_WIN32

Tor Lillqvist's avatar
Tor Lillqvist committed
133
gchar *
134
_glib_get_dll_directory (void)
Tor Lillqvist's avatar
Tor Lillqvist committed
135
{
136 137 138 139
  gchar *retval;
  gchar *p;
  wchar_t wc_fn[MAX_PATH];

140
#ifdef DLL_EXPORT
Tor Lillqvist's avatar
Tor Lillqvist committed
141 142
  if (glib_dll == NULL)
    return NULL;
143
#endif
144 145 146 147 148 149 150 151 152

  /* This code is different from that in
   * g_win32_get_package_installation_directory_of_module() in that
   * here we return the actual folder where the GLib DLL is. We don't
   * do the check for it being in a "bin" or "lib" subfolder and then
   * returning the parent of that.
   *
   * In a statically built GLib, glib_dll will be NULL and we will
   * thus look up the application's .exe file's location.
153
   */
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  if (!GetModuleFileNameW (glib_dll, wc_fn, MAX_PATH))
    return NULL;

  retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);

  p = strrchr (retval, G_DIR_SEPARATOR);
  if (p == NULL)
    {
      /* Wtf? */
      return NULL;
    }
  *p = '\0';

  return retval;
Tor Lillqvist's avatar
Tor Lillqvist committed
168 169
}

170 171
#endif

172 173 174 175 176 177 178 179 180
/**
 * g_memmove: 
 * @dest: the destination address to copy the bytes to.
 * @src: the source address to copy the bytes from.
 * @len: the number of bytes to copy.
 *
 * Copies a block of memory @len bytes long, from @src to @dest.
 * The source and destination areas may overlap.
 *
Dan Winship's avatar
Dan Winship committed
181
 * Deprecated:2.40: Just use memmove().
182
 */
183

184 185 186 187
#ifdef G_OS_WIN32
#undef g_atexit
#endif

188 189
/**
 * g_atexit:
190
 * @func: (scope async): the function to call on normal program termination.
191 192
 * 
 * Specifies a function to be called at normal program termination.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
 *
 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
 * macro that maps to a call to the atexit() function in the C
 * library. This means that in case the code that calls g_atexit(),
 * i.e. atexit(), is in a DLL, the function will be called when the
 * DLL is detached from the program. This typically makes more sense
 * than that the function is called when the GLib DLL is detached,
 * which happened earlier when g_atexit() was a function in the GLib
 * DLL.
 *
 * The behaviour of atexit() in the context of dynamically loaded
 * modules is not formally specified and varies wildly.
 *
 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
 * loaded module which is unloaded before the program terminates might
 * well cause a crash at program exit.
 *
 * Some POSIX systems implement atexit() like Windows, and have each
 * dynamically loaded module maintain an own atexit chain that is
 * called when the module is unloaded.
 *
 * On other POSIX systems, before a dynamically loaded module is
 * unloaded, the registered atexit functions (if any) residing in that
 * module are called, regardless where the code that registered them
 * resided. This is presumably the most robust approach.
 *
 * As can be seen from the above, for portability it's best to avoid
 * calling g_atexit() (or atexit()) except in the main executable of a
 * program.
Matthias Clasen's avatar
Matthias Clasen committed
222
 *
223
 * Deprecated:2.32: It is best to avoid g_atexit().
224
 */
225 226 227 228
void
g_atexit (GVoidFunc func)
{
  gint result;
229
  int errsv;
230 231

  result = atexit ((void (*)(void)) func);
232
  errsv = errno;
233
  if (result)
Dan Winship's avatar
Dan Winship committed
234 235
    {
      g_error ("Could not register atexit() function: %s",
236
               g_strerror (errsv));
Dan Winship's avatar
Dan Winship committed
237
    }
238 239
}

Havoc Pennington's avatar
Havoc Pennington committed
240 241 242 243 244
/* Based on execvp() from GNU Libc.
 * Some of this code is cut-and-pasted into gspawn.c
 */

static gchar*
245 246
my_strchrnul (const gchar *str, 
	      gchar        c)
Havoc Pennington's avatar
Havoc Pennington committed
247 248 249 250 251 252 253 254
{
  gchar *p = (gchar*)str;
  while (*p && (*p != c))
    ++p;

  return p;
}

255 256
#ifdef G_OS_WIN32

257
static gchar *inner_find_program_in_path (const gchar *program);
258 259 260 261 262 263

gchar*
g_find_program_in_path (const gchar *program)
{
  const gchar *last_dot = strrchr (program, '.');

264 265 266
  if (last_dot == NULL ||
      strchr (last_dot, '\\') != NULL ||
      strchr (last_dot, '/') != NULL)
267 268
    {
      const gint program_length = strlen (program);
269 270 271 272 273
      gchar *pathext = g_build_path (";",
				     ".exe;.cmd;.bat;.com",
				     g_getenv ("PATHEXT"),
				     NULL);
      gchar *p;
274 275 276 277 278 279
      gchar *decorated_program;
      gchar *retval;

      p = pathext;
      do
	{
280
	  gchar *q = my_strchrnul (p, ';');
281

282
	  decorated_program = g_malloc (program_length + (q-p) + 1);
283
	  memcpy (decorated_program, program, program_length);
284 285
	  memcpy (decorated_program+program_length, p, q-p);
	  decorated_program [program_length + (q-p)] = '\0';
286 287 288 289 290
	  
	  retval = inner_find_program_in_path (decorated_program);
	  g_free (decorated_program);

	  if (retval != NULL)
291 292 293 294 295
	    {
	      g_free (pathext);
	      return retval;
	    }
	  p = q;
296
	} while (*p++ != '\0');
297
      g_free (pathext);
298 299 300 301 302 303 304 305
      return NULL;
    }
  else
    return inner_find_program_in_path (program);
}

#endif

Havoc Pennington's avatar
Havoc Pennington committed
306 307
/**
 * g_find_program_in_path:
308
 * @program: (type filename): a program name in the GLib file name encoding
Havoc Pennington's avatar
Havoc Pennington committed
309
 * 
Havoc Pennington's avatar
Havoc Pennington committed
310
 * Locates the first executable named @program in the user's path, in the
Havoc Pennington's avatar
Havoc Pennington committed
311
 * same way that execvp() would locate it. Returns an allocated string
312
 * with the absolute path name, or %NULL if the program is not found in
Havoc Pennington's avatar
Havoc Pennington committed
313
 * the path. If @program is already an absolute path, returns a copy of
314 315
 * @program if @program exists and is executable, and %NULL otherwise.
 *  
316 317
 * On Windows, if @program does not have a file type suffix, tries
 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
318
 * the `PATHEXT` environment variable. 
319 320
 * 
 * On Windows, it looks for the file in the same way as CreateProcess() 
321
 * would. This means first in the directory where the executing
322 323
 * program was loaded from, then in the current directory, then in the
 * Windows 32-bit system directory, then in the Windows directory, and
324 325 326
 * finally in the directories in the `PATH` environment variable. If
 * the program is found, the return value contains the full name
 * including the type suffix.
327
 *
328 329
 * Returns: (type filename): a newly-allocated string with the absolute path,
 *     or %NULL
Havoc Pennington's avatar
Havoc Pennington committed
330
 **/
331
#ifdef G_OS_WIN32
332 333 334
static gchar *
inner_find_program_in_path (const gchar *program)
#else
Havoc Pennington's avatar
Havoc Pennington committed
335
gchar*
Havoc Pennington's avatar
Havoc Pennington committed
336
g_find_program_in_path (const gchar *program)
337
#endif
Havoc Pennington's avatar
Havoc Pennington committed
338
{
Havoc Pennington's avatar
Havoc Pennington committed
339 340
  const gchar *path, *p;
  gchar *name, *freeme;
341
#ifdef G_OS_WIN32
342 343 344
  const gchar *path_copy;
  gchar *filename = NULL, *appdir = NULL;
  gchar *sysdir = NULL, *windir = NULL;
345 346 347
  int n;
  wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
    wwindir[MAXPATHLEN];
348
#endif
349 350
  gsize len;
  gsize pathlen;
Havoc Pennington's avatar
Havoc Pennington committed
351

Havoc Pennington's avatar
Havoc Pennington committed
352
  g_return_val_if_fail (program != NULL, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
353

354 355 356 357
  /* If it is an absolute path, or a relative path including subdirectories,
   * don't look in PATH.
   */
  if (g_path_is_absolute (program)
358 359 360 361 362
      || strchr (program, G_DIR_SEPARATOR) != NULL
#ifdef G_OS_WIN32
      || strchr (program, '/') != NULL
#endif
      )
Havoc Pennington's avatar
Havoc Pennington committed
363
    {
364 365
      if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
	  !g_file_test (program, G_FILE_TEST_IS_DIR))
Havoc Pennington's avatar
Havoc Pennington committed
366
        return g_strdup (program);
Havoc Pennington's avatar
Havoc Pennington committed
367 368 369 370 371
      else
        return NULL;
    }
  
  path = g_getenv ("PATH");
372
#if defined(G_OS_UNIX)
Havoc Pennington's avatar
Havoc Pennington committed
373 374
  if (path == NULL)
    {
375
      /* There is no 'PATH' in the environment.  The default
Havoc Pennington's avatar
Havoc Pennington committed
376
       * search path in GNU libc is the current directory followed by
377
       * the path 'confstr' returns for '_CS_PATH'.
Havoc Pennington's avatar
Havoc Pennington committed
378 379 380 381 382 383 384 385 386
       */
      
      /* In GLib we put . last, for security, and don't use the
       * unportable confstr(); UNIX98 does not actually specify
       * what to search if PATH is unset. POSIX may, dunno.
       */
      
      path = "/bin:/usr/bin:.";
    }
387
#else
388 389 390 391 392 393 394 395 396 397 398
  n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
  if (n > 0 && n < MAXPATHLEN)
    filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
  
  n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
  if (n > 0 && n < MAXPATHLEN)
    sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
  
  n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
  if (n > 0 && n < MAXPATHLEN)
    windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  
  if (filename)
    {
      appdir = g_path_get_dirname (filename);
      g_free (filename);
    }
  
  path = g_strdup (path);

  if (windir)
    {
      const gchar *tem = path;
      path = g_strconcat (windir, ";", path, NULL);
      g_free ((gchar *) tem);
      g_free (windir);
    }
  
  if (sysdir)
    {
      const gchar *tem = path;
      path = g_strconcat (sysdir, ";", path, NULL);
      g_free ((gchar *) tem);
      g_free (sysdir);
    }
  
424
  {
425 426 427
    const gchar *tem = path;
    path = g_strconcat (".;", path, NULL);
    g_free ((gchar *) tem);
428
  }
429 430 431 432 433 434 435 436 437 438
  
  if (appdir)
    {
      const gchar *tem = path;
      path = g_strconcat (appdir, ";", path, NULL);
      g_free ((gchar *) tem);
      g_free (appdir);
    }

  path_copy = path;
439
#endif
Havoc Pennington's avatar
Havoc Pennington committed
440
  
Havoc Pennington's avatar
Havoc Pennington committed
441
  len = strlen (program) + 1;
Havoc Pennington's avatar
Havoc Pennington committed
442 443 444 445
  pathlen = strlen (path);
  freeme = name = g_malloc (pathlen + len + 1);
  
  /* Copy the file name at the top, including '\0'  */
Havoc Pennington's avatar
Havoc Pennington committed
446
  memcpy (name + pathlen + 1, program, len);
Havoc Pennington's avatar
Havoc Pennington committed
447 448
  name = name + pathlen;
  /* And add the slash before the filename  */
449
  *name = G_DIR_SEPARATOR;
Havoc Pennington's avatar
Havoc Pennington committed
450 451 452 453 454 455 456
  
  p = path;
  do
    {
      char *startp;

      path = p;
457
      p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
Havoc Pennington's avatar
Havoc Pennington committed
458 459 460

      if (p == path)
        /* Two adjacent colons, or a colon at the beginning or the end
461
         * of 'PATH' means to search the current directory.
Havoc Pennington's avatar
Havoc Pennington committed
462 463 464 465 466
         */
        startp = name + 1;
      else
        startp = memcpy (name - (p - path), path, p - path);

467 468
      if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
	  !g_file_test (startp, G_FILE_TEST_IS_DIR))
Havoc Pennington's avatar
Havoc Pennington committed
469 470 471 472
        {
          gchar *ret;
          ret = g_strdup (startp);
          g_free (freeme);
473
#ifdef G_OS_WIN32
474
	  g_free ((gchar *) path_copy);
475
#endif
Havoc Pennington's avatar
Havoc Pennington committed
476 477 478 479 480 481
          return ret;
        }
    }
  while (*p++ != '\0');
  
  g_free (freeme);
482
#ifdef G_OS_WIN32
483
  g_free ((gchar *) path_copy);
484
#endif
Havoc Pennington's avatar
Havoc Pennington committed
485 486 487 488

  return NULL;
}

489 490 491 492
/* The functions below are defined this way for compatibility reasons.
 * See the note in gutils.h.
 */

493 494 495 496 497 498 499 500 501 502
/**
 * g_bit_nth_lsf:
 * @mask: a #gulong containing flags
 * @nth_bit: the index of the bit to start the search from
 *
 * Find the position of the first bit set in @mask, searching
 * from (but not including) @nth_bit upwards. Bits are numbered
 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
 * usually). To start searching from the 0th bit, set @nth_bit to -1.
 *
503 504
 * Returns: the index of the first bit set which is higher than @nth_bit, or -1
 *    if no higher bits are set
505
 */
506 507 508 509 510 511
gint
(g_bit_nth_lsf) (gulong mask,
                 gint   nth_bit)
{
  return g_bit_nth_lsf_impl (mask, nth_bit);
}
512 513 514 515 516 517 518 519 520 521 522 523

/**
 * g_bit_nth_msf:
 * @mask: a #gulong containing flags
 * @nth_bit: the index of the bit to start the search from
 *
 * Find the position of the first bit set in @mask, searching
 * from (but not including) @nth_bit downwards. Bits are numbered
 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
 * usually). To start searching from the last bit, set @nth_bit to
 * -1 or GLIB_SIZEOF_LONG * 8.
 *
524 525
 * Returns: the index of the first bit set which is lower than @nth_bit, or -1
 *    if no lower bits are set
526
 */
527 528 529 530 531 532 533
gint
(g_bit_nth_msf) (gulong mask,
                 gint   nth_bit)
{
  return g_bit_nth_msf_impl (mask, nth_bit);
}

534 535 536 537 538 539 540 541 542 543

/**
 * g_bit_storage:
 * @number: a #guint
 *
 * Gets the number of bits used to hold @number,
 * e.g. if @number is 4, 3 bits are needed.
 *
 * Returns: the number of bits used to hold @number
 */
544 545 546 547 548
guint
(g_bit_storage) (gulong number)
{
  return g_bit_storage_impl (number);
}
549

550
G_LOCK_DEFINE_STATIC (g_utils_global);
551

552 553 554 555 556 557
typedef struct
{
  gchar *user_name;
  gchar *real_name;
  gchar *home_dir;
} UserDatabaseEntry;
558

559
/* These must all be read/written with @g_utils_global held. */
560 561 562 563
static  gchar   *g_user_data_dir = NULL;
static  gchar  **g_system_data_dirs = NULL;
static  gchar   *g_user_cache_dir = NULL;
static  gchar   *g_user_config_dir = NULL;
564
static  gchar   *g_user_runtime_dir = NULL;
565
static  gchar  **g_system_config_dirs = NULL;
566 567 568 569 570
static  gchar  **g_user_special_dirs = NULL;

/* fifteen minutes of fame for everybody */
#define G_USER_DIRS_EXPIRE      15 * 60

571 572 573 574 575
#ifdef G_OS_WIN32

static gchar *
get_special_folder (int csidl)
{
576
  wchar_t path[MAX_PATH+1];
577 578 579 580 581 582 583 584
  HRESULT hr;
  LPITEMIDLIST pidl = NULL;
  BOOL b;
  gchar *retval = NULL;

  hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
  if (hr == S_OK)
    {
585
      b = SHGetPathFromIDListW (pidl, path);
586
      if (b)
587
	retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
588 589
      CoTaskMemFree (pidl);
    }
590
  return retval;
591 592
}

593 594 595
static char *
get_windows_directory_root (void)
{
596
  wchar_t wwindowsdir[MAX_PATH];
597

598
  if (GetWindowsDirectoryW (wwindowsdir, G_N_ELEMENTS (wwindowsdir)))
599 600 601 602
    {
      /* Usually X:\Windows, but in terminal server environments
       * might be an UNC path, AFAIK.
       */
603 604 605 606 607 608 609
      char *windowsdir = g_utf16_to_utf8 (wwindowsdir, -1, NULL, NULL, NULL);
      char *p;

      if (windowsdir == NULL)
	return g_strdup ("C:\\");

      p = (char *) g_path_skip_root (windowsdir);
610 611 612
      if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
	p--;
      *p = '\0';
613
      return windowsdir;
614 615 616 617 618
    }
  else
    return g_strdup ("C:\\");
}

619 620
#endif

621
/* HOLDS: g_utils_global_lock */
622 623
static UserDatabaseEntry *
g_get_user_database_entry (void)
624
{
625
  static UserDatabaseEntry *entry;
626

627
  if (g_once_init_enter (&entry))
628
    {
629
      static UserDatabaseEntry e;
630

631
#ifdef G_OS_UNIX
632 633 634 635 636
      {
        struct passwd *pw = NULL;
        gpointer buffer = NULL;
        gint error;
        gchar *logname;
637

638
#  if defined (HAVE_GETPWUID_R)
639 640 641 642 643 644 645
        struct passwd pwd;
#    ifdef _SC_GETPW_R_SIZE_MAX
        /* This reurns the maximum length */
        glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);

        if (bufsize < 0)
          bufsize = 64;
646
#    else /* _SC_GETPW_R_SIZE_MAX */
647
        glong bufsize = 64;
648
#    endif /* _SC_GETPW_R_SIZE_MAX */
649

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
        logname = (gchar *) g_getenv ("LOGNAME");

        do
          {
            g_free (buffer);
            /* we allocate 6 extra bytes to work around a bug in
             * Mac OS < 10.3. See #156446
             */
            buffer = g_malloc (bufsize + 6);
            errno = 0;

            if (logname) {
              error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
              if (!pw || (pw->pw_uid != getuid ())) {
                /* LOGNAME is lying, fall back to looking up the uid */
                error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
              }
            } else {
              error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
            }
            error = error < 0 ? errno : error;

            if (!pw)
              {
                /* we bail out prematurely if the user id can't be found
                 * (should be pretty rare case actually), or if the buffer
                 * should be sufficiently big and lookups are still not
                 * successful.
                 */
                if (error == 0 || error == ENOENT)
                  {
                    g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
                               (gulong) getuid ());
                    break;
                  }
                if (bufsize > 32 * 1024)
                  {
                    g_warning ("getpwuid_r(): failed due to: %s.",
                               g_strerror (error));
                    break;
                  }

                bufsize *= 2;
              }
          }
        while (!pw);
696
#  endif /* HAVE_GETPWUID_R */
697 698 699 700 701 702 703 704 705

        if (!pw)
          {
            pw = getpwuid (getuid ());
          }
        if (pw)
          {
            e.user_name = g_strdup (pw->pw_name);

706
#ifndef __BIONIC__
707 708 709 710 711 712 713 714 715 716 717 718 719
            if (pw->pw_gecos && *pw->pw_gecos != '\0')
              {
                gchar **gecos_fields;
                gchar **name_parts;

                /* split the gecos field and substitute '&' */
                gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
                name_parts = g_strsplit (gecos_fields[0], "&", 0);
                pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
                e.real_name = g_strjoinv (pw->pw_name, name_parts);
                g_strfreev (gecos_fields);
                g_strfreev (name_parts);
              }
720
#endif
721 722 723 724 725

            if (!e.home_dir)
              e.home_dir = g_strdup (pw->pw_dir);
          }
        g_free (buffer);
726
      }
727

728
#endif /* G_OS_UNIX */
729

730
#ifdef G_OS_WIN32
731
      {
732 733 734 735 736 737 738 739
        guint len = UNLEN+1;
        wchar_t buffer[UNLEN+1];

        if (GetUserNameW (buffer, (LPDWORD) &len))
          {
            e.user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
            e.real_name = g_strdup (e.user_name);
          }
740
      }
741
#endif /* G_OS_WIN32 */
742

743 744 745 746
      if (!e.user_name)
        e.user_name = g_strdup ("somebody");
      if (!e.real_name)
        e.real_name = g_strdup ("Unknown");
747

748 749
      g_once_init_leave (&entry, &e);
    }
750

751
  return entry;
752 753
}

754 755 756 757 758 759 760 761
/**
 * g_get_user_name:
 *
 * Gets the user name of the current user. The encoding of the returned
 * string is system-defined. On UNIX, it might be the preferred file name
 * encoding, or something else, and there is no guarantee that it is even
 * consistent on a machine. On Windows, it is always UTF-8.
 *
762
 * Returns: (type filename): the user name of the current user.
763
 */
764
const gchar *
765 766
g_get_user_name (void)
{
767 768 769 770 771
  UserDatabaseEntry *entry;

  entry = g_get_user_database_entry ();

  return entry->user_name;
772 773
}

774 775 776
/**
 * g_get_real_name:
 *
777 778 779 780
 * Gets the real name of the user. This usually comes from the user's
 * entry in the `passwd` file. The encoding of the returned string is
 * system-defined. (On Windows, it is, however, always UTF-8.) If the
 * real user name cannot be determined, the string "Unknown" is 
781 782
 * returned.
 *
783
 * Returns: (type filename): the user's real name.
784
 */
785
const gchar *
786 787
g_get_real_name (void)
{
788 789 790 791 792
  UserDatabaseEntry *entry;

  entry = g_get_user_database_entry ();

  return entry->real_name;
793 794
}

795 796 797
/* Protected by @g_utils_global_lock. */
static gchar *g_home_dir = NULL;  /* (owned) (nullable before initialised) */

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
static gchar *
g_build_home_dir (void)
{
  gchar *home_dir;

  /* We first check HOME and use it if it is set */
  home_dir = g_strdup (g_getenv ("HOME"));

#ifdef G_OS_WIN32
  /* Only believe HOME if it is an absolute path and exists.
   *
   * We only do this check on Windows for a couple of reasons.
   * Historically, we only did it there because we used to ignore $HOME
   * on UNIX.  There are concerns about enabling it now on UNIX because
   * of things like autofs.  In short, if the user has a bogus value in
   * $HOME then they get what they pay for...
   */
  if (home_dir != NULL)
    {
      if (!(g_path_is_absolute (home_dir) &&
            g_file_test (home_dir, G_FILE_TEST_IS_DIR)))
        g_clear_pointer (&home_dir, g_free);
    }

  /* In case HOME is Unix-style (it happens), convert it to
   * Windows style.
   */
  if (home_dir != NULL)
    {
      gchar *p;
      while ((p = strchr (home_dir, '/')) != NULL)
        *p = '\\';
    }

  if (home_dir == NULL)
    {
      /* USERPROFILE is probably the closest equivalent to $HOME? */
      if (g_getenv ("USERPROFILE") != NULL)
        home_dir = g_strdup (g_getenv ("USERPROFILE"));
    }

  if (home_dir == NULL)
    home_dir = get_special_folder (CSIDL_PROFILE);

  if (home_dir == NULL)
    home_dir = get_windows_directory_root ();
#endif /* G_OS_WIN32 */

  if (home_dir == NULL)
    {
      /* If we didn't get it from any of those methods, we will have
       * to read the user database entry.
       */
      UserDatabaseEntry *entry = g_get_user_database_entry ();
      home_dir = g_strdup (entry->home_dir);
    }

  /* If we have been denied access to /etc/passwd (for example, by an
   * overly-zealous LSM), make up a junk value. The return value at this
   * point is explicitly documented as ‘undefined’. */
  if (home_dir == NULL)
    {
      g_warning ("Could not find home directory: $HOME is not set, and "
                 "user database could not be read.");
      home_dir = g_strdup ("/");
    }

  return g_steal_pointer (&home_dir);
}

868 869 870
/**
 * g_get_home_dir:
 *
871 872 873
 * Gets the current user's home directory.
 *
 * As with most UNIX tools, this function will return the value of the
874
 * `HOME` environment variable if it is set to an existing absolute path
875
 * name, falling back to the `passwd` file in the case that it is unset.
876
 *
877 878
 * If the path given in `HOME` is non-absolute, does not exist, or is
 * not a directory, the result is undefined.
879
 *
880
 * Before version 2.36 this function would ignore the `HOME` environment
881 882 883 884
 * variable, taking the value from the `passwd` database instead. This was
 * changed to increase the compatibility of GLib with other programs (and
 * the XDG basedir specification) and to increase testability of programs
 * based on GLib (by making it easier to run them from test frameworks).
885 886 887 888
 *
 * If your program has a strong requirement for either the new or the
 * old behaviour (and if you don't wish to increase your GLib
 * dependency to ensure that the new behaviour is in effect) then you
889 890
 * should either directly check the `HOME` environment variable yourself
 * or unset it before calling any functions in GLib.
Matthias Clasen's avatar
Matthias Clasen committed
891
 *
892
 * Returns: (type filename): the current user's home directory
893
 */
894
const gchar *
895 896
g_get_home_dir (void)
{
897 898
  const gchar *home_dir;

899
  G_LOCK (g_utils_global);
900

901
  if (g_home_dir == NULL)
902
    g_home_dir = g_build_home_dir ();
903 904
  home_dir = g_home_dir;

905 906
  G_UNLOCK (g_utils_global);

907
  return home_dir;
908 909
}

910 911 912
/**
 * g_get_tmp_dir:
 *
913 914
 * Gets the directory to use for temporary files.
 *
915
 * On UNIX, this is taken from the `TMPDIR` environment variable.
916
 * If the variable is not set, `P_tmpdir` is
917 918
 * used, as defined by the system C library. Failing that, a
 * hard-coded default of "/tmp" is returned.
919
 *
920 921
 * On Windows, the `TEMP` environment variable is used, with the
 * root directory of the Windows installation (eg: "C:\") used
922 923
 * as a default.
 *
924 925 926
 * The encoding of the returned string is system-defined. On Windows,
 * it is always UTF-8. The return value is never %NULL or the empty
 * string.
927
 *
928
 * Returns: (type filename): the directory to use for temporary files.
929
 */
930
const gchar *
931 932
g_get_tmp_dir (void)
{
933 934 935 936 937 938 939
  static gchar *tmp_dir;

  if (g_once_init_enter (&tmp_dir))
    {
      gchar *tmp;

#ifdef G_OS_WIN32
940 941
      tmp = g_strdup (g_getenv ("TEMP"));

942 943 944 945 946
      if (tmp == NULL || *tmp == '\0')
        {
          g_free (tmp);
          tmp = get_windows_directory_root ();
        }
947 948
#else /* G_OS_WIN32 */
      tmp = g_strdup (g_getenv ("TMPDIR"));
949 950 951 952 953 954 955 956 957 958 959

#ifdef P_tmpdir
      if (tmp == NULL || *tmp == '\0')
        {
          gsize k;
          g_free (tmp);
          tmp = g_strdup (P_tmpdir);
          k = strlen (tmp);
          if (k > 1 && G_IS_DIR_SEPARATOR (tmp[k - 1]))
            tmp[k - 1] = '\0';
        }
960
#endif /* P_tmpdir */
961 962 963 964 965 966 967 968 969 970 971 972

      if (tmp == NULL || *tmp == '\0')
        {
          g_free (tmp);
          tmp = g_strdup ("/tmp");
        }
#endif /* !G_OS_WIN32 */

      g_once_init_leave (&tmp_dir, tmp);
    }

  return tmp_dir;
973 974
}

975 976 977 978 979 980 981 982 983 984 985 986 987
/**
 * g_get_host_name:
 *
 * Return a name for the machine. 
 *
 * The returned name is not necessarily a fully-qualified domain name,
 * or even present in DNS or some other name service at all. It need
 * not even be unique on your local network or site, but usually it
 * is. Callers should not rely on the return value having any specific
 * properties like uniqueness for security purposes. Even if the name
 * of the machine is changed while an application is running, the
 * return value from this function does not change. The returned
 * string is owned by GLib and should not be modified or freed. If no
988
 * name can be determined, a default fixed string "localhost" is
989
 * returned.
990
 *
991 992
 * The encoding of the returned string is UTF-8.
 *
Ross Burton's avatar
Ross Burton committed
993 994
 * Returns: the host name of the machine.
 *
995
 * Since: 2.8
996 997 998 999
 */
const gchar *
g_get_host_name (void)
{
1000 1001 1002 1003 1004
  static gchar *hostname;

  if (g_once_init_enter (&hostname))
    {
      gboolean failed;
1005
      gchar *utmp;
1006 1007

#ifndef G_OS_WIN32
1008 1009
      gchar *tmp = g_malloc (sizeof (gchar) * 100);
      failed = (gethostname (tmp, sizeof (gchar) * 100) == -1);
1010 1011
      if (failed)
        g_clear_pointer (&tmp, g_free);
1012
      utmp = tmp;
1013
#else
1014 1015 1016 1017 1018 1019 1020
      wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
      DWORD size = sizeof (tmp) / sizeof (tmp[0]);
      failed = (!GetComputerNameW (tmp, &size));
      if (!failed)
        utmp = g_utf16_to_utf8 (tmp, size, NULL, NULL, NULL);
      if (utmp == NULL)
        failed = TRUE;
1021 1022
#endif

1023
      g_once_init_leave (&hostname, failed ? g_strdup ("localhost") : utmp);
1024 1025 1026
    }

  return hostname;
1027 1028
}

1029
G_LOCK_DEFINE_STATIC (g_prgname);
1030 1031
static gchar *g_prgname = NULL;

1032 1033 1034
/**
 * g_get_prgname:
 *
1035 1036 1037
 * Gets the name of the program. This name should not be localized,
 * in contrast to g_get_application_name().
 *
1038 1039 1040 1041 1042
 * If you are using #GApplication the program name is set in
 * g_application_run(). In case of GDK or GTK+ it is set in
 * gdk_init(), which is called by gtk_init() and the
 * #GtkApplication::startup handler. The program name is found by
 * taking the last component of @argv[0].
1043 1044
 *
 * Returns: the name of the program. The returned string belongs 
1045
 *     to GLib and must not be modified or freed.
1046
 */
1047
const gchar*
1048 1049
g_get_prgname (void)
{
1050 1051
  gchar* retval;

1052
  G_LOCK (g_prgname);
1053 1054 1055 1056 1057 1058 1059 1060
#ifdef G_OS_WIN32
  if (g_prgname == NULL)
    {
      static gboolean beenhere = FALSE;

      if (!beenhere)
	{
	  gchar *utf8_buf = NULL;
1061
	  wchar_t buf[MAX_PATH+1];
1062 1063

	  beenhere = TRUE;
1064 1065 1066 1067
	  if (GetModuleFileNameW (GetModuleHandle (NULL),
				  buf, G_N_ELEMENTS (buf)) > 0)
	    utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);

1068 1069 1070 1071 1072 1073 1074 1075
	  if (utf8_buf)
	    {
	      g_prgname = g_path_get_basename (utf8_buf);
	      g_free (utf8_buf);
	    }
	}
    }
#endif
1076
  retval = g_prgname;
1077
  G_UNLOCK (g_prgname);
1078 1079

  return retval;
1080 1081
}

1082 1083 1084 1085
/**
 * g_set_prgname:
 * @prgname: the name of the program.
 *
1086 1087 1088
 * Sets the name of the program. This name should not be localized,
 * in contrast to g_set_application_name().
 *
1089 1090 1091 1092 1093 1094
 * If you are using #GApplication the program name is set in
 * g_application_run(). In case of GDK or GTK+ it is set in
 * gdk_init(), which is called by gtk_init() and the
 * #GtkApplication::startup handler. The program name is found by
 * taking the last component of @argv[0].
 *
1095
 * Note that for thread-safety reasons this function can only be called once.
1096
 */
1097 1098 1099
void
g_set_prgname (const gchar *prgname)
{
1100
  G_LOCK (g_prgname);
1101 1102
  g_free (g_prgname);
  g_prgname = g_strdup (prgname);
1103
  G_UNLOCK (g_prgname);
1104 1105
}

1106
G_LOCK_DEFINE_STATIC (g_application_name);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
static gchar *g_application_name = NULL;

/**
 * g_get_application_name:
 * 
 * Gets a human-readable name for the application, as set by
 * g_set_application_name(). This name should be localized if
 * possible, and is intended for display to the user.  Contrast with
 * g_get_prgname(), which gets a non-localized name. If
 * g_set_application_name() has not been called, returns the result of
 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
 * been called).
 * 
1120
 * Returns: human-readable application name. may return %NULL
1121 1122
 *
 * Since: 2.2
1123
 **/
1124
const gchar *
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
g_get_application_name (void)
{
  gchar* retval;

  G_LOCK (g_application_name);
  retval = g_application_name;
  G_UNLOCK (g_application_name);

  if (retval == NULL)
    return g_get_prgname ();
  
  return retval;
}

/**
 * g_set_application_name:
 * @application_name: localized name of the application
 *
 * Sets a human-readable name for the application. This name should be
 * localized if possible, and is intended for display to the user.
 * Contrast with g_set_prgname(), which sets a non-localized name.
 * g_set_prgname() will be called automatically by gtk_init(),
 * but g_set_application_name() will not.
 *
 * Note that for thread safety reasons, this function can only
 * be called once.
 *
 * The application name will be used in contexts such as error messages,
 * or when displaying an application's name in the task list.
 * 
1155
 * Since: 2.2
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
 **/
void
g_set_application_name (const gchar *application_name)
{
  gboolean already_set = FALSE;
	
  G_LOCK (g_application_name);
  if (g_application_name)
    already_set = TRUE;
  else
    g_application_name = g_strdup (application_name);
  G_UNLOCK (g_application_name);

  if (already_set)
1170
    g_warning ("g_set_application_name() called multiple times");
1171 1172
}

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
/* Set @global_str to a copy of @new_value if it’s currently unset or has a
 * different value. If its current value matches @new_value, do nothing. If
 * replaced, we have to leak the old value as client code could still have
 * pointers to it. */
static void
set_str_if_different (gchar       **global_str,
                      const gchar  *type,
                      const gchar  *new_value)
{
  if (*global_str == NULL ||
      !g_str_equal (new_value, *global_str))
    {
      g_debug ("g_set_user_dirs: Setting %s to %s", type, new_value);

      /* We have to leak the old value, as user code could be retaining pointers
       * to it. */
      *global_str = g_strdup (new_value);
    }
}

static void
set_strv_if_different (gchar                ***global_strv,
                       const gchar            *type,
                       const gchar  * const   *new_value)
{
  if (*global_strv == NULL ||
      !g_strv_equal (new_value, (const gchar * const *) *global_strv))
    {
      gchar *new_value_str = g_strjoinv (":", (gchar **) new_value);
      g_debug ("g_set_user_dirs: Setting %s to %s", type, new_value_str);
      g_free (new_value_str);

      /* We have to leak the old value, as user code could be retaining pointers
       * to it. */
      *global_strv = g_strdupv ((gchar **) new_value);
    }
}

/*
 * g_set_user_dirs:
 * @first_dir_type: Type of the first directory to set
 * @...: Value to set the first directory to, followed by additional type/value
 *    pairs, followed by %NULL
 *
 * Set one or more ‘user’ directories to custom values. This is intended to be
 * used by test code (particularly with the %G_TEST_OPTION_ISOLATE_DIRS option)
 * to override the values returned by the following functions, so that test
 * code can be run without touching an installed system and user data:
 *
 *  - g_get_home_dir() — use type `HOME`, pass a string
 *  - g_get_user_cache_dir() — use type `XDG_CACHE_HOME`, pass a string
 *  - g_get_system_config_dirs() — use type `XDG_CONFIG_DIRS`, pass a
 *    %NULL-terminated string array
 *  - g_get_user_config_dir() — use type `XDG_CONFIG_HOME`, pass a string
 *  - g_get_system_data_dirs() — use type `XDG_DATA_DIRS`, pass a
 *    %NULL-terminated string array
 *  - g_get_user_data_dir() — use type `XDG_DATA_HOME`, pass a string
 *  - g_get_user_runtime_dir() — use type `XDG_RUNTIME_DIR`, pass a string
 *
 * The list must be terminated with a %NULL type. All of the values must be
 * non-%NULL — passing %NULL as a value won’t reset a directory. If a reference
 * to a directory from the calling environment needs to be kept, copy it before
 * the first call to g_set_user_dirs(). g_set_user_dirs() can be called multiple
 * times.
 *
 * Since: 2.60
 */
/*< private > */
void
g_set_user_dirs (const gchar *first_dir_type,
                 ...)
{
  va_list args;
  const gchar *dir_type;

  G_LOCK (g_utils_global);

  va_start (args, first_dir_type);

  for (dir_type = first_dir_type; dir_type != NULL; dir_type = va_arg (args, const gchar *))
    {
      gconstpointer dir_value = va_arg (args, gconstpointer);
      g_assert (dir_value != NULL);

      if (g_str_equal (dir_type, "HOME"))
        set_str_if_different (&g_home_dir, dir_type, dir_value);
      else if (g_str_equal (dir_type, "XDG_CACHE_HOME"))
        set_str_if_different (&g_user_cache_dir, dir_type, dir_value);
      else if (g_str_equal (dir_type, "XDG_CONFIG_DIRS"))
        set_strv_if_different (&g_system_config_dirs, dir_type, dir_value);
      else if (g_str_equal (dir_type, "XDG_CONFIG_HOME"))
        set_str_if_different (&g_user_config_dir, dir_type, dir_value);
      else if (g_str_equal (dir_type, "XDG_DATA_DIRS"))
        set_strv_if_different (&g_system_data_dirs, dir_type, dir_value);
      else if (g_str_equal (dir_type, "XDG_DATA_HOME"))
        set_str_if_different (&g_user_data_dir, dir_type, dir_value);
      else if (g_str_equal (dir_type, "XDG_RUNTIME_DIR"))
        set_str_if_different (&g_user_runtime_dir, dir_type, dir_value);
      else
        g_assert_not_reached ();
    }

  va_end (args);

  G_UNLOCK (g_utils_global);
}

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
static gchar *
g_build_user_data_dir (void)
{
  gchar *data_dir = NULL;
  const gchar *data_dir_env = g_getenv ("XDG_DATA_HOME");

  if (data_dir_env && data_dir_env[0])
    data_dir = g_strdup (data_dir_env);
#ifdef G_OS_WIN32
  else
    data_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
#endif
  if (!data_dir || !data_dir[0])
    {
1294
      gchar *home_dir = g_build_home_dir ();
1295
      data_dir = g_build_filename (home_dir, ".local", "share", NULL);
1296
      g_free (home_dir);
1297 1298 1299 1300 1301
    }

  return g_steal_pointer (&data_dir);
}

1302 1303 1304 1305 1306 1307
/**
 * g_get_user_data_dir:
 * 
 * Returns a base directory in which to access application data such
 * as icons that is customized for a particular user.  
 *
1308 1309 1310 1311
 * On UNIX platforms this is determined using the mechanisms described
 * in the
 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
 * In this case the directory retrieved will be `XDG_DATA_HOME`.
1312
 *
1313 1314 1315 1316 1317 1318
 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_HOME`
 * is defined. If `XDG_DATA_HOME` is undefined, the folder to use for local (as
 * opposed to roaming) application data is used instead. See the
 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata).
 * Note that in this case on Windows it will be the same
 * as what g_get_user_config_dir() returns.
1319
 *
1320
 * Returns: (type filename): a string owned by GLib that must not be modified
1321 1322 1323
 *               or freed.
 * Since: 2.6
 **/
1324
const gchar *
1325 1326
g_get_user_data_dir (void)
{
1327 1328
  const gchar *user_data_dir;

1329 1330
  G_LOCK (g_utils_global);

1331 1332
  if (g_user_data_dir == NULL)
    g_user_data_dir = g_build_user_data_dir ();
1333
  user_data_dir = g_user_data_dir;
1334 1335 1336

  G_UNLOCK (g_utils_global);

1337
  return user_data_dir;
1338 1339
}

1340 1341
static gchar *
g_build_user_config_dir (void)
1342
{
1343
  gchar *config_dir = NULL;
1344
  const gchar *config_dir_env = g_getenv ("XDG_CONFIG_HOME");
1345

1346 1347
  if (config_dir_env && config_dir_env[0])
    config_dir = g_strdup (config_dir_env);
1348
#ifdef G_OS_WIN32
1349 1350
  else
    config_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
1351
#endif
1352 1353
  if (!config_dir || !config_dir[0])
    {
1354
      gchar *home_dir = g_build_home_dir ();
1355
      config_dir = g_build_filename (home_dir, ".config", NULL);
1356
      g_free (home_dir);
1357
    }
1358 1359

  return g_steal_pointer (&config_dir);
1360 1361 1362 1363 1364 1365 1366 1367
}

/**
 * g_get_user_config_dir:
 * 
 * Returns a base directory in which to store user-specific application 
 * configuration information such as user preferences and settings. 
 *
1368 1369 1370 1371
 * On UNIX platforms this is determined using the mechanisms described
 * in the
 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
 * In this case the directory retrieved will be `XDG_CONFIG_HOME`.
1372
 *
1373 1374 1375 1376 1377 1378
 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_HOME` is defined.
 * If `XDG_CONFIG_HOME` is undefined, the folder to use for local (as opposed
 * to roaming) application data is used instead. See the
 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata).
 * Note that in this case on Windows it will be  the same
 * as what g_get_user_data_dir() returns.
1379
 *
1380
 * Returns: (type filename): a string owned by GLib that must not be modified
1381 1382 1383
 *               or freed.
 * Since: 2.6
 **/
1384
const gchar *
1385 1386
g_get_user_config_dir (void)
{
1387 1388
  const gchar *user_config_dir;

1389 1390
  G_LOCK (g_utils_global);

1391 1392
  if (g_user_config_dir == NULL)
    g_user_config_dir = g_build_user_config_dir ();
1393
  user_config_dir = g_user_config_dir;
1394 1395 1396

  G_UNLOCK (g_utils_global);

1397
  return user_config_dir;
1398 1399
}

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
static gchar *
g_build_user_cache_dir (void)
{
  gchar *cache_dir = NULL;
  const gchar *cache_dir_env = g_getenv ("XDG_CACHE_HOME");

  if (cache_dir_env && cache_dir_env[0])
    cache_dir = g_strdup (cache_dir_env);
#ifdef G_OS_WIN32
  else
    cache_dir = get_special_folder (CSIDL_INTERNET_CACHE);
#endif
  if (!cache_dir || !cache_dir[0])
    {
1414
      gchar *home_dir = g_build_home_dir ();
1415
      cache_dir = g_build_filename (home_dir, ".cache", NULL);
1416
      g_free (home_dir);
1417 1418 1419 1420 1421
    }

  return g_steal_pointer (&cache_dir);
}

1422 1423 1424 1425 1426 1427
/**
 * g_get_user_cache_dir:
 * 
 * Returns a base directory in which to store non-essential, cached
 * data specific to particular user.
 *
1428 1429 1430
 * On UNIX platforms this is determined using the mechanisms described
 * in the
 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1431
 * In this case the directory retrieved will be `XDG_CACHE_HOME`.
1432
 *
1433 1434 1435 1436 1437
 * On Windows it follows XDG Base Directory Specification if `XDG_CACHE_HOME` is defined.
 * If `XDG_CACHE_HOME` is undefined, the directory that serves as a common
 * repository for temporary Internet files is used instead. A typical path is
 * `C:\Documents and Settings\username\Local Settings\Temporary Internet Files`.
 * See the [documentation for `CSIDL_INTERNET_CACHE`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_internet_cache).
1438
 *
1439
 * Returns: (type filename): a string owned by GLib that must not be modified
1440 1441 1442
 *               or freed.
 * Since: 2.6
 **/
1443
const gchar *
1444 1445
g_get_user_cache_dir (void)
{
1446 1447
  const gchar *user_cache_dir;

1448 1449
  G_LOCK (g_utils_global);

1450 1451
  if (g_user_cache_dir == NULL)
    g_user_cache_dir = g_build_user_cache_dir ();
1452
  user_cache_dir = g_user_cache_dir;
1453

1454
  G_UNLOCK (g_utils_global);
1455

1456
  return user_cache_dir;
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
}

static gchar *
g_build_user_runtime_dir (void)
{
  gchar *runtime_dir = NULL;
  const gchar *runtime_dir_env = g_getenv ("XDG_RUNTIME_DIR");

  if (runtime_dir_env && runtime_dir_env[0])
    runtime_dir = g_strdup (runtime_dir_env);
1467
  else
1468 1469
    {
      runtime_dir = g_build_user_cache_dir ();
1470

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
      /* The user should be able to rely on the directory existing
       * when the function returns.  Probably it already does, but
       * let's make sure.  Just do mkdir() directly since it will be
       * no more expensive than a stat() in the case that the
       * directory already exists and is a lot easier.
       *
       * $XDG_CACHE_HOME is probably ~/.cache/ so as long as $HOME
       * exists this will work.  If the user changed $XDG_CACHE_HOME
       * then they can make sure that it exists...
       */
      (void) g_mkdir (runtime_dir, 0700);
    }
1483

1484
  return g_steal_pointer (&runtime_dir);
1485 1486
}

1487 1488 1489 1490 1491 1492
/**
 * g_get_user_runtime_dir:
 *
 * Returns a directory that is unique to the current user on the local
 * system.
 *
1493
 * This is determined using the mechanisms described
1494 1495 1496
 * in the 
 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
 * This is the directory
1497
 * specified in the `XDG_RUNTIME_DIR` environment variable.
1498 1499
 * In the case that this variable is not set, we return the value of
 * g_get_user_cache_dir(), after verifying that it exists.
1500
 *
1501 1502
 * Returns: (type filename): a string owned by GLib that must not be
 *     modified or freed.
Matthias Clasen's avatar
Matthias Clasen committed
1503 1504
 *
 * Since: 2.28
1505 1506 1507 1508
 **/
const gchar *
g_get_user_runtime_dir (void)
{
1509 1510
  const gchar *user_runtime_dir;

1511
  G_LOCK (g_utils_global);
1512

1513 1514
  if (g_user_runtime_dir == NULL)
    g_user_runtime_dir = g_build_user_runtime_dir ();
1515
  user_runtime_dir = g_user_runtime_dir;
1516

1517
  G_UNLOCK (g_utils_global);
1518

1519
  return user_runtime_dir;
1520 1521
}

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565