Add gettext support.
[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 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
382   static gboolean first_call = TRUE;
383
384   if (first_call)
385     {
386       g_message ("g_basename is deprecated. Use g_path_get_basename instead. "
387                  "Beware that the string returned by g_path_get_basename() has "
388                  " to be g_free()ed.");
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 #ifdef G_OS_WIN32
475   /* Skip \\server\share */
476   if (file_name[0] == G_DIR_SEPARATOR &&
477       file_name[1] == G_DIR_SEPARATOR &&
478       file_name[2])
479     {
480       gchar *p, *q;
481
482       if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
483           p[1])
484         {
485           file_name = p + 1;
486
487           while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
488             file_name++;
489
490           /* Possibly skip a backslash after the share name */
491           if (file_name[0] == G_DIR_SEPARATOR)
492             file_name++;
493
494           return file_name;
495         }
496     }
497 #endif
498   
499   /* Skip initial slashes */
500   if (file_name[0] == G_DIR_SEPARATOR)
501     {
502       while (file_name[0] == G_DIR_SEPARATOR)
503         file_name++;
504       return file_name;
505     }
506
507 #ifdef G_OS_WIN32
508   /* Skip X:\ */
509   if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
510     return file_name + 3;
511 #endif
512
513   return NULL;
514 }
515
516 gchar*
517 g_path_get_dirname (const gchar    *file_name)
518 {
519   register gchar *base;
520   register guint len;
521   
522   g_return_val_if_fail (file_name != NULL, NULL);
523   
524   base = strrchr (file_name, G_DIR_SEPARATOR);
525   if (!base)
526     return g_strdup (".");
527   while (base > file_name && *base == G_DIR_SEPARATOR)
528     base--;
529   len = (guint) 1 + base - file_name;
530   
531   base = g_new (gchar, len + 1);
532   g_memmove (base, file_name, len);
533   base[len] = 0;
534   
535   return base;
536 }
537
538 gchar*
539 g_dirname (const gchar     *file_name)
540 {
541 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
542   static gboolean first_call = TRUE;
543
544   if (first_call)
545     {
546       g_message ("g_dirname() is deprecated. Use g_path_get_dirname() instead.");
547       first_call = FALSE;
548     }
549 #endif /* G_ENABLE_DEBUG */
550
551   return g_path_get_dirname (file_name);
552 }
553
554 gchar*
555 g_get_current_dir (void)
556 {
557   gchar *buffer = NULL;
558   gchar *dir = NULL;
559   static gulong max_len = 0;
560
561   if (max_len == 0) 
562     max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
563   
564   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
565    * and, if that wasn't bad enough, hangs in doing so.
566    */
567 #if     (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
568   buffer = g_new (gchar, max_len + 1);
569   *buffer = 0;
570   dir = getwd (buffer);
571 #else   /* !sun || !HAVE_GETCWD */
572   while (max_len < G_MAXULONG / 2)
573     {
574       buffer = g_new (gchar, max_len + 1);
575       *buffer = 0;
576       dir = getcwd (buffer, max_len);
577
578       if (dir || errno != ERANGE)
579         break;
580
581       g_free (buffer);
582       max_len *= 2;
583     }
584 #endif  /* !sun || !HAVE_GETCWD */
585   
586   if (!dir || !*buffer)
587     {
588       /* hm, should we g_error() out here?
589        * this can happen if e.g. "./" has mode \0000
590        */
591       buffer[0] = G_DIR_SEPARATOR;
592       buffer[1] = 0;
593     }
594
595   dir = g_strdup (buffer);
596   g_free (buffer);
597   
598   return dir;
599 }
600
601 gchar*
602 g_getenv (const gchar *variable)
603 {
604 #ifndef G_OS_WIN32
605   g_return_val_if_fail (variable != NULL, NULL);
606
607   return getenv (variable);
608 #else
609   G_LOCK_DEFINE_STATIC (getenv);
610   struct env_struct
611   {
612     gchar *key;
613     gchar *value;
614   } *env;
615   static GArray *environs = NULL;
616   gchar *system_env;
617   guint length, i;
618   gchar dummy[2];
619
620   g_return_val_if_fail (variable != NULL, NULL);
621   
622   G_LOCK (getenv);
623
624   if (!environs)
625     environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
626
627   /* First we try to find the envinronment variable inside the already
628    * found ones.
629    */
630
631   for (i = 0; i < environs->len; i++)
632     {
633       env = &g_array_index (environs, struct env_struct, i);
634       if (strcmp (env->key, variable) == 0)
635         {
636           g_assert (env->value);
637           G_UNLOCK (getenv);
638           return env->value;
639         }
640     }
641
642   /* If not found, we ask the system */
643
644   system_env = getenv (variable);
645   if (!system_env)
646     {
647       G_UNLOCK (getenv);
648       return NULL;
649     }
650
651   /* On Windows NT, it is relatively typical that environment variables
652    * contain references to other environment variables. Handle that by
653    * calling ExpandEnvironmentStrings.
654    */
655
656   g_array_set_size (environs, environs->len + 1);
657
658   env = &g_array_index (environs, struct env_struct, environs->len - 1);
659
660   /* First check how much space we need */
661   length = ExpandEnvironmentStrings (system_env, dummy, 2);
662
663   /* Then allocate that much, and actualy do the expansion and insert
664    * the new found pair into our buffer 
665    */
666
667   env->value = g_malloc (length);
668   env->key = g_strdup (variable);
669
670   ExpandEnvironmentStrings (system_env, env->value, length);
671
672   G_UNLOCK (getenv);
673   return env->value;
674 #endif
675 }
676
677
678 G_LOCK_DEFINE_STATIC (g_utils_global);
679
680 static  gchar   *g_tmp_dir = NULL;
681 static  gchar   *g_user_name = NULL;
682 static  gchar   *g_real_name = NULL;
683 static  gchar   *g_home_dir = NULL;
684
685 /* HOLDS: g_utils_global_lock */
686 static void
687 g_get_any_init (void)
688 {
689   if (!g_tmp_dir)
690     {
691       g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
692       if (!g_tmp_dir)
693         g_tmp_dir = g_strdup (g_getenv ("TMP"));
694       if (!g_tmp_dir)
695         g_tmp_dir = g_strdup (g_getenv ("TEMP"));
696       
697 #ifdef P_tmpdir
698       if (!g_tmp_dir)
699         {
700           int k;
701           g_tmp_dir = g_strdup (P_tmpdir);
702           k = strlen (g_tmp_dir);
703           if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
704             g_tmp_dir[k-1] = '\0';
705         }
706 #endif
707       
708       if (!g_tmp_dir)
709         {
710 #ifndef G_OS_WIN32
711           g_tmp_dir = g_strdup ("/tmp");
712 #else /* G_OS_WIN32 */
713           g_tmp_dir = g_strdup ("C:\\");
714 #endif /* G_OS_WIN32 */
715         }
716       
717       if (!g_home_dir)
718         g_home_dir = g_strdup (g_getenv ("HOME"));
719       
720 #ifdef G_OS_WIN32
721       /* In case HOME is Unix-style (it happens), convert it to
722        * Windows style.
723        */
724       if (g_home_dir)
725         {
726           gchar *p;
727           while ((p = strchr (g_home_dir, '/')) != NULL)
728             *p = '\\';
729         }
730       else
731         {
732           /* The official way to specify a home directory on NT is
733            * the HOMEDRIVE and HOMEPATH environment variables. At least
734            * it was at some time.
735            */
736           if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
737             {
738               gchar *homedrive, *homepath;
739               
740               homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
741               homepath = g_strdup (g_getenv ("HOMEPATH"));
742               
743               g_home_dir = g_strconcat (homedrive, homepath, NULL);
744               g_free (homedrive);
745               g_free (homepath);
746             }
747         }
748 #endif /* G_OS_WIN32 */
749       
750 #ifdef HAVE_PWD_H
751       {
752         struct passwd *pw = NULL;
753         gpointer buffer = NULL;
754         
755 #  if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
756         struct passwd pwd;
757 #    ifdef _SC_GETPW_R_SIZE_MAX  
758         /* This reurns the maximum length */
759         guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
760 #    else /* _SC_GETPW_R_SIZE_MAX */
761         guint bufsize = 64;
762 #    endif /* _SC_GETPW_R_SIZE_MAX */
763         gint error;
764         
765         do
766           {
767             g_free (buffer);
768             buffer = g_malloc (bufsize);
769             errno = 0;
770             
771 #    ifdef HAVE_POSIX_GETPWUID_R
772             error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
773             error = error < 0 ? errno : error;
774 #    else /* HAVE_NONPOSIX_GETPWUID_R */
775 #      ifdef _AIX
776             error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
777             pw = error == 0 ? &pwd : NULL;
778 #      else /* !_AIX */
779             pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
780             error = pw ? 0 : errno;
781 #      endif /* !_AIX */            
782 #    endif /* HAVE_NONPOSIX_GETPWUID_R */
783             
784             if (!pw)
785               {
786                 /* we bail out prematurely if the user id can't be found
787                  * (should be pretty rare case actually), or if the buffer
788                  * should be sufficiently big and lookups are still not
789                  * successfull.
790                  */
791                 if (error == 0 || error == ENOENT)
792                   {
793                     g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
794                                (gulong) getuid ());
795                     break;
796                   }
797                 if (bufsize > 32 * 1024)
798                   {
799                     g_warning ("getpwuid_r(): failed due to: %s.",
800                                g_strerror (error));
801                     break;
802                   }
803                 
804                 bufsize *= 2;
805               }
806           }
807         while (!pw);
808 #  endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
809         
810         if (!pw)
811           {
812             setpwent ();
813             pw = getpwuid (getuid ());
814             endpwent ();
815           }
816         if (pw)
817           {
818             g_user_name = g_strdup (pw->pw_name);
819             g_real_name = g_strdup (pw->pw_gecos);
820             if (!g_home_dir)
821               g_home_dir = g_strdup (pw->pw_dir);
822           }
823         g_free (buffer);
824       }
825       
826 #else /* !HAVE_PWD_H */
827       
828 #  ifdef G_OS_WIN32
829       {
830         guint len = 17;
831         gchar buffer[17];
832         
833         if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
834           {
835             g_user_name = g_strdup (buffer);
836             g_real_name = g_strdup (buffer);
837           }
838       }
839 #  endif /* G_OS_WIN32 */
840       
841 #endif /* !HAVE_PWD_H */
842       
843 #ifdef __EMX__
844       /* change '\\' in %HOME% to '/' */
845       g_strdelimit (g_home_dir, "\\",'/');
846 #endif
847       if (!g_user_name)
848         g_user_name = g_strdup ("somebody");
849       if (!g_real_name)
850         g_real_name = g_strdup ("Unknown");
851       else
852         {
853           gchar *p;
854
855           for (p = g_real_name; *p; p++)
856             if (*p == ',')
857               {
858                 *p = 0;
859                 p = g_strdup (g_real_name);
860                 g_free (g_real_name);
861                 g_real_name = p;
862                 break;
863               }
864         }
865     }
866 }
867
868 gchar*
869 g_get_user_name (void)
870 {
871   G_LOCK (g_utils_global);
872   if (!g_tmp_dir)
873     g_get_any_init ();
874   G_UNLOCK (g_utils_global);
875   
876   return g_user_name;
877 }
878
879 gchar*
880 g_get_real_name (void)
881 {
882   G_LOCK (g_utils_global);
883   if (!g_tmp_dir)
884     g_get_any_init ();
885   G_UNLOCK (g_utils_global);
886  
887   return g_real_name;
888 }
889
890 /* Return the home directory of the user. If there is a HOME
891  * environment variable, its value is returned, otherwise use some
892  * system-dependent way of finding it out. If no home directory can be
893  * deduced, return NULL.
894  */
895
896 gchar*
897 g_get_home_dir (void)
898 {
899   G_LOCK (g_utils_global);
900   if (!g_tmp_dir)
901     g_get_any_init ();
902   G_UNLOCK (g_utils_global);
903   
904   return g_home_dir;
905 }
906
907 /* Return a directory to be used to store temporary files. This is the
908  * value of the TMPDIR, TMP or TEMP environment variables (they are
909  * checked in that order). If none of those exist, use P_tmpdir from
910  * stdio.h.  If that isn't defined, return "/tmp" on POSIXly systems,
911  * and C:\ on Windows.
912  */
913
914 gchar*
915 g_get_tmp_dir (void)
916 {
917   G_LOCK (g_utils_global);
918   if (!g_tmp_dir)
919     g_get_any_init ();
920   G_UNLOCK (g_utils_global);
921   
922   return g_tmp_dir;
923 }
924
925 static gchar *g_prgname = NULL;
926
927 gchar*
928 g_get_prgname (void)
929 {
930   gchar* retval;
931
932   G_LOCK (g_utils_global);
933   retval = g_prgname;
934   G_UNLOCK (g_utils_global);
935
936   return retval;
937 }
938
939 void
940 g_set_prgname (const gchar *prgname)
941 {
942   gchar *c;
943     
944   G_LOCK (g_utils_global);
945   c = g_prgname;
946   g_prgname = g_strdup (prgname);
947   g_free (c);
948   G_UNLOCK (g_utils_global);
949 }
950
951 guint
952 g_direct_hash (gconstpointer v)
953 {
954   return GPOINTER_TO_UINT (v);
955 }
956
957 gboolean
958 g_direct_equal (gconstpointer v1,
959                 gconstpointer v2)
960 {
961   return v1 == v2;
962 }
963
964 gboolean
965 g_int_equal (gconstpointer v1,
966              gconstpointer v2)
967 {
968   return *((const gint*) v1) == *((const gint*) v2);
969 }
970
971 guint
972 g_int_hash (gconstpointer v)
973 {
974   return *(const gint*) v;
975 }
976
977 /**
978  * g_get_codeset:
979  * 
980  * Get the codeset for the current locale.
981  * 
982  * Return value: a newly allocated string containing the name
983  * of the codeset. This string must be freed with g_free().
984  **/
985 gchar *
986 g_get_codeset (void)
987 {
988 #ifdef HAVE_CODESET  
989   char *result = nl_langinfo (CODESET);
990   return g_strdup (result);
991 #else
992 #ifndef G_OS_WIN32
993   /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
994    */
995   return g_strdup ("ISO-8859-1");
996 #else
997   /* On Win32 we always use UTF-8. At least in GDK. SO should we
998    * therefore return that?
999    */
1000   return g_strdup ("UTF-8");
1001 #endif
1002 #endif
1003 }
1004
1005 #ifdef ENABLE_NLS
1006
1007 #include <libintl.h>
1008
1009 int _glib_gettext_initialized = 0;
1010
1011 char *_glib_gettext_init (const char *str)
1012 {
1013   bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1014   
1015   return dgettext (GETTEXT_PACKAGE, str);
1016 }
1017
1018 #endif /* ENABLE_NLS */
1019
1020