Ok, I'm a moron. When I originally implemented ENABLE_GC_FRIENDLY, I
[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 #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 /* Based on execvp() from GNU Libc.
147  * Some of this code is cut-and-pasted into gspawn.c
148  */
149
150 static gchar*
151 my_strchrnul (const gchar *str, gchar c)
152 {
153   gchar *p = (gchar*)str;
154   while (*p && (*p != c))
155     ++p;
156
157   return p;
158 }
159
160 /**
161  * g_find_program_in_path:
162  * @program: a program name
163  * 
164  * Locates the first executable named @program in the user's path, in the
165  * same way that execvp() would locate it. Returns an allocated string
166  * with the absolute path name, or NULL if the program is not found in
167  * the path. If @program is already an absolute path, returns a copy of
168  * @program if @program exists and is executable, and NULL otherwise.
169  * 
170  * Return value: absolute path, or NULL
171  **/
172 gchar*
173 g_find_program_in_path (const gchar *program)
174 {
175   gchar *path, *p, *name, *freeme;
176   size_t len;
177   size_t pathlen;
178
179   g_return_val_if_fail (program != NULL, NULL);
180
181   if (*program == '/')
182     {
183       if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
184         return g_strdup (program);
185       else
186         return NULL;
187     }
188   
189   path = g_getenv ("PATH");
190   if (path == NULL)
191     {
192       /* There is no `PATH' in the environment.  The default
193        * search path in GNU libc is the current directory followed by
194        * the path `confstr' returns for `_CS_PATH'.
195        */
196       
197       /* In GLib we put . last, for security, and don't use the
198        * unportable confstr(); UNIX98 does not actually specify
199        * what to search if PATH is unset. POSIX may, dunno.
200        */
201       
202       path = "/bin:/usr/bin:.";
203     }
204   
205   len = strlen (program) + 1;
206   pathlen = strlen (path);
207   freeme = name = g_malloc (pathlen + len + 1);
208   
209   /* Copy the file name at the top, including '\0'  */
210   memcpy (name + pathlen + 1, program, len);
211   name = name + pathlen;
212   /* And add the slash before the filename  */
213   *name = '/';
214   
215   p = path;
216   do
217     {
218       char *startp;
219
220       path = p;
221       p = my_strchrnul (path, ':');
222
223       if (p == path)
224         /* Two adjacent colons, or a colon at the beginning or the end
225          * of `PATH' means to search the current directory.
226          */
227         startp = name + 1;
228       else
229         startp = memcpy (name - (p - path), path, p - path);
230
231       if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
232         {
233           gchar *ret;
234           ret = g_strdup (startp);
235           g_free (freeme);
236           return ret;
237         }
238     }
239   while (*p++ != '\0');
240   
241   g_free (freeme);
242
243   return NULL;
244 }
245
246 gint
247 g_snprintf (gchar       *str,
248             gulong       n,
249             gchar const *fmt,
250             ...)
251 {
252 #ifdef  HAVE_VSNPRINTF
253   va_list args;
254   gint retval;
255   
256   g_return_val_if_fail (str != NULL, 0);
257   g_return_val_if_fail (n > 0, 0);
258   g_return_val_if_fail (fmt != NULL, 0);
259
260   va_start (args, fmt);
261   retval = vsnprintf (str, n, fmt, args);
262   va_end (args);
263
264   if (retval < 0)
265     {
266       str[n-1] = '\0';
267       retval = strlen (str);
268     }
269
270   return retval;
271 #else   /* !HAVE_VSNPRINTF */
272   gchar *printed;
273   va_list args;
274   
275   g_return_val_if_fail (str != NULL, 0);
276   g_return_val_if_fail (n > 0, 0);
277   g_return_val_if_fail (fmt != NULL, 0);
278
279   va_start (args, fmt);
280   printed = g_strdup_vprintf (fmt, args);
281   va_end (args);
282   
283   strncpy (str, printed, n);
284   str[n-1] = '\0';
285
286   g_free (printed);
287   
288   return strlen (str);
289 #endif  /* !HAVE_VSNPRINTF */
290 }
291
292 gint
293 g_vsnprintf (gchar       *str,
294              gulong       n,
295              gchar const *fmt,
296              va_list      args)
297 {
298 #ifdef  HAVE_VSNPRINTF
299   gint retval;
300   
301   g_return_val_if_fail (str != NULL, 0);
302   g_return_val_if_fail (n > 0, 0);
303   g_return_val_if_fail (fmt != NULL, 0);
304
305   retval = vsnprintf (str, n, fmt, args);
306   
307   if (retval < 0)
308     {
309       str[n-1] = '\0';
310       retval = strlen (str);
311     }
312
313   return retval;
314 #else   /* !HAVE_VSNPRINTF */
315   gchar *printed;
316   
317   g_return_val_if_fail (str != NULL, 0);
318   g_return_val_if_fail (n > 0, 0);
319   g_return_val_if_fail (fmt != NULL, 0);
320
321   printed = g_strdup_vprintf (fmt, args);
322   strncpy (str, printed, n);
323   str[n-1] = '\0';
324
325   g_free (printed);
326   
327   return strlen (str);
328 #endif /* !HAVE_VSNPRINTF */
329 }
330
331 guint        
332 g_parse_debug_string  (const gchar *string, 
333                        GDebugKey   *keys, 
334                        guint        nkeys)
335 {
336   guint i;
337   guint result = 0;
338   
339   g_return_val_if_fail (string != NULL, 0);
340   
341   if (!g_strcasecmp (string, "all"))
342     {
343       for (i=0; i<nkeys; i++)
344         result |= keys[i].value;
345     }
346   else
347     {
348       gchar *str = g_strdup (string);
349       gchar *p = str;
350       gchar *q;
351       gboolean done = FALSE;
352       
353       while (*p && !done)
354         {
355           q = strchr (p, ':');
356           if (!q)
357             {
358               q = p + strlen(p);
359               done = TRUE;
360             }
361           
362           *q = 0;
363           
364           for (i=0; i<nkeys; i++)
365             if (!g_strcasecmp(keys[i].key, p))
366               result |= keys[i].value;
367           
368           p = q+1;
369         }
370       
371       g_free (str);
372     }
373   
374   return result;
375 }
376
377 gchar*
378 g_basename (const gchar    *file_name)
379 {
380   register gchar *base;
381 #ifdef G_ENABLE_DEBUG
382   static gboolean first_call = TRUE;
383
384   if (first_call)
385     {
386       g_warning("g_basename is deprecated. Use g_path_get_basename instead.");
387       g_warning("Watch out! You have to g_free the string returned by "
388                 "g_path_get_basename.");
389       first_call = FALSE;
390     }
391 #endif /* G_ENABLE_DEBUG */
392   
393   g_return_val_if_fail (file_name != NULL, NULL);
394   
395   base = strrchr (file_name, G_DIR_SEPARATOR);
396   if (base)
397     return base + 1;
398
399 #ifdef G_OS_WIN32
400   if (isalpha (file_name[0]) && file_name[1] == ':')
401     return (gchar*) file_name + 2;
402 #endif /* G_OS_WIN32 */
403   
404   return (gchar*) file_name;
405 }
406
407 gchar*
408 g_path_get_basename (const gchar   *file_name)
409 {
410   register gint base;
411   register gint last_nonslash;
412   guint len;
413   gchar *retval;
414  
415   g_return_val_if_fail (file_name != NULL, NULL);
416   
417   if (file_name[0] == '\0')
418     /* empty string */
419     return g_strdup (".");
420
421   last_nonslash = strlen (file_name) - 1;
422
423   while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
424     last_nonslash--;
425
426   if (last_nonslash == -1)
427     /* string only containing slashes */
428     return g_strdup (G_DIR_SEPARATOR_S);
429
430 #ifdef G_OS_WIN32
431   if (last_nonslash == 1 && isalpha (file_name[0]) && file_name[1] == ':')
432     /* string only containing slashes and a drive */
433     return g_strdup (G_DIR_SEPARATOR_S);
434 #endif /* G_OS_WIN32 */
435
436   base = last_nonslash;
437
438   while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
439     base--;
440
441 #ifdef G_OS_WIN32
442   if (base == -1 && isalpha (file_name[0]) && file_name[1] == ':')
443     base = 1;
444 #endif /* G_OS_WIN32 */
445
446   len = last_nonslash - base;
447   retval = g_malloc (len + 1);
448   memcpy (retval, file_name + base + 1, len);
449   retval [len] = '\0';
450   return retval;
451 }
452
453 gboolean
454 g_path_is_absolute (const gchar *file_name)
455 {
456   g_return_val_if_fail (file_name != NULL, FALSE);
457   
458   if (file_name[0] == G_DIR_SEPARATOR)
459     return TRUE;
460
461 #ifdef G_OS_WIN32
462   if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
463     return TRUE;
464 #endif
465
466   return FALSE;
467 }
468
469 gchar*
470 g_path_skip_root (gchar *file_name)
471 {
472   g_return_val_if_fail (file_name != NULL, NULL);
473   
474   if (file_name[0] == G_DIR_SEPARATOR)
475     return file_name + 1;
476
477 #ifdef G_OS_WIN32
478   if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
479     return file_name + 3;
480 #endif
481
482   return NULL;
483 }
484
485 gchar*
486 g_path_get_dirname (const gchar    *file_name)
487 {
488   register gchar *base;
489   register guint len;
490   
491   g_return_val_if_fail (file_name != NULL, NULL);
492   
493   base = strrchr (file_name, G_DIR_SEPARATOR);
494   if (!base)
495     return g_strdup (".");
496   while (base > file_name && *base == G_DIR_SEPARATOR)
497     base--;
498   len = (guint) 1 + base - file_name;
499   
500   base = g_new (gchar, len + 1);
501   g_memmove (base, file_name, len);
502   base[len] = 0;
503   
504   return base;
505 }
506
507 gchar*
508 g_dirname (const gchar     *file_name)
509 {
510 #ifdef G_ENABLE_DEBUG
511   static gboolean first_call = TRUE;
512
513   if (first_call)
514     {
515       g_warning("g_dirname is deprecated. Use g_path_get_dirname instead.");
516       first_call = FALSE;
517     }
518 #endif /* G_ENABLE_DEBUG */
519
520   return g_path_get_dirname (file_name);
521 }
522
523 gchar*
524 g_get_current_dir (void)
525 {
526   gchar *buffer = NULL;
527   gchar *dir = NULL;
528   static gulong max_len = 0;
529
530   if (max_len == 0) 
531     max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
532   
533   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
534    * and, if that wasn't bad enough, hangs in doing so.
535    */
536 #if     (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
537   buffer = g_new (gchar, max_len + 1);
538   *buffer = 0;
539   dir = getwd (buffer);
540 #else   /* !sun || !HAVE_GETCWD */
541   while (max_len < G_MAXULONG / 2)
542     {
543       buffer = g_new (gchar, max_len + 1);
544       *buffer = 0;
545       dir = getcwd (buffer, max_len);
546
547       if (dir || errno != ERANGE)
548         break;
549
550       g_free (buffer);
551       max_len *= 2;
552     }
553 #endif  /* !sun || !HAVE_GETCWD */
554   
555   if (!dir || !*buffer)
556     {
557       /* hm, should we g_error() out here?
558        * this can happen if e.g. "./" has mode \0000
559        */
560       buffer[0] = G_DIR_SEPARATOR;
561       buffer[1] = 0;
562     }
563
564   dir = g_strdup (buffer);
565   g_free (buffer);
566   
567   return dir;
568 }
569
570 gchar*
571 g_getenv (const gchar *variable)
572 {
573 #ifndef G_OS_WIN32
574   g_return_val_if_fail (variable != NULL, NULL);
575
576   return getenv (variable);
577 #else
578   G_LOCK_DEFINE_STATIC (getenv);
579   struct env_struct
580   {
581     gchar *key;
582     gchar *value;
583   } *env;
584   static GArray *environs = NULL;
585   gchar *system_env;
586   guint length, i;
587   gchar dummy[2];
588
589   g_return_val_if_fail (variable != NULL, NULL);
590   
591   G_LOCK (getenv);
592
593   if (!environs)
594     environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
595
596   /* First we try to find the envinronment variable inside the already
597    * found ones.
598    */
599
600   for (i = 0; i < environs->len; i++)
601     {
602       env = &g_array_index (environs, struct env_struct, i);
603       if (strcmp (env->key, variable) == 0)
604         {
605           g_assert (env->value);
606           G_UNLOCK (getenv);
607           return env->value;
608         }
609     }
610
611   /* If not found, we ask the system */
612
613   system_env = getenv (variable);
614   if (!system_env)
615     {
616       G_UNLOCK (getenv);
617       return NULL;
618     }
619
620   /* On Windows NT, it is relatively typical that environment variables
621    * contain references to other environment variables. Handle that by
622    * calling ExpandEnvironmentStrings.
623    */
624
625   g_array_set_size (environs, environs->len + 1);
626
627   env = &g_array_index (environs, struct env_struct, environs->len - 1);
628
629   /* First check how much space we need */
630   length = ExpandEnvironmentStrings (system_env, dummy, 2);
631
632   /* Then allocate that much, and actualy do the expansion and insert
633    * the new found pair into our buffer 
634    */
635
636   env->value = g_malloc (length);
637   env->key = g_strdup (variable);
638
639   ExpandEnvironmentStrings (system_env, env->value, length);
640
641   G_UNLOCK (getenv);
642   return env->value;
643 #endif
644 }
645
646
647 G_LOCK_DEFINE_STATIC (g_utils_global);
648
649 static  gchar   *g_tmp_dir = NULL;
650 static  gchar   *g_user_name = NULL;
651 static  gchar   *g_real_name = NULL;
652 static  gchar   *g_home_dir = NULL;
653
654 /* HOLDS: g_utils_global_lock */
655 static void
656 g_get_any_init (void)
657 {
658   if (!g_tmp_dir)
659     {
660       g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
661       if (!g_tmp_dir)
662         g_tmp_dir = g_strdup (g_getenv ("TMP"));
663       if (!g_tmp_dir)
664         g_tmp_dir = g_strdup (g_getenv ("TEMP"));
665       
666 #ifdef P_tmpdir
667       if (!g_tmp_dir)
668         {
669           int k;
670           g_tmp_dir = g_strdup (P_tmpdir);
671           k = strlen (g_tmp_dir);
672           if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
673             g_tmp_dir[k-1] = '\0';
674         }
675 #endif
676       
677       if (!g_tmp_dir)
678         {
679 #ifndef G_OS_WIN32
680           g_tmp_dir = g_strdup ("/tmp");
681 #else /* G_OS_WIN32 */
682           g_tmp_dir = g_strdup ("C:\\");
683 #endif /* G_OS_WIN32 */
684         }
685       
686       if (!g_home_dir)
687         g_home_dir = g_strdup (g_getenv ("HOME"));
688       
689 #ifdef G_OS_WIN32
690       if (!g_home_dir)
691         {
692           /* The official way to specify a home directory on NT is
693            * the HOMEDRIVE and HOMEPATH environment variables.
694            *
695            * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
696            * HOME should be a POSIX style pathname.
697            */
698           
699           if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
700             {
701               gchar *homedrive, *homepath;
702               
703               homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
704               homepath = g_strdup (g_getenv ("HOMEPATH"));
705               
706               g_home_dir = g_strconcat (homedrive, homepath, NULL);
707               g_free (homedrive);
708               g_free (homepath);
709             }
710         }
711 #endif /* G_OS_WIN32 */
712       
713 #ifdef HAVE_PWD_H
714       {
715         struct passwd *pw = NULL;
716         gpointer buffer = NULL;
717         
718 #  if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
719         struct passwd pwd;
720 #    ifdef _SC_GETPW_R_SIZE_MAX  
721         /* This reurns the maximum length */
722         guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
723 #    else /* _SC_GETPW_R_SIZE_MAX */
724         guint bufsize = 64;
725 #    endif /* _SC_GETPW_R_SIZE_MAX */
726         gint error;
727         
728         do
729           {
730             g_free (buffer);
731             buffer = g_malloc (bufsize);
732             errno = 0;
733             
734 #    ifdef HAVE_POSIX_GETPWUID_R
735             error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
736             error = error < 0 ? errno : error;
737 #    else /* HAVE_NONPOSIX_GETPWUID_R */
738 #      ifdef _AIX
739             error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
740             pw = error == 0 ? &pwd : NULL;
741 #      else /* !_AIX */
742             pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
743             error = pw ? 0 : errno;
744 #      endif /* !_AIX */            
745 #    endif /* HAVE_NONPOSIX_GETPWUID_R */
746             
747             if (!pw)
748               {
749                 /* we bail out prematurely if the user id can't be found
750                  * (should be pretty rare case actually), or if the buffer
751                  * should be sufficiently big and lookups are still not
752                  * successfull.
753                  */
754                 if (error == 0 || error == ENOENT)
755                   {
756                     g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
757                                (gulong) getuid ());
758                     break;
759                   }
760                 if (bufsize > 32 * 1024)
761                   {
762                     g_warning ("getpwuid_r(): failed due to: %s.",
763                                g_strerror (error));
764                     break;
765                   }
766                 
767                 bufsize *= 2;
768               }
769           }
770         while (!pw);
771 #  endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
772         
773         if (!pw)
774           {
775             setpwent ();
776             pw = getpwuid (getuid ());
777             endpwent ();
778           }
779         if (pw)
780           {
781             g_user_name = g_strdup (pw->pw_name);
782             g_real_name = g_strdup (pw->pw_gecos);
783             if (!g_home_dir)
784               g_home_dir = g_strdup (pw->pw_dir);
785           }
786         g_free (buffer);
787       }
788       
789 #else /* !HAVE_PWD_H */
790       
791 #  ifdef G_OS_WIN32
792       {
793         guint len = 17;
794         gchar buffer[17];
795         
796         if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
797           {
798             g_user_name = g_strdup (buffer);
799             g_real_name = g_strdup (buffer);
800           }
801       }
802 #  endif /* G_OS_WIN32 */
803       
804 #endif /* !HAVE_PWD_H */
805       
806 #ifdef __EMX__
807       /* change '\\' in %HOME% to '/' */
808       g_strdelimit (g_home_dir, "\\",'/');
809 #endif
810       if (!g_user_name)
811         g_user_name = g_strdup ("somebody");
812       if (!g_real_name)
813         g_real_name = g_strdup ("Unknown");
814       else
815         {
816           gchar *p;
817
818           for (p = g_real_name; *p; p++)
819             if (*p == ',')
820               {
821                 *p = 0;
822                 p = g_strdup (g_real_name);
823                 g_free (g_real_name);
824                 g_real_name = p;
825                 break;
826               }
827         }
828     }
829 }
830
831 gchar*
832 g_get_user_name (void)
833 {
834   G_LOCK (g_utils_global);
835   if (!g_tmp_dir)
836     g_get_any_init ();
837   G_UNLOCK (g_utils_global);
838   
839   return g_user_name;
840 }
841
842 gchar*
843 g_get_real_name (void)
844 {
845   G_LOCK (g_utils_global);
846   if (!g_tmp_dir)
847     g_get_any_init ();
848   G_UNLOCK (g_utils_global);
849  
850   return g_real_name;
851 }
852
853 /* Return the home directory of the user. If there is a HOME
854  * environment variable, its value is returned, otherwise use some
855  * system-dependent way of finding it out. If no home directory can be
856  * deduced, return NULL.
857  */
858
859 gchar*
860 g_get_home_dir (void)
861 {
862   G_LOCK (g_utils_global);
863   if (!g_tmp_dir)
864     g_get_any_init ();
865   G_UNLOCK (g_utils_global);
866   
867   return g_home_dir;
868 }
869
870 /* Return a directory to be used to store temporary files. This is the
871  * value of the TMPDIR, TMP or TEMP environment variables (they are
872  * checked in that order). If none of those exist, use P_tmpdir from
873  * stdio.h.  If that isn't defined, return "/tmp" on POSIXly systems,
874  * and C:\ on Windows.
875  */
876
877 gchar*
878 g_get_tmp_dir (void)
879 {
880   G_LOCK (g_utils_global);
881   if (!g_tmp_dir)
882     g_get_any_init ();
883   G_UNLOCK (g_utils_global);
884   
885   return g_tmp_dir;
886 }
887
888 static gchar *g_prgname = NULL;
889
890 gchar*
891 g_get_prgname (void)
892 {
893   gchar* retval;
894
895   G_LOCK (g_utils_global);
896   retval = g_prgname;
897   G_UNLOCK (g_utils_global);
898
899   return retval;
900 }
901
902 void
903 g_set_prgname (const gchar *prgname)
904 {
905   gchar *c;
906     
907   G_LOCK (g_utils_global);
908   c = g_prgname;
909   g_prgname = g_strdup (prgname);
910   g_free (c);
911   G_UNLOCK (g_utils_global);
912 }
913
914 guint
915 g_direct_hash (gconstpointer v)
916 {
917   return GPOINTER_TO_UINT (v);
918 }
919
920 gboolean
921 g_direct_equal (gconstpointer v1,
922                 gconstpointer v2)
923 {
924   return v1 == v2;
925 }
926
927 gboolean
928 g_int_equal (gconstpointer v1,
929              gconstpointer v2)
930 {
931   return *((const gint*) v1) == *((const gint*) v2);
932 }
933
934 guint
935 g_int_hash (gconstpointer v)
936 {
937   return *(const gint*) v;
938 }
939
940 /**
941  * g_get_codeset:
942  * 
943  * Get the codeset for the current locale.
944  * 
945  * Return value: a newly allocated string containing the name
946  * of the codeset. This string must be freed with g_free().
947  **/
948 gchar *
949 g_get_codeset (void)
950 {
951 #ifdef HAVE_CODESET  
952   char *result = nl_langinfo (CODESET);
953   return g_strdup (result);
954 #else
955 #ifndef G_OS_WIN32
956   /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
957    */
958   return g_strdup ("ISO-8859-1");
959 #else
960   /* On Win32 we always use UTF-8. At least in GDK. SO should we
961    * therefore return that?
962    */
963   return g_strdup ("UTF-8");
964 #endif
965 #endif
966 }