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