Guard against the POSIX allowed behavior where access (file, X_OK)
[platform/upstream/glib.git] / glib / gutils.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* 
28  * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
29  */
30
31 #include "config.h"
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <errno.h>
41 #ifdef HAVE_PWD_H
42 #include <pwd.h>
43 #endif
44 #include <sys/types.h>
45 #ifdef HAVE_SYS_PARAM_H
46 #include <sys/param.h>
47 #endif
48
49 /* implement gutils's inline functions
50  */
51 #define G_IMPLEMENT_INLINES 1
52 #define __G_UTILS_C__
53 #include "glib.h"
54 #include "gprintfint.h"
55
56 #ifdef  MAXPATHLEN
57 #define G_PATH_LENGTH   MAXPATHLEN
58 #elif   defined (PATH_MAX)
59 #define G_PATH_LENGTH   PATH_MAX
60 #elif   defined (_PC_PATH_MAX)
61 #define G_PATH_LENGTH   sysconf(_PC_PATH_MAX)
62 #else   
63 #define G_PATH_LENGTH   2048
64 #endif
65
66 #ifdef G_PLATFORM_WIN32
67 #  define STRICT                /* Strict typing, please */
68 #  include <windows.h>
69 #  undef STRICT
70 #  include <lmcons.h>           /* For UNLEN */
71 #  include <ctype.h>
72 #endif /* G_PLATFORM_WIN32 */
73
74 #ifdef G_OS_WIN32
75 #  include <direct.h>
76 #endif
77
78 #ifdef HAVE_CODESET
79 #include <langinfo.h>
80 #endif
81
82 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
83 #include <libintl.h>
84 #endif
85
86 const guint glib_major_version = GLIB_MAJOR_VERSION;
87 const guint glib_minor_version = GLIB_MINOR_VERSION;
88 const guint glib_micro_version = GLIB_MICRO_VERSION;
89 const guint glib_interface_age = GLIB_INTERFACE_AGE;
90 const guint glib_binary_age = GLIB_BINARY_AGE;
91
92 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
93 void 
94 g_memmove (gpointer dest, gconstpointer src, gulong len)
95 {
96   gchar* destptr = dest;
97   const gchar* srcptr = src;
98   if (src + len < dest || dest + len < src)
99     {
100       bcopy (src, dest, len);
101       return;
102     }
103   else if (dest <= src)
104     {
105       while (len--)
106         *(destptr++) = *(srcptr++);
107     }
108   else
109     {
110       destptr += len;
111       srcptr += len;
112       while (len--)
113         *(--destptr) = *(--srcptr);
114     }
115 }
116 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
117
118 void
119 g_atexit (GVoidFunc func)
120 {
121   gint result;
122   const gchar *error = NULL;
123
124   /* keep this in sync with glib.h */
125
126 #ifdef  G_NATIVE_ATEXIT
127   result = ATEXIT (func);
128   if (result)
129     error = g_strerror (errno);
130 #elif defined (HAVE_ATEXIT)
131 #  ifdef NeXT /* @#%@! NeXTStep */
132   result = !atexit ((void (*)(void)) func);
133   if (result)
134     error = g_strerror (errno);
135 #  else
136   result = atexit ((void (*)(void)) func);
137   if (result)
138     error = g_strerror (errno);
139 #  endif /* NeXT */
140 #elif defined (HAVE_ON_EXIT)
141   result = on_exit ((void (*)(int, void *)) func, NULL);
142   if (result)
143     error = g_strerror (errno);
144 #else
145   result = 0;
146   error = "no implementation";
147 #endif /* G_NATIVE_ATEXIT */
148
149   if (error)
150     g_error ("Could not register atexit() function: %s", error);
151 }
152
153 /* Based on execvp() from GNU Libc.
154  * Some of this code is cut-and-pasted into gspawn.c
155  */
156
157 static gchar*
158 my_strchrnul (const gchar *str, gchar c)
159 {
160   gchar *p = (gchar*)str;
161   while (*p && (*p != c))
162     ++p;
163
164   return p;
165 }
166
167 #ifdef G_OS_WIN32
168
169 gchar *inner_find_program_in_path (const gchar *program);
170
171 gchar*
172 g_find_program_in_path (const gchar *program)
173 {
174   const gchar *last_dot = strrchr (program, '.');
175
176   if (last_dot == NULL || strchr (last_dot, '\\') != NULL)
177     {
178       const gint program_length = strlen (program);
179       const gchar *pathext = getenv ("PATHEXT");
180       const gchar *p;
181       gchar *decorated_program;
182       gchar *retval;
183
184       if (pathext == NULL)
185         pathext = ".com;.exe;.bat";
186
187       p = pathext;
188       do
189         {
190           pathext = p;
191           p = my_strchrnul (pathext, ';');
192
193           decorated_program = g_malloc (program_length + (p-pathext) + 1);
194           memcpy (decorated_program, program, program_length);
195           memcpy (decorated_program+program_length, pathext, p-pathext);
196           decorated_program [program_length + (p-pathext)] = '\0';
197           
198           retval = inner_find_program_in_path (decorated_program);
199           g_free (decorated_program);
200
201           if (retval != NULL)
202             return retval;
203         } while (*p++ != '\0');
204       return NULL;
205     }
206   else
207     return inner_find_program_in_path (program);
208 }
209
210 #define g_find_program_in_path inner_find_program_in_path
211 #endif
212
213 /**
214  * g_find_program_in_path:
215  * @program: a program name
216  * 
217  * Locates the first executable named @program in the user's path, in the
218  * same way that execvp() would locate it. Returns an allocated string
219  * with the absolute path name, or NULL if the program is not found in
220  * the path. If @program is already an absolute path, returns a copy of
221  * @program if @program exists and is executable, and NULL otherwise.
222  * 
223  * On Windows, if @program does not have a file type suffix, tries to
224  * append the suffixes in the PATHEXT environment variable (if that
225  * doesn't exists, the suffixes .com, .exe, and .bat) in turn, and
226  * then look for the resulting file name in the same way as
227  * CreateProcess() would. This means first in the directory where the
228  * program was loaded from, then in the current directory, then in the
229  * Windows 32-bit system directory, then in the Windows directory, and
230  * finally in the directories in the PATH environment variable. If
231  * the program is found, the return value contains the full name
232  * including the type suffix.
233  *
234  * Return value: absolute path, or NULL
235  **/
236 gchar*
237 g_find_program_in_path (const gchar *program)
238 {
239   const gchar *path, *p;
240   gchar *name, *freeme;
241 #ifdef G_OS_WIN32
242   gchar *path_tmp;
243 #endif
244   size_t len;
245   size_t pathlen;
246
247   g_return_val_if_fail (program != NULL, NULL);
248
249   /* If it is an absolute path, or a relative path including subdirectories,
250    * don't look in PATH.
251    */
252   if (g_path_is_absolute (program)
253       || strchr (program, G_DIR_SEPARATOR) != NULL)
254     {
255       if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
256         return g_strdup (program);
257       else
258         return NULL;
259     }
260   
261   path = g_getenv ("PATH");
262 #ifdef G_OS_UNIX
263   if (path == NULL)
264     {
265       /* There is no `PATH' in the environment.  The default
266        * search path in GNU libc is the current directory followed by
267        * the path `confstr' returns for `_CS_PATH'.
268        */
269       
270       /* In GLib we put . last, for security, and don't use the
271        * unportable confstr(); UNIX98 does not actually specify
272        * what to search if PATH is unset. POSIX may, dunno.
273        */
274       
275       path = "/bin:/usr/bin:.";
276     }
277 #else
278   {
279     gchar *tmp;
280     gchar moddir[MAXPATHLEN], sysdir[MAXPATHLEN], windir[MAXPATHLEN];
281
282     GetModuleFileName (NULL, moddir, sizeof (moddir));
283     tmp = g_path_get_dirname (moddir);
284     GetSystemDirectory (sysdir, sizeof (sysdir));
285     GetWindowsDirectory (windir, sizeof (windir));
286     path_tmp = g_strconcat (tmp, ";.;", sysdir, ";", windir,
287                             (path != NULL ? ";" : NULL),
288                             (path != NULL ? path : NULL),
289                             NULL);
290     g_free (tmp);
291     path = path_tmp;
292   }
293 #endif
294   
295   len = strlen (program) + 1;
296   pathlen = strlen (path);
297   freeme = name = g_malloc (pathlen + len + 1);
298   
299   /* Copy the file name at the top, including '\0'  */
300   memcpy (name + pathlen + 1, program, len);
301   name = name + pathlen;
302   /* And add the slash before the filename  */
303   *name = G_DIR_SEPARATOR;
304   
305   p = path;
306   do
307     {
308       char *startp;
309
310       path = p;
311       p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
312
313       if (p == path)
314         /* Two adjacent colons, or a colon at the beginning or the end
315          * of `PATH' means to search the current directory.
316          */
317         startp = name + 1;
318       else
319         startp = memcpy (name - (p - path), path, p - path);
320
321       if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
322         {
323           gchar *ret;
324           ret = g_strdup (startp);
325           g_free (freeme);
326 #ifdef G_OS_WIN32
327           g_free (path_tmp);
328 #endif
329           return ret;
330         }
331     }
332   while (*p++ != '\0');
333   
334   g_free (freeme);
335 #ifdef G_OS_WIN32
336   g_free (path_tmp);
337 #endif
338
339   return NULL;
340 }
341
342 guint        
343 g_parse_debug_string  (const gchar     *string, 
344                        const GDebugKey *keys, 
345                        guint            nkeys)
346 {
347   guint i;
348   guint result = 0;
349   
350   g_return_val_if_fail (string != NULL, 0);
351   
352   if (!g_ascii_strcasecmp (string, "all"))
353     {
354       for (i=0; i<nkeys; i++)
355         result |= keys[i].value;
356     }
357   else
358     {
359       const gchar *p = string;
360       const gchar *q;
361       gboolean done = FALSE;
362       
363       while (*p && !done)
364         {
365           q = strchr (p, ':');
366           if (!q)
367             {
368               q = p + strlen(p);
369               done = TRUE;
370             }
371           
372           for (i=0; i<nkeys; i++)
373             if (g_ascii_strncasecmp(keys[i].key, p, q - p) == 0 &&
374                 keys[i].key[q - p] == '\0')
375               result |= keys[i].value;
376           
377           p = q + 1;
378         }
379     }
380   
381   return result;
382 }
383
384 /**
385  * g_basename:
386  * @file_name: the name of the file.
387  * 
388  * Gets the name of the file without any leading directory components.  
389  * It returns a pointer into the given file name string.
390  * 
391  * Return value: the name of the file without any leading directory components.
392  *
393  * Deprecated: Use g_path_get_basename() instead. 
394  **/
395 G_CONST_RETURN gchar*
396 g_basename (const gchar    *file_name)
397 {
398   register gchar *base;
399   
400   g_return_val_if_fail (file_name != NULL, NULL);
401   
402   base = strrchr (file_name, G_DIR_SEPARATOR);
403   if (base)
404     return base + 1;
405
406 #ifdef G_OS_WIN32
407   if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
408     return (gchar*) file_name + 2;
409 #endif /* G_OS_WIN32 */
410   
411   return (gchar*) file_name;
412 }
413
414 gchar*
415 g_path_get_basename (const gchar   *file_name)
416 {
417   register gssize base;             
418   register gssize last_nonslash;    
419   gsize len;    
420   gchar *retval;
421  
422   g_return_val_if_fail (file_name != NULL, NULL);
423
424   if (file_name[0] == '\0')
425     /* empty string */
426     return g_strdup (".");
427   
428   last_nonslash = strlen (file_name) - 1;
429
430   while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
431     last_nonslash--;
432
433   if (last_nonslash == -1)
434     /* string only containing slashes */
435     return g_strdup (G_DIR_SEPARATOR_S);
436
437 #ifdef G_OS_WIN32
438   if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
439     /* string only containing slashes and a drive */
440     return g_strdup (G_DIR_SEPARATOR_S);
441 #endif /* G_OS_WIN32 */
442
443   base = last_nonslash;
444
445   while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
446     base--;
447
448 #ifdef G_OS_WIN32
449   if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
450     base = 1;
451 #endif /* G_OS_WIN32 */
452
453   len = last_nonslash - base;
454   retval = g_malloc (len + 1);
455   memcpy (retval, file_name + base + 1, len);
456   retval [len] = '\0';
457   return retval;
458 }
459
460 gboolean
461 g_path_is_absolute (const gchar *file_name)
462 {
463   g_return_val_if_fail (file_name != NULL, FALSE);
464   
465   if (file_name[0] == G_DIR_SEPARATOR
466 #ifdef G_OS_WIN32
467       || file_name[0] == '/'
468 #endif
469                                      )
470     return TRUE;
471
472 #ifdef G_OS_WIN32
473   /* Recognize drive letter on native Windows */
474   if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && (file_name[2] == G_DIR_SEPARATOR || file_name[2] == '/'))
475     return TRUE;
476 #endif /* G_OS_WIN32 */
477
478   return FALSE;
479 }
480
481 G_CONST_RETURN gchar*
482 g_path_skip_root (const gchar *file_name)
483 {
484   g_return_val_if_fail (file_name != NULL, NULL);
485   
486 #ifdef G_PLATFORM_WIN32
487   /* Skip \\server\share (Win32) or //server/share (Cygwin) */
488   if (file_name[0] == G_DIR_SEPARATOR &&
489       file_name[1] == G_DIR_SEPARATOR &&
490       file_name[2])
491     {
492       gchar *p;
493
494       if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
495           p[1])
496         {
497           file_name = p + 1;
498
499           while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
500             file_name++;
501
502           /* Possibly skip a backslash after the share name */
503           if (file_name[0] == G_DIR_SEPARATOR)
504             file_name++;
505
506           return (gchar *)file_name;
507         }
508     }
509 #endif
510   
511   /* Skip initial slashes */
512   if (file_name[0] == G_DIR_SEPARATOR)
513     {
514       while (file_name[0] == G_DIR_SEPARATOR)
515         file_name++;
516       return (gchar *)file_name;
517     }
518
519 #ifdef G_OS_WIN32
520   /* Skip X:\ */
521   if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
522     return (gchar *)file_name + 3;
523 #endif
524
525   return NULL;
526 }
527
528 gchar*
529 g_path_get_dirname (const gchar    *file_name)
530 {
531   register gchar *base;
532   register gsize len;    
533   
534   g_return_val_if_fail (file_name != NULL, NULL);
535   
536   base = strrchr (file_name, G_DIR_SEPARATOR);
537   if (!base)
538     return g_strdup (".");
539   while (base > file_name && *base == G_DIR_SEPARATOR)
540     base--;
541   len = (guint) 1 + base - file_name;
542   
543   base = g_new (gchar, len + 1);
544   g_memmove (base, file_name, len);
545   base[len] = 0;
546   
547   return base;
548 }
549
550 gchar*
551 g_get_current_dir (void)
552 {
553   gchar *buffer = NULL;
554   gchar *dir = NULL;
555   static gulong max_len = 0;
556
557   if (max_len == 0) 
558     max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
559   
560   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
561    * and, if that wasn't bad enough, hangs in doing so.
562    */
563 #if     (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
564   buffer = g_new (gchar, max_len + 1);
565   *buffer = 0;
566   dir = getwd (buffer);
567 #else   /* !sun || !HAVE_GETCWD */
568   while (max_len < G_MAXULONG / 2)
569     {
570       buffer = g_new (gchar, max_len + 1);
571       *buffer = 0;
572       dir = getcwd (buffer, max_len);
573
574       if (dir || errno != ERANGE)
575         break;
576
577       g_free (buffer);
578       max_len *= 2;
579     }
580 #endif  /* !sun || !HAVE_GETCWD */
581   
582   if (!dir || !*buffer)
583     {
584       /* hm, should we g_error() out here?
585        * this can happen if e.g. "./" has mode \0000
586        */
587       buffer[0] = G_DIR_SEPARATOR;
588       buffer[1] = 0;
589     }
590
591   dir = g_strdup (buffer);
592   g_free (buffer);
593   
594   return dir;
595 }
596
597 G_CONST_RETURN gchar*
598 g_getenv (const gchar *variable)
599 {
600 #ifndef G_OS_WIN32
601   g_return_val_if_fail (variable != NULL, NULL);
602
603   return getenv (variable);
604 #else
605   G_LOCK_DEFINE_STATIC (getenv);
606   struct env_struct
607   {
608     gchar *key;
609     gchar *value;
610   } *env;
611   static GArray *environs = NULL;
612   gchar *system_env;
613   guint length, i;
614   gchar dummy[2];
615
616   g_return_val_if_fail (variable != NULL, NULL);
617   
618   G_LOCK (getenv);
619
620   if (!environs)
621     environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
622
623   /* First we try to find the envinronment variable inside the already
624    * found ones.
625    */
626
627   for (i = 0; i < environs->len; i++)
628     {
629       env = &g_array_index (environs, struct env_struct, i);
630       if (strcmp (env->key, variable) == 0)
631         {
632           g_assert (env->value);
633           G_UNLOCK (getenv);
634           return env->value;
635         }
636     }
637
638   /* If not found, we ask the system */
639
640   system_env = getenv (variable);
641   if (!system_env)
642     {
643       G_UNLOCK (getenv);
644       return NULL;
645     }
646
647   /* On Windows NT, it is relatively typical that environment variables
648    * contain references to other environment variables. Handle that by
649    * calling ExpandEnvironmentStrings.
650    */
651
652   g_array_set_size (environs, environs->len + 1);
653
654   env = &g_array_index (environs, struct env_struct, environs->len - 1);
655
656   /* First check how much space we need */
657   length = ExpandEnvironmentStrings (system_env, dummy, 2);
658
659   /* Then allocate that much, and actualy do the expansion and insert
660    * the new found pair into our buffer 
661    */
662
663   env->value = g_malloc (length);
664   env->key = g_strdup (variable);
665
666   ExpandEnvironmentStrings (system_env, env->value, length);
667
668   G_UNLOCK (getenv);
669   return env->value;
670 #endif
671 }
672
673
674 G_LOCK_DEFINE_STATIC (g_utils_global);
675
676 static  gchar   *g_tmp_dir = NULL;
677 static  gchar   *g_user_name = NULL;
678 static  gchar   *g_real_name = NULL;
679 static  gchar   *g_home_dir = NULL;
680
681 /* HOLDS: g_utils_global_lock */
682 static void
683 g_get_any_init (void)
684 {
685   if (!g_tmp_dir)
686     {
687       g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
688       if (!g_tmp_dir)
689         g_tmp_dir = g_strdup (g_getenv ("TMP"));
690       if (!g_tmp_dir)
691         g_tmp_dir = g_strdup (g_getenv ("TEMP"));
692       
693 #ifdef P_tmpdir
694       if (!g_tmp_dir)
695         {
696           gsize k;    
697           g_tmp_dir = g_strdup (P_tmpdir);
698           k = strlen (g_tmp_dir);
699           if (k > 1 && g_tmp_dir[k - 1] == G_DIR_SEPARATOR)
700             g_tmp_dir[k - 1] = '\0';
701         }
702 #endif
703       
704       if (!g_tmp_dir)
705         {
706 #ifndef G_OS_WIN32
707           g_tmp_dir = g_strdup ("/tmp");
708 #else /* G_OS_WIN32 */
709           g_tmp_dir = g_strdup ("C:\\");
710 #endif /* G_OS_WIN32 */
711         }
712       
713 #ifdef G_OS_WIN32
714       /* We check $HOME first for Win32, though it is a last resort for Unix
715        * where we prefer the results of getpwuid().
716        */
717       g_home_dir = g_strdup (g_getenv ("HOME"));
718       
719       /* In case HOME is Unix-style (it happens), convert it to
720        * Windows style.
721        */
722       if (g_home_dir)
723         {
724           gchar *p;
725           while ((p = strchr (g_home_dir, '/')) != NULL)
726             *p = '\\';
727         }
728
729       if (!g_home_dir)
730         {
731           /* USERPROFILE is probably the closest equivalent to $HOME? */
732           if (getenv ("USERPROFILE") != NULL)
733             g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
734         }
735
736       if (!g_home_dir)
737         {
738           /* At least at some time, HOMEDRIVE and HOMEPATH were used
739            * to point to the home directory, I think. But on Windows
740            * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
741            * HOMEPATH is its root "\"?
742            */
743           if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
744             {
745               gchar *homedrive, *homepath;
746               
747               homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
748               homepath = g_strdup (g_getenv ("HOMEPATH"));
749               
750               g_home_dir = g_strconcat (homedrive, homepath, NULL);
751               g_free (homedrive);
752               g_free (homepath);
753             }
754         }
755 #endif /* G_OS_WIN32 */
756       
757 #ifdef HAVE_PWD_H
758       {
759         struct passwd *pw = NULL;
760         gpointer buffer = NULL;
761         gint error;
762         
763 #  if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
764         struct passwd pwd;
765 #    ifdef _SC_GETPW_R_SIZE_MAX  
766         /* This reurns the maximum length */
767         glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
768         
769         if (bufsize < 0)
770           bufsize = 64;
771 #    else /* _SC_GETPW_R_SIZE_MAX */
772         glong bufsize = 64;
773 #    endif /* _SC_GETPW_R_SIZE_MAX */
774         
775         do
776           {
777             g_free (buffer);
778             buffer = g_malloc (bufsize);
779             errno = 0;
780             
781 #    ifdef HAVE_POSIX_GETPWUID_R
782             error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
783             error = error < 0 ? errno : error;
784 #    else /* HAVE_NONPOSIX_GETPWUID_R */
785 #      ifdef _AIX
786             error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
787             pw = error == 0 ? &pwd : NULL;
788 #      else /* !_AIX */
789             pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
790             error = pw ? 0 : errno;
791 #      endif /* !_AIX */            
792 #    endif /* HAVE_NONPOSIX_GETPWUID_R */
793             
794             if (!pw)
795               {
796                 /* we bail out prematurely if the user id can't be found
797                  * (should be pretty rare case actually), or if the buffer
798                  * should be sufficiently big and lookups are still not
799                  * successfull.
800                  */
801                 if (error == 0 || error == ENOENT)
802                   {
803                     g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
804                                (gulong) getuid ());
805                     break;
806                   }
807                 if (bufsize > 32 * 1024)
808                   {
809                     g_warning ("getpwuid_r(): failed due to: %s.",
810                                g_strerror (error));
811                     break;
812                   }
813                 
814                 bufsize *= 2;
815               }
816           }
817         while (!pw);
818 #  endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
819         
820         if (!pw)
821           {
822             setpwent ();
823             pw = getpwuid (getuid ());
824             endpwent ();
825           }
826         if (pw)
827           {
828             g_user_name = g_strdup (pw->pw_name);
829             g_real_name = g_strdup (pw->pw_gecos);
830             if (!g_home_dir)
831               g_home_dir = g_strdup (pw->pw_dir);
832           }
833         g_free (buffer);
834       }
835       
836 #else /* !HAVE_PWD_H */
837       
838 #  ifdef G_OS_WIN32
839       {
840         guint len = UNLEN+1;
841         gchar buffer[UNLEN+1];
842         
843         if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
844           {
845             g_user_name = g_strdup (buffer);
846             g_real_name = g_strdup (buffer);
847           }
848       }
849 #  endif /* G_OS_WIN32 */
850
851 #endif /* !HAVE_PWD_H */
852
853       if (!g_home_dir)
854         g_home_dir = g_strdup (g_getenv ("HOME"));
855       
856 #ifdef __EMX__
857       /* change '\\' in %HOME% to '/' */
858       g_strdelimit (g_home_dir, "\\",'/');
859 #endif
860       if (!g_user_name)
861         g_user_name = g_strdup ("somebody");
862       if (!g_real_name)
863         g_real_name = g_strdup ("Unknown");
864       else
865         {
866           gchar *p;
867
868           for (p = g_real_name; *p; p++)
869             if (*p == ',')
870               {
871                 *p = 0;
872                 p = g_strdup (g_real_name);
873                 g_free (g_real_name);
874                 g_real_name = p;
875                 break;
876               }
877         }
878     }
879 }
880
881 G_CONST_RETURN gchar*
882 g_get_user_name (void)
883 {
884   G_LOCK (g_utils_global);
885   if (!g_tmp_dir)
886     g_get_any_init ();
887   G_UNLOCK (g_utils_global);
888   
889   return g_user_name;
890 }
891
892 G_CONST_RETURN gchar*
893 g_get_real_name (void)
894 {
895   G_LOCK (g_utils_global);
896   if (!g_tmp_dir)
897     g_get_any_init ();
898   G_UNLOCK (g_utils_global);
899  
900   return g_real_name;
901 }
902
903 /* Return the home directory of the user. If there is a HOME
904  * environment variable, its value is returned, otherwise use some
905  * system-dependent way of finding it out. If no home directory can be
906  * deduced, return NULL.
907  */
908
909 G_CONST_RETURN gchar*
910 g_get_home_dir (void)
911 {
912   G_LOCK (g_utils_global);
913   if (!g_tmp_dir)
914     g_get_any_init ();
915   G_UNLOCK (g_utils_global);
916   
917   return g_home_dir;
918 }
919
920 /* Return a directory to be used to store temporary files. This is the
921  * value of the TMPDIR, TMP or TEMP environment variables (they are
922  * checked in that order). If none of those exist, use P_tmpdir from
923  * stdio.h.  If that isn't defined, return "/tmp" on POSIXly systems,
924  * and C:\ on Windows.
925  */
926
927 G_CONST_RETURN gchar*
928 g_get_tmp_dir (void)
929 {
930   G_LOCK (g_utils_global);
931   if (!g_tmp_dir)
932     g_get_any_init ();
933   G_UNLOCK (g_utils_global);
934   
935   return g_tmp_dir;
936 }
937
938 G_LOCK_DEFINE (g_prgname);
939 static gchar *g_prgname = NULL;
940
941 gchar*
942 g_get_prgname (void)
943 {
944   gchar* retval;
945
946   G_LOCK (g_prgname);
947   retval = g_prgname;
948   G_UNLOCK (g_prgname);
949
950   return retval;
951 }
952
953 void
954 g_set_prgname (const gchar *prgname)
955 {
956   G_LOCK (g_prgname);
957   g_free (g_prgname);
958   g_prgname = g_strdup (prgname);
959   G_UNLOCK (g_prgname);
960 }
961
962 G_LOCK_DEFINE (g_application_name);
963 static gchar *g_application_name = NULL;
964
965 /**
966  * g_get_application_name:
967  * 
968  * Gets a human-readable name for the application, as set by
969  * g_set_application_name(). This name should be localized if
970  * possible, and is intended for display to the user.  Contrast with
971  * g_get_prgname(), which gets a non-localized name. If
972  * g_set_application_name() has not been called, returns the result of
973  * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
974  * been called).
975  * 
976  * Return value: human-readable application name. may return %NULL
977  *
978  * Since: 2.2
979  **/
980 G_CONST_RETURN gchar*
981 g_get_application_name (void)
982 {
983   gchar* retval;
984
985   G_LOCK (g_application_name);
986   retval = g_application_name;
987   G_UNLOCK (g_application_name);
988
989   if (retval == NULL)
990     return g_get_prgname ();
991   
992   return retval;
993 }
994
995 /**
996  * g_set_application_name:
997  * @application_name: localized name of the application
998  *
999  * Sets a human-readable name for the application. This name should be
1000  * localized if possible, and is intended for display to the user.
1001  * Contrast with g_set_prgname(), which sets a non-localized name.
1002  * g_set_prgname() will be called automatically by gtk_init(),
1003  * but g_set_application_name() will not.
1004  *
1005  * Note that for thread safety reasons, this function can only
1006  * be called once.
1007  *
1008  * The application name will be used in contexts such as error messages,
1009  * or when displaying an application's name in the task list.
1010  * 
1011  **/
1012 void
1013 g_set_application_name (const gchar *application_name)
1014 {
1015   gboolean already_set = FALSE;
1016         
1017   G_LOCK (g_application_name);
1018   if (g_application_name)
1019     already_set = TRUE;
1020   else
1021     g_application_name = g_strdup (application_name);
1022   G_UNLOCK (g_application_name);
1023
1024   if (already_set)
1025     g_warning ("g_set_application() name called multiple times");
1026 }
1027
1028 guint
1029 g_direct_hash (gconstpointer v)
1030 {
1031   return GPOINTER_TO_UINT (v);
1032 }
1033
1034 gboolean
1035 g_direct_equal (gconstpointer v1,
1036                 gconstpointer v2)
1037 {
1038   return v1 == v2;
1039 }
1040
1041 gboolean
1042 g_int_equal (gconstpointer v1,
1043              gconstpointer v2)
1044 {
1045   return *((const gint*) v1) == *((const gint*) v2);
1046 }
1047
1048 guint
1049 g_int_hash (gconstpointer v)
1050 {
1051   return *(const gint*) v;
1052 }
1053
1054 /**
1055  * g_nullify_pointer:
1056  * @nullify_location: the memory address of the pointer.
1057  * 
1058  * Set the pointer at the specified location to %NULL.
1059  **/
1060 void
1061 g_nullify_pointer (gpointer *nullify_location)
1062 {
1063   g_return_if_fail (nullify_location != NULL);
1064
1065   *nullify_location = NULL;
1066 }
1067
1068 /**
1069  * g_get_codeset:
1070  * 
1071  * Get the codeset for the current locale.
1072  * 
1073  * Return value: a newly allocated string containing the name
1074  * of the codeset. This string must be freed with g_free().
1075  **/
1076 gchar *
1077 g_get_codeset (void)
1078 {
1079 #ifdef HAVE_CODESET  
1080   char *result = nl_langinfo (CODESET);
1081   return g_strdup (result);
1082 #else
1083 #ifdef G_PLATFORM_WIN32
1084   return g_strdup_printf ("CP%d", GetACP ());
1085 #else
1086   /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
1087    */
1088   return g_strdup ("ISO-8859-1");
1089 #endif
1090 #endif
1091 }
1092
1093 #ifdef ENABLE_NLS
1094
1095 #include <libintl.h>
1096
1097 #ifdef G_PLATFORM_WIN32
1098
1099 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
1100
1101 static const gchar *
1102 _glib_get_locale_dir (void)
1103 {
1104   static const gchar *cache = NULL;
1105   if (cache == NULL)
1106     cache = g_win32_get_package_installation_subdirectory
1107       (GETTEXT_PACKAGE, dll_name, "lib\\locale");
1108
1109   return cache;
1110 }
1111
1112 #undef GLIB_LOCALE_DIR
1113 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
1114
1115 #endif /* G_PLATFORM_WIN32 */
1116
1117 G_CONST_RETURN gchar *
1118 _glib_gettext (const gchar *str)
1119 {
1120   static gboolean _glib_gettext_initialized = FALSE;
1121
1122   if (!_glib_gettext_initialized)
1123     {
1124       bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1125 #    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1126       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1127 #    endif
1128       _glib_gettext_initialized = TRUE;
1129     }
1130   
1131   return dgettext (GETTEXT_PACKAGE, str);
1132 }
1133
1134 #endif /* ENABLE_NLS */