~ubuntu-branches/ubuntu/utopic/glib2.0/utopic

« back to all changes in this revision

Viewing changes to glib/gutils.c

Tags: upstream-2.12.12
ImportĀ upstreamĀ versionĀ 2.12.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GLIB - Library of useful routines for C programming
 
2
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
 
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
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
/*
 
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 
22
 * file for a list of people on the GLib Team.  See the ChangeLog
 
23
 * files for a list of changes.  These files are distributed with
 
24
 * 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
 */
 
30
 
 
31
#include "config.h"
 
32
 
 
33
#ifdef HAVE_UNISTD_H
 
34
#include <unistd.h>
 
35
#endif
 
36
#include <stdarg.h>
 
37
#include <stdlib.h>
 
38
#include <stdio.h>
 
39
#include <locale.h>
 
40
#include <string.h>
 
41
#include <ctype.h>              /* For tolower() */
 
42
#include <errno.h>
 
43
#ifdef HAVE_PWD_H
 
44
#include <pwd.h>
 
45
#endif
 
46
#include <sys/types.h>
 
47
#ifdef HAVE_SYS_PARAM_H
 
48
#include <sys/param.h>
 
49
#endif
 
50
#ifdef HAVE_CRT_EXTERNS_H 
 
51
#include <crt_externs.h> /* for _NSGetEnviron */
 
52
#endif
 
53
 
 
54
/* implement gutils's inline functions
 
55
 */
 
56
#define G_IMPLEMENT_INLINES 1
 
57
#define __G_UTILS_C__
 
58
#include "glib.h"
 
59
#include "gprintfint.h"
 
60
#include "gthreadprivate.h"
 
61
#include "galias.h"
 
62
 
 
63
#ifdef  MAXPATHLEN
 
64
#define G_PATH_LENGTH   MAXPATHLEN
 
65
#elif   defined (PATH_MAX)
 
66
#define G_PATH_LENGTH   PATH_MAX
 
67
#elif   defined (_PC_PATH_MAX)
 
68
#define G_PATH_LENGTH   sysconf(_PC_PATH_MAX)
 
69
#else   
 
70
#define G_PATH_LENGTH   2048
 
71
#endif
 
72
 
 
73
#ifdef G_PLATFORM_WIN32
 
74
#  define STRICT                /* Strict typing, please */
 
75
#  include <windows.h>
 
76
#  undef STRICT
 
77
#  ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
 
78
#    define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
 
79
#    define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
 
80
#  endif
 
81
#  include <lmcons.h>           /* For UNLEN */
 
82
#endif /* G_PLATFORM_WIN32 */
 
83
 
 
84
#ifdef G_OS_WIN32
 
85
#  include <direct.h>
 
86
#  include <shlobj.h>
 
87
   /* older SDK (e.g. msvc 5.0) does not have these*/
 
88
#  ifndef CSIDL_INTERNET_CACHE
 
89
#    define CSIDL_INTERNET_CACHE 32
 
90
#  endif
 
91
#  ifndef CSIDL_COMMON_APPDATA
 
92
#    define CSIDL_COMMON_APPDATA 35
 
93
#  endif
 
94
#  ifndef CSIDL_COMMON_DOCUMENTS
 
95
#    define CSIDL_COMMON_DOCUMENTS 46
 
96
#  endif
 
97
#  ifndef CSIDL_PROFILE
 
98
#    define CSIDL_PROFILE 40
 
99
#  endif
 
100
#  include <process.h>
 
101
#endif
 
102
 
 
103
#ifdef HAVE_CODESET
 
104
#include <langinfo.h>
 
105
#endif
 
106
 
 
107
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
 
108
#include <libintl.h>
 
109
#endif
 
110
 
 
111
const guint glib_major_version = GLIB_MAJOR_VERSION;
 
112
const guint glib_minor_version = GLIB_MINOR_VERSION;
 
113
const guint glib_micro_version = GLIB_MICRO_VERSION;
 
114
const guint glib_interface_age = GLIB_INTERFACE_AGE;
 
115
const guint glib_binary_age = GLIB_BINARY_AGE;
 
116
 
 
117
#ifdef G_PLATFORM_WIN32
 
118
 
 
119
G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
 
120
 
 
121
#endif
 
122
 
 
123
/**
 
124
 * glib_check_version:
 
125
 * @required_major: the required major version.
 
126
 * @required_minor: the required minor version.
 
127
 * @required_micro: the required micro version.
 
128
 *
 
129
 * Checks that the GLib library in use is compatible with the
 
130
 * given version. Generally you would pass in the constants
 
131
 * #GLIB_MAJOR_VERSION, #GLIB_MINOR_VERSION, #GLIB_MICRO_VERSION
 
132
 * as the three arguments to this function; that produces
 
133
 * a check that the library in use is compatible with
 
134
 * the version of GLib the application or module was compiled
 
135
 * against.
 
136
 *
 
137
 * Compatibility is defined by two things: first the version
 
138
 * of the running library is newer than the version
 
139
 * @required_major.required_minor.@required_micro. Second
 
140
 * the running library must be binary compatible with the
 
141
 * version @required_major.required_minor.@required_micro
 
142
 * (same major version.)
 
143
 *
 
144
 * Return value: %NULL if the GLib library is compatible with the
 
145
 *   given version, or a string describing the version mismatch.
 
146
 *   The returned string is owned by GLib and must not be modified
 
147
 *   or freed.
 
148
 *
 
149
 * Since: 2.6
 
150
 **/
 
151
const gchar *
 
152
glib_check_version (guint required_major,
 
153
                    guint required_minor,
 
154
                    guint required_micro)
 
155
{
 
156
  gint glib_effective_micro = 100 * GLIB_MINOR_VERSION + GLIB_MICRO_VERSION;
 
157
  gint required_effective_micro = 100 * required_minor + required_micro;
 
158
 
 
159
  if (required_major > GLIB_MAJOR_VERSION)
 
160
    return "GLib version too old (major mismatch)";
 
161
  if (required_major < GLIB_MAJOR_VERSION)
 
162
    return "GLib version too new (major mismatch)";
 
163
  if (required_effective_micro < glib_effective_micro - GLIB_BINARY_AGE)
 
164
    return "GLib version too new (micro mismatch)";
 
165
  if (required_effective_micro > glib_effective_micro)
 
166
    return "GLib version too old (micro mismatch)";
 
167
  return NULL;
 
168
}
 
169
 
 
170
#if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
 
171
/**
 
172
 * g_memmove: 
 
173
 * @dest: the destination address to copy the bytes to.
 
174
 * @src: the source address to copy the bytes from.
 
175
 * @len: the number of bytes to copy.
 
176
 *
 
177
 * Copies a block of memory @len bytes long, from @src to @dest.
 
178
 * The source and destination areas may overlap.
 
179
 *
 
180
 * In order to use this function, you must include 
 
181
 * <filename>string.h</filename> yourself, because this macro will 
 
182
 * typically simply resolve to memmove() and GLib does not include 
 
183
 * <filename>string.h</filename> for you.
 
184
 */
 
185
void 
 
186
g_memmove (gpointer      dest, 
 
187
           gconstpointer src, 
 
188
           gulong        len)
 
189
{
 
190
  gchar* destptr = dest;
 
191
  const gchar* srcptr = src;
 
192
  if (src + len < dest || dest + len < src)
 
193
    {
 
194
      bcopy (src, dest, len);
 
195
      return;
 
196
    }
 
197
  else if (dest <= src)
 
198
    {
 
199
      while (len--)
 
200
        *(destptr++) = *(srcptr++);
 
201
    }
 
202
  else
 
203
    {
 
204
      destptr += len;
 
205
      srcptr += len;
 
206
      while (len--)
 
207
        *(--destptr) = *(--srcptr);
 
208
    }
 
209
}
 
210
#endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
 
211
 
 
212
#ifdef G_OS_WIN32
 
213
#undef g_atexit
 
214
#endif
 
215
 
 
216
/**
 
217
 * g_atexit:
 
218
 * @func: the function to call on normal program termination.
 
219
 * 
 
220
 * Specifies a function to be called at normal program termination.
 
221
 *
 
222
 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
 
223
 * macro that maps to a call to the atexit() function in the C
 
224
 * library. This means that in case the code that calls g_atexit(),
 
225
 * i.e. atexit(), is in a DLL, the function will be called when the
 
226
 * DLL is detached from the program. This typically makes more sense
 
227
 * than that the function is called when the GLib DLL is detached,
 
228
 * which happened earlier when g_atexit() was a function in the GLib
 
229
 * DLL.
 
230
 *
 
231
 * The behaviour of atexit() in the context of dynamically loaded
 
232
 * modules is not formally specified and varies wildly.
 
233
 *
 
234
 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
 
235
 * loaded module which is unloaded before the program terminates might
 
236
 * well cause a crash at program exit.
 
237
 *
 
238
 * Some POSIX systems implement atexit() like Windows, and have each
 
239
 * dynamically loaded module maintain an own atexit chain that is
 
240
 * called when the module is unloaded.
 
241
 *
 
242
 * On other POSIX systems, before a dynamically loaded module is
 
243
 * unloaded, the registered atexit functions (if any) residing in that
 
244
 * module are called, regardless where the code that registered them
 
245
 * resided. This is presumably the most robust approach.
 
246
 *
 
247
 * As can be seen from the above, for portability it's best to avoid
 
248
 * calling g_atexit() (or atexit()) except in the main executable of a
 
249
 * program.
 
250
 */
 
251
void
 
252
g_atexit (GVoidFunc func)
 
253
{
 
254
  gint result;
 
255
  const gchar *error = NULL;
 
256
 
 
257
  /* keep this in sync with glib.h */
 
258
 
 
259
#ifdef  G_NATIVE_ATEXIT
 
260
  result = ATEXIT (func);
 
261
  if (result)
 
262
    error = g_strerror (errno);
 
263
#elif defined (HAVE_ATEXIT)
 
264
#  ifdef NeXT /* @#%@! NeXTStep */
 
265
  result = !atexit ((void (*)(void)) func);
 
266
  if (result)
 
267
    error = g_strerror (errno);
 
268
#  else
 
269
  result = atexit ((void (*)(void)) func);
 
270
  if (result)
 
271
    error = g_strerror (errno);
 
272
#  endif /* NeXT */
 
273
#elif defined (HAVE_ON_EXIT)
 
274
  result = on_exit ((void (*)(int, void *)) func, NULL);
 
275
  if (result)
 
276
    error = g_strerror (errno);
 
277
#else
 
278
  result = 0;
 
279
  error = "no implementation";
 
280
#endif /* G_NATIVE_ATEXIT */
 
281
 
 
282
  if (error)
 
283
    g_error ("Could not register atexit() function: %s", error);
 
284
}
 
285
 
 
286
/* Based on execvp() from GNU Libc.
 
287
 * Some of this code is cut-and-pasted into gspawn.c
 
288
 */
 
289
 
 
290
static gchar*
 
291
my_strchrnul (const gchar *str, 
 
292
              gchar        c)
 
293
{
 
294
  gchar *p = (gchar*)str;
 
295
  while (*p && (*p != c))
 
296
    ++p;
 
297
 
 
298
  return p;
 
299
}
 
300
 
 
301
#ifdef G_OS_WIN32
 
302
 
 
303
static gchar *inner_find_program_in_path (const gchar *program);
 
304
 
 
305
gchar*
 
306
g_find_program_in_path (const gchar *program)
 
307
{
 
308
  const gchar *last_dot = strrchr (program, '.');
 
309
 
 
310
  if (last_dot == NULL ||
 
311
      strchr (last_dot, '\\') != NULL ||
 
312
      strchr (last_dot, '/') != NULL)
 
313
    {
 
314
      const gint program_length = strlen (program);
 
315
      gchar *pathext = g_build_path (";",
 
316
                                     ".exe;.cmd;.bat;.com",
 
317
                                     g_getenv ("PATHEXT"),
 
318
                                     NULL);
 
319
      gchar *p;
 
320
      gchar *decorated_program;
 
321
      gchar *retval;
 
322
 
 
323
      p = pathext;
 
324
      do
 
325
        {
 
326
          gchar *q = my_strchrnul (p, ';');
 
327
 
 
328
          decorated_program = g_malloc (program_length + (q-p) + 1);
 
329
          memcpy (decorated_program, program, program_length);
 
330
          memcpy (decorated_program+program_length, p, q-p);
 
331
          decorated_program [program_length + (q-p)] = '\0';
 
332
          
 
333
          retval = inner_find_program_in_path (decorated_program);
 
334
          g_free (decorated_program);
 
335
 
 
336
          if (retval != NULL)
 
337
            {
 
338
              g_free (pathext);
 
339
              return retval;
 
340
            }
 
341
          p = q;
 
342
        } while (*p++ != '\0');
 
343
      g_free (pathext);
 
344
      return NULL;
 
345
    }
 
346
  else
 
347
    return inner_find_program_in_path (program);
 
348
}
 
349
 
 
350
#endif
 
351
 
 
352
/**
 
353
 * g_find_program_in_path:
 
354
 * @program: a program name in the GLib file name encoding
 
355
 * 
 
356
 * Locates the first executable named @program in the user's path, in the
 
357
 * same way that execvp() would locate it. Returns an allocated string
 
358
 * with the absolute path name, or %NULL if the program is not found in
 
359
 * the path. If @program is already an absolute path, returns a copy of
 
360
 * @program if @program exists and is executable, and %NULL otherwise.
 
361
 *  
 
362
 * On Windows, if @program does not have a file type suffix, tries
 
363
 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
 
364
 * the <envar>PATHEXT</envar> environment variable. 
 
365
 * 
 
366
 * On Windows, it looks for the file in the same way as CreateProcess() 
 
367
 * would. This means first in the directory where the executing
 
368
 * program was loaded from, then in the current directory, then in the
 
369
 * Windows 32-bit system directory, then in the Windows directory, and
 
370
 * finally in the directories in the <envar>PATH</envar> environment 
 
371
 * variable. If the program is found, the return value contains the 
 
372
 * full name including the type suffix.
 
373
 *
 
374
 * Return value: absolute path, or %NULL
 
375
 **/
 
376
#ifdef G_OS_WIN32
 
377
static gchar *
 
378
inner_find_program_in_path (const gchar *program)
 
379
#else
 
380
gchar*
 
381
g_find_program_in_path (const gchar *program)
 
382
#endif
 
383
{
 
384
  const gchar *path, *p;
 
385
  gchar *name, *freeme;
 
386
#ifdef G_OS_WIN32
 
387
  const gchar *path_copy;
 
388
  gchar *filename = NULL, *appdir = NULL;
 
389
  gchar *sysdir = NULL, *windir = NULL;
 
390
#endif
 
391
  size_t len;
 
392
  size_t pathlen;
 
393
 
 
394
  g_return_val_if_fail (program != NULL, NULL);
 
395
 
 
396
  /* If it is an absolute path, or a relative path including subdirectories,
 
397
   * don't look in PATH.
 
398
   */
 
399
  if (g_path_is_absolute (program)
 
400
      || strchr (program, G_DIR_SEPARATOR) != NULL
 
401
#ifdef G_OS_WIN32
 
402
      || strchr (program, '/') != NULL
 
403
#endif
 
404
      )
 
405
    {
 
406
      if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
 
407
          !g_file_test (program, G_FILE_TEST_IS_DIR))
 
408
        return g_strdup (program);
 
409
      else
 
410
        return NULL;
 
411
    }
 
412
  
 
413
  path = g_getenv ("PATH");
 
414
#if defined(G_OS_UNIX) || defined(G_OS_BEOS)
 
415
  if (path == NULL)
 
416
    {
 
417
      /* There is no `PATH' in the environment.  The default
 
418
       * search path in GNU libc is the current directory followed by
 
419
       * the path `confstr' returns for `_CS_PATH'.
 
420
       */
 
421
      
 
422
      /* In GLib we put . last, for security, and don't use the
 
423
       * unportable confstr(); UNIX98 does not actually specify
 
424
       * what to search if PATH is unset. POSIX may, dunno.
 
425
       */
 
426
      
 
427
      path = "/bin:/usr/bin:.";
 
428
    }
 
429
#else
 
430
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
431
    {
 
432
      int n;
 
433
      wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
 
434
        wwindir[MAXPATHLEN];
 
435
      
 
436
      n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
 
437
      if (n > 0 && n < MAXPATHLEN)
 
438
        filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
 
439
      
 
440
      n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
 
441
      if (n > 0 && n < MAXPATHLEN)
 
442
        sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
 
443
      
 
444
      n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
 
445
      if (n > 0 && n < MAXPATHLEN)
 
446
        windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
 
447
    }
 
448
  else
 
449
    {
 
450
      int n;
 
451
      gchar cpfilename[MAXPATHLEN], cpsysdir[MAXPATHLEN],
 
452
        cpwindir[MAXPATHLEN];
 
453
      
 
454
      n = GetModuleFileNameA (NULL, cpfilename, MAXPATHLEN);
 
455
      if (n > 0 && n < MAXPATHLEN)
 
456
        filename = g_locale_to_utf8 (cpfilename, -1, NULL, NULL, NULL);
 
457
      
 
458
      n = GetSystemDirectoryA (cpsysdir, MAXPATHLEN);
 
459
      if (n > 0 && n < MAXPATHLEN)
 
460
        sysdir = g_locale_to_utf8 (cpsysdir, -1, NULL, NULL, NULL);
 
461
      
 
462
      n = GetWindowsDirectoryA (cpwindir, MAXPATHLEN);
 
463
      if (n > 0 && n < MAXPATHLEN)
 
464
        windir = g_locale_to_utf8 (cpwindir, -1, NULL, NULL, NULL);
 
465
    }
 
466
  
 
467
  if (filename)
 
468
    {
 
469
      appdir = g_path_get_dirname (filename);
 
470
      g_free (filename);
 
471
    }
 
472
  
 
473
  path = g_strdup (path);
 
474
 
 
475
  if (windir)
 
476
    {
 
477
      const gchar *tem = path;
 
478
      path = g_strconcat (windir, ";", path, NULL);
 
479
      g_free ((gchar *) tem);
 
480
      g_free (windir);
 
481
    }
 
482
  
 
483
  if (sysdir)
 
484
    {
 
485
      const gchar *tem = path;
 
486
      path = g_strconcat (sysdir, ";", path, NULL);
 
487
      g_free ((gchar *) tem);
 
488
      g_free (sysdir);
 
489
    }
 
490
  
 
491
  {
 
492
    const gchar *tem = path;
 
493
    path = g_strconcat (".;", path, NULL);
 
494
    g_free ((gchar *) tem);
 
495
  }
 
496
  
 
497
  if (appdir)
 
498
    {
 
499
      const gchar *tem = path;
 
500
      path = g_strconcat (appdir, ";", path, NULL);
 
501
      g_free ((gchar *) tem);
 
502
      g_free (appdir);
 
503
    }
 
504
 
 
505
  path_copy = path;
 
506
#endif
 
507
  
 
508
  len = strlen (program) + 1;
 
509
  pathlen = strlen (path);
 
510
  freeme = name = g_malloc (pathlen + len + 1);
 
511
  
 
512
  /* Copy the file name at the top, including '\0'  */
 
513
  memcpy (name + pathlen + 1, program, len);
 
514
  name = name + pathlen;
 
515
  /* And add the slash before the filename  */
 
516
  *name = G_DIR_SEPARATOR;
 
517
  
 
518
  p = path;
 
519
  do
 
520
    {
 
521
      char *startp;
 
522
 
 
523
      path = p;
 
524
      p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
 
525
 
 
526
      if (p == path)
 
527
        /* Two adjacent colons, or a colon at the beginning or the end
 
528
         * of `PATH' means to search the current directory.
 
529
         */
 
530
        startp = name + 1;
 
531
      else
 
532
        startp = memcpy (name - (p - path), path, p - path);
 
533
 
 
534
      if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
 
535
          !g_file_test (startp, G_FILE_TEST_IS_DIR))
 
536
        {
 
537
          gchar *ret;
 
538
          ret = g_strdup (startp);
 
539
          g_free (freeme);
 
540
#ifdef G_OS_WIN32
 
541
          g_free ((gchar *) path_copy);
 
542
#endif
 
543
          return ret;
 
544
        }
 
545
    }
 
546
  while (*p++ != '\0');
 
547
  
 
548
  g_free (freeme);
 
549
#ifdef G_OS_WIN32
 
550
  g_free ((gchar *) path_copy);
 
551
#endif
 
552
 
 
553
  return NULL;
 
554
}
 
555
 
 
556
static gboolean
 
557
debug_key_matches (const gchar *key,
 
558
                   const gchar *token,
 
559
                   guint        length)
 
560
{
 
561
  for (; length; length--, key++, token++)
 
562
    {
 
563
      char k = (*key   == '_') ? '-' : tolower (*key  );
 
564
      char t = (*token == '_') ? '-' : tolower (*token);
 
565
 
 
566
      if (k != t)
 
567
        return FALSE;
 
568
    }
 
569
 
 
570
  return *key == '\0';
 
571
}
 
572
 
 
573
/**
 
574
 * g_parse_debug_string:
 
575
 * @string: a list of debug options separated by colons, spaces, or
 
576
 * commas; or the string "all" to set all flags.
 
577
 * @keys: pointer to an array of #GDebugKey which associate 
 
578
 *     strings with bit flags.
 
579
 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
 
580
 *
 
581
 * Parses a string containing debugging options
 
582
 * into a %guint containing bit flags. This is used 
 
583
 * within GDK and GTK+ to parse the debug options passed on the
 
584
 * command line or through environment variables.
 
585
 *
 
586
 * Returns: the combined set of bit flags.
 
587
 */
 
588
guint        
 
589
g_parse_debug_string  (const gchar     *string, 
 
590
                       const GDebugKey *keys, 
 
591
                       guint            nkeys)
 
592
{
 
593
  guint i;
 
594
  guint result = 0;
 
595
  
 
596
  g_return_val_if_fail (string != NULL, 0);
 
597
 
 
598
  /* this function is used by gmem.c/gslice.c initialization code,
 
599
   * so introducing malloc dependencies here would require adaptions
 
600
   * of those code portions.
 
601
   */
 
602
  
 
603
  if (!g_ascii_strcasecmp (string, "all"))
 
604
    {
 
605
      for (i=0; i<nkeys; i++)
 
606
        result |= keys[i].value;
 
607
    }
 
608
  else
 
609
    {
 
610
      const gchar *p = string;
 
611
      const gchar *q;
 
612
      
 
613
      while (*p)
 
614
        {
 
615
          q = strpbrk (p, ":;, \t");
 
616
          if (!q)
 
617
            q = p + strlen(p);
 
618
          
 
619
          for (i = 0; i < nkeys; i++)
 
620
            if (debug_key_matches (keys[i].key, p, q - p))
 
621
              result |= keys[i].value;
 
622
          
 
623
          p = q;
 
624
          if (*p)
 
625
            p++;
 
626
        }
 
627
    }
 
628
  
 
629
  return result;
 
630
}
 
631
 
 
632
/**
 
633
 * g_basename:
 
634
 * @file_name: the name of the file.
 
635
 * 
 
636
 * Gets the name of the file without any leading directory components.  
 
637
 * It returns a pointer into the given file name string.
 
638
 * 
 
639
 * Return value: the name of the file without any leading directory components.
 
640
 *
 
641
 * Deprecated:2.2: Use g_path_get_basename() instead, but notice that
 
642
 * g_path_get_basename() allocates new memory for the returned string, unlike
 
643
 * this function which returns a pointer into the argument.
 
644
 **/
 
645
G_CONST_RETURN gchar*
 
646
g_basename (const gchar    *file_name)
 
647
{
 
648
  register gchar *base;
 
649
  
 
650
  g_return_val_if_fail (file_name != NULL, NULL);
 
651
  
 
652
  base = strrchr (file_name, G_DIR_SEPARATOR);
 
653
 
 
654
#ifdef G_OS_WIN32
 
655
  {
 
656
    gchar *q = strrchr (file_name, '/');
 
657
    if (base == NULL || (q != NULL && q > base))
 
658
        base = q;
 
659
  }
 
660
#endif
 
661
 
 
662
  if (base)
 
663
    return base + 1;
 
664
 
 
665
#ifdef G_OS_WIN32
 
666
  if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
 
667
    return (gchar*) file_name + 2;
 
668
#endif /* G_OS_WIN32 */
 
669
  
 
670
  return (gchar*) file_name;
 
671
}
 
672
 
 
673
/**
 
674
 * g_path_get_basename:
 
675
 * @file_name: the name of the file.
 
676
 *
 
677
 * Gets the last component of the filename. If @file_name ends with a 
 
678
 * directory separator it gets the component before the last slash. If 
 
679
 * @file_name consists only of directory separators (and on Windows, 
 
680
 * possibly a drive letter), a single separator is returned. If
 
681
 * @file_name is empty, it gets ".".
 
682
 *
 
683
 * Return value: a newly allocated string containing the last component of 
 
684
 *   the filename.
 
685
 */
 
686
gchar*
 
687
g_path_get_basename (const gchar   *file_name)
 
688
{
 
689
  register gssize base;             
 
690
  register gssize last_nonslash;    
 
691
  gsize len;    
 
692
  gchar *retval;
 
693
 
 
694
  g_return_val_if_fail (file_name != NULL, NULL);
 
695
 
 
696
  if (file_name[0] == '\0')
 
697
    /* empty string */
 
698
    return g_strdup (".");
 
699
  
 
700
  last_nonslash = strlen (file_name) - 1;
 
701
 
 
702
  while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
 
703
    last_nonslash--;
 
704
 
 
705
  if (last_nonslash == -1)
 
706
    /* string only containing slashes */
 
707
    return g_strdup (G_DIR_SEPARATOR_S);
 
708
 
 
709
#ifdef G_OS_WIN32
 
710
  if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
 
711
    /* string only containing slashes and a drive */
 
712
    return g_strdup (G_DIR_SEPARATOR_S);
 
713
#endif /* G_OS_WIN32 */
 
714
 
 
715
  base = last_nonslash;
 
716
 
 
717
  while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
 
718
    base--;
 
719
 
 
720
#ifdef G_OS_WIN32
 
721
  if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
 
722
    base = 1;
 
723
#endif /* G_OS_WIN32 */
 
724
 
 
725
  len = last_nonslash - base;
 
726
  retval = g_malloc (len + 1);
 
727
  memcpy (retval, file_name + base + 1, len);
 
728
  retval [len] = '\0';
 
729
  return retval;
 
730
}
 
731
 
 
732
/**
 
733
 * g_path_is_absolute:
 
734
 * @file_name: a file name.
 
735
 *
 
736
 * Returns %TRUE if the given @file_name is an absolute file name,
 
737
 * i.e. it contains a full path from the root directory such as "/usr/local"
 
738
 * on UNIX or "C:\windows" on Windows systems.
 
739
 *
 
740
 * Returns: %TRUE if @file_name is an absolute path. 
 
741
 */
 
742
gboolean
 
743
g_path_is_absolute (const gchar *file_name)
 
744
{
 
745
  g_return_val_if_fail (file_name != NULL, FALSE);
 
746
  
 
747
  if (G_IS_DIR_SEPARATOR (file_name[0]))
 
748
    return TRUE;
 
749
 
 
750
#ifdef G_OS_WIN32
 
751
  /* Recognize drive letter on native Windows */
 
752
  if (g_ascii_isalpha (file_name[0]) && 
 
753
      file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
 
754
    return TRUE;
 
755
#endif /* G_OS_WIN32 */
 
756
 
 
757
  return FALSE;
 
758
}
 
759
 
 
760
/**
 
761
 * g_path_skip_root:
 
762
 * @file_name: a file name.
 
763
 *
 
764
 * Returns a pointer into @file_name after the root component, i.e. after
 
765
 * the "/" in UNIX or "C:\" under Windows. If @file_name is not an absolute
 
766
 * path it returns %NULL.
 
767
 *
 
768
 * Returns: a pointer into @file_name after the root component.
 
769
 */
 
770
G_CONST_RETURN gchar*
 
771
g_path_skip_root (const gchar *file_name)
 
772
{
 
773
  g_return_val_if_fail (file_name != NULL, NULL);
 
774
  
 
775
#ifdef G_PLATFORM_WIN32
 
776
  /* Skip \\server\share or //server/share */
 
777
  if (G_IS_DIR_SEPARATOR (file_name[0]) &&
 
778
      G_IS_DIR_SEPARATOR (file_name[1]) &&
 
779
      file_name[2] &&
 
780
      !G_IS_DIR_SEPARATOR (file_name[2]))
 
781
    {
 
782
      gchar *p;
 
783
 
 
784
      p = strchr (file_name + 2, G_DIR_SEPARATOR);
 
785
#ifdef G_OS_WIN32
 
786
      {
 
787
        gchar *q = strchr (file_name + 2, '/');
 
788
        if (p == NULL || (q != NULL && q < p))
 
789
          p = q;
 
790
      }
 
791
#endif
 
792
      if (p &&
 
793
          p > file_name + 2 &&
 
794
          p[1])
 
795
        {
 
796
          file_name = p + 1;
 
797
 
 
798
          while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
 
799
            file_name++;
 
800
 
 
801
          /* Possibly skip a backslash after the share name */
 
802
          if (G_IS_DIR_SEPARATOR (file_name[0]))
 
803
            file_name++;
 
804
 
 
805
          return (gchar *)file_name;
 
806
        }
 
807
    }
 
808
#endif
 
809
  
 
810
  /* Skip initial slashes */
 
811
  if (G_IS_DIR_SEPARATOR (file_name[0]))
 
812
    {
 
813
      while (G_IS_DIR_SEPARATOR (file_name[0]))
 
814
        file_name++;
 
815
      return (gchar *)file_name;
 
816
    }
 
817
 
 
818
#ifdef G_OS_WIN32
 
819
  /* Skip X:\ */
 
820
  if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
 
821
    return (gchar *)file_name + 3;
 
822
#endif
 
823
 
 
824
  return NULL;
 
825
}
 
826
 
 
827
/**
 
828
 * g_path_get_dirname:
 
829
 * @file_name: the name of the file.
 
830
 *
 
831
 * Gets the directory components of a file name.  If the file name has no
 
832
 * directory components "." is returned.  The returned string should be
 
833
 * freed when no longer needed.
 
834
 * 
 
835
 * Returns: the directory components of the file.
 
836
 */
 
837
gchar*
 
838
g_path_get_dirname (const gchar    *file_name)
 
839
{
 
840
  register gchar *base;
 
841
  register gsize len;    
 
842
  
 
843
  g_return_val_if_fail (file_name != NULL, NULL);
 
844
  
 
845
  base = strrchr (file_name, G_DIR_SEPARATOR);
 
846
#ifdef G_OS_WIN32
 
847
  {
 
848
    gchar *q = strrchr (file_name, '/');
 
849
    if (base == NULL || (q != NULL && q > base))
 
850
        base = q;
 
851
  }
 
852
#endif
 
853
  if (!base)
 
854
    {
 
855
#ifdef G_OS_WIN32
 
856
      if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
 
857
        {
 
858
          gchar drive_colon_dot[4];
 
859
 
 
860
          drive_colon_dot[0] = file_name[0];
 
861
          drive_colon_dot[1] = ':';
 
862
          drive_colon_dot[2] = '.';
 
863
          drive_colon_dot[3] = '\0';
 
864
 
 
865
          return g_strdup (drive_colon_dot);
 
866
        }
 
867
#endif
 
868
    return g_strdup (".");
 
869
    }
 
870
 
 
871
  while (base > file_name && G_IS_DIR_SEPARATOR (*base))
 
872
    base--;
 
873
 
 
874
#ifdef G_OS_WIN32
 
875
  /* base points to the char before the last slash.
 
876
   *
 
877
   * In case file_name is the root of a drive (X:\) or a child of the
 
878
   * root of a drive (X:\foo), include the slash.
 
879
   *
 
880
   * In case file_name is the root share of an UNC path
 
881
   * (\\server\share), add a slash, returning \\server\share\ .
 
882
   *
 
883
   * In case file_name is a direct child of a share in an UNC path
 
884
   * (\\server\share\foo), include the slash after the share name,
 
885
   * returning \\server\share\ .
 
886
   */
 
887
  if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
 
888
    base++;
 
889
  else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
 
890
           G_IS_DIR_SEPARATOR (file_name[1]) &&
 
891
           file_name[2] &&
 
892
           !G_IS_DIR_SEPARATOR (file_name[2]) &&
 
893
           base >= file_name + 2)
 
894
    {
 
895
      const gchar *p = file_name + 2;
 
896
      while (*p && !G_IS_DIR_SEPARATOR (*p))
 
897
        p++;
 
898
      if (p == base + 1)
 
899
        {
 
900
          len = (guint) strlen (file_name) + 1;
 
901
          base = g_new (gchar, len + 1);
 
902
          strcpy (base, file_name);
 
903
          base[len-1] = G_DIR_SEPARATOR;
 
904
          base[len] = 0;
 
905
          return base;
 
906
        }
 
907
      if (G_IS_DIR_SEPARATOR (*p))
 
908
        {
 
909
          p++;
 
910
          while (*p && !G_IS_DIR_SEPARATOR (*p))
 
911
            p++;
 
912
          if (p == base + 1)
 
913
            base++;
 
914
        }
 
915
    }
 
916
#endif
 
917
 
 
918
  len = (guint) 1 + base - file_name;
 
919
  
 
920
  base = g_new (gchar, len + 1);
 
921
  g_memmove (base, file_name, len);
 
922
  base[len] = 0;
 
923
  
 
924
  return base;
 
925
}
 
926
 
 
927
/**
 
928
 * g_get_current_dir:
 
929
 *
 
930
 * Gets the current directory.
 
931
 * The returned string should be freed when no longer needed. The encoding 
 
932
 * of the returned string is system defined. On Windows, it is always UTF-8.
 
933
 * 
 
934
 * Returns: the current directory.
 
935
 */
 
936
gchar*
 
937
g_get_current_dir (void)
 
938
{
 
939
#ifdef G_OS_WIN32
 
940
 
 
941
  gchar *dir = NULL;
 
942
 
 
943
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
944
    {
 
945
      wchar_t dummy[2], *wdir;
 
946
      int len;
 
947
 
 
948
      len = GetCurrentDirectoryW (2, dummy);
 
949
      wdir = g_new (wchar_t, len);
 
950
 
 
951
      if (GetCurrentDirectoryW (len, wdir) == len - 1)
 
952
        dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
 
953
 
 
954
      g_free (wdir);
 
955
    }
 
956
  else
 
957
    {
 
958
      gchar dummy[2], *cpdir;
 
959
      int len;
 
960
 
 
961
      len = GetCurrentDirectoryA (2, dummy);
 
962
      cpdir = g_new (gchar, len);
 
963
 
 
964
      if (GetCurrentDirectoryA (len, cpdir) == len - 1)
 
965
        dir = g_locale_to_utf8 (cpdir, -1, NULL, NULL, NULL);
 
966
 
 
967
      g_free (cpdir);
 
968
    }
 
969
 
 
970
  if (dir == NULL)
 
971
    dir = g_strdup ("\\");
 
972
 
 
973
  return dir;
 
974
 
 
975
#else
 
976
 
 
977
  gchar *buffer = NULL;
 
978
  gchar *dir = NULL;
 
979
  static gulong max_len = 0;
 
980
 
 
981
  if (max_len == 0) 
 
982
    max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
 
983
  
 
984
  /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
 
985
   * and, if that wasn't bad enough, hangs in doing so.
 
986
   */
 
987
#if     (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
 
988
  buffer = g_new (gchar, max_len + 1);
 
989
  *buffer = 0;
 
990
  dir = getwd (buffer);
 
991
#else   /* !sun || !HAVE_GETCWD */
 
992
  while (max_len < G_MAXULONG / 2)
 
993
    {
 
994
      buffer = g_new (gchar, max_len + 1);
 
995
      *buffer = 0;
 
996
      dir = getcwd (buffer, max_len);
 
997
 
 
998
      if (dir || errno != ERANGE)
 
999
        break;
 
1000
 
 
1001
      g_free (buffer);
 
1002
      max_len *= 2;
 
1003
    }
 
1004
#endif  /* !sun || !HAVE_GETCWD */
 
1005
  
 
1006
  if (!dir || !*buffer)
 
1007
    {
 
1008
      /* hm, should we g_error() out here?
 
1009
       * this can happen if e.g. "./" has mode \0000
 
1010
       */
 
1011
      buffer[0] = G_DIR_SEPARATOR;
 
1012
      buffer[1] = 0;
 
1013
    }
 
1014
 
 
1015
  dir = g_strdup (buffer);
 
1016
  g_free (buffer);
 
1017
  
 
1018
  return dir;
 
1019
#endif /* !Win32 */
 
1020
}
 
1021
 
 
1022
/**
 
1023
 * g_getenv:
 
1024
 * @variable: the environment variable to get, in the GLib file name encoding.
 
1025
 * 
 
1026
 * Returns the value of an environment variable. The name and value
 
1027
 * are in the GLib file name encoding. On UNIX, this means the actual
 
1028
 * bytes which might or might not be in some consistent character set
 
1029
 * and encoding. On Windows, it is in UTF-8. On Windows, in case the
 
1030
 * environment variable's value contains references to other
 
1031
 * environment variables, they are expanded.
 
1032
 * 
 
1033
 * Return value: the value of the environment variable, or %NULL if
 
1034
 * the environment variable is not found. The returned string may be
 
1035
 * overwritten by the next call to g_getenv(), g_setenv() or
 
1036
 * g_unsetenv().
 
1037
 **/
 
1038
G_CONST_RETURN gchar*
 
1039
g_getenv (const gchar *variable)
 
1040
{
 
1041
#ifndef G_OS_WIN32
 
1042
 
 
1043
  g_return_val_if_fail (variable != NULL, NULL);
 
1044
 
 
1045
  return getenv (variable);
 
1046
 
 
1047
#else /* G_OS_WIN32 */
 
1048
 
 
1049
  GQuark quark;
 
1050
  gchar *value;
 
1051
 
 
1052
  g_return_val_if_fail (variable != NULL, NULL);
 
1053
  g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
 
1054
 
 
1055
  /* On Windows NT, it is relatively typical that environment
 
1056
   * variables contain references to other environment variables. If
 
1057
   * so, use ExpandEnvironmentStrings(). (In an ideal world, such
 
1058
   * environment variables would be stored in the Registry as
 
1059
   * REG_EXPAND_SZ type values, and would then get automatically
 
1060
   * expanded before a program sees them. But there is broken software
 
1061
   * that stores environment variables as REG_SZ values even if they
 
1062
   * contain references to other environment variables.)
 
1063
   */
 
1064
 
 
1065
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
1066
    {
 
1067
      wchar_t dummy[2], *wname, *wvalue;
 
1068
      int len;
 
1069
      
 
1070
      wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
 
1071
 
 
1072
      len = GetEnvironmentVariableW (wname, dummy, 2);
 
1073
 
 
1074
      if (len == 0)
 
1075
        {
 
1076
          g_free (wname);
 
1077
          return NULL;
 
1078
        }
 
1079
      else if (len == 1)
 
1080
        len = 2;
 
1081
 
 
1082
      wvalue = g_new (wchar_t, len);
 
1083
 
 
1084
      if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
 
1085
        {
 
1086
          g_free (wname);
 
1087
          g_free (wvalue);
 
1088
          return NULL;
 
1089
        }
 
1090
 
 
1091
      if (wcschr (wvalue, L'%') != NULL)
 
1092
        {
 
1093
          wchar_t *tem = wvalue;
 
1094
 
 
1095
          len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
 
1096
 
 
1097
          if (len > 0)
 
1098
            {
 
1099
              wvalue = g_new (wchar_t, len);
 
1100
 
 
1101
              if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
 
1102
                {
 
1103
                  g_free (wvalue);
 
1104
                  wvalue = tem;
 
1105
                }
 
1106
              else
 
1107
                g_free (tem);
 
1108
            }
 
1109
        }
 
1110
 
 
1111
      value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
 
1112
 
 
1113
      g_free (wname);
 
1114
      g_free (wvalue);
 
1115
    }
 
1116
  else
 
1117
    {
 
1118
      gchar dummy[3], *cpname, *cpvalue;
 
1119
      int len;
 
1120
      
 
1121
      cpname = g_locale_from_utf8 (variable, -1, NULL, NULL, NULL);
 
1122
 
 
1123
      g_return_val_if_fail (cpname != NULL, NULL);
 
1124
 
 
1125
      len = GetEnvironmentVariableA (cpname, dummy, 2);
 
1126
 
 
1127
      if (len == 0)
 
1128
        {
 
1129
          g_free (cpname);
 
1130
          return NULL;
 
1131
        }
 
1132
      else if (len == 1)
 
1133
        len = 2;
 
1134
 
 
1135
      cpvalue = g_new (gchar, len);
 
1136
 
 
1137
      if (GetEnvironmentVariableA (cpname, cpvalue, len) != len - 1)
 
1138
        {
 
1139
          g_free (cpname);
 
1140
          g_free (cpvalue);
 
1141
          return NULL;
 
1142
        }
 
1143
 
 
1144
      if (strchr (cpvalue, '%') != NULL)
 
1145
        {
 
1146
          gchar *tem = cpvalue;
 
1147
 
 
1148
          len = ExpandEnvironmentStringsA (cpvalue, dummy, 3);
 
1149
 
 
1150
          if (len > 0)
 
1151
            {
 
1152
              cpvalue = g_new (gchar, len);
 
1153
 
 
1154
              if (ExpandEnvironmentStringsA (tem, cpvalue, len) != len)
 
1155
                {
 
1156
                  g_free (cpvalue);
 
1157
                  cpvalue = tem;
 
1158
                }
 
1159
              else
 
1160
                g_free (tem);
 
1161
            }
 
1162
        }
 
1163
 
 
1164
      value = g_locale_to_utf8 (cpvalue, -1, NULL, NULL, NULL);
 
1165
 
 
1166
      g_free (cpname);
 
1167
      g_free (cpvalue);
 
1168
    }
 
1169
 
 
1170
  quark = g_quark_from_string (value);
 
1171
  g_free (value);
 
1172
  
 
1173
  return g_quark_to_string (quark);
 
1174
 
 
1175
#endif /* G_OS_WIN32 */
 
1176
}
 
1177
 
 
1178
/* _g_getenv_nomalloc
 
1179
 * this function does a getenv() without doing any kind of allocation
 
1180
 * through glib. it's suitable for chars <= 127 only (both, for the
 
1181
 * variable name and the contents) and for contents < 1024 chars in
 
1182
 * length. also, it aliases "" to a NULL return value.
 
1183
 **/
 
1184
const gchar*
 
1185
_g_getenv_nomalloc (const gchar *variable,
 
1186
                    gchar        buffer[1024])
 
1187
{
 
1188
  const gchar *retval = getenv (variable);
 
1189
  if (retval && retval[0])
 
1190
    {
 
1191
      gint l = strlen (retval);
 
1192
      if (l < 1024)
 
1193
        {
 
1194
          strncpy (buffer, retval, l);
 
1195
          buffer[l] = 0;
 
1196
          return buffer;
 
1197
        }
 
1198
    }
 
1199
  return NULL;
 
1200
}
 
1201
 
 
1202
/**
 
1203
 * g_setenv:
 
1204
 * @variable: the environment variable to set, must not contain '='.
 
1205
 * @value: the value for to set the variable to.
 
1206
 * @overwrite: whether to change the variable if it already exists.
 
1207
 *
 
1208
 * Sets an environment variable. Both the variable's name and value
 
1209
 * should be in the GLib file name encoding. On UNIX, this means that
 
1210
 * they can be any sequence of bytes. On Windows, they should be in
 
1211
 * UTF-8.
 
1212
 *
 
1213
 * Note that on some systems, when variables are overwritten, the memory 
 
1214
 * used for the previous variables and its value isn't reclaimed.
 
1215
 *
 
1216
 * Returns: %FALSE if the environment variable couldn't be set.
 
1217
 *
 
1218
 * Since: 2.4
 
1219
 */
 
1220
gboolean
 
1221
g_setenv (const gchar *variable, 
 
1222
          const gchar *value, 
 
1223
          gboolean     overwrite)
 
1224
{
 
1225
#ifndef G_OS_WIN32
 
1226
 
 
1227
  gint result;
 
1228
#ifndef HAVE_SETENV
 
1229
  gchar *string;
 
1230
#endif
 
1231
 
 
1232
  g_return_val_if_fail (variable != NULL, FALSE);
 
1233
  g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
 
1234
 
 
1235
#ifdef HAVE_SETENV
 
1236
  result = setenv (variable, value, overwrite);
 
1237
#else
 
1238
  if (!overwrite && getenv (variable) != NULL)
 
1239
    return TRUE;
 
1240
  
 
1241
  /* This results in a leak when you overwrite existing
 
1242
   * settings. It would be fairly easy to fix this by keeping
 
1243
   * our own parallel array or hash table.
 
1244
   */
 
1245
  string = g_strconcat (variable, "=", value, NULL);
 
1246
  result = putenv (string);
 
1247
#endif
 
1248
  return result == 0;
 
1249
 
 
1250
#else /* G_OS_WIN32 */
 
1251
 
 
1252
  gboolean retval;
 
1253
 
 
1254
  g_return_val_if_fail (variable != NULL, FALSE);
 
1255
  g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
 
1256
  g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
 
1257
  g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
 
1258
 
 
1259
  if (!overwrite && g_getenv (variable) != NULL)
 
1260
    return TRUE;
 
1261
 
 
1262
  /* We want to (if possible) set both the environment variable copy
 
1263
   * kept by the C runtime and the one kept by the system.
 
1264
   *
 
1265
   * We can't use only the C runtime's putenv or _wputenv() as that
 
1266
   * won't work for arbitrary Unicode strings in a "non-Unicode" app
 
1267
   * (with main() and not wmain()). In a "main()" app the C runtime
 
1268
   * initializes the C runtime's environment table by converting the
 
1269
   * real (wide char) environment variables to system codepage, thus
 
1270
   * breaking those that aren't representable in the system codepage.
 
1271
   *
 
1272
   * As the C runtime's putenv() will also set the system copy, we do
 
1273
   * the putenv() first, then call SetEnvironmentValueW ourselves.
 
1274
   */
 
1275
 
 
1276
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
1277
    {
 
1278
      wchar_t *wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
 
1279
      wchar_t *wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
 
1280
      gchar *tem = g_strconcat (variable, "=", value, NULL);
 
1281
      wchar_t *wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
 
1282
      
 
1283
      g_free (tem);
 
1284
      _wputenv (wassignment);
 
1285
      g_free (wassignment);
 
1286
 
 
1287
      retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
 
1288
 
 
1289
      g_free (wname);
 
1290
      g_free (wvalue);
 
1291
    }
 
1292
  else
 
1293
    {
 
1294
      /* In the non-Unicode case (Win9x), just putenv() is good
 
1295
       * enough.
 
1296
       */
 
1297
      gchar *tem = g_strconcat (variable, "=", value, NULL);
 
1298
      gchar *cpassignment = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
 
1299
 
 
1300
      g_free (tem);
 
1301
      
 
1302
      retval = (putenv (cpassignment) == 0);
 
1303
 
 
1304
      g_free (cpassignment);
 
1305
    }
 
1306
 
 
1307
  return retval;
 
1308
 
 
1309
#endif /* G_OS_WIN32 */
 
1310
}
 
1311
 
 
1312
#ifdef HAVE__NSGETENVIRON
 
1313
#define environ (*_NSGetEnviron())
 
1314
#elif !defined(G_OS_WIN32)
 
1315
 
 
1316
/* According to the Single Unix Specification, environ is not in 
 
1317
 * any system header, although unistd.h often declares it.
 
1318
 */
 
1319
extern char **environ;
 
1320
#endif
 
1321
 
 
1322
/**
 
1323
 * g_unsetenv:
 
1324
 * @variable: the environment variable to remove, must not contain '='.
 
1325
 * 
 
1326
 * Removes an environment variable from the environment.
 
1327
 *
 
1328
 * Note that on some systems, when variables are overwritten, the memory 
 
1329
 * used for the previous variables and its value isn't reclaimed.
 
1330
 * Furthermore, this function can't be guaranteed to operate in a 
 
1331
 * threadsafe way.
 
1332
 *
 
1333
 * Since: 2.4 
 
1334
 **/
 
1335
void
 
1336
g_unsetenv (const gchar *variable)
 
1337
{
 
1338
#ifndef G_OS_WIN32
 
1339
 
 
1340
#ifdef HAVE_UNSETENV
 
1341
  g_return_if_fail (variable != NULL);
 
1342
  g_return_if_fail (strchr (variable, '=') == NULL);
 
1343
 
 
1344
  unsetenv (variable);
 
1345
#else /* !HAVE_UNSETENV */
 
1346
  int len;
 
1347
  gchar **e, **f;
 
1348
 
 
1349
  g_return_if_fail (variable != NULL);
 
1350
  g_return_if_fail (strchr (variable, '=') == NULL);
 
1351
 
 
1352
  len = strlen (variable);
 
1353
  
 
1354
  /* Mess directly with the environ array.
 
1355
   * This seems to be the only portable way to do this.
 
1356
   *
 
1357
   * Note that we remove *all* environment entries for
 
1358
   * the variable name, not just the first.
 
1359
   */
 
1360
  e = f = environ;
 
1361
  while (*e != NULL) 
 
1362
    {
 
1363
      if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=') 
 
1364
        {
 
1365
          *f = *e;
 
1366
          f++;
 
1367
        }
 
1368
      e++;
 
1369
    }
 
1370
  *f = NULL;
 
1371
#endif /* !HAVE_UNSETENV */
 
1372
 
 
1373
#else  /* G_OS_WIN32 */
 
1374
 
 
1375
  g_return_if_fail (variable != NULL);
 
1376
  g_return_if_fail (strchr (variable, '=') == NULL);
 
1377
  g_return_if_fail (g_utf8_validate (variable, -1, NULL));
 
1378
 
 
1379
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
1380
    {
 
1381
      wchar_t *wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
 
1382
      gchar *tem = g_strconcat (variable, "=", NULL);
 
1383
      wchar_t *wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
 
1384
      
 
1385
      g_free (tem);
 
1386
      _wputenv (wassignment);
 
1387
      g_free (wassignment);
 
1388
 
 
1389
      SetEnvironmentVariableW (wname, NULL);
 
1390
 
 
1391
      g_free (wname);
 
1392
    }
 
1393
  else
 
1394
    {
 
1395
      /* In the non-Unicode case (Win9x), just putenv() is good
 
1396
       * enough.
 
1397
       */
 
1398
      gchar *tem = g_strconcat (variable, "=", NULL);
 
1399
      gchar *cpassignment = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
 
1400
 
 
1401
      g_free (tem);
 
1402
      
 
1403
      putenv (cpassignment);
 
1404
 
 
1405
      g_free (cpassignment);
 
1406
    }
 
1407
 
 
1408
#endif /* G_OS_WIN32 */
 
1409
}
 
1410
 
 
1411
/**
 
1412
 * g_listenv:
 
1413
 *
 
1414
 * Gets the names of all variables set in the environment.
 
1415
 * 
 
1416
 * Returns: a %NULL-terminated list of strings which must be freed
 
1417
 * with g_strfreev().
 
1418
 *
 
1419
 * Programs that want to be portable to Windows should typically use
 
1420
 * this function and g_getenv() instead of using the environ array
 
1421
 * from the C library directly. On Windows, the strings in the environ
 
1422
 * array are in system codepage encoding, while in most of the typical
 
1423
 * use cases for environment variables in GLib-using programs you want
 
1424
 * the UTF-8 encoding that this function and g_getenv() provide.
 
1425
 *
 
1426
 * Since: 2.8
 
1427
 */
 
1428
gchar **
 
1429
g_listenv (void)
 
1430
{
 
1431
#ifndef G_OS_WIN32
 
1432
  gchar **result, *eq;
 
1433
  gint len, i, j;
 
1434
 
 
1435
  len = g_strv_length (environ);
 
1436
  result = g_new0 (gchar *, len + 1);
 
1437
  
 
1438
  j = 0;
 
1439
  for (i = 0; i < len; i++)
 
1440
    {
 
1441
      eq = strchr (environ[i], '=');
 
1442
      if (eq)
 
1443
        result[j++] = g_strndup (environ[i], eq - environ[i]);
 
1444
    }
 
1445
 
 
1446
  result[j] = NULL;
 
1447
 
 
1448
  return result;
 
1449
#else
 
1450
  gchar **result, *eq;
 
1451
  gint len = 0, i, j;
 
1452
 
 
1453
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
1454
    {
 
1455
      wchar_t *p, *q;
 
1456
 
 
1457
      p = (wchar_t *) GetEnvironmentStringsW ();
 
1458
      if (p != NULL)
 
1459
        {
 
1460
          q = p;
 
1461
          while (*q)
 
1462
            {
 
1463
              q += wcslen (q) + 1;
 
1464
              len++;
 
1465
            }
 
1466
        }
 
1467
      result = g_new0 (gchar *, len + 1);
 
1468
 
 
1469
      j = 0;
 
1470
      q = p;
 
1471
      while (*q)
 
1472
        {
 
1473
          result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
 
1474
          if (result[j] != NULL)
 
1475
            {
 
1476
              eq = strchr (result[j], '=');
 
1477
              if (eq && eq > result[j])
 
1478
                {
 
1479
                  *eq = '\0';
 
1480
                  j++;
 
1481
                }
 
1482
              else
 
1483
                g_free (result[j]);
 
1484
            }
 
1485
          q += wcslen (q) + 1;
 
1486
        }
 
1487
      result[j] = NULL;
 
1488
      FreeEnvironmentStringsW (p);
 
1489
    }
 
1490
  else
 
1491
    {
 
1492
      len = g_strv_length (environ);
 
1493
      result = g_new0 (gchar *, len + 1);
 
1494
      
 
1495
      j = 0;
 
1496
      for (i = 0; i < len; i++)
 
1497
        {
 
1498
          result[j] = g_locale_to_utf8 (environ[i], -1, NULL, NULL, NULL);
 
1499
          if (result[j] != NULL)
 
1500
            {
 
1501
              eq = strchr (result[j], '=');
 
1502
              if (eq && eq > result[j])
 
1503
                {
 
1504
                  *eq = '\0';
 
1505
                  j++;
 
1506
                }
 
1507
              else
 
1508
                g_free (result[j]);
 
1509
            }
 
1510
        }
 
1511
      result[j] = NULL;
 
1512
    }
 
1513
 
 
1514
  return result;
 
1515
#endif
 
1516
}
 
1517
 
 
1518
G_LOCK_DEFINE_STATIC (g_utils_global);
 
1519
 
 
1520
static  gchar   *g_tmp_dir = NULL;
 
1521
static  gchar   *g_user_name = NULL;
 
1522
static  gchar   *g_real_name = NULL;
 
1523
static  gchar   *g_home_dir = NULL;
 
1524
static  gchar   *g_host_name = NULL;
 
1525
 
 
1526
#ifdef G_OS_WIN32
 
1527
/* System codepage versions of the above, kept at file level so that they,
 
1528
 * too, are produced only once.
 
1529
 */
 
1530
static  gchar   *g_tmp_dir_cp = NULL;
 
1531
static  gchar   *g_user_name_cp = NULL;
 
1532
static  gchar   *g_real_name_cp = NULL;
 
1533
static  gchar   *g_home_dir_cp = NULL;
 
1534
#endif
 
1535
 
 
1536
static  gchar   *g_user_data_dir = NULL;
 
1537
static  gchar  **g_system_data_dirs = NULL;
 
1538
static  gchar   *g_user_cache_dir = NULL;
 
1539
static  gchar   *g_user_config_dir = NULL;
 
1540
static  gchar  **g_system_config_dirs = NULL;
 
1541
 
 
1542
#ifdef G_OS_WIN32
 
1543
 
 
1544
static gchar *
 
1545
get_special_folder (int csidl)
 
1546
{
 
1547
  union {
 
1548
    char c[MAX_PATH+1];
 
1549
    wchar_t wc[MAX_PATH+1];
 
1550
  } path;
 
1551
  HRESULT hr;
 
1552
  LPITEMIDLIST pidl = NULL;
 
1553
  BOOL b;
 
1554
  gchar *retval = NULL;
 
1555
 
 
1556
  hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
 
1557
  if (hr == S_OK)
 
1558
    {
 
1559
      if (G_WIN32_HAVE_WIDECHAR_API ())
 
1560
        {
 
1561
          b = SHGetPathFromIDListW (pidl, path.wc);
 
1562
          if (b)
 
1563
            retval = g_utf16_to_utf8 (path.wc, -1, NULL, NULL, NULL);
 
1564
        }
 
1565
      else
 
1566
        {
 
1567
          b = SHGetPathFromIDListA (pidl, path.c);
 
1568
          if (b)
 
1569
            retval = g_locale_to_utf8 (path.c, -1, NULL, NULL, NULL);
 
1570
        }
 
1571
      CoTaskMemFree (pidl);
 
1572
    }
 
1573
  return retval;
 
1574
}
 
1575
 
 
1576
static char *
 
1577
get_windows_directory_root (void)
 
1578
{
 
1579
  char windowsdir[MAX_PATH];
 
1580
 
 
1581
  if (GetWindowsDirectory (windowsdir, sizeof (windowsdir)))
 
1582
    {
 
1583
      /* Usually X:\Windows, but in terminal server environments
 
1584
       * might be an UNC path, AFAIK.
 
1585
       */
 
1586
      char *p = (char *) g_path_skip_root (windowsdir);
 
1587
      if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
 
1588
        p--;
 
1589
      *p = '\0';
 
1590
      return g_strdup (windowsdir);
 
1591
    }
 
1592
  else
 
1593
    return g_strdup ("C:\\");
 
1594
}
 
1595
 
 
1596
#endif
 
1597
 
 
1598
/* HOLDS: g_utils_global_lock */
 
1599
static void
 
1600
g_get_any_init_do (void)
 
1601
{
 
1602
  gchar hostname[100];
 
1603
 
 
1604
  g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
 
1605
  if (!g_tmp_dir)
 
1606
    g_tmp_dir = g_strdup (g_getenv ("TMP"));
 
1607
  if (!g_tmp_dir)
 
1608
    g_tmp_dir = g_strdup (g_getenv ("TEMP"));
 
1609
 
 
1610
#ifdef G_OS_WIN32
 
1611
  if (!g_tmp_dir)
 
1612
    g_tmp_dir = get_windows_directory_root ();
 
1613
#else  
 
1614
#ifdef P_tmpdir
 
1615
  if (!g_tmp_dir)
 
1616
    {
 
1617
      gsize k;    
 
1618
      g_tmp_dir = g_strdup (P_tmpdir);
 
1619
      k = strlen (g_tmp_dir);
 
1620
      if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
 
1621
        g_tmp_dir[k - 1] = '\0';
 
1622
    }
 
1623
#endif
 
1624
  
 
1625
  if (!g_tmp_dir)
 
1626
    {
 
1627
      g_tmp_dir = g_strdup ("/tmp");
 
1628
    }
 
1629
#endif  /* !G_OS_WIN32 */
 
1630
  
 
1631
#ifdef G_OS_WIN32
 
1632
  /* We check $HOME first for Win32, though it is a last resort for Unix
 
1633
   * where we prefer the results of getpwuid().
 
1634
   */
 
1635
  g_home_dir = g_strdup (g_getenv ("HOME"));
 
1636
 
 
1637
  /* Only believe HOME if it is an absolute path and exists */
 
1638
  if (g_home_dir)
 
1639
    {
 
1640
      if (!(g_path_is_absolute (g_home_dir) &&
 
1641
            g_file_test (g_home_dir, G_FILE_TEST_IS_DIR)))
 
1642
        {
 
1643
          g_free (g_home_dir);
 
1644
          g_home_dir = NULL;
 
1645
        }
 
1646
    }
 
1647
  
 
1648
  /* In case HOME is Unix-style (it happens), convert it to
 
1649
   * Windows style.
 
1650
   */
 
1651
  if (g_home_dir)
 
1652
    {
 
1653
      gchar *p;
 
1654
      while ((p = strchr (g_home_dir, '/')) != NULL)
 
1655
        *p = '\\';
 
1656
    }
 
1657
 
 
1658
  if (!g_home_dir)
 
1659
    {
 
1660
      /* USERPROFILE is probably the closest equivalent to $HOME? */
 
1661
      if (g_getenv ("USERPROFILE") != NULL)
 
1662
        g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
 
1663
    }
 
1664
 
 
1665
  if (!g_home_dir)
 
1666
    g_home_dir = get_special_folder (CSIDL_PROFILE);
 
1667
  
 
1668
  if (!g_home_dir)
 
1669
    g_home_dir = get_windows_directory_root ();
 
1670
#endif /* G_OS_WIN32 */
 
1671
  
 
1672
#ifdef HAVE_PWD_H
 
1673
  {
 
1674
    struct passwd *pw = NULL;
 
1675
    gpointer buffer = NULL;
 
1676
    gint error;
 
1677
    gchar *logname;
 
1678
 
 
1679
#  if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
 
1680
    struct passwd pwd;
 
1681
#    ifdef _SC_GETPW_R_SIZE_MAX  
 
1682
    /* This reurns the maximum length */
 
1683
    glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
 
1684
    
 
1685
    if (bufsize < 0)
 
1686
      bufsize = 64;
 
1687
#    else /* _SC_GETPW_R_SIZE_MAX */
 
1688
    glong bufsize = 64;
 
1689
#    endif /* _SC_GETPW_R_SIZE_MAX */
 
1690
 
 
1691
    logname = (gchar *) g_getenv ("LOGNAME");
 
1692
        
 
1693
    do
 
1694
      {
 
1695
        g_free (buffer);
 
1696
        /* we allocate 6 extra bytes to work around a bug in 
 
1697
         * Mac OS < 10.3. See #156446
 
1698
         */
 
1699
        buffer = g_malloc (bufsize + 6);
 
1700
        errno = 0;
 
1701
        
 
1702
#    ifdef HAVE_POSIX_GETPWUID_R
 
1703
        if (logname) {
 
1704
          error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
 
1705
          if (!pw || (pw->pw_uid != getuid ())) {
 
1706
            /* LOGNAME is lying, fall back to looking up the uid */
 
1707
            error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
 
1708
          }
 
1709
        } else {
 
1710
          error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
 
1711
        }
 
1712
        error = error < 0 ? errno : error;
 
1713
#    else /* HAVE_NONPOSIX_GETPWUID_R */
 
1714
   /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
 
1715
#      if defined(_AIX) || defined(__hpux)
 
1716
        error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
 
1717
        pw = error == 0 ? &pwd : NULL;
 
1718
#      else /* !_AIX */
 
1719
        if (logname) {
 
1720
          pw = getpwnam_r (logname, &pwd, buffer, bufsize);
 
1721
          if (!pw || (pw->pw_uid != getuid ())) {
 
1722
            /* LOGNAME is lying, fall back to looking up the uid */
 
1723
            pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
 
1724
          }
 
1725
        } else {
 
1726
          pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
 
1727
        }
 
1728
        error = pw ? 0 : errno;
 
1729
#      endif /* !_AIX */            
 
1730
#    endif /* HAVE_NONPOSIX_GETPWUID_R */
 
1731
        
 
1732
        if (!pw)
 
1733
          {
 
1734
            /* we bail out prematurely if the user id can't be found
 
1735
             * (should be pretty rare case actually), or if the buffer
 
1736
             * should be sufficiently big and lookups are still not
 
1737
             * successfull.
 
1738
             */
 
1739
            if (error == 0 || error == ENOENT)
 
1740
              {
 
1741
                g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
 
1742
                           (gulong) getuid ());
 
1743
                break;
 
1744
              }
 
1745
            if (bufsize > 32 * 1024)
 
1746
              {
 
1747
                g_warning ("getpwuid_r(): failed due to: %s.",
 
1748
                           g_strerror (error));
 
1749
                break;
 
1750
              }
 
1751
            
 
1752
            bufsize *= 2;
 
1753
          }
 
1754
      }
 
1755
    while (!pw);
 
1756
#  endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
 
1757
    
 
1758
    if (!pw)
 
1759
      {
 
1760
        setpwent ();
 
1761
        pw = getpwuid (getuid ());
 
1762
        endpwent ();
 
1763
      }
 
1764
    if (pw)
 
1765
      {
 
1766
        g_user_name = g_strdup (pw->pw_name);
 
1767
 
 
1768
        if (pw->pw_gecos && *pw->pw_gecos != '\0') 
 
1769
          {
 
1770
            gchar **gecos_fields;
 
1771
            gchar **name_parts;
 
1772
 
 
1773
            /* split the gecos field and substitute '&' */
 
1774
            gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
 
1775
            name_parts = g_strsplit (gecos_fields[0], "&", 0);
 
1776
            pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
 
1777
            g_real_name = g_strjoinv (pw->pw_name, name_parts);
 
1778
            g_strfreev (gecos_fields);
 
1779
            g_strfreev (name_parts);
 
1780
          }
 
1781
 
 
1782
        if (!g_home_dir)
 
1783
          g_home_dir = g_strdup (pw->pw_dir);
 
1784
      }
 
1785
    g_free (buffer);
 
1786
  }
 
1787
  
 
1788
#else /* !HAVE_PWD_H */
 
1789
  
 
1790
#ifdef G_OS_WIN32
 
1791
  if (G_WIN32_HAVE_WIDECHAR_API ())
 
1792
    {
 
1793
      guint len = UNLEN+1;
 
1794
      wchar_t buffer[UNLEN+1];
 
1795
      
 
1796
      if (GetUserNameW (buffer, (LPDWORD) &len))
 
1797
        {
 
1798
          g_user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
 
1799
          g_real_name = g_strdup (g_user_name);
 
1800
        }
 
1801
    }
 
1802
  else
 
1803
    {
 
1804
      guint len = UNLEN+1;
 
1805
      char buffer[UNLEN+1];
 
1806
      
 
1807
      if (GetUserNameA (buffer, (LPDWORD) &len))
 
1808
        {
 
1809
          g_user_name = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
 
1810
          g_real_name = g_strdup (g_user_name);
 
1811
        }
 
1812
    }
 
1813
#endif /* G_OS_WIN32 */
 
1814
 
 
1815
#endif /* !HAVE_PWD_H */
 
1816
 
 
1817
#ifndef G_OS_WIN32
 
1818
  if (!g_home_dir)
 
1819
    g_home_dir = g_strdup (g_getenv ("HOME"));
 
1820
#endif
 
1821
 
 
1822
#ifdef __EMX__
 
1823
  /* change '\\' in %HOME% to '/' */
 
1824
  g_strdelimit (g_home_dir, "\\",'/');
 
1825
#endif
 
1826
  if (!g_user_name)
 
1827
    g_user_name = g_strdup ("somebody");
 
1828
  if (!g_real_name)
 
1829
    g_real_name = g_strdup ("Unknown");
 
1830
 
 
1831
  {
 
1832
#ifndef G_OS_WIN32
 
1833
    gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
 
1834
#else
 
1835
    DWORD size = sizeof (hostname);
 
1836
    gboolean hostname_fail = (!GetComputerName (hostname, &size));
 
1837
#endif
 
1838
    g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
 
1839
  }
 
1840
 
 
1841
#ifdef G_OS_WIN32
 
1842
  g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
 
1843
  g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
 
1844
  g_real_name_cp = g_locale_from_utf8 (g_real_name, -1, NULL, NULL, NULL);
 
1845
 
 
1846
  if (!g_tmp_dir_cp)
 
1847
    g_tmp_dir_cp = g_strdup ("\\");
 
1848
  if (!g_user_name_cp)
 
1849
    g_user_name_cp = g_strdup ("somebody");
 
1850
  if (!g_real_name_cp)
 
1851
    g_real_name_cp = g_strdup ("Unknown");
 
1852
 
 
1853
  /* home_dir might be NULL, unlike tmp_dir, user_name and
 
1854
   * real_name.
 
1855
   */
 
1856
  if (g_home_dir)
 
1857
    g_home_dir_cp = g_locale_from_utf8 (g_home_dir, -1, NULL, NULL, NULL);
 
1858
  else
 
1859
    g_home_dir_cp = NULL;
 
1860
#endif /* G_OS_WIN32 */
 
1861
}
 
1862
 
 
1863
static inline void
 
1864
g_get_any_init (void)
 
1865
{
 
1866
  if (!g_tmp_dir)
 
1867
    g_get_any_init_do ();
 
1868
}
 
1869
 
 
1870
static inline void
 
1871
g_get_any_init_locked (void)
 
1872
{
 
1873
  G_LOCK (g_utils_global);
 
1874
  g_get_any_init ();
 
1875
  G_UNLOCK (g_utils_global);
 
1876
}
 
1877
 
 
1878
 
 
1879
/**
 
1880
 * g_get_user_name:
 
1881
 *
 
1882
 * Gets the user name of the current user. The encoding of the returned
 
1883
 * string is system-defined. On UNIX, it might be the preferred file name
 
1884
 * encoding, or something else, and there is no guarantee that it is even
 
1885
 * consistent on a machine. On Windows, it is always UTF-8.
 
1886
 *
 
1887
 * Returns: the user name of the current user.
 
1888
 */
 
1889
G_CONST_RETURN gchar*
 
1890
g_get_user_name (void)
 
1891
{
 
1892
  g_get_any_init_locked ();
 
1893
  return g_user_name;
 
1894
}
 
1895
 
 
1896
/**
 
1897
 * g_get_real_name:
 
1898
 *
 
1899
 * Gets the real name of the user. This usually comes from the user's entry 
 
1900
 * in the <filename>passwd</filename> file. The encoding of the returned 
 
1901
 * string is system-defined. (On Windows, it is, however, always UTF-8.) 
 
1902
 * If the real user name cannot be determined, the string "Unknown" is 
 
1903
 * returned.
 
1904
 *
 
1905
 * Returns: the user's real name.
 
1906
 */
 
1907
G_CONST_RETURN gchar*
 
1908
g_get_real_name (void)
 
1909
{
 
1910
  g_get_any_init_locked ();
 
1911
  return g_real_name;
 
1912
}
 
1913
 
 
1914
/**
 
1915
 * g_get_home_dir:
 
1916
 *
 
1917
 * Gets the current user's home directory. 
 
1918
 *
 
1919
 * Note that in contrast to traditional UNIX tools, this function 
 
1920
 * prefers <filename>passwd</filename> entries over the <envar>HOME</envar> 
 
1921
 * environment variable.
 
1922
 *
 
1923
 * Returns: the current user's home directory.
 
1924
 */
 
1925
G_CONST_RETURN gchar*
 
1926
g_get_home_dir (void)
 
1927
{
 
1928
  g_get_any_init_locked ();
 
1929
  return g_home_dir;
 
1930
}
 
1931
 
 
1932
/**
 
1933
 * g_get_tmp_dir:
 
1934
 *
 
1935
 * Gets the directory to use for temporary files. This is found from 
 
1936
 * inspecting the environment variables <envar>TMPDIR</envar>, 
 
1937
 * <envar>TMP</envar>, and <envar>TEMP</envar> in that order. If none 
 
1938
 * of those are defined "/tmp" is returned on UNIX and "C:\" on Windows. 
 
1939
 * The encoding of the returned string is system-defined. On Windows, 
 
1940
 * it is always UTF-8. The return value is never %NULL.
 
1941
 *
 
1942
 * Returns: the directory to use for temporary files.
 
1943
 */
 
1944
G_CONST_RETURN gchar*
 
1945
g_get_tmp_dir (void)
 
1946
{
 
1947
  g_get_any_init_locked ();
 
1948
  return g_tmp_dir;
 
1949
}
 
1950
 
 
1951
/**
 
1952
 * g_get_host_name:
 
1953
 *
 
1954
 * Return a name for the machine. 
 
1955
 *
 
1956
 * The returned name is not necessarily a fully-qualified domain name,
 
1957
 * or even present in DNS or some other name service at all. It need
 
1958
 * not even be unique on your local network or site, but usually it
 
1959
 * is. Callers should not rely on the return value having any specific
 
1960
 * properties like uniqueness for security purposes. Even if the name
 
1961
 * of the machine is changed while an application is running, the
 
1962
 * return value from this function does not change. The returned
 
1963
 * string is owned by GLib and should not be modified or freed. If no
 
1964
 * name can be determined, a default fixed string "localhost" is
 
1965
 * returned.
 
1966
 *
 
1967
 * Returns: the host name of the machine.
 
1968
 *
 
1969
 * Since: 2.8
 
1970
 */
 
1971
const gchar *
 
1972
g_get_host_name (void)
 
1973
{
 
1974
  g_get_any_init_locked ();
 
1975
  return g_host_name;
 
1976
}
 
1977
 
 
1978
G_LOCK_DEFINE_STATIC (g_prgname);
 
1979
static gchar *g_prgname = NULL;
 
1980
 
 
1981
/**
 
1982
 * g_get_prgname:
 
1983
 *
 
1984
 * Gets the name of the program. This name should <emphasis>not</emphasis> 
 
1985
 * be localized, contrast with g_get_application_name().
 
1986
 * (If you are using GDK or GTK+ the program name is set in gdk_init(), 
 
1987
 * which is called by gtk_init(). The program name is found by taking 
 
1988
 * the last component of <literal>argv[0]</literal>.)
 
1989
 *
 
1990
 * Returns: the name of the program. The returned string belongs 
 
1991
 * to GLib and must not be modified or freed.
 
1992
 */
 
1993
gchar*
 
1994
g_get_prgname (void)
 
1995
{
 
1996
  gchar* retval;
 
1997
 
 
1998
  G_LOCK (g_prgname);
 
1999
#ifdef G_OS_WIN32
 
2000
  if (g_prgname == NULL)
 
2001
    {
 
2002
      static gboolean beenhere = FALSE;
 
2003
 
 
2004
      if (!beenhere)
 
2005
        {
 
2006
          gchar *utf8_buf = NULL;
 
2007
 
 
2008
          beenhere = TRUE;
 
2009
          if (G_WIN32_HAVE_WIDECHAR_API ())
 
2010
            {
 
2011
              wchar_t buf[MAX_PATH+1];
 
2012
              if (GetModuleFileNameW (GetModuleHandle (NULL),
 
2013
                                      buf, G_N_ELEMENTS (buf)) > 0)
 
2014
                utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
 
2015
            }
 
2016
          else
 
2017
            {
 
2018
              gchar buf[MAX_PATH+1];
 
2019
              if (GetModuleFileNameA (GetModuleHandle (NULL),
 
2020
                                      buf, G_N_ELEMENTS (buf)) > 0)
 
2021
                utf8_buf = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
 
2022
            }
 
2023
          if (utf8_buf)
 
2024
            {
 
2025
              g_prgname = g_path_get_basename (utf8_buf);
 
2026
              g_free (utf8_buf);
 
2027
            }
 
2028
        }
 
2029
    }
 
2030
#endif
 
2031
  retval = g_prgname;
 
2032
  G_UNLOCK (g_prgname);
 
2033
 
 
2034
  return retval;
 
2035
}
 
2036
 
 
2037
/**
 
2038
 * g_set_prgname:
 
2039
 * @prgname: the name of the program.
 
2040
 *
 
2041
 * Sets the name of the program. This name should <emphasis>not</emphasis> 
 
2042
 * be localized, contrast with g_set_application_name(). Note that for 
 
2043
 * thread-safety reasons this function can only be called once.
 
2044
 */
 
2045
void
 
2046
g_set_prgname (const gchar *prgname)
 
2047
{
 
2048
  G_LOCK (g_prgname);
 
2049
  g_free (g_prgname);
 
2050
  g_prgname = g_strdup (prgname);
 
2051
  G_UNLOCK (g_prgname);
 
2052
}
 
2053
 
 
2054
G_LOCK_DEFINE_STATIC (g_application_name);
 
2055
static gchar *g_application_name = NULL;
 
2056
 
 
2057
/**
 
2058
 * g_get_application_name:
 
2059
 * 
 
2060
 * Gets a human-readable name for the application, as set by
 
2061
 * g_set_application_name(). This name should be localized if
 
2062
 * possible, and is intended for display to the user.  Contrast with
 
2063
 * g_get_prgname(), which gets a non-localized name. If
 
2064
 * g_set_application_name() has not been called, returns the result of
 
2065
 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
 
2066
 * been called).
 
2067
 * 
 
2068
 * Return value: human-readable application name. may return %NULL
 
2069
 *
 
2070
 * Since: 2.2
 
2071
 **/
 
2072
G_CONST_RETURN gchar*
 
2073
g_get_application_name (void)
 
2074
{
 
2075
  gchar* retval;
 
2076
 
 
2077
  G_LOCK (g_application_name);
 
2078
  retval = g_application_name;
 
2079
  G_UNLOCK (g_application_name);
 
2080
 
 
2081
  if (retval == NULL)
 
2082
    return g_get_prgname ();
 
2083
  
 
2084
  return retval;
 
2085
}
 
2086
 
 
2087
/**
 
2088
 * g_set_application_name:
 
2089
 * @application_name: localized name of the application
 
2090
 *
 
2091
 * Sets a human-readable name for the application. This name should be
 
2092
 * localized if possible, and is intended for display to the user.
 
2093
 * Contrast with g_set_prgname(), which sets a non-localized name.
 
2094
 * g_set_prgname() will be called automatically by gtk_init(),
 
2095
 * but g_set_application_name() will not.
 
2096
 *
 
2097
 * Note that for thread safety reasons, this function can only
 
2098
 * be called once.
 
2099
 *
 
2100
 * The application name will be used in contexts such as error messages,
 
2101
 * or when displaying an application's name in the task list.
 
2102
 * 
 
2103
 **/
 
2104
void
 
2105
g_set_application_name (const gchar *application_name)
 
2106
{
 
2107
  gboolean already_set = FALSE;
 
2108
        
 
2109
  G_LOCK (g_application_name);
 
2110
  if (g_application_name)
 
2111
    already_set = TRUE;
 
2112
  else
 
2113
    g_application_name = g_strdup (application_name);
 
2114
  G_UNLOCK (g_application_name);
 
2115
 
 
2116
  if (already_set)
 
2117
    g_warning ("g_set_application() name called multiple times");
 
2118
}
 
2119
 
 
2120
/**
 
2121
 * g_get_user_data_dir:
 
2122
 * 
 
2123
 * Returns a base directory in which to access application data such
 
2124
 * as icons that is customized for a particular user.  
 
2125
 *
 
2126
 * On UNIX platforms this is determined using the mechanisms described in
 
2127
 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
 
2128
 * XDG Base Directory Specification</ulink>
 
2129
 * 
 
2130
 * Return value: a string owned by GLib that must not be modified 
 
2131
 *               or freed.
 
2132
 * Since: 2.6
 
2133
 **/
 
2134
G_CONST_RETURN gchar*
 
2135
g_get_user_data_dir (void)
 
2136
{
 
2137
  gchar *data_dir;  
 
2138
 
 
2139
  G_LOCK (g_utils_global);
 
2140
 
 
2141
  if (!g_user_data_dir)
 
2142
    {
 
2143
#ifdef G_OS_WIN32
 
2144
      data_dir = get_special_folder (CSIDL_PERSONAL);
 
2145
#else
 
2146
      data_dir = (gchar *) g_getenv ("XDG_DATA_HOME");
 
2147
 
 
2148
      if (data_dir && data_dir[0])
 
2149
        data_dir = g_strdup (data_dir);
 
2150
#endif
 
2151
      if (!data_dir || !data_dir[0])
 
2152
        {
 
2153
          g_get_any_init ();
 
2154
 
 
2155
          if (g_home_dir)
 
2156
            data_dir = g_build_filename (g_home_dir, ".local", 
 
2157
                                         "share", NULL);
 
2158
          else
 
2159
            data_dir = g_build_filename (g_tmp_dir, g_user_name, ".local", 
 
2160
                                         "share", NULL);
 
2161
        }
 
2162
 
 
2163
      g_user_data_dir = data_dir;
 
2164
    }
 
2165
  else
 
2166
    data_dir = g_user_data_dir;
 
2167
 
 
2168
  G_UNLOCK (g_utils_global);
 
2169
 
 
2170
  return data_dir;
 
2171
}
 
2172
 
 
2173
/**
 
2174
 * g_get_user_config_dir:
 
2175
 * 
 
2176
 * Returns a base directory in which to store user-specific application 
 
2177
 * configuration information such as user preferences and settings. 
 
2178
 *
 
2179
 * On UNIX platforms this is determined using the mechanisms described in
 
2180
 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
 
2181
 * XDG Base Directory Specification</ulink>
 
2182
 * 
 
2183
 * Return value: a string owned by GLib that must not be modified 
 
2184
 *               or freed.
 
2185
 * Since: 2.6
 
2186
 **/
 
2187
G_CONST_RETURN gchar*
 
2188
g_get_user_config_dir (void)
 
2189
{
 
2190
  gchar *config_dir;  
 
2191
 
 
2192
  G_LOCK (g_utils_global);
 
2193
 
 
2194
  if (!g_user_config_dir)
 
2195
    {
 
2196
#ifdef G_OS_WIN32
 
2197
      config_dir = get_special_folder (CSIDL_APPDATA);
 
2198
#else
 
2199
      config_dir = (gchar *) g_getenv ("XDG_CONFIG_HOME");
 
2200
 
 
2201
      if (config_dir && config_dir[0])
 
2202
        config_dir = g_strdup (config_dir);
 
2203
#endif
 
2204
      if (!config_dir || !config_dir[0])
 
2205
        {
 
2206
          g_get_any_init ();
 
2207
 
 
2208
          if (g_home_dir)
 
2209
            config_dir = g_build_filename (g_home_dir, ".config", NULL);
 
2210
          else
 
2211
            config_dir = g_build_filename (g_tmp_dir, g_user_name, ".config", NULL);
 
2212
        }
 
2213
      g_user_config_dir = config_dir;
 
2214
    }
 
2215
  else
 
2216
    config_dir = g_user_config_dir;
 
2217
 
 
2218
  G_UNLOCK (g_utils_global);
 
2219
 
 
2220
  return config_dir;
 
2221
}
 
2222
 
 
2223
/**
 
2224
 * g_get_user_cache_dir:
 
2225
 * 
 
2226
 * Returns a base directory in which to store non-essential, cached
 
2227
 * data specific to particular user.
 
2228
 *
 
2229
 * On UNIX platforms this is determined using the mechanisms described in
 
2230
 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
 
2231
 * XDG Base Directory Specification</ulink>
 
2232
 * 
 
2233
 * Return value: a string owned by GLib that must not be modified 
 
2234
 *               or freed.
 
2235
 * Since: 2.6
 
2236
 **/
 
2237
G_CONST_RETURN gchar*
 
2238
g_get_user_cache_dir (void)
 
2239
{
 
2240
  gchar *cache_dir;  
 
2241
 
 
2242
  G_LOCK (g_utils_global);
 
2243
 
 
2244
  if (!g_user_cache_dir)
 
2245
    {
 
2246
#ifdef G_OS_WIN32
 
2247
      cache_dir = get_special_folder (CSIDL_INTERNET_CACHE); /* XXX correct? */
 
2248
#else
 
2249
      cache_dir = (gchar *) g_getenv ("XDG_CACHE_HOME");
 
2250
 
 
2251
      if (cache_dir && cache_dir[0])
 
2252
          cache_dir = g_strdup (cache_dir);
 
2253
#endif
 
2254
      if (!cache_dir || !cache_dir[0])
 
2255
        {
 
2256
          g_get_any_init ();
 
2257
        
 
2258
          if (g_home_dir)
 
2259
            cache_dir = g_build_filename (g_home_dir, ".cache", NULL);
 
2260
          else
 
2261
            cache_dir = g_build_filename (g_tmp_dir, g_user_name, ".cache", NULL);
 
2262
        }
 
2263
      g_user_cache_dir = cache_dir;
 
2264
    }
 
2265
  else
 
2266
    cache_dir = g_user_cache_dir;
 
2267
 
 
2268
  G_UNLOCK (g_utils_global);
 
2269
 
 
2270
  return cache_dir;
 
2271
}
 
2272
 
 
2273
#ifdef G_OS_WIN32
 
2274
 
 
2275
#undef g_get_system_data_dirs
 
2276
 
 
2277
static HMODULE
 
2278
get_module_for_address (gconstpointer address)
 
2279
{
 
2280
  /* Holds the g_utils_global lock */
 
2281
 
 
2282
  static gboolean beenhere = FALSE;
 
2283
  typedef BOOL (WINAPI *t_GetModuleHandleExA) (DWORD, LPCTSTR, HMODULE *);
 
2284
  static t_GetModuleHandleExA p_GetModuleHandleExA = NULL;
 
2285
  HMODULE hmodule;
 
2286
 
 
2287
  if (!address)
 
2288
    return NULL;
 
2289
 
 
2290
  if (!beenhere)
 
2291
    {
 
2292
      p_GetModuleHandleExA =
 
2293
        (t_GetModuleHandleExA) GetProcAddress (LoadLibrary ("kernel32.dll"),
 
2294
                                               "GetModuleHandleExA");
 
2295
      beenhere = TRUE;
 
2296
    }
 
2297
 
 
2298
  if (p_GetModuleHandleExA == NULL ||
 
2299
      !(*p_GetModuleHandleExA) (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
 
2300
                                GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
 
2301
                                address, &hmodule))
 
2302
    {
 
2303
      MEMORY_BASIC_INFORMATION mbi;
 
2304
      VirtualQuery (address, &mbi, sizeof (mbi));
 
2305
      hmodule = (HMODULE) mbi.AllocationBase;
 
2306
    }
 
2307
 
 
2308
  return hmodule;
 
2309
}
 
2310
 
 
2311
static gchar *
 
2312
get_module_share_dir (gconstpointer address)
 
2313
{
 
2314
  HMODULE hmodule;
 
2315
  gchar *filename = NULL;
 
2316
  gchar *p, *retval;
 
2317
 
 
2318
  hmodule = get_module_for_address (address);
 
2319
  if (hmodule == NULL)
 
2320
    return NULL;
 
2321
 
 
2322
  if (G_WIN32_IS_NT_BASED ())
 
2323
    {
 
2324
      wchar_t wfilename[MAX_PATH];
 
2325
      if (GetModuleFileNameW (hmodule, wfilename, G_N_ELEMENTS (wfilename)))
 
2326
        filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
 
2327
    }
 
2328
  else
 
2329
    {
 
2330
      char cpfilename[MAX_PATH];
 
2331
      if (GetModuleFileNameA (hmodule, cpfilename, G_N_ELEMENTS (cpfilename)))
 
2332
        filename = g_locale_to_utf8 (cpfilename, -1, NULL, NULL, NULL);
 
2333
    }
 
2334
 
 
2335
  if (filename == NULL)
 
2336
    return NULL;
 
2337
 
 
2338
  if ((p = strrchr (filename, G_DIR_SEPARATOR)) != NULL)
 
2339
    *p = '\0';
 
2340
 
 
2341
  p = strrchr (filename, G_DIR_SEPARATOR);
 
2342
  if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0))
 
2343
    *p = '\0';
 
2344
 
 
2345
  retval = g_build_filename (filename, "share", NULL);
 
2346
  g_free (filename);
 
2347
 
 
2348
  return retval;
 
2349
}
 
2350
 
 
2351
G_CONST_RETURN gchar * G_CONST_RETURN *
 
2352
g_win32_get_system_data_dirs_for_module (gconstpointer address)
 
2353
{
 
2354
  GArray *data_dirs;
 
2355
  HMODULE hmodule;
 
2356
  static GHashTable *per_module_data_dirs = NULL;
 
2357
  gchar **retval;
 
2358
  gchar *p;
 
2359
      
 
2360
  if (address)
 
2361
    {
 
2362
      G_LOCK (g_utils_global);
 
2363
      hmodule = get_module_for_address (address);
 
2364
      if (hmodule != NULL)
 
2365
        {
 
2366
          if (per_module_data_dirs == NULL)
 
2367
            per_module_data_dirs = g_hash_table_new (NULL, NULL);
 
2368
          else
 
2369
            {
 
2370
              retval = g_hash_table_lookup (per_module_data_dirs, hmodule);
 
2371
              
 
2372
              if (retval != NULL)
 
2373
                {
 
2374
                  G_UNLOCK (g_utils_global);
 
2375
                  return (G_CONST_RETURN gchar * G_CONST_RETURN *) retval;
 
2376
                }
 
2377
            }
 
2378
        }
 
2379
    }
 
2380
 
 
2381
  data_dirs = g_array_new (TRUE, TRUE, sizeof (char *));
 
2382
 
 
2383
  /* Documents and Settings\All Users\Application Data */
 
2384
  p = get_special_folder (CSIDL_COMMON_APPDATA);
 
2385
  if (p)
 
2386
    g_array_append_val (data_dirs, p);
 
2387
  
 
2388
  /* Documents and Settings\All Users\Documents */
 
2389
  p = get_special_folder (CSIDL_COMMON_DOCUMENTS);
 
2390
  if (p)
 
2391
    g_array_append_val (data_dirs, p);
 
2392
        
 
2393
  /* Using the above subfolders of Documents and Settings perhaps
 
2394
   * makes sense from a Windows perspective.
 
2395
   *
 
2396
   * But looking at the actual use cases of this function in GTK+
 
2397
   * and GNOME software, what we really want is the "share"
 
2398
   * subdirectory of the installation directory for the package
 
2399
   * our caller is a part of.
 
2400
   *
 
2401
   * The address parameter, if non-NULL, points to a function in the
 
2402
   * calling module. Use that to determine that module's installation
 
2403
   * folder, and use its "share" subfolder.
 
2404
   *
 
2405
   * Additionally, also use the "share" subfolder of the installation
 
2406
   * locations of GLib and the .exe file being run.
 
2407
   *
 
2408
   * To guard against none of the above being what is really wanted,
 
2409
   * callers of this function should have Win32-specific code to look
 
2410
   * up their installation folder themselves, and handle a subfolder
 
2411
   * "share" of it in the same way as the folders returned from this
 
2412
   * function.
 
2413
   */
 
2414
 
 
2415
  p = get_module_share_dir (address);
 
2416
  if (p)
 
2417
    g_array_append_val (data_dirs, p);
 
2418
    
 
2419
  p = g_win32_get_package_installation_subdirectory (NULL, dll_name, "share");
 
2420
  if (p)
 
2421
    g_array_append_val (data_dirs, p);
 
2422
  
 
2423
  p = g_win32_get_package_installation_subdirectory (NULL, NULL, "share");
 
2424
  if (p)
 
2425
    g_array_append_val (data_dirs, p);
 
2426
 
 
2427
  retval = (gchar **) g_array_free (data_dirs, FALSE);
 
2428
 
 
2429
  if (address)
 
2430
    {
 
2431
      if (hmodule != NULL)
 
2432
        g_hash_table_insert (per_module_data_dirs, hmodule, retval);
 
2433
      G_UNLOCK (g_utils_global);
 
2434
    }
 
2435
 
 
2436
  return (G_CONST_RETURN gchar * G_CONST_RETURN *) retval;
 
2437
}
 
2438
 
 
2439
#endif
 
2440
 
 
2441
/**
 
2442
 * g_get_system_data_dirs:
 
2443
 * 
 
2444
 * Returns an ordered list of base directories in which to access 
 
2445
 * system-wide application data.
 
2446
 *
 
2447
 * On UNIX platforms this is determined using the mechanisms described in
 
2448
 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
 
2449
 * XDG Base Directory Specification</ulink>
 
2450
 * 
 
2451
 * On Windows the first elements in the list are the Application Data
 
2452
 * and Documents folders for All Users. (These can be determined only
 
2453
 * on Windows 2000 or later and are not present in the list on other
 
2454
 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
 
2455
 * CSIDL_COMMON_DOCUMENTS.
 
2456
 *
 
2457
 * Then follows the "share" subfolder in the installation folder for
 
2458
 * the package containing the DLL that calls this function, if it can
 
2459
 * be determined.
 
2460
 * 
 
2461
 * Finally the list contains the "share" subfolder in the installation
 
2462
 * folder for GLib, and in the installation folder for the package the
 
2463
 * application's .exe file belongs to.
 
2464
 *
 
2465
 * The installation folders above are determined by looking up the
 
2466
 * folder where the module (DLL or EXE) in question is located. If the
 
2467
 * folder's name is "bin", its parent is used, otherwise the folder
 
2468
 * itself.
 
2469
 *
 
2470
 * Note that on Windows the returned list can vary depending on where
 
2471
 * this function is called.
 
2472
 *
 
2473
 * Return value: a %NULL-terminated array of strings owned by GLib that must 
 
2474
 *               not be modified or freed.
 
2475
 * Since: 2.6
 
2476
 **/
 
2477
G_CONST_RETURN gchar * G_CONST_RETURN * 
 
2478
g_get_system_data_dirs (void)
 
2479
{
 
2480
  gchar **data_dir_vector;
 
2481
 
 
2482
  G_LOCK (g_utils_global);
 
2483
 
 
2484
  if (!g_system_data_dirs)
 
2485
    {
 
2486
#ifdef G_OS_WIN32
 
2487
      data_dir_vector = (gchar **) g_win32_get_system_data_dirs_for_module (NULL);
 
2488
#else
 
2489
      gchar *data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS");
 
2490
 
 
2491
      if (!data_dirs || !data_dirs[0])
 
2492
          data_dirs = "/usr/local/share/:/usr/share/";
 
2493
 
 
2494
      data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
 
2495
#endif
 
2496
 
 
2497
      g_system_data_dirs = data_dir_vector;
 
2498
    }
 
2499
  else
 
2500
    data_dir_vector = g_system_data_dirs;
 
2501
 
 
2502
  G_UNLOCK (g_utils_global);
 
2503
 
 
2504
  return (G_CONST_RETURN gchar * G_CONST_RETURN *) data_dir_vector;
 
2505
}
 
2506
 
 
2507
/**
 
2508
 * g_get_system_config_dirs:
 
2509
 * 
 
2510
 * Returns an ordered list of base directories in which to access 
 
2511
 * system-wide configuration information.
 
2512
 *
 
2513
 * On UNIX platforms this is determined using the mechanisms described in
 
2514
 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
 
2515
 * XDG Base Directory Specification</ulink>
 
2516
 * 
 
2517
 * Return value: a %NULL-terminated array of strings owned by GLib that must 
 
2518
 *               not be modified or freed.
 
2519
 * Since: 2.6
 
2520
 **/
 
2521
G_CONST_RETURN gchar * G_CONST_RETURN *
 
2522
g_get_system_config_dirs (void)
 
2523
{
 
2524
  gchar *conf_dirs, **conf_dir_vector;
 
2525
 
 
2526
  G_LOCK (g_utils_global);
 
2527
 
 
2528
  if (!g_system_config_dirs)
 
2529
    {
 
2530
#ifdef G_OS_WIN32
 
2531
      conf_dirs = get_special_folder (CSIDL_COMMON_APPDATA);
 
2532
      if (conf_dirs)
 
2533
        {
 
2534
          conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
 
2535
          g_free (conf_dirs);
 
2536
        }
 
2537
      else
 
2538
        {
 
2539
          /* Return empty list */
 
2540
          conf_dir_vector = g_strsplit ("", G_SEARCHPATH_SEPARATOR_S, 0);
 
2541
        }
 
2542
#else
 
2543
      conf_dirs = (gchar *) g_getenv ("XDG_CONFIG_DIRS");
 
2544
 
 
2545
      if (!conf_dirs || !conf_dirs[0])
 
2546
          conf_dirs = "/etc/xdg";
 
2547
 
 
2548
      conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
 
2549
#endif
 
2550
 
 
2551
      g_system_config_dirs = conf_dir_vector;
 
2552
    }
 
2553
  else
 
2554
    conf_dir_vector = g_system_config_dirs;
 
2555
  G_UNLOCK (g_utils_global);
 
2556
 
 
2557
  return (G_CONST_RETURN gchar * G_CONST_RETURN *) conf_dir_vector;
 
2558
}
 
2559
 
 
2560
#ifndef G_OS_WIN32
 
2561
 
 
2562
static GHashTable *alias_table = NULL;
 
2563
 
 
2564
/* read an alias file for the locales */
 
2565
static void
 
2566
read_aliases (gchar *file)
 
2567
{
 
2568
  FILE *fp;
 
2569
  char buf[256];
 
2570
  
 
2571
  if (!alias_table)
 
2572
    alias_table = g_hash_table_new (g_str_hash, g_str_equal);
 
2573
  fp = fopen (file,"r");
 
2574
  if (!fp)
 
2575
    return;
 
2576
  while (fgets (buf, 256, fp))
 
2577
    {
 
2578
      char *p, *q;
 
2579
 
 
2580
      g_strstrip (buf);
 
2581
 
 
2582
      /* Line is a comment */
 
2583
      if ((buf[0] == '#') || (buf[0] == '\0'))
 
2584
        continue;
 
2585
 
 
2586
      /* Reads first column */
 
2587
      for (p = buf, q = NULL; *p; p++) {
 
2588
        if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
 
2589
          *p = '\0';
 
2590
          q = p+1;
 
2591
          while ((*q == '\t') || (*q == ' ')) {
 
2592
            q++;
 
2593
          }
 
2594
          break;
 
2595
        }
 
2596
      }
 
2597
      /* The line only had one column */
 
2598
      if (!q || *q == '\0')
 
2599
        continue;
 
2600
      
 
2601
      /* Read second column */
 
2602
      for (p = q; *p; p++) {
 
2603
        if ((*p == '\t') || (*p == ' ')) {
 
2604
          *p = '\0';
 
2605
          break;
 
2606
        }
 
2607
      }
 
2608
 
 
2609
      /* Add to alias table if necessary */
 
2610
      if (!g_hash_table_lookup (alias_table, buf)) {
 
2611
        g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
 
2612
      }
 
2613
    }
 
2614
  fclose (fp);
 
2615
}
 
2616
 
 
2617
#endif
 
2618
 
 
2619
static char *
 
2620
unalias_lang (char *lang)
 
2621
{
 
2622
#ifndef G_OS_WIN32
 
2623
  char *p;
 
2624
  int i;
 
2625
 
 
2626
  if (!alias_table)
 
2627
    read_aliases ("/usr/share/locale/locale.alias");
 
2628
 
 
2629
  i = 0;
 
2630
  while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
 
2631
    {
 
2632
      lang = p;
 
2633
      if (i++ == 30)
 
2634
        {
 
2635
          static gboolean said_before = FALSE;
 
2636
          if (!said_before)
 
2637
            g_warning ("Too many alias levels for a locale, "
 
2638
                       "may indicate a loop");
 
2639
          said_before = TRUE;
 
2640
          return lang;
 
2641
        }
 
2642
    }
 
2643
#endif
 
2644
  return lang;
 
2645
}
 
2646
 
 
2647
/* Mask for components of locale spec. The ordering here is from
 
2648
 * least significant to most significant
 
2649
 */
 
2650
enum
 
2651
{
 
2652
  COMPONENT_CODESET =   1 << 0,
 
2653
  COMPONENT_TERRITORY = 1 << 1,
 
2654
  COMPONENT_MODIFIER =  1 << 2
 
2655
};
 
2656
 
 
2657
/* Break an X/Open style locale specification into components
 
2658
 */
 
2659
static guint
 
2660
explode_locale (const gchar *locale,
 
2661
                gchar      **language, 
 
2662
                gchar      **territory, 
 
2663
                gchar      **codeset, 
 
2664
                gchar      **modifier)
 
2665
{
 
2666
  const gchar *uscore_pos;
 
2667
  const gchar *at_pos;
 
2668
  const gchar *dot_pos;
 
2669
 
 
2670
  guint mask = 0;
 
2671
 
 
2672
  uscore_pos = strchr (locale, '_');
 
2673
  dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
 
2674
  at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
 
2675
 
 
2676
  if (at_pos)
 
2677
    {
 
2678
      mask |= COMPONENT_MODIFIER;
 
2679
      *modifier = g_strdup (at_pos);
 
2680
    }
 
2681
  else
 
2682
    at_pos = locale + strlen (locale);
 
2683
 
 
2684
  if (dot_pos)
 
2685
    {
 
2686
      mask |= COMPONENT_CODESET;
 
2687
      *codeset = g_strndup (dot_pos, at_pos - dot_pos);
 
2688
    }
 
2689
  else
 
2690
    dot_pos = at_pos;
 
2691
 
 
2692
  if (uscore_pos)
 
2693
    {
 
2694
      mask |= COMPONENT_TERRITORY;
 
2695
      *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
 
2696
    }
 
2697
  else
 
2698
    uscore_pos = dot_pos;
 
2699
 
 
2700
  *language = g_strndup (locale, uscore_pos - locale);
 
2701
 
 
2702
  return mask;
 
2703
}
 
2704
 
 
2705
/*
 
2706
 * Compute all interesting variants for a given locale name -
 
2707
 * by stripping off different components of the value.
 
2708
 *
 
2709
 * For simplicity, we assume that the locale is in
 
2710
 * X/Open format: language[_territory][.codeset][@modifier]
 
2711
 *
 
2712
 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
 
2713
 *       as well. We could just copy the code from glibc wholesale
 
2714
 *       but it is big, ugly, and complicated, so I'm reluctant
 
2715
 *       to do so when this should handle 99% of the time...
 
2716
 */
 
2717
GSList *
 
2718
_g_compute_locale_variants (const gchar *locale)
 
2719
{
 
2720
  GSList *retval = NULL;
 
2721
 
 
2722
  gchar *language = NULL;
 
2723
  gchar *territory = NULL;
 
2724
  gchar *codeset = NULL;
 
2725
  gchar *modifier = NULL;
 
2726
 
 
2727
  guint mask;
 
2728
  guint i;
 
2729
 
 
2730
  g_return_val_if_fail (locale != NULL, NULL);
 
2731
 
 
2732
  mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
 
2733
 
 
2734
  /* Iterate through all possible combinations, from least attractive
 
2735
   * to most attractive.
 
2736
   */
 
2737
  for (i = 0; i <= mask; i++)
 
2738
    if ((i & ~mask) == 0)
 
2739
      {
 
2740
        gchar *val = g_strconcat (language,
 
2741
                                  (i & COMPONENT_TERRITORY) ? territory : "",
 
2742
                                  (i & COMPONENT_CODESET) ? codeset : "",
 
2743
                                  (i & COMPONENT_MODIFIER) ? modifier : "",
 
2744
                                  NULL);
 
2745
        retval = g_slist_prepend (retval, val);
 
2746
      }
 
2747
 
 
2748
  g_free (language);
 
2749
  if (mask & COMPONENT_CODESET)
 
2750
    g_free (codeset);
 
2751
  if (mask & COMPONENT_TERRITORY)
 
2752
    g_free (territory);
 
2753
  if (mask & COMPONENT_MODIFIER)
 
2754
    g_free (modifier);
 
2755
 
 
2756
  return retval;
 
2757
}
 
2758
 
 
2759
/* The following is (partly) taken from the gettext package.
 
2760
   Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.  */
 
2761
 
 
2762
static const gchar *
 
2763
guess_category_value (const gchar *category_name)
 
2764
{
 
2765
  const gchar *retval;
 
2766
 
 
2767
  /* The highest priority value is the `LANGUAGE' environment
 
2768
     variable.  This is a GNU extension.  */
 
2769
  retval = g_getenv ("LANGUAGE");
 
2770
  if ((retval != NULL) && (retval[0] != '\0'))
 
2771
    return retval;
 
2772
 
 
2773
  /* `LANGUAGE' is not set.  So we have to proceed with the POSIX
 
2774
     methods of looking to `LC_ALL', `LC_xxx', and `LANG'.  On some
 
2775
     systems this can be done by the `setlocale' function itself.  */
 
2776
 
 
2777
  /* Setting of LC_ALL overwrites all other.  */
 
2778
  retval = g_getenv ("LC_ALL");  
 
2779
  if ((retval != NULL) && (retval[0] != '\0'))
 
2780
    return retval;
 
2781
 
 
2782
  /* Next comes the name of the desired category.  */
 
2783
  retval = g_getenv (category_name);
 
2784
  if ((retval != NULL) && (retval[0] != '\0'))
 
2785
    return retval;
 
2786
 
 
2787
  /* Last possibility is the LANG environment variable.  */
 
2788
  retval = g_getenv ("LANG");
 
2789
  if ((retval != NULL) && (retval[0] != '\0'))
 
2790
    return retval;
 
2791
 
 
2792
#ifdef G_PLATFORM_WIN32
 
2793
  /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
 
2794
   * LANG, which we already did above. Oh well. The main point of
 
2795
   * calling g_win32_getlocale() is to get the thread's locale as used
 
2796
   * by Windows and the Microsoft C runtime (in the "English_United
 
2797
   * States" format) translated into the Unixish format.
 
2798
   */
 
2799
  retval = g_win32_getlocale ();
 
2800
  if ((retval != NULL) && (retval[0] != '\0'))
 
2801
    return retval;
 
2802
#endif  
 
2803
 
 
2804
  return NULL;
 
2805
}
 
2806
 
 
2807
typedef struct _GLanguageNamesCache GLanguageNamesCache;
 
2808
 
 
2809
struct _GLanguageNamesCache {
 
2810
  gchar *languages;
 
2811
  gchar **language_names;
 
2812
};
 
2813
 
 
2814
static void
 
2815
language_names_cache_free (gpointer data)
 
2816
{
 
2817
  GLanguageNamesCache *cache = data;
 
2818
  g_free (cache->languages);
 
2819
  g_strfreev (cache->language_names);
 
2820
  g_free (cache);
 
2821
}
 
2822
 
 
2823
/**
 
2824
 * g_get_language_names:
 
2825
 * 
 
2826
 * Computes a list of applicable locale names, which can be used to 
 
2827
 * e.g. construct locale-dependent filenames or search paths. The returned 
 
2828
 * list is sorted from most desirable to least desirable and always contains 
 
2829
 * the default locale "C".
 
2830
 *
 
2831
 * For example, if LANGUAGE=de:en_US, then the returned list is
 
2832
 * "de", "en_US", "en", "C".
 
2833
 *
 
2834
 * This function consults the environment variables <envar>LANGUAGE</envar>, 
 
2835
 * <envar>LC_ALL</envar>, <envar>LC_MESSAGES</envar> and <envar>LANG</envar> 
 
2836
 * to find the list of locales specified by the user.
 
2837
 * 
 
2838
 * Return value: a %NULL-terminated array of strings owned by GLib 
 
2839
 *    that must not be modified or freed.
 
2840
 *
 
2841
 * Since: 2.6
 
2842
 **/
 
2843
G_CONST_RETURN gchar * G_CONST_RETURN * 
 
2844
g_get_language_names (void)
 
2845
{
 
2846
  static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
 
2847
  GLanguageNamesCache *cache = g_static_private_get (&cache_private);
 
2848
  const gchar *value;
 
2849
 
 
2850
  if (!cache)
 
2851
    {
 
2852
      cache = g_new0 (GLanguageNamesCache, 1);
 
2853
      g_static_private_set (&cache_private, cache, language_names_cache_free);
 
2854
    }
 
2855
 
 
2856
  value = guess_category_value ("LC_MESSAGES");
 
2857
  if (!value)
 
2858
    value = "C";
 
2859
 
 
2860
  if (!(cache->languages && strcmp (cache->languages, value) == 0))
 
2861
    {
 
2862
      gchar **languages;
 
2863
      gchar **alist, **a;
 
2864
      GSList *list, *l;
 
2865
      gint i;
 
2866
 
 
2867
      g_free (cache->languages);
 
2868
      g_strfreev (cache->language_names);
 
2869
      cache->languages = g_strdup (value);
 
2870
 
 
2871
      alist = g_strsplit (value, ":", 0);
 
2872
      list = NULL;
 
2873
      for (a = alist; *a; a++)
 
2874
        {
 
2875
          gchar *b = unalias_lang (*a);
 
2876
          list = g_slist_concat (list, _g_compute_locale_variants (b));
 
2877
        }
 
2878
      g_strfreev (alist);
 
2879
      list = g_slist_append (list, g_strdup ("C"));
 
2880
 
 
2881
      cache->language_names = languages = g_new (gchar *, g_slist_length (list) + 1);
 
2882
      for (l = list, i = 0; l; l = l->next, i++)
 
2883
        languages[i] = l->data;
 
2884
      languages[i] = NULL;
 
2885
 
 
2886
      g_slist_free (list);
 
2887
    }
 
2888
 
 
2889
  return (G_CONST_RETURN gchar * G_CONST_RETURN *) cache->language_names;
 
2890
}
 
2891
 
 
2892
/**
 
2893
 * g_direct_hash:
 
2894
 * @v: a #gpointer key
 
2895
 *
 
2896
 * Converts a gpointer to a hash value.
 
2897
 * It can be passed to g_hash_table_new() as the @hash_func parameter, 
 
2898
 * when using pointers as keys in a #GHashTable.
 
2899
 *
 
2900
 * Returns: a hash value corresponding to the key.
 
2901
 */
 
2902
guint
 
2903
g_direct_hash (gconstpointer v)
 
2904
{
 
2905
  return GPOINTER_TO_UINT (v);
 
2906
}
 
2907
 
 
2908
/**
 
2909
 * g_direct_equal:
 
2910
 * @v1: a key.
 
2911
 * @v2: a key to compare with @v1.
 
2912
 *
 
2913
 * Compares two #gpointer arguments and returns %TRUE if they are equal.
 
2914
 * It can be passed to g_hash_table_new() as the @key_equal_func
 
2915
 * parameter, when using pointers as keys in a #GHashTable.
 
2916
 * 
 
2917
 * Returns: %TRUE if the two keys match.
 
2918
 */
 
2919
gboolean
 
2920
g_direct_equal (gconstpointer v1,
 
2921
                gconstpointer v2)
 
2922
{
 
2923
  return v1 == v2;
 
2924
}
 
2925
 
 
2926
/**
 
2927
 * g_int_equal:
 
2928
 * @v1: a pointer to a #gint key.
 
2929
 * @v2: a pointer to a #gint key to compare with @v1.
 
2930
 *
 
2931
 * Compares the two #gint values being pointed to and returns 
 
2932
 * %TRUE if they are equal.
 
2933
 * It can be passed to g_hash_table_new() as the @key_equal_func
 
2934
 * parameter, when using pointers to integers as keys in a #GHashTable.
 
2935
 * 
 
2936
 * Returns: %TRUE if the two keys match.
 
2937
 */
 
2938
gboolean
 
2939
g_int_equal (gconstpointer v1,
 
2940
             gconstpointer v2)
 
2941
{
 
2942
  return *((const gint*) v1) == *((const gint*) v2);
 
2943
}
 
2944
 
 
2945
/**
 
2946
 * g_int_hash:
 
2947
 * @v: a pointer to a #gint key
 
2948
 *
 
2949
 * Converts a pointer to a #gint to a hash value.
 
2950
 * It can be passed to g_hash_table_new() as the @hash_func parameter, 
 
2951
 * when using pointers to integers values as keys in a #GHashTable.
 
2952
 *
 
2953
 * Returns: a hash value corresponding to the key.
 
2954
 */
 
2955
guint
 
2956
g_int_hash (gconstpointer v)
 
2957
{
 
2958
  return *(const gint*) v;
 
2959
}
 
2960
 
 
2961
/**
 
2962
 * g_nullify_pointer:
 
2963
 * @nullify_location: the memory address of the pointer.
 
2964
 * 
 
2965
 * Set the pointer at the specified location to %NULL.
 
2966
 **/
 
2967
void
 
2968
g_nullify_pointer (gpointer *nullify_location)
 
2969
{
 
2970
  g_return_if_fail (nullify_location != NULL);
 
2971
 
 
2972
  *nullify_location = NULL;
 
2973
}
 
2974
 
 
2975
/**
 
2976
 * g_get_codeset:
 
2977
 * 
 
2978
 * Get the codeset for the current locale.
 
2979
 * 
 
2980
 * Return value: a newly allocated string containing the name
 
2981
 * of the codeset. This string must be freed with g_free().
 
2982
 **/
 
2983
gchar *
 
2984
g_get_codeset (void)
 
2985
{
 
2986
  const gchar *charset;
 
2987
 
 
2988
  g_get_charset (&charset);
 
2989
 
 
2990
  return g_strdup (charset);
 
2991
}
 
2992
 
 
2993
/* This is called from g_thread_init(). It's used to
 
2994
 * initialize some static data in a threadsafe way.
 
2995
 */
 
2996
void
 
2997
_g_utils_thread_init (void)
 
2998
{
 
2999
  g_get_language_names ();
 
3000
}
 
3001
 
 
3002
#ifdef ENABLE_NLS
 
3003
 
 
3004
#include <libintl.h>
 
3005
 
 
3006
#ifdef G_OS_WIN32
 
3007
 
 
3008
/**
 
3009
 * _glib_get_locale_dir:
 
3010
 *
 
3011
 * Return the path to the lib\locale subfolder of the GLib
 
3012
 * installation folder. The path is in the system codepage. We have to
 
3013
 * use system codepage as bindtextdomain() doesn't have a UTF-8
 
3014
 * interface.
 
3015
 */
 
3016
static const gchar *
 
3017
_glib_get_locale_dir (void)
 
3018
{
 
3019
  gchar *dir, *cp_dir;
 
3020
  gchar *retval = NULL;
 
3021
 
 
3022
  dir = g_win32_get_package_installation_directory (GETTEXT_PACKAGE, dll_name);
 
3023
  cp_dir = g_win32_locale_filename_from_utf8 (dir);
 
3024
  g_free (dir);
 
3025
 
 
3026
  if (cp_dir)
 
3027
    {
 
3028
      /* Don't use g_build_filename() on pathnames in the system
 
3029
       * codepage. In CJK locales cp_dir might end with a double-byte
 
3030
       * character whose trailing byte is a backslash.
 
3031
       */
 
3032
      retval = g_strconcat (cp_dir, "\\lib\\locale", NULL);
 
3033
      g_free (cp_dir);
 
3034
    }
 
3035
 
 
3036
  if (retval)
 
3037
    return retval;
 
3038
  else
 
3039
    return g_strdup ("");
 
3040
}
 
3041
 
 
3042
#undef GLIB_LOCALE_DIR
 
3043
#define GLIB_LOCALE_DIR _glib_get_locale_dir ()
 
3044
 
 
3045
#endif /* G_OS_WIN32 */
 
3046
 
 
3047
G_CONST_RETURN gchar *
 
3048
_glib_gettext (const gchar *str)
 
3049
{
 
3050
  static gboolean _glib_gettext_initialized = FALSE;
 
3051
 
 
3052
  if (!_glib_gettext_initialized)
 
3053
    {
 
3054
      bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
 
3055
#    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
 
3056
      bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
3057
#    endif
 
3058
      _glib_gettext_initialized = TRUE;
 
3059
    }
 
3060
  
 
3061
  return dgettext (GETTEXT_PACKAGE, str);
 
3062
}
 
3063
 
 
3064
#endif /* ENABLE_NLS */
 
3065
 
 
3066
#ifdef G_OS_WIN32
 
3067
 
 
3068
/* Binary compatibility versions. Not for newly compiled code. */
 
3069
 
 
3070
#undef g_find_program_in_path
 
3071
 
 
3072
gchar*
 
3073
g_find_program_in_path (const gchar *program)
 
3074
{
 
3075
  gchar *utf8_program = g_locale_to_utf8 (program, -1, NULL, NULL, NULL);
 
3076
  gchar *utf8_retval = g_find_program_in_path_utf8 (utf8_program);
 
3077
  gchar *retval;
 
3078
 
 
3079
  g_free (utf8_program);
 
3080
  if (utf8_retval == NULL)
 
3081
    return NULL;
 
3082
  retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
 
3083
  g_free (utf8_retval);
 
3084
 
 
3085
  return retval;
 
3086
}
 
3087
 
 
3088
#undef g_get_current_dir
 
3089
 
 
3090
gchar*
 
3091
g_get_current_dir (void)
 
3092
{
 
3093
  gchar *utf8_dir = g_get_current_dir_utf8 ();
 
3094
  gchar *dir = g_locale_from_utf8 (utf8_dir, -1, NULL, NULL, NULL);
 
3095
  g_free (utf8_dir);
 
3096
  return dir;
 
3097
}
 
3098
 
 
3099
#undef g_getenv
 
3100
 
 
3101
G_CONST_RETURN gchar*
 
3102
g_getenv (const gchar *variable)
 
3103
{
 
3104
  gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
 
3105
  const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
 
3106
  gchar *value;
 
3107
  GQuark quark;
 
3108
 
 
3109
  g_free (utf8_variable);
 
3110
  if (!utf8_value)
 
3111
    return NULL;
 
3112
  value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
 
3113
  quark = g_quark_from_string (value);
 
3114
  g_free (value);
 
3115
 
 
3116
  return g_quark_to_string (quark);
 
3117
}
 
3118
 
 
3119
#undef g_setenv
 
3120
 
 
3121
gboolean
 
3122
g_setenv (const gchar *variable, 
 
3123
          const gchar *value, 
 
3124
          gboolean     overwrite)
 
3125
{
 
3126
  gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
 
3127
  gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
 
3128
  gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
 
3129
 
 
3130
  g_free (utf8_variable);
 
3131
  g_free (utf8_value);
 
3132
 
 
3133
  return retval;
 
3134
}
 
3135
 
 
3136
#undef g_unsetenv
 
3137
 
 
3138
void
 
3139
g_unsetenv (const gchar *variable)
 
3140
{
 
3141
  gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
 
3142
 
 
3143
  g_unsetenv_utf8 (utf8_variable);
 
3144
 
 
3145
  g_free (utf8_variable);
 
3146
}
 
3147
 
 
3148
#undef g_get_user_name
 
3149
 
 
3150
G_CONST_RETURN gchar*
 
3151
g_get_user_name (void)
 
3152
{
 
3153
  g_get_any_init_locked ();
 
3154
  return g_user_name_cp;
 
3155
}
 
3156
 
 
3157
#undef g_get_real_name
 
3158
 
 
3159
G_CONST_RETURN gchar*
 
3160
g_get_real_name (void)
 
3161
{
 
3162
  g_get_any_init_locked ();
 
3163
  return g_real_name_cp;
 
3164
}
 
3165
 
 
3166
#undef g_get_home_dir
 
3167
 
 
3168
G_CONST_RETURN gchar*
 
3169
g_get_home_dir (void)
 
3170
{
 
3171
  g_get_any_init_locked ();
 
3172
  return g_home_dir_cp;
 
3173
}
 
3174
 
 
3175
#undef g_get_tmp_dir
 
3176
 
 
3177
G_CONST_RETURN gchar*
 
3178
g_get_tmp_dir (void)
 
3179
{
 
3180
  g_get_any_init_locked ();
 
3181
  return g_tmp_dir_cp;
 
3182
}
 
3183
 
 
3184
#endif
 
3185
 
 
3186
#define __G_UTILS_C__
 
3187
#include "galiasdef.c"