fix indentation (GConvertError): generic error is conventionally called
[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_IMPLEMENT_INLINES 1
54 #define __G_UTILS_C__
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 = 0;
429
430   if (max_len == 0) 
431     max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
432   
433   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
434    * and, if that wasn't bad enough, hangs in doing so.
435    */
436 #if     (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
437   buffer = g_new (gchar, max_len + 1);
438   *buffer = 0;
439   dir = getwd (buffer);
440 #else   /* !sun || !HAVE_GETCWD */
441   while (max_len < G_MAXULONG / 2)
442     {
443       buffer = g_new (gchar, max_len + 1);
444       *buffer = 0;
445       dir = getcwd (buffer, max_len);
446
447       if (dir || errno != ERANGE)
448         break;
449
450       g_free (buffer);
451       max_len *= 2;
452     }
453 #endif  /* !sun || !HAVE_GETCWD */
454   
455   if (!dir || !*buffer)
456     {
457       /* hm, should we g_error() out here?
458        * this can happen if e.g. "./" has mode \0000
459        */
460       buffer[0] = G_DIR_SEPARATOR;
461       buffer[1] = 0;
462     }
463
464   dir = g_strdup (buffer);
465   g_free (buffer);
466   
467   return dir;
468 }
469
470 gchar*
471 g_getenv (const gchar *variable)
472 {
473 #ifndef G_OS_WIN32
474   g_return_val_if_fail (variable != NULL, NULL);
475
476   return getenv (variable);
477 #else
478   G_LOCK_DEFINE_STATIC (getenv);
479   struct env_struct
480   {
481     gchar *key;
482     gchar *value;
483   } *env;
484   static GArray *environs = NULL;
485   gchar *system_env;
486   guint length, i;
487   gchar dummy[2];
488
489   g_return_val_if_fail (variable != NULL, NULL);
490   
491   G_LOCK (getenv);
492
493   if (!environs)
494     environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
495
496   /* First we try to find the envinronment variable inside the already
497    * found ones.
498    */
499
500   for (i = 0; i < environs->len; i++)
501     {
502       env = &g_array_index (environs, struct env_struct, i);
503       if (strcmp (env->key, variable) == 0)
504         {
505           g_assert (env->value);
506           G_UNLOCK (getenv);
507           return env->value;
508         }
509     }
510
511   /* If not found, we ask the system */
512
513   system_env = getenv (variable);
514   if (!system_env)
515     {
516       G_UNLOCK (getenv);
517       return NULL;
518     }
519
520   /* On Windows NT, it is relatively typical that environment variables
521    * contain references to other environment variables. Handle that by
522    * calling ExpandEnvironmentStrings.
523    */
524
525   g_array_set_size (environs, environs->len + 1);
526
527   env = &g_array_index (environs, struct env_struct, environs->len - 1);
528
529   /* First check how much space we need */
530   length = ExpandEnvironmentStrings (system_env, dummy, 2);
531
532   /* Then allocate that much, and actualy do the expansion and insert
533    * the new found pair into our buffer 
534    */
535
536   env->value = g_malloc (length);
537   env->key = g_strdup (variable);
538
539   ExpandEnvironmentStrings (system_env, env->value, length);
540
541   G_UNLOCK (getenv);
542   return env->value;
543 #endif
544 }
545
546
547 G_LOCK_DEFINE_STATIC (g_utils_global);
548
549 static  gchar   *g_tmp_dir = NULL;
550 static  gchar   *g_user_name = NULL;
551 static  gchar   *g_real_name = NULL;
552 static  gchar   *g_home_dir = NULL;
553
554 /* HOLDS: g_utils_global_lock */
555 static void
556 g_get_any_init (void)
557 {
558   if (!g_tmp_dir)
559     {
560       g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
561       if (!g_tmp_dir)
562         g_tmp_dir = g_strdup (g_getenv ("TMP"));
563       if (!g_tmp_dir)
564         g_tmp_dir = g_strdup (g_getenv ("TEMP"));
565       
566 #ifdef P_tmpdir
567       if (!g_tmp_dir)
568         {
569           int k;
570           g_tmp_dir = g_strdup (P_tmpdir);
571           k = strlen (g_tmp_dir);
572           if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
573             g_tmp_dir[k-1] = '\0';
574         }
575 #endif
576       
577       if (!g_tmp_dir)
578         {
579 #ifndef G_OS_WIN32
580           g_tmp_dir = g_strdup ("/tmp");
581 #else /* G_OS_WIN32 */
582           g_tmp_dir = g_strdup ("C:\\");
583 #endif /* G_OS_WIN32 */
584         }
585       
586       if (!g_home_dir)
587         g_home_dir = g_strdup (g_getenv ("HOME"));
588       
589 #ifdef G_OS_WIN32
590       if (!g_home_dir)
591         {
592           /* The official way to specify a home directory on NT is
593            * the HOMEDRIVE and HOMEPATH environment variables.
594            *
595            * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
596            * HOME should be a POSIX style pathname.
597            */
598           
599           if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
600             {
601               gchar *homedrive, *homepath;
602               
603               homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
604               homepath = g_strdup (g_getenv ("HOMEPATH"));
605               
606               g_home_dir = g_strconcat (homedrive, homepath, NULL);
607               g_free (homedrive);
608               g_free (homepath);
609             }
610         }
611 #endif /* G_OS_WIN32 */
612       
613 #ifdef HAVE_PWD_H
614       {
615         struct passwd *pw = NULL;
616         gpointer buffer = NULL;
617         
618 #  ifdef HAVE_GETPWUID_R
619         struct passwd pwd;
620 #    ifdef _SC_GETPW_R_SIZE_MAX  
621         /* This reurns the maximum length */
622         guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
623 #    else /* _SC_GETPW_R_SIZE_MAX */
624         guint bufsize = 64;
625 #    endif /* _SC_GETPW_R_SIZE_MAX */
626         gint error;
627         
628         do
629           {
630             g_free (buffer);
631             buffer = g_malloc (bufsize);
632             errno = 0;
633             
634 #    ifdef HAVE_GETPWUID_R_POSIX
635             error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
636             error = error < 0 ? errno : error;
637 #    else /* !HAVE_GETPWUID_R_POSIX */
638 #      ifdef _AIX
639             error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
640             pw = error == 0 ? &pwd : NULL;
641 #      else /* !_AIX */
642             pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
643             error = pw ? 0 : errno;
644 #      endif /* !_AIX */            
645 #    endif /* !HAVE_GETPWUID_R_POSIX */
646             
647             if (!pw)
648               {
649                 /* we bail out prematurely if the user id can't be found
650                  * (should be pretty rare case actually), or if the buffer
651                  * should be sufficiently big and lookups are still not
652                  * successfull.
653                  */
654                 if (error == 0 || error == ENOENT)
655                   {
656                     g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
657                                (gulong) getuid ());
658                     break;
659                   }
660                 if (bufsize > 32 * 1024)
661                   {
662                     g_warning ("getpwuid_r(): failed due to: %s.",
663                                g_strerror (error));
664                     break;
665                   }
666                 
667                 bufsize *= 2;
668               }
669           }
670         while (!pw);
671 #  endif /* !HAVE_GETPWUID_R */
672         
673         if (!pw)
674           {
675             setpwent ();
676             pw = getpwuid (getuid ());
677             endpwent ();
678           }
679         if (pw)
680           {
681             g_user_name = g_strdup (pw->pw_name);
682             g_real_name = g_strdup (pw->pw_gecos);
683             if (!g_home_dir)
684               g_home_dir = g_strdup (pw->pw_dir);
685           }
686         g_free (buffer);
687       }
688       
689 #else /* !HAVE_PWD_H */
690       
691 #  ifdef G_OS_WIN32
692       {
693         guint len = 17;
694         gchar buffer[17];
695         
696         if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
697           {
698             g_user_name = g_strdup (buffer);
699             g_real_name = g_strdup (buffer);
700           }
701       }
702 #  endif /* G_OS_WIN32 */
703       
704 #endif /* !HAVE_PWD_H */
705       
706 #ifdef __EMX__
707       /* change '\\' in %HOME% to '/' */
708       g_strdelimit (g_home_dir, "\\",'/');
709 #endif
710       if (!g_user_name)
711         g_user_name = g_strdup ("somebody");
712       if (!g_real_name)
713         g_real_name = g_strdup ("Unknown");
714       else
715         {
716           gchar *p;
717
718           for (p = g_real_name; *p; p++)
719             if (*p == ',')
720               {
721                 *p = 0;
722                 p = g_strdup (g_real_name);
723                 g_free (g_real_name);
724                 g_real_name = p;
725                 break;
726               }
727         }
728     }
729 }
730
731 gchar*
732 g_get_user_name (void)
733 {
734   G_LOCK (g_utils_global);
735   if (!g_tmp_dir)
736     g_get_any_init ();
737   G_UNLOCK (g_utils_global);
738   
739   return g_user_name;
740 }
741
742 gchar*
743 g_get_real_name (void)
744 {
745   G_LOCK (g_utils_global);
746   if (!g_tmp_dir)
747     g_get_any_init ();
748   G_UNLOCK (g_utils_global);
749  
750   return g_real_name;
751 }
752
753 /* Return the home directory of the user. If there is a HOME
754  * environment variable, its value is returned, otherwise use some
755  * system-dependent way of finding it out. If no home directory can be
756  * deduced, return NULL.
757  */
758
759 gchar*
760 g_get_home_dir (void)
761 {
762   G_LOCK (g_utils_global);
763   if (!g_tmp_dir)
764     g_get_any_init ();
765   G_UNLOCK (g_utils_global);
766   
767   return g_home_dir;
768 }
769
770 /* Return a directory to be used to store temporary files. This is the
771  * value of the TMPDIR, TMP or TEMP environment variables (they are
772  * checked in that order). If none of those exist, use P_tmpdir from
773  * stdio.h.  If that isn't defined, return "/tmp" on POSIXly systems,
774  * and C:\ on Windows.
775  */
776
777 gchar*
778 g_get_tmp_dir (void)
779 {
780   G_LOCK (g_utils_global);
781   if (!g_tmp_dir)
782     g_get_any_init ();
783   G_UNLOCK (g_utils_global);
784   
785   return g_tmp_dir;
786 }
787
788 static gchar *g_prgname = NULL;
789
790 gchar*
791 g_get_prgname (void)
792 {
793   gchar* retval;
794
795   G_LOCK (g_utils_global);
796   retval = g_prgname;
797   G_UNLOCK (g_utils_global);
798
799   return retval;
800 }
801
802 void
803 g_set_prgname (const gchar *prgname)
804 {
805   gchar *c;
806     
807   G_LOCK (g_utils_global);
808   c = g_prgname;
809   g_prgname = g_strdup (prgname);
810   g_free (c);
811   G_UNLOCK (g_utils_global);
812 }
813
814 guint
815 g_direct_hash (gconstpointer v)
816 {
817   return GPOINTER_TO_UINT (v);
818 }
819
820 gint
821 g_direct_equal (gconstpointer v1,
822                 gconstpointer v2)
823 {
824   return v1 == v2;
825 }
826
827 gint
828 g_int_equal (gconstpointer v1,
829              gconstpointer v2)
830 {
831   return *((const gint*) v1) == *((const gint*) v2);
832 }
833
834 guint
835 g_int_hash (gconstpointer v)
836 {
837   return *(const gint*) v;
838 }
839
840 /**
841  * g_get_codeset:
842  * 
843  * Get the codeset for the current locale.
844  * 
845  * Return value: a newly allocated string containing the name
846  * of the codeset. This string must be freed with g_free().
847  **/
848 gchar *
849 g_get_codeset (void)
850 {
851 #ifdef HAVE_CODESET  
852   char *result = nl_langinfo (CODESET);
853   return g_strdup (result);
854 #else
855 #ifndef G_OS_WIN32
856   /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
857    */
858   return g_strdup ("ISO-8859-1");
859 #else
860   /* On Win32 we always use UTF-8. At least in GDK. SO should we
861    * therefore return that?
862    */
863   return g_strdup ("UTF-8");
864 #endif
865 #endif
866 }