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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
26 #include <sys/types.h>
27 #include <sys/param.h>
29 /* implement Glib's inline functions
31 #define G_INLINE_FUNC extern
32 #define G_CAN_INLINE 1
35 const guint glib_major_version = GLIB_MAJOR_VERSION;
36 const guint glib_minor_version = GLIB_MINOR_VERSION;
37 const guint glib_micro_version = GLIB_MICRO_VERSION;
38 const guint glib_interface_age = GLIB_INTERFACE_AGE;
39 const guint glib_binary_age = GLIB_BINARY_AGE;
43 g_atexit (GVoidFunc func)
48 /* keep this in sync with glib.h */
50 #ifdef G_NATIVE_ATEXIT
51 result = ATEXIT (func);
53 error = g_strerror (errno);
54 #elif defined (HAVE_ATEXIT)
55 # ifdef NeXT /* @#%@! NeXTStep */
56 result = !atexit ((void (*)(void)) func);
58 error = g_strerror (errno);
60 result = atexit ((void (*)(void)) func);
62 error = g_strerror (errno);
64 #elif defined (HAVE_ON_EXIT)
65 result = on_exit ((void (*)(int, void *)) func, NULL);
67 error = g_strerror (errno);
70 error = "no implementation";
71 #endif /* G_NATIVE_ATEXIT */
74 g_error ("Could not register atexit() function: %s", error);
78 g_snprintf (gchar *str,
88 retval = vsnprintf (str, n, fmt, args);
92 #else /* !HAVE_VSNPRINTF */
97 printed = g_strdup_vprintf (fmt, args);
100 strncpy (str, printed, n);
106 #endif /* !HAVE_VSNPRINTF */
110 g_vsnprintf (gchar *str,
115 #ifdef HAVE_VSNPRINTF
118 retval = vsnprintf (str, n, fmt, args);
121 #else /* !HAVE_VSNPRINTF */
124 printed = g_strdup_vprintf (fmt, args);
125 strncpy (str, printed, n);
131 #endif /* !HAVE_VSNPRINTF */
135 g_parse_debug_string (const gchar *string,
142 g_return_val_if_fail (string != NULL, 0);
144 if (!g_strcasecmp (string, "all"))
146 for (i=0; i<nkeys; i++)
147 result |= keys[i].value;
151 gchar *str = g_strdup (string);
154 gboolean done = FALSE;
167 for (i=0; i<nkeys; i++)
168 if (!g_strcasecmp(keys[i].key, p))
169 result |= keys[i].value;
181 g_basename (const gchar *file_name)
183 register gchar *base;
185 g_return_val_if_fail (file_name != NULL, NULL);
187 base = strrchr (file_name, '/');
191 return (gchar*) file_name;
195 g_dirname (const gchar *file_name)
197 register gchar *base;
200 g_return_val_if_fail (file_name != NULL, NULL);
202 base = strrchr (file_name, '/');
204 return g_strdup (".");
205 while (base > file_name && *base == '/')
207 len = (guint) 1 + base - file_name;
209 base = g_new (gchar, len + 1);
210 g_memmove (base, file_name, len);
217 #define G_PATH_LENGTH (MAXPATHLEN + 1)
218 #elif defined (PATH_MAX)
219 #define G_PATH_LENGTH (PATH_MAX + 1)
220 #else /* !MAXPATHLEN */
221 #define G_PATH_LENGTH (2048 + 1)
222 #endif /* !MAXPATHLEN && !PATH_MAX */
225 g_get_current_dir (void)
230 buffer = g_new (gchar, G_PATH_LENGTH);
233 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
234 * and, if that wasn't bad enough, hangs in doing so.
236 #if defined (sun) && !defined (__SVR4)
237 dir = getwd (buffer);
239 dir = getcwd (buffer, G_PATH_LENGTH - 1);
242 if (!dir || !*buffer)
244 /* hm, should we g_error() out here?
245 * this can happen if e.g. "./" has mode \0000
251 dir = g_strdup (buffer);
257 static gchar *g_tmp_dir = NULL;
258 static gchar *g_user_name = NULL;
259 static gchar *g_real_name = NULL;
260 static gchar *g_home_dir = NULL;
263 g_get_any_init (void)
269 g_tmp_dir = g_strdup (getenv ("TMPDIR"));
271 g_tmp_dir = g_strdup (getenv ("TMP"));
273 g_tmp_dir = g_strdup (getenv ("TEMP"));
275 g_tmp_dir = g_strdup ("/tmp");
277 g_home_dir = g_strdup (getenv ("HOME"));
280 pw = getpwuid (getuid ());
285 g_user_name = g_strdup (pw->pw_name);
286 g_real_name = g_strdup (pw->pw_gecos);
288 g_home_dir = g_strdup (pw->pw_dir);
294 g_get_user_name (void)
303 g_get_real_name (void)
312 g_get_home_dir (void)
329 static gchar *g_prgname = NULL;
338 g_set_prgname (const gchar *prgname)
340 gchar *c = g_prgname;
342 g_prgname = g_strdup (prgname);
347 g_direct_hash(gconstpointer v)
349 return GPOINTER_TO_UINT (v);
353 g_direct_equal(gconstpointer v, gconstpointer v2)
355 return GPOINTER_TO_UINT (v) == GPOINTER_TO_UINT (v2);
359 g_int_equal (gconstpointer v, gconstpointer v2)
361 return *((const gint*) v) == *((const gint*) v2);
365 g_int_hash (gconstpointer v)
367 return *(const gint*) v;