Version 11, interface, binary age 0.
[glib.git] / glib / gutils.c
blob6727181b95a84cacaac6f7a65bb0a02f2848b2a0
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 /*
28 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
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
51 /* implement gutils's inline functions
53 #define G_IMPLEMENT_INLINES 1
54 #define __G_UTILS_C__
55 #include "glib.h"
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
67 #ifdef G_PLATFORM_WIN32
68 # define STRICT /* Strict typing, please */
69 # include <windows.h>
70 # undef STRICT
71 # include <ctype.h>
72 #endif /* G_PLATFORM_WIN32 */
74 #ifdef G_OS_WIN32
75 # include <direct.h>
76 #endif
78 #ifdef HAVE_CODESET
79 #include <langinfo.h>
80 #endif
82 const guint glib_major_version = GLIB_MAJOR_VERSION;
83 const guint glib_minor_version = GLIB_MINOR_VERSION;
84 const guint glib_micro_version = GLIB_MICRO_VERSION;
85 const guint glib_interface_age = GLIB_INTERFACE_AGE;
86 const guint glib_binary_age = GLIB_BINARY_AGE;
88 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
89 void
90 g_memmove (gpointer dest, gconstpointer src, gulong len)
92 gchar* destptr = dest;
93 const gchar* srcptr = src;
94 if (src + len < dest || dest + len < src)
96 bcopy (src, dest, len);
97 return;
99 else if (dest <= src)
101 while (len--)
102 *(destptr++) = *(srcptr++);
104 else
106 destptr += len;
107 srcptr += len;
108 while (len--)
109 *(--destptr) = *(--srcptr);
112 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
114 void
115 g_atexit (GVoidFunc func)
117 gint result;
118 const gchar *error = NULL;
120 /* keep this in sync with glib.h */
122 #ifdef G_NATIVE_ATEXIT
123 result = ATEXIT (func);
124 if (result)
125 error = g_strerror (errno);
126 #elif defined (HAVE_ATEXIT)
127 # ifdef NeXT /* @#%@! NeXTStep */
128 result = !atexit ((void (*)(void)) func);
129 if (result)
130 error = g_strerror (errno);
131 # else
132 result = atexit ((void (*)(void)) func);
133 if (result)
134 error = g_strerror (errno);
135 # endif /* NeXT */
136 #elif defined (HAVE_ON_EXIT)
137 result = on_exit ((void (*)(int, void *)) func, NULL);
138 if (result)
139 error = g_strerror (errno);
140 #else
141 result = 0;
142 error = "no implementation";
143 #endif /* G_NATIVE_ATEXIT */
145 if (error)
146 g_error ("Could not register atexit() function: %s", error);
149 /* Based on execvp() from GNU Libc.
150 * Some of this code is cut-and-pasted into gspawn.c
153 static gchar*
154 my_strchrnul (const gchar *str, gchar c)
156 gchar *p = (gchar*)str;
157 while (*p && (*p != c))
158 ++p;
160 return p;
163 #ifdef G_OS_WIN32
165 gchar *inner_find_program_in_path (const gchar *program);
167 gchar*
168 g_find_program_in_path (const gchar *program)
170 const gchar *last_dot = strrchr (program, '.');
172 if (last_dot == NULL || strchr (last_dot, '\\') != NULL)
174 const gint program_length = strlen (program);
175 const gchar *pathext = getenv ("PATHEXT");
176 const gchar *p;
177 gchar *decorated_program;
178 gchar *retval;
180 if (pathext == NULL)
181 pathext = ".com;.exe;.bat";
183 p = pathext;
186 pathext = p;
187 p = my_strchrnul (pathext, ';');
189 decorated_program = g_malloc (program_length + (p-pathext) + 1);
190 memcpy (decorated_program, program, program_length);
191 memcpy (decorated_program+program_length, pathext, p-pathext);
192 decorated_program [program_length + (p-pathext)] = '\0';
194 retval = inner_find_program_in_path (decorated_program);
195 g_free (decorated_program);
197 if (retval != NULL)
198 return retval;
199 } while (*p++ != '\0');
200 return NULL;
202 else
203 return inner_find_program_in_path (program);
206 #define g_find_program_in_path inner_find_program_in_path
207 #endif
210 * g_find_program_in_path:
211 * @program: a program name
213 * Locates the first executable named @program in the user's path, in the
214 * same way that execvp() would locate it. Returns an allocated string
215 * with the absolute path name, or NULL if the program is not found in
216 * the path. If @program is already an absolute path, returns a copy of
217 * @program if @program exists and is executable, and NULL otherwise.
219 * On Windows, if @program does not have a file type suffix, tries to
220 * append the suffixes in the PATHEXT environment variable (if that
221 * doesn't exists, the suffixes .com, .exe, and .bat) in turn, and
222 * then look for the resulting file name in the same way as
223 * CreateProcess() would. This means first in the directory where the
224 * program was loaded from, then in the current directory, then in the
225 * Windows 32-bit system directory, then in the Windows directory, and
226 * finally in the directories in the PATH environment variable. If
227 * the program is found, the return value contains the full name
228 * including the type suffix.
230 * Return value: absolute path, or NULL
232 gchar*
233 g_find_program_in_path (const gchar *program)
235 const gchar *path, *p;
236 gchar *name, *freeme;
237 #ifdef G_OS_WIN32
238 gchar *path_tmp;
239 #endif
240 size_t len;
241 size_t pathlen;
243 g_return_val_if_fail (program != NULL, NULL);
245 /* If it is an absolute path, or a relative path including subdirectories,
246 * don't look in PATH.
248 if (g_path_is_absolute (program)
249 || strchr (program, G_DIR_SEPARATOR) != NULL)
251 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
252 return g_strdup (program);
253 else
254 return NULL;
257 path = g_getenv ("PATH");
258 #ifdef G_OS_UNIX
259 if (path == NULL)
261 /* There is no `PATH' in the environment. The default
262 * search path in GNU libc is the current directory followed by
263 * the path `confstr' returns for `_CS_PATH'.
266 /* In GLib we put . last, for security, and don't use the
267 * unportable confstr(); UNIX98 does not actually specify
268 * what to search if PATH is unset. POSIX may, dunno.
271 path = "/bin:/usr/bin:.";
273 #else
275 gchar *tmp;
276 gchar moddir[MAXPATHLEN], sysdir[MAXPATHLEN], windir[MAXPATHLEN];
278 GetModuleFileName (NULL, moddir, sizeof (moddir));
279 tmp = g_path_get_dirname (moddir);
280 GetSystemDirectory (sysdir, sizeof (sysdir));
281 GetWindowsDirectory (windir, sizeof (windir));
282 path_tmp = g_strconcat (tmp, ";.;", sysdir, ";", windir,
283 (path != NULL ? ";" : NULL),
284 (path != NULL ? path : NULL),
285 NULL);
286 g_free (tmp);
287 path = path_tmp;
289 #endif
291 len = strlen (program) + 1;
292 pathlen = strlen (path);
293 freeme = name = g_malloc (pathlen + len + 1);
295 /* Copy the file name at the top, including '\0' */
296 memcpy (name + pathlen + 1, program, len);
297 name = name + pathlen;
298 /* And add the slash before the filename */
299 *name = G_DIR_SEPARATOR;
301 p = path;
304 char *startp;
306 path = p;
307 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
309 if (p == path)
310 /* Two adjacent colons, or a colon at the beginning or the end
311 * of `PATH' means to search the current directory.
313 startp = name + 1;
314 else
315 startp = memcpy (name - (p - path), path, p - path);
317 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
319 gchar *ret;
320 ret = g_strdup (startp);
321 g_free (freeme);
322 #ifdef G_OS_WIN32
323 g_free (path_tmp);
324 #endif
325 return ret;
328 while (*p++ != '\0');
330 g_free (freeme);
331 #ifdef G_OS_WIN32
332 g_free (path_tmp);
333 #endif
335 return NULL;
338 gint
339 g_snprintf (gchar *str,
340 gulong n,
341 gchar const *fmt,
342 ...)
344 #ifdef HAVE_VSNPRINTF
345 va_list args;
346 gint retval;
348 g_return_val_if_fail (str != NULL, 0);
349 g_return_val_if_fail (n > 0, 0);
350 g_return_val_if_fail (fmt != NULL, 0);
352 va_start (args, fmt);
353 retval = vsnprintf (str, n, fmt, args);
354 va_end (args);
356 if (retval < 0)
358 str[n-1] = '\0';
359 retval = strlen (str);
362 return retval;
363 #else /* !HAVE_VSNPRINTF */
364 gchar *printed;
365 va_list args;
367 g_return_val_if_fail (str != NULL, 0);
368 g_return_val_if_fail (n > 0, 0);
369 g_return_val_if_fail (fmt != NULL, 0);
371 va_start (args, fmt);
372 printed = g_strdup_vprintf (fmt, args);
373 va_end (args);
375 strncpy (str, printed, n);
376 str[n-1] = '\0';
378 g_free (printed);
380 return strlen (str);
381 #endif /* !HAVE_VSNPRINTF */
384 gint
385 g_vsnprintf (gchar *str,
386 gulong n,
387 gchar const *fmt,
388 va_list args)
390 #ifdef HAVE_VSNPRINTF
391 gint retval;
393 g_return_val_if_fail (str != NULL, 0);
394 g_return_val_if_fail (n > 0, 0);
395 g_return_val_if_fail (fmt != NULL, 0);
397 retval = vsnprintf (str, n, fmt, args);
399 if (retval < 0)
401 str[n-1] = '\0';
402 retval = strlen (str);
405 return retval;
406 #else /* !HAVE_VSNPRINTF */
407 gchar *printed;
409 g_return_val_if_fail (str != NULL, 0);
410 g_return_val_if_fail (n > 0, 0);
411 g_return_val_if_fail (fmt != NULL, 0);
413 printed = g_strdup_vprintf (fmt, args);
414 strncpy (str, printed, n);
415 str[n-1] = '\0';
417 g_free (printed);
419 return strlen (str);
420 #endif /* !HAVE_VSNPRINTF */
423 guint
424 g_parse_debug_string (const gchar *string,
425 const GDebugKey *keys,
426 guint nkeys)
428 guint i;
429 guint result = 0;
431 g_return_val_if_fail (string != NULL, 0);
433 if (!g_ascii_strcasecmp (string, "all"))
435 for (i=0; i<nkeys; i++)
436 result |= keys[i].value;
438 else
440 const gchar *p = string;
441 const gchar *q;
442 gboolean done = FALSE;
444 while (*p && !done)
446 q = strchr (p, ':');
447 if (!q)
449 q = p + strlen(p);
450 done = TRUE;
453 for (i=0; i<nkeys; i++)
454 if (g_ascii_strncasecmp(keys[i].key, p, q - p) == 0 &&
455 keys[i].key[q - p] == '\0')
456 result |= keys[i].value;
458 p = q + 1;
462 return result;
465 G_CONST_RETURN gchar*
466 g_basename (const gchar *file_name)
468 register gchar *base;
470 g_return_val_if_fail (file_name != NULL, NULL);
472 base = strrchr (file_name, G_DIR_SEPARATOR);
473 if (base)
474 return base + 1;
476 #ifdef G_OS_WIN32
477 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
478 return (gchar*) file_name + 2;
479 #endif /* G_OS_WIN32 */
481 return (gchar*) file_name;
484 gchar*
485 g_path_get_basename (const gchar *file_name)
487 register gssize base;
488 register gssize last_nonslash;
489 gsize len;
490 gchar *retval;
492 g_return_val_if_fail (file_name != NULL, NULL);
494 if (file_name[0] == '\0')
495 /* empty string */
496 return g_strdup (".");
498 last_nonslash = strlen (file_name) - 1;
500 while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
501 last_nonslash--;
503 if (last_nonslash == -1)
504 /* string only containing slashes */
505 return g_strdup (G_DIR_SEPARATOR_S);
507 #ifdef G_OS_WIN32
508 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
509 /* string only containing slashes and a drive */
510 return g_strdup (G_DIR_SEPARATOR_S);
511 #endif /* G_OS_WIN32 */
513 base = last_nonslash;
515 while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
516 base--;
518 #ifdef G_OS_WIN32
519 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
520 base = 1;
521 #endif /* G_OS_WIN32 */
523 len = last_nonslash - base;
524 retval = g_malloc (len + 1);
525 memcpy (retval, file_name + base + 1, len);
526 retval [len] = '\0';
527 return retval;
530 gboolean
531 g_path_is_absolute (const gchar *file_name)
533 g_return_val_if_fail (file_name != NULL, FALSE);
535 if (file_name[0] == G_DIR_SEPARATOR
536 #ifdef G_OS_WIN32
537 || file_name[0] == '/'
538 #endif
540 return TRUE;
542 #ifdef G_OS_WIN32
543 /* Recognize drive letter on native Windows */
544 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && (file_name[2] == G_DIR_SEPARATOR || file_name[2] == '/'))
545 return TRUE;
546 #endif /* G_OS_WIN32 */
548 return FALSE;
551 G_CONST_RETURN gchar*
552 g_path_skip_root (const gchar *file_name)
554 g_return_val_if_fail (file_name != NULL, NULL);
556 #ifdef G_PLATFORM_WIN32
557 /* Skip \\server\share (Win32) or //server/share (Cygwin) */
558 if (file_name[0] == G_DIR_SEPARATOR &&
559 file_name[1] == G_DIR_SEPARATOR &&
560 file_name[2])
562 gchar *p;
564 if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
565 p[1])
567 file_name = p + 1;
569 while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
570 file_name++;
572 /* Possibly skip a backslash after the share name */
573 if (file_name[0] == G_DIR_SEPARATOR)
574 file_name++;
576 return (gchar *)file_name;
579 #endif
581 /* Skip initial slashes */
582 if (file_name[0] == G_DIR_SEPARATOR)
584 while (file_name[0] == G_DIR_SEPARATOR)
585 file_name++;
586 return (gchar *)file_name;
589 #ifdef G_OS_WIN32
590 /* Skip X:\ */
591 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
592 return (gchar *)file_name + 3;
593 #endif
595 return NULL;
598 gchar*
599 g_path_get_dirname (const gchar *file_name)
601 register gchar *base;
602 register gsize len;
604 g_return_val_if_fail (file_name != NULL, NULL);
606 base = strrchr (file_name, G_DIR_SEPARATOR);
607 if (!base)
608 return g_strdup (".");
609 while (base > file_name && *base == G_DIR_SEPARATOR)
610 base--;
611 len = (guint) 1 + base - file_name;
613 base = g_new (gchar, len + 1);
614 g_memmove (base, file_name, len);
615 base[len] = 0;
617 return base;
620 gchar*
621 g_get_current_dir (void)
623 gchar *buffer = NULL;
624 gchar *dir = NULL;
625 static gulong max_len = 0;
627 if (max_len == 0)
628 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
630 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
631 * and, if that wasn't bad enough, hangs in doing so.
633 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
634 buffer = g_new (gchar, max_len + 1);
635 *buffer = 0;
636 dir = getwd (buffer);
637 #else /* !sun || !HAVE_GETCWD */
638 while (max_len < G_MAXULONG / 2)
640 buffer = g_new (gchar, max_len + 1);
641 *buffer = 0;
642 dir = getcwd (buffer, max_len);
644 if (dir || errno != ERANGE)
645 break;
647 g_free (buffer);
648 max_len *= 2;
650 #endif /* !sun || !HAVE_GETCWD */
652 if (!dir || !*buffer)
654 /* hm, should we g_error() out here?
655 * this can happen if e.g. "./" has mode \0000
657 buffer[0] = G_DIR_SEPARATOR;
658 buffer[1] = 0;
661 dir = g_strdup (buffer);
662 g_free (buffer);
664 return dir;
667 G_CONST_RETURN gchar*
668 g_getenv (const gchar *variable)
670 #ifndef G_OS_WIN32
671 g_return_val_if_fail (variable != NULL, NULL);
673 return getenv (variable);
674 #else
675 G_LOCK_DEFINE_STATIC (getenv);
676 struct env_struct
678 gchar *key;
679 gchar *value;
680 } *env;
681 static GArray *environs = NULL;
682 gchar *system_env;
683 guint length, i;
684 gchar dummy[2];
686 g_return_val_if_fail (variable != NULL, NULL);
688 G_LOCK (getenv);
690 if (!environs)
691 environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
693 /* First we try to find the envinronment variable inside the already
694 * found ones.
697 for (i = 0; i < environs->len; i++)
699 env = &g_array_index (environs, struct env_struct, i);
700 if (strcmp (env->key, variable) == 0)
702 g_assert (env->value);
703 G_UNLOCK (getenv);
704 return env->value;
708 /* If not found, we ask the system */
710 system_env = getenv (variable);
711 if (!system_env)
713 G_UNLOCK (getenv);
714 return NULL;
717 /* On Windows NT, it is relatively typical that environment variables
718 * contain references to other environment variables. Handle that by
719 * calling ExpandEnvironmentStrings.
722 g_array_set_size (environs, environs->len + 1);
724 env = &g_array_index (environs, struct env_struct, environs->len - 1);
726 /* First check how much space we need */
727 length = ExpandEnvironmentStrings (system_env, dummy, 2);
729 /* Then allocate that much, and actualy do the expansion and insert
730 * the new found pair into our buffer
733 env->value = g_malloc (length);
734 env->key = g_strdup (variable);
736 ExpandEnvironmentStrings (system_env, env->value, length);
738 G_UNLOCK (getenv);
739 return env->value;
740 #endif
744 G_LOCK_DEFINE_STATIC (g_utils_global);
746 static gchar *g_tmp_dir = NULL;
747 static gchar *g_user_name = NULL;
748 static gchar *g_real_name = NULL;
749 static gchar *g_home_dir = NULL;
751 /* HOLDS: g_utils_global_lock */
752 static void
753 g_get_any_init (void)
755 if (!g_tmp_dir)
757 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
758 if (!g_tmp_dir)
759 g_tmp_dir = g_strdup (g_getenv ("TMP"));
760 if (!g_tmp_dir)
761 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
763 #ifdef P_tmpdir
764 if (!g_tmp_dir)
766 gsize k;
767 g_tmp_dir = g_strdup (P_tmpdir);
768 k = strlen (g_tmp_dir);
769 if (k > 1 && g_tmp_dir[k - 1] == G_DIR_SEPARATOR)
770 g_tmp_dir[k - 1] = '\0';
772 #endif
774 if (!g_tmp_dir)
776 #ifndef G_OS_WIN32
777 g_tmp_dir = g_strdup ("/tmp");
778 #else /* G_OS_WIN32 */
779 g_tmp_dir = g_strdup ("C:\\");
780 #endif /* G_OS_WIN32 */
783 if (!g_home_dir)
784 g_home_dir = g_strdup (g_getenv ("HOME"));
786 #ifdef G_OS_WIN32
787 /* In case HOME is Unix-style (it happens), convert it to
788 * Windows style.
790 if (g_home_dir)
792 gchar *p;
793 while ((p = strchr (g_home_dir, '/')) != NULL)
794 *p = '\\';
797 if (!g_home_dir)
799 /* USERPROFILE is probably the closest equivalent to $HOME? */
800 if (getenv ("USERPROFILE") != NULL)
801 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
804 if (!g_home_dir)
806 /* At least at some time, HOMEDRIVE and HOMEPATH were used
807 * to point to the home directory, I think. But on Windows
808 * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
809 * HOMEPATH is its root "\"?
811 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
813 gchar *homedrive, *homepath;
815 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
816 homepath = g_strdup (g_getenv ("HOMEPATH"));
818 g_home_dir = g_strconcat (homedrive, homepath, NULL);
819 g_free (homedrive);
820 g_free (homepath);
823 #endif /* G_OS_WIN32 */
825 #ifdef HAVE_PWD_H
827 struct passwd *pw = NULL;
828 gpointer buffer = NULL;
829 gint error;
831 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
832 struct passwd pwd;
833 # ifdef _SC_GETPW_R_SIZE_MAX
834 /* This reurns the maximum length */
835 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
837 if (bufsize < 0)
838 bufsize = 64;
839 # else /* _SC_GETPW_R_SIZE_MAX */
840 glong bufsize = 64;
841 # endif /* _SC_GETPW_R_SIZE_MAX */
845 g_free (buffer);
846 buffer = g_malloc (bufsize);
847 errno = 0;
849 # ifdef HAVE_POSIX_GETPWUID_R
850 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
851 error = error < 0 ? errno : error;
852 # else /* HAVE_NONPOSIX_GETPWUID_R */
853 # ifdef _AIX
854 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
855 pw = error == 0 ? &pwd : NULL;
856 # else /* !_AIX */
857 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
858 error = pw ? 0 : errno;
859 # endif /* !_AIX */
860 # endif /* HAVE_NONPOSIX_GETPWUID_R */
862 if (!pw)
864 /* we bail out prematurely if the user id can't be found
865 * (should be pretty rare case actually), or if the buffer
866 * should be sufficiently big and lookups are still not
867 * successfull.
869 if (error == 0 || error == ENOENT)
871 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
872 (gulong) getuid ());
873 break;
875 if (bufsize > 32 * 1024)
877 g_warning ("getpwuid_r(): failed due to: %s.",
878 g_strerror (error));
879 break;
882 bufsize *= 2;
885 while (!pw);
886 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
888 if (!pw)
890 setpwent ();
891 pw = getpwuid (getuid ());
892 endpwent ();
894 if (pw)
896 g_user_name = g_strdup (pw->pw_name);
897 g_real_name = g_strdup (pw->pw_gecos);
898 if (!g_home_dir)
899 g_home_dir = g_strdup (pw->pw_dir);
901 g_free (buffer);
904 #else /* !HAVE_PWD_H */
906 # ifdef G_OS_WIN32
908 guint len = 17;
909 gchar buffer[17];
911 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
913 g_user_name = g_strdup (buffer);
914 g_real_name = g_strdup (buffer);
917 # endif /* G_OS_WIN32 */
919 #endif /* !HAVE_PWD_H */
921 #ifdef __EMX__
922 /* change '\\' in %HOME% to '/' */
923 g_strdelimit (g_home_dir, "\\",'/');
924 #endif
925 if (!g_user_name)
926 g_user_name = g_strdup ("somebody");
927 if (!g_real_name)
928 g_real_name = g_strdup ("Unknown");
929 else
931 gchar *p;
933 for (p = g_real_name; *p; p++)
934 if (*p == ',')
936 *p = 0;
937 p = g_strdup (g_real_name);
938 g_free (g_real_name);
939 g_real_name = p;
940 break;
946 G_CONST_RETURN gchar*
947 g_get_user_name (void)
949 G_LOCK (g_utils_global);
950 if (!g_tmp_dir)
951 g_get_any_init ();
952 G_UNLOCK (g_utils_global);
954 return g_user_name;
957 G_CONST_RETURN gchar*
958 g_get_real_name (void)
960 G_LOCK (g_utils_global);
961 if (!g_tmp_dir)
962 g_get_any_init ();
963 G_UNLOCK (g_utils_global);
965 return g_real_name;
968 /* Return the home directory of the user. If there is a HOME
969 * environment variable, its value is returned, otherwise use some
970 * system-dependent way of finding it out. If no home directory can be
971 * deduced, return NULL.
974 G_CONST_RETURN gchar*
975 g_get_home_dir (void)
977 G_LOCK (g_utils_global);
978 if (!g_tmp_dir)
979 g_get_any_init ();
980 G_UNLOCK (g_utils_global);
982 return g_home_dir;
985 /* Return a directory to be used to store temporary files. This is the
986 * value of the TMPDIR, TMP or TEMP environment variables (they are
987 * checked in that order). If none of those exist, use P_tmpdir from
988 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
989 * and C:\ on Windows.
992 G_CONST_RETURN gchar*
993 g_get_tmp_dir (void)
995 G_LOCK (g_utils_global);
996 if (!g_tmp_dir)
997 g_get_any_init ();
998 G_UNLOCK (g_utils_global);
1000 return g_tmp_dir;
1003 G_LOCK_DEFINE (g_prgname);
1004 static gchar *g_prgname = NULL;
1006 gchar*
1007 g_get_prgname (void)
1009 gchar* retval;
1011 G_LOCK (g_prgname);
1012 retval = g_prgname;
1013 G_UNLOCK (g_prgname);
1015 return retval;
1018 void
1019 g_set_prgname (const gchar *prgname)
1021 G_LOCK (g_prgname);
1022 g_free (g_prgname);
1023 g_prgname = g_strdup (prgname);
1024 G_UNLOCK (g_prgname);
1027 guint
1028 g_direct_hash (gconstpointer v)
1030 return GPOINTER_TO_UINT (v);
1033 gboolean
1034 g_direct_equal (gconstpointer v1,
1035 gconstpointer v2)
1037 return v1 == v2;
1040 gboolean
1041 g_int_equal (gconstpointer v1,
1042 gconstpointer v2)
1044 return *((const gint*) v1) == *((const gint*) v2);
1047 guint
1048 g_int_hash (gconstpointer v)
1050 return *(const gint*) v;
1054 * g_nullify_pointer:
1055 * @nullify_location: the memory address of the pointer.
1057 * Set the pointer at the specified location to %NULL.
1059 void
1060 g_nullify_pointer (gpointer *nullify_location)
1062 g_return_if_fail (nullify_location != NULL);
1064 *nullify_location = NULL;
1068 * g_get_codeset:
1070 * Get the codeset for the current locale.
1072 * Return value: a newly allocated string containing the name
1073 * of the codeset. This string must be freed with g_free().
1075 gchar *
1076 g_get_codeset (void)
1078 #ifdef HAVE_CODESET
1079 char *result = nl_langinfo (CODESET);
1080 return g_strdup (result);
1081 #else
1082 #ifdef G_PLATFORM_WIN32
1083 return g_strdup_printf ("CP%d", GetACP ());
1084 #else
1085 /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
1087 return g_strdup ("ISO-8859-1");
1088 #endif
1089 #endif
1092 #ifdef ENABLE_NLS
1094 #include <libintl.h>
1096 #ifdef G_PLATFORM_WIN32
1098 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
1100 static const gchar *
1101 _glib_get_locale_dir (void)
1103 static const gchar *cache = NULL;
1104 if (cache == NULL)
1105 cache = g_win32_get_package_installation_subdirectory
1106 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
1108 return cache;
1111 #undef GLIB_LOCALE_DIR
1112 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
1114 #endif /* G_PLATFORM_WIN32 */
1116 G_CONST_RETURN gchar *
1117 _glib_gettext (const gchar *str)
1119 static gboolean _glib_gettext_initialized = FALSE;
1121 if (!_glib_gettext_initialized)
1123 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1124 _glib_gettext_initialized = TRUE;
1127 return dgettext (GETTEXT_PACKAGE, str);
1130 #endif /* ENABLE_NLS */