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