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/.
28 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
51 /* implement Glib's inline functions
53 #define G_IMPLEMENT_INLINES 1
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)
64 #define G_PATH_LENGTH 2048
68 # define STRICT /* Strict typing, please */
72 #endif /* G_OS_WIN32 */
78 const guint glib_major_version = GLIB_MAJOR_VERSION;
79 const guint glib_minor_version = GLIB_MINOR_VERSION;
80 const guint glib_micro_version = GLIB_MICRO_VERSION;
81 const guint glib_interface_age = GLIB_INTERFACE_AGE;
82 const guint glib_binary_age = GLIB_BINARY_AGE;
84 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
86 g_memmove (gpointer dest, gconstpointer src, gulong len)
88 gchar* destptr = dest;
89 const gchar* srcptr = src;
90 if (src + len < dest || dest + len < src)
92 bcopy (src, dest, len);
98 *(destptr++) = *(srcptr++);
105 *(--destptr) = *(--srcptr);
108 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
111 g_atexit (GVoidFunc func)
116 /* keep this in sync with glib.h */
118 #ifdef G_NATIVE_ATEXIT
119 result = ATEXIT (func);
121 error = g_strerror (errno);
122 #elif defined (HAVE_ATEXIT)
123 # ifdef NeXT /* @#%@! NeXTStep */
124 result = !atexit ((void (*)(void)) func);
126 error = g_strerror (errno);
128 result = atexit ((void (*)(void)) func);
130 error = g_strerror (errno);
132 #elif defined (HAVE_ON_EXIT)
133 result = on_exit ((void (*)(int, void *)) func, NULL);
135 error = g_strerror (errno);
138 error = "no implementation";
139 #endif /* G_NATIVE_ATEXIT */
142 g_error ("Could not register atexit() function: %s", error);
145 /* Based on execvp() from GNU Libc.
146 * Some of this code is cut-and-pasted into gspawn.c
150 my_strchrnul (const gchar *str, gchar c)
152 gchar *p = (gchar*)str;
153 while (*p && (*p != c))
160 * g_find_program_in_path:
161 * @program: a program name
163 * Locates the first executable named @program in the user's path, in the
164 * same way that execvp() would locate it. Returns an allocated string
165 * with the absolute path name, or NULL if the program is not found in
166 * the path. If @program is already an absolute path, returns a copy of
167 * @program if @program exists and is executable, and NULL otherwise.
169 * Return value: absolute path, or NULL
172 g_find_program_in_path (const gchar *program)
174 gchar *path, *p, *name, *freeme;
178 g_return_val_if_fail (program != NULL, NULL);
182 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
183 return g_strdup (program);
188 path = g_getenv ("PATH");
191 /* There is no `PATH' in the environment. The default
192 * search path in GNU libc is the current directory followed by
193 * the path `confstr' returns for `_CS_PATH'.
196 /* In GLib we put . last, for security, and don't use the
197 * unportable confstr(); UNIX98 does not actually specify
198 * what to search if PATH is unset. POSIX may, dunno.
201 path = "/bin:/usr/bin:.";
204 len = strlen (program) + 1;
205 pathlen = strlen (path);
206 freeme = name = g_malloc (pathlen + len + 1);
208 /* Copy the file name at the top, including '\0' */
209 memcpy (name + pathlen + 1, program, len);
210 name = name + pathlen;
211 /* And add the slash before the filename */
220 p = my_strchrnul (path, ':');
223 /* Two adjacent colons, or a colon at the beginning or the end
224 * of `PATH' means to search the current directory.
228 startp = memcpy (name - (p - path), path, p - path);
230 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
233 ret = g_strdup (startp);
238 while (*p++ != '\0');
246 g_snprintf (gchar *str,
251 #ifdef HAVE_VSNPRINTF
255 g_return_val_if_fail (str != NULL, 0);
256 g_return_val_if_fail (n > 0, 0);
257 g_return_val_if_fail (fmt != NULL, 0);
259 va_start (args, fmt);
260 retval = vsnprintf (str, n, fmt, args);
266 retval = strlen (str);
270 #else /* !HAVE_VSNPRINTF */
274 g_return_val_if_fail (str != NULL, 0);
275 g_return_val_if_fail (n > 0, 0);
276 g_return_val_if_fail (fmt != NULL, 0);
278 va_start (args, fmt);
279 printed = g_strdup_vprintf (fmt, args);
282 strncpy (str, printed, n);
288 #endif /* !HAVE_VSNPRINTF */
292 g_vsnprintf (gchar *str,
297 #ifdef HAVE_VSNPRINTF
300 g_return_val_if_fail (str != NULL, 0);
301 g_return_val_if_fail (n > 0, 0);
302 g_return_val_if_fail (fmt != NULL, 0);
304 retval = vsnprintf (str, n, fmt, args);
309 retval = strlen (str);
313 #else /* !HAVE_VSNPRINTF */
316 g_return_val_if_fail (str != NULL, 0);
317 g_return_val_if_fail (n > 0, 0);
318 g_return_val_if_fail (fmt != NULL, 0);
320 printed = g_strdup_vprintf (fmt, args);
321 strncpy (str, printed, n);
327 #endif /* !HAVE_VSNPRINTF */
331 g_parse_debug_string (const gchar *string,
338 g_return_val_if_fail (string != NULL, 0);
340 if (!g_strcasecmp (string, "all"))
342 for (i=0; i<nkeys; i++)
343 result |= keys[i].value;
347 gchar *str = g_strdup (string);
350 gboolean done = FALSE;
363 for (i=0; i<nkeys; i++)
364 if (!g_strcasecmp(keys[i].key, p))
365 result |= keys[i].value;
377 g_basename (const gchar *file_name)
379 register gchar *base;
380 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
381 static gboolean first_call = TRUE;
385 g_message ("g_basename is deprecated. Use g_path_get_basename instead. "
386 "Beware that the string returned by g_path_get_basename() has "
387 " to be g_free()ed.");
390 #endif /* G_ENABLE_DEBUG */
392 g_return_val_if_fail (file_name != NULL, NULL);
394 base = strrchr (file_name, G_DIR_SEPARATOR);
399 if (isalpha (file_name[0]) && file_name[1] == ':')
400 return (gchar*) file_name + 2;
401 #endif /* G_OS_WIN32 */
403 return (gchar*) file_name;
407 g_path_get_basename (const gchar *file_name)
410 register gint last_nonslash;
414 g_return_val_if_fail (file_name != NULL, NULL);
416 if (file_name[0] == '\0')
418 return g_strdup (".");
420 last_nonslash = strlen (file_name) - 1;
422 while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
425 if (last_nonslash == -1)
426 /* string only containing slashes */
427 return g_strdup (G_DIR_SEPARATOR_S);
430 if (last_nonslash == 1 && isalpha (file_name[0]) && file_name[1] == ':')
431 /* string only containing slashes and a drive */
432 return g_strdup (G_DIR_SEPARATOR_S);
433 #endif /* G_OS_WIN32 */
435 base = last_nonslash;
437 while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
441 if (base == -1 && isalpha (file_name[0]) && file_name[1] == ':')
443 #endif /* G_OS_WIN32 */
445 len = last_nonslash - base;
446 retval = g_malloc (len + 1);
447 memcpy (retval, file_name + base + 1, len);
453 g_path_is_absolute (const gchar *file_name)
455 g_return_val_if_fail (file_name != NULL, FALSE);
457 if (file_name[0] == G_DIR_SEPARATOR)
461 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
469 g_path_skip_root (gchar *file_name)
471 g_return_val_if_fail (file_name != NULL, NULL);
474 /* Skip \\server\share */
475 if (file_name[0] == G_DIR_SEPARATOR &&
476 file_name[1] == G_DIR_SEPARATOR &&
481 if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
486 while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
489 /* Possibly skip a backslash after the share name */
490 if (file_name[0] == G_DIR_SEPARATOR)
498 /* Skip initial slashes */
499 if (file_name[0] == G_DIR_SEPARATOR)
501 while (file_name[0] == G_DIR_SEPARATOR)
508 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
509 return file_name + 3;
516 g_path_get_dirname (const gchar *file_name)
518 register gchar *base;
521 g_return_val_if_fail (file_name != NULL, NULL);
523 base = strrchr (file_name, G_DIR_SEPARATOR);
525 return g_strdup (".");
526 while (base > file_name && *base == G_DIR_SEPARATOR)
528 len = (guint) 1 + base - file_name;
530 base = g_new (gchar, len + 1);
531 g_memmove (base, file_name, len);
538 g_dirname (const gchar *file_name)
540 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
541 static gboolean first_call = TRUE;
545 g_message ("g_dirname() is deprecated. Use g_path_get_dirname() instead.");
548 #endif /* G_ENABLE_DEBUG */
550 return g_path_get_dirname (file_name);
554 g_get_current_dir (void)
556 gchar *buffer = NULL;
558 static gulong max_len = 0;
561 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
563 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
564 * and, if that wasn't bad enough, hangs in doing so.
566 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
567 buffer = g_new (gchar, max_len + 1);
569 dir = getwd (buffer);
570 #else /* !sun || !HAVE_GETCWD */
571 while (max_len < G_MAXULONG / 2)
573 buffer = g_new (gchar, max_len + 1);
575 dir = getcwd (buffer, max_len);
577 if (dir || errno != ERANGE)
583 #endif /* !sun || !HAVE_GETCWD */
585 if (!dir || !*buffer)
587 /* hm, should we g_error() out here?
588 * this can happen if e.g. "./" has mode \0000
590 buffer[0] = G_DIR_SEPARATOR;
594 dir = g_strdup (buffer);
601 g_getenv (const gchar *variable)
604 g_return_val_if_fail (variable != NULL, NULL);
606 return getenv (variable);
608 G_LOCK_DEFINE_STATIC (getenv);
614 static GArray *environs = NULL;
619 g_return_val_if_fail (variable != NULL, NULL);
624 environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
626 /* First we try to find the envinronment variable inside the already
630 for (i = 0; i < environs->len; i++)
632 env = &g_array_index (environs, struct env_struct, i);
633 if (strcmp (env->key, variable) == 0)
635 g_assert (env->value);
641 /* If not found, we ask the system */
643 system_env = getenv (variable);
650 /* On Windows NT, it is relatively typical that environment variables
651 * contain references to other environment variables. Handle that by
652 * calling ExpandEnvironmentStrings.
655 g_array_set_size (environs, environs->len + 1);
657 env = &g_array_index (environs, struct env_struct, environs->len - 1);
659 /* First check how much space we need */
660 length = ExpandEnvironmentStrings (system_env, dummy, 2);
662 /* Then allocate that much, and actualy do the expansion and insert
663 * the new found pair into our buffer
666 env->value = g_malloc (length);
667 env->key = g_strdup (variable);
669 ExpandEnvironmentStrings (system_env, env->value, length);
677 G_LOCK_DEFINE_STATIC (g_utils_global);
679 static gchar *g_tmp_dir = NULL;
680 static gchar *g_user_name = NULL;
681 static gchar *g_real_name = NULL;
682 static gchar *g_home_dir = NULL;
684 /* HOLDS: g_utils_global_lock */
686 g_get_any_init (void)
690 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
692 g_tmp_dir = g_strdup (g_getenv ("TMP"));
694 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
700 g_tmp_dir = g_strdup (P_tmpdir);
701 k = strlen (g_tmp_dir);
702 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
703 g_tmp_dir[k-1] = '\0';
710 g_tmp_dir = g_strdup ("/tmp");
711 #else /* G_OS_WIN32 */
712 g_tmp_dir = g_strdup ("C:\\");
713 #endif /* G_OS_WIN32 */
717 g_home_dir = g_strdup (g_getenv ("HOME"));
720 /* In case HOME is Unix-style (it happens), convert it to
726 while ((p = strchr (g_home_dir, '/')) != NULL)
731 /* The official way to specify a home directory on NT is
732 * the HOMEDRIVE and HOMEPATH environment variables. At least
733 * it was at some time.
735 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
737 gchar *homedrive, *homepath;
739 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
740 homepath = g_strdup (g_getenv ("HOMEPATH"));
742 g_home_dir = g_strconcat (homedrive, homepath, NULL);
747 #endif /* G_OS_WIN32 */
751 struct passwd *pw = NULL;
752 gpointer buffer = NULL;
754 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
756 # ifdef _SC_GETPW_R_SIZE_MAX
757 /* This reurns the maximum length */
758 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
759 # else /* _SC_GETPW_R_SIZE_MAX */
761 # endif /* _SC_GETPW_R_SIZE_MAX */
767 buffer = g_malloc (bufsize);
770 # ifdef HAVE_POSIX_GETPWUID_R
771 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
772 error = error < 0 ? errno : error;
773 # else /* HAVE_NONPOSIX_GETPWUID_R */
775 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
776 pw = error == 0 ? &pwd : NULL;
778 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
779 error = pw ? 0 : errno;
781 # endif /* HAVE_NONPOSIX_GETPWUID_R */
785 /* we bail out prematurely if the user id can't be found
786 * (should be pretty rare case actually), or if the buffer
787 * should be sufficiently big and lookups are still not
790 if (error == 0 || error == ENOENT)
792 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
796 if (bufsize > 32 * 1024)
798 g_warning ("getpwuid_r(): failed due to: %s.",
807 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
812 pw = getpwuid (getuid ());
817 g_user_name = g_strdup (pw->pw_name);
818 g_real_name = g_strdup (pw->pw_gecos);
820 g_home_dir = g_strdup (pw->pw_dir);
825 #else /* !HAVE_PWD_H */
832 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
834 g_user_name = g_strdup (buffer);
835 g_real_name = g_strdup (buffer);
838 # endif /* G_OS_WIN32 */
840 #endif /* !HAVE_PWD_H */
843 /* change '\\' in %HOME% to '/' */
844 g_strdelimit (g_home_dir, "\\",'/');
847 g_user_name = g_strdup ("somebody");
849 g_real_name = g_strdup ("Unknown");
854 for (p = g_real_name; *p; p++)
858 p = g_strdup (g_real_name);
859 g_free (g_real_name);
868 g_get_user_name (void)
870 G_LOCK (g_utils_global);
873 G_UNLOCK (g_utils_global);
879 g_get_real_name (void)
881 G_LOCK (g_utils_global);
884 G_UNLOCK (g_utils_global);
889 /* Return the home directory of the user. If there is a HOME
890 * environment variable, its value is returned, otherwise use some
891 * system-dependent way of finding it out. If no home directory can be
892 * deduced, return NULL.
896 g_get_home_dir (void)
898 G_LOCK (g_utils_global);
901 G_UNLOCK (g_utils_global);
906 /* Return a directory to be used to store temporary files. This is the
907 * value of the TMPDIR, TMP or TEMP environment variables (they are
908 * checked in that order). If none of those exist, use P_tmpdir from
909 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
910 * and C:\ on Windows.
916 G_LOCK (g_utils_global);
919 G_UNLOCK (g_utils_global);
924 static gchar *g_prgname = NULL;
931 G_LOCK (g_utils_global);
933 G_UNLOCK (g_utils_global);
939 g_set_prgname (const gchar *prgname)
943 G_LOCK (g_utils_global);
945 g_prgname = g_strdup (prgname);
947 G_UNLOCK (g_utils_global);
951 g_direct_hash (gconstpointer v)
953 return GPOINTER_TO_UINT (v);
957 g_direct_equal (gconstpointer v1,
964 g_int_equal (gconstpointer v1,
967 return *((const gint*) v1) == *((const gint*) v2);
971 g_int_hash (gconstpointer v)
973 return *(const gint*) v;
979 * Get the codeset for the current locale.
981 * Return value: a newly allocated string containing the name
982 * of the codeset. This string must be freed with g_free().
988 char *result = nl_langinfo (CODESET);
989 return g_strdup (result);
992 /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
994 return g_strdup ("ISO-8859-1");
996 return g_strdup_printf ("CP%d", GetACP ());
1003 #include <libintl.h>
1008 #define GLIB_LOCALE_DIR \
1009 g_win32_get_package_installation_subdirectory \
1010 (GETTEXT_PACKAGE, g_strdup_printf ("glib-%d.%d.dll", \
1011 GLIB_MAJOR_VERSION, \
1012 GLIB_MINOR_VERSION), \
1015 #endif /* G_OS_WIN32 */
1018 _glib_gettext (const gchar *str)
1020 gboolean _glib_gettext_initialized = FALSE;
1022 if (!_glib_gettext_initialized)
1024 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1025 _glib_gettext_initialized = TRUE;
1028 return dgettext (GETTEXT_PACKAGE, str);
1031 #endif /* ENABLE_NLS */