applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[platform/upstream/glib.git] / 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 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 #ifdef HAVE_PWD_H
44 #include <pwd.h>
45 #endif
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
49 #endif
50
51 /* implement Glib's inline functions
52  */
53 #define G_INLINE_FUNC extern
54 #define G_CAN_INLINE 1
55 #include "glib.h"
56
57 #ifdef  MAXPATHLEN
58 #define G_PATH_LENGTH   MAXPATHLEN
59 #elif   defined (PATH_MAX)
60 #define G_PATH_LENGTH   PATH_MAX
61 #elif   defined (_PC_PATH_MAX)
62 #define G_PATH_LENGTH   sysconf(_PC_PATH_MAX)
63 #else   
64 #define G_PATH_LENGTH   2048
65 #endif
66
67 #ifdef G_OS_WIN32
68 #  define STRICT                        /* Strict typing, please */
69 #  include <windows.h>
70 #  include <ctype.h>
71 #  include <direct.h>
72 #  include <io.h>
73 #endif /* G_OS_WIN32 */
74
75 #ifdef HAVE_CODESET
76 #include <langinfo.h>
77 #endif
78
79 const guint glib_major_version = GLIB_MAJOR_VERSION;
80 const guint glib_minor_version = GLIB_MINOR_VERSION;
81 const guint glib_micro_version = GLIB_MICRO_VERSION;
82 const guint glib_interface_age = GLIB_INTERFACE_AGE;
83 const guint glib_binary_age = GLIB_BINARY_AGE;
84
85 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
86 void 
87 g_memmove (gpointer dest, gconstpointer src, gulong len)
88 {
89   gchar* destptr = dest;
90   const gchar* srcptr = src;
91   if (src + len < dest || dest + len < src)
92     {
93       bcopy (src, dest, len);
94       return;
95     }
96   else if (dest <= src)
97     {
98       while (len--)
99         *(destptr++) = *(srcptr++);
100     }
101   else
102     {
103       destptr += len;
104       srcptr += len;
105       while (len--)
106         *(--destptr) = *(--srcptr);
107     }
108 }
109 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
110
111 void
112 g_atexit (GVoidFunc func)
113 {
114   gint result;
115   gchar *error = NULL;
116
117   /* keep this in sync with glib.h */
118
119 #ifdef  G_NATIVE_ATEXIT
120   result = ATEXIT (func);
121   if (result)
122     error = g_strerror (errno);
123 #elif defined (HAVE_ATEXIT)
124 #  ifdef NeXT /* @#%@! NeXTStep */
125   result = !atexit ((void (*)(void)) func);
126   if (result)
127     error = g_strerror (errno);
128 #  else
129   result = atexit ((void (*)(void)) func);
130   if (result)
131     error = g_strerror (errno);
132 #  endif /* NeXT */
133 #elif defined (HAVE_ON_EXIT)
134   result = on_exit ((void (*)(int, void *)) func, NULL);
135   if (result)
136     error = g_strerror (errno);
137 #else
138   result = 0;
139   error = "no implementation";
140 #endif /* G_NATIVE_ATEXIT */
141
142   if (error)
143     g_error ("Could not register atexit() function: %s", error);
144 }
145
146 gint
147 g_snprintf (gchar       *str,
148             gulong       n,
149             gchar const *fmt,
150             ...)
151 {
152 #ifdef  HAVE_VSNPRINTF
153   va_list args;
154   gint retval;
155   
156   g_return_val_if_fail (str != NULL, 0);
157   g_return_val_if_fail (n > 0, 0);
158   g_return_val_if_fail (fmt != NULL, 0);
159
160   va_start (args, fmt);
161   retval = vsnprintf (str, n, fmt, args);
162   va_end (args);
163
164   if (retval < 0)
165     {
166       str[n-1] = '\0';
167       retval = strlen (str);
168     }
169
170   return retval;
171 #else   /* !HAVE_VSNPRINTF */
172   gchar *printed;
173   va_list args;
174   
175   g_return_val_if_fail (str != NULL, 0);
176   g_return_val_if_fail (n > 0, 0);
177   g_return_val_if_fail (fmt != NULL, 0);
178
179   va_start (args, fmt);
180   printed = g_strdup_vprintf (fmt, args);
181   va_end (args);
182   
183   strncpy (str, printed, n);
184   str[n-1] = '\0';
185
186   g_free (printed);
187   
188   return strlen (str);
189 #endif  /* !HAVE_VSNPRINTF */
190 }
191
192 gint
193 g_vsnprintf (gchar       *str,
194              gulong       n,
195              gchar const *fmt,
196              va_list      args)
197 {
198 #ifdef  HAVE_VSNPRINTF
199   gint retval;
200   
201   g_return_val_if_fail (str != NULL, 0);
202   g_return_val_if_fail (n > 0, 0);
203   g_return_val_if_fail (fmt != NULL, 0);
204
205   retval = vsnprintf (str, n, fmt, args);
206   
207   if (retval < 0)
208     {
209       str[n-1] = '\0';
210       retval = strlen (str);
211     }
212
213   return retval;
214 #else   /* !HAVE_VSNPRINTF */
215   gchar *printed;
216   
217   g_return_val_if_fail (str != NULL, 0);
218   g_return_val_if_fail (n > 0, 0);
219   g_return_val_if_fail (fmt != NULL, 0);
220
221   printed = g_strdup_vprintf (fmt, args);
222   strncpy (str, printed, n);
223   str[n-1] = '\0';
224
225   g_free (printed);
226   
227   return strlen (str);
228 #endif /* !HAVE_VSNPRINTF */
229 }
230
231 guint        
232 g_parse_debug_string  (const gchar *string, 
233                        GDebugKey   *keys, 
234                        guint        nkeys)
235 {
236   guint i;
237   guint result = 0;
238   
239   g_return_val_if_fail (string != NULL, 0);
240   
241   if (!g_strcasecmp (string, "all"))
242     {
243       for (i=0; i<nkeys; i++)
244         result |= keys[i].value;
245     }
246   else
247     {
248       gchar *str = g_strdup (string);
249       gchar *p = str;
250       gchar *q;
251       gboolean done = FALSE;
252       
253       while (*p && !done)
254         {
255           q = strchr (p, ':');
256           if (!q)
257             {
258               q = p + strlen(p);
259               done = TRUE;
260             }
261           
262           *q = 0;
263           
264           for (i=0; i<nkeys; i++)
265             if (!g_strcasecmp(keys[i].key, p))
266               result |= keys[i].value;
267           
268           p = q+1;
269         }
270       
271       g_free (str);
272     }
273   
274   return result;
275 }
276
277 gchar*
278 g_basename (const gchar    *file_name)
279 {
280   register gchar *base;
281 #ifdef G_ENABLE_DEBUG
282   static gboolean first_call = TRUE;
283
284   if (first_call)
285     {
286       g_warning("g_basename is deprecated. Use g_path_get_basename instead.");
287       g_warning("Watch out! You have to g_free the string returned by "
288                 "g_path_get_basename.");
289       first_call = FALSE;
290     }
291 #endif /* G_ENABLE_DEBUG */
292   
293   g_return_val_if_fail (file_name != NULL, NULL);
294   
295   base = strrchr (file_name, G_DIR_SEPARATOR);
296   if (base)
297     return base + 1;
298
299 #ifdef G_OS_WIN32
300   if (isalpha (file_name[0]) && file_name[1] == ':')
301     return (gchar*) file_name + 2;
302 #endif /* G_OS_WIN32 */
303   
304   return (gchar*) file_name;
305 }
306
307 gchar*
308 g_path_get_basename (const gchar   *file_name)
309 {
310   register gint base;
311   register gint last_nonslash;
312   guint len;
313   gchar *retval;
314  
315   g_return_val_if_fail (file_name != NULL, NULL);
316   
317   if (file_name[0] == '\0')
318     /* empty string */
319     return g_strdup (".");
320
321   last_nonslash = strlen (file_name) - 1;
322
323   while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
324     last_nonslash--;
325
326   if (last_nonslash == -1)
327     /* string only containing slashes */
328     return g_strdup (G_DIR_SEPARATOR_S);
329
330 #ifdef G_OS_WIN32
331   if (last_nonslash == 1 && isalpha (file_name[0]) && file_name[1] == ':')
332     /* string only containing slashes and a drive */
333     return g_strdup (G_DIR_SEPARATOR_S);
334 #endif /* G_OS_WIN32 */
335
336   base = last_nonslash;
337
338   while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
339     base--;
340
341 #ifdef G_OS_WIN32
342   if (base == -1 && isalpha (file_name[0]) && file_name[1] == ':')
343     base = 1;
344 #endif /* G_OS_WIN32 */
345
346   len = last_nonslash - base;
347   retval = g_malloc (len + 1);
348   memcpy (retval, file_name + base + 1, len);
349   retval [len] = '\0';
350   return retval;
351 }
352
353 gboolean
354 g_path_is_absolute (const gchar *file_name)
355 {
356   g_return_val_if_fail (file_name != NULL, FALSE);
357   
358   if (file_name[0] == G_DIR_SEPARATOR)
359     return TRUE;
360
361 #ifdef G_OS_WIN32
362   if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
363     return TRUE;
364 #endif
365
366   return FALSE;
367 }
368
369 gchar*
370 g_path_skip_root (gchar *file_name)
371 {
372   g_return_val_if_fail (file_name != NULL, NULL);
373   
374   if (file_name[0] == G_DIR_SEPARATOR)
375     return file_name + 1;
376
377 #ifdef G_OS_WIN32
378   if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
379     return file_name + 3;
380 #endif
381
382   return NULL;
383 }
384
385 gchar*
386 g_path_get_dirname (const gchar    *file_name)
387 {
388   register gchar *base;
389   register guint len;
390   
391   g_return_val_if_fail (file_name != NULL, NULL);
392   
393   base = strrchr (file_name, G_DIR_SEPARATOR);
394   if (!base)
395     return g_strdup (".");
396   while (base > file_name && *base == G_DIR_SEPARATOR)
397     base--;
398   len = (guint) 1 + base - file_name;
399   
400   base = g_new (gchar, len + 1);
401   g_memmove (base, file_name, len);
402   base[len] = 0;
403   
404   return base;
405 }
406
407 gchar*
408 g_dirname (const gchar     *file_name)
409 {
410 #ifdef G_ENABLE_DEBUG
411   static gboolean first_call = TRUE;
412
413   if (first_call)
414     {
415       g_warning("g_dirname is deprecated. Use g_path_get_dirname instead.");
416       first_call = FALSE;
417     }
418 #endif /* G_ENABLE_DEBUG */
419
420   return g_path_get_dirname (file_name);
421 }
422
423 gchar*
424 g_get_current_dir (void)
425 {
426   gchar *buffer = NULL;
427   gchar *dir = NULL;
428   static gulong max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
429   
430   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
431    * and, if that wasn't bad enough, hangs in doing so.
432    */
433 #if     defined (sun) && !defined (__SVR4)
434   buffer = g_new (gchar, max_len + 1);
435   *buffer = 0;
436   dir = getwd (buffer);
437 #else   /* !sun */
438   while (max_len < G_MAXULONG / 2)
439     {
440       buffer = g_new (gchar, max_len + 1);
441       *buffer = 0;
442       dir = getcwd (buffer, max_len);
443
444       if (dir || errno != ERANGE)
445         break;
446
447       g_free (buffer);
448       max_len *= 2;
449     }
450 #endif  /* !sun */
451   
452   if (!dir || !*buffer)
453     {
454       /* hm, should we g_error() out here?
455        * this can happen if e.g. "./" has mode \0000
456        */
457       buffer[0] = G_DIR_SEPARATOR;
458       buffer[1] = 0;
459     }
460
461   dir = g_strdup (buffer);
462   g_free (buffer);
463   
464   return dir;
465 }
466
467 gchar*
468 g_getenv (const gchar *variable)
469 {
470 #ifndef G_OS_WIN32
471   g_return_val_if_fail (variable != NULL, NULL);
472
473   return getenv (variable);
474 #else
475   G_LOCK_DEFINE_STATIC (getenv);
476   struct env_struct
477   {
478     gchar *key;
479     gchar *value;
480   } *env;
481   static GArray *environs = NULL;
482   gchar *system_env;
483   guint length, i;
484   gchar dummy[2];
485
486   g_return_val_if_fail (variable != NULL, NULL);
487   
488   G_LOCK (getenv);
489
490   if (!environs)
491     environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
492
493   /* First we try to find the envinronment variable inside the already
494    * found ones.
495    */
496
497   for (i = 0; i < environs->len; i++)
498     {
499       env = &g_array_index (environs, struct env_struct, i);
500       if (strcmp (env->key, variable) == 0)
501         {
502           g_assert (env->value);
503           G_UNLOCK (getenv);
504           return env->value;
505         }
506     }
507
508   /* If not found, we ask the system */
509
510   system_env = getenv (variable);
511   if (!system_env)
512     {
513       G_UNLOCK (getenv);
514       return NULL;
515     }
516
517   /* On Windows NT, it is relatively typical that environment variables
518    * contain references to other environment variables. Handle that by
519    * calling ExpandEnvironmentStrings.
520    */
521
522   g_array_set_size (environs, environs->len + 1);
523
524   env = &g_array_index (environs, struct env_struct, environs->len - 1);
525
526   /* First check how much space we need */
527   length = ExpandEnvironmentStrings (system_env, dummy, 2);
528
529   /* Then allocate that much, and actualy do the expansion and insert
530    * the new found pair into our buffer 
531    */
532
533   env->value = g_malloc (length);
534   env->key = g_strdup (variable);
535
536   ExpandEnvironmentStrings (system_env, env->value, length);
537
538   G_UNLOCK (getenv);
539   return env->value;
540 #endif
541 }
542
543
544 G_LOCK_DEFINE_STATIC (g_utils_global);
545
546 static  gchar   *g_tmp_dir = NULL;
547 static  gchar   *g_user_name = NULL;
548 static  gchar   *g_real_name = NULL;
549 static  gchar   *g_home_dir = NULL;
550
551 /* HOLDS: g_utils_global_lock */
552 static void
553 g_get_any_init (void)
554 {
555   if (!g_tmp_dir)
556     {
557       g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
558       if (!g_tmp_dir)
559         g_tmp_dir = g_strdup (g_getenv ("TMP"));
560       if (!g_tmp_dir)
561         g_tmp_dir = g_strdup (g_getenv ("TEMP"));
562       
563 #ifdef P_tmpdir
564       if (!g_tmp_dir)
565         {
566           int k;
567           g_tmp_dir = g_strdup (P_tmpdir);
568           k = strlen (g_tmp_dir);
569           if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
570             g_tmp_dir[k-1] = '\0';
571         }
572 #endif
573       
574       if (!g_tmp_dir)
575         {
576 #ifndef G_OS_WIN32
577           g_tmp_dir = g_strdup ("/tmp");
578 #else /* G_OS_WIN32 */
579           g_tmp_dir = g_strdup ("C:\\");
580 #endif /* G_OS_WIN32 */
581         }
582       
583       if (!g_home_dir)
584         g_home_dir = g_strdup (g_getenv ("HOME"));
585       
586 #ifdef G_OS_WIN32
587       if (!g_home_dir)
588         {
589           /* The official way to specify a home directory on NT is
590            * the HOMEDRIVE and HOMEPATH environment variables.
591            *
592            * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
593            * HOME should be a POSIX style pathname.
594            */
595           
596           if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
597             {
598               gchar *homedrive, *homepath;
599               
600               homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
601               homepath = g_strdup (g_getenv ("HOMEPATH"));
602               
603               g_home_dir = g_strconcat (homedrive, homepath, NULL);
604               g_free (homedrive);
605               g_free (homepath);
606             }
607         }
608 #endif /* G_OS_WIN32 */
609       
610 #ifdef HAVE_PWD_H
611       {
612         struct passwd *pw = NULL;
613         gpointer buffer = NULL;
614         
615 #  ifdef HAVE_GETPWUID_R
616         struct passwd pwd;
617 #    ifdef _SC_GETPW_R_SIZE_MAX  
618         /* This reurns the maximum length */
619         guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
620 #    else /* _SC_GETPW_R_SIZE_MAX */
621         guint bufsize = 64;
622 #    endif /* _SC_GETPW_R_SIZE_MAX */
623         gint error;
624         
625         do
626           {
627             g_free (buffer);
628             buffer = g_malloc (bufsize);
629             errno = 0;
630             
631 #    ifdef HAVE_GETPWUID_R_POSIX
632             error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
633             error = error < 0 ? errno : error;
634 #    else /* !HAVE_GETPWUID_R_POSIX */
635 #      ifdef _AIX
636             error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
637             pw = error == 0 ? &pwd : NULL;
638 #      else /* !_AIX */
639             pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
640             error = pw ? 0 : errno;
641 #      endif /* !_AIX */            
642 #    endif /* !HAVE_GETPWUID_R_POSIX */
643             
644             if (!pw)
645               {
646                 /* we bail out prematurely if the user id can't be found
647                  * (should be pretty rare case actually), or if the buffer
648                  * should be sufficiently big and lookups are still not
649                  * successfull.
650                  */
651                 if (error == 0 || error == ENOENT)
652                   {
653                     g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
654                                (gulong) getuid ());
655                     break;
656                   }
657                 if (bufsize > 32 * 1024)
658                   {
659                     g_warning ("getpwuid_r(): failed due to: %s.",
660                                g_strerror (error));
661                     break;
662                   }
663                 
664                 bufsize *= 2;
665               }
666           }
667         while (!pw);
668 #  endif /* !HAVE_GETPWUID_R */
669         
670         if (!pw)
671           {
672             setpwent ();
673             pw = getpwuid (getuid ());
674             endpwent ();
675           }
676         if (pw)
677           {
678             g_user_name = g_strdup (pw->pw_name);
679             g_real_name = g_strdup (pw->pw_gecos);
680             if (!g_home_dir)
681               g_home_dir = g_strdup (pw->pw_dir);
682           }
683         g_free (buffer);
684       }
685       
686 #else /* !HAVE_PWD_H */
687       
688 #  ifdef G_OS_WIN32
689       {
690         guint len = 17;
691         gchar buffer[17];
692         
693         if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
694           {
695             g_user_name = g_strdup (buffer);
696             g_real_name = g_strdup (buffer);
697           }
698       }
699 #  endif /* G_OS_WIN32 */
700       
701 #endif /* !HAVE_PWD_H */
702       
703 #ifdef __EMX__
704       /* change '\\' in %HOME% to '/' */
705       g_strdelimit (g_home_dir, "\\",'/');
706 #endif
707       if (!g_user_name)
708         g_user_name = g_strdup ("somebody");
709       if (!g_real_name)
710         g_real_name = g_strdup ("Unknown");
711       else
712         {
713           gchar *p;
714
715           for (p = g_real_name; *p; p++)
716             if (*p == ',')
717               {
718                 *p = 0;
719                 p = g_strdup (g_real_name);
720                 g_free (g_real_name);
721                 g_real_name = p;
722                 break;
723               }
724         }
725     }
726 }
727
728 gchar*
729 g_get_user_name (void)
730 {
731   G_LOCK (g_utils_global);
732   if (!g_tmp_dir)
733     g_get_any_init ();
734   G_UNLOCK (g_utils_global);
735   
736   return g_user_name;
737 }
738
739 gchar*
740 g_get_real_name (void)
741 {
742   G_LOCK (g_utils_global);
743   if (!g_tmp_dir)
744     g_get_any_init ();
745   G_UNLOCK (g_utils_global);
746  
747   return g_real_name;
748 }
749
750 /* Return the home directory of the user. If there is a HOME
751  * environment variable, its value is returned, otherwise use some
752  * system-dependent way of finding it out. If no home directory can be
753  * deduced, return NULL.
754  */
755
756 gchar*
757 g_get_home_dir (void)
758 {
759   G_LOCK (g_utils_global);
760   if (!g_tmp_dir)
761     g_get_any_init ();
762   G_UNLOCK (g_utils_global);
763   
764   return g_home_dir;
765 }
766
767 /* Return a directory to be used to store temporary files. This is the
768  * value of the TMPDIR, TMP or TEMP environment variables (they are
769  * checked in that order). If none of those exist, use P_tmpdir from
770  * stdio.h.  If that isn't defined, return "/tmp" on POSIXly systems,
771  * and C:\ on Windows.
772  */
773
774 gchar*
775 g_get_tmp_dir (void)
776 {
777   G_LOCK (g_utils_global);
778   if (!g_tmp_dir)
779     g_get_any_init ();
780   G_UNLOCK (g_utils_global);
781   
782   return g_tmp_dir;
783 }
784
785 static gchar *g_prgname = NULL;
786
787 gchar*
788 g_get_prgname (void)
789 {
790   gchar* retval;
791
792   G_LOCK (g_utils_global);
793   retval = g_prgname;
794   G_UNLOCK (g_utils_global);
795
796   return retval;
797 }
798
799 void
800 g_set_prgname (const gchar *prgname)
801 {
802   gchar *c;
803     
804   G_LOCK (g_utils_global);
805   c = g_prgname;
806   g_prgname = g_strdup (prgname);
807   g_free (c);
808   G_UNLOCK (g_utils_global);
809 }
810
811 guint
812 g_direct_hash (gconstpointer v)
813 {
814   return GPOINTER_TO_UINT (v);
815 }
816
817 gint
818 g_direct_equal (gconstpointer v1,
819                 gconstpointer v2)
820 {
821   return v1 == v2;
822 }
823
824 gint
825 g_int_equal (gconstpointer v1,
826              gconstpointer v2)
827 {
828   return *((const gint*) v1) == *((const gint*) v2);
829 }
830
831 guint
832 g_int_hash (gconstpointer v)
833 {
834   return *(const gint*) v;
835 }
836
837 /**
838  * g_get_codeset:
839  * 
840  * Get the codeset for the current locale.
841  * 
842  * Return value: a newly allocated string containing the name
843  * of the codeset. This string must be freed with g_free().
844  **/
845 gchar *
846 g_get_codeset (void)
847 {
848 #ifdef HAVE_CODESET  
849   char *result = nl_langinfo (CODESET);
850   return g_strdup (result);
851 #else
852 #ifndef G_OS_WIN32
853   /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
854    */
855   return g_strdup ("ISO-8859-1");
856 #else
857   /* On Win32 we always use UTF-8. At least in GDK. SO should we
858    * therefore return that?
859    */
860   return g_strdup ("UTF-8");
861 #endif
862 #endif
863 }