version bump to 1.1.3, binary age 0, interface age 0.
[platform/upstream/glib.git] / gutils.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  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 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.
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  * Library General Public License for more details.
13  *
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.
18  */
19 #include <unistd.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <pwd.h>
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include "glib.h"
28
29 const guint glib_major_version = GLIB_MAJOR_VERSION;
30 const guint glib_minor_version = GLIB_MINOR_VERSION;
31 const guint glib_micro_version = GLIB_MICRO_VERSION;
32 const guint glib_interface_age = GLIB_INTERFACE_AGE;
33 const guint glib_binary_age = GLIB_BINARY_AGE;
34
35 extern char* g_vsprintf (const gchar *fmt, va_list *args, va_list *args2);
36
37 gint
38 g_snprintf (gchar       *str,
39             gulong       n,
40             gchar const *fmt,
41             ...)
42 {
43 #ifdef  HAVE_VSNPRINTF
44   va_list args;
45   gint retval;
46   
47   va_start (args, fmt);
48   retval = vsnprintf (str, n, fmt, args);
49   va_end (args);
50   
51   return retval;
52 #else   /* !HAVE_VSNPRINTF */
53   gchar *printed;
54   va_list args, args2;
55   
56   va_start (args, fmt);
57   va_start (args2, fmt);
58   
59   printed = g_vsprintf (fmt, &args, &args2);
60   strncpy (str, printed, n);
61   str[n-1] = '\0';
62   
63   va_end (args2);
64   va_end (args);
65   
66   return strlen (str);
67 #endif  /* !HAVE_VSNPRINTF */
68 }
69
70 gint
71 g_vsnprintf (gchar       *str,
72              gulong       n,
73              gchar const *fmt,
74              va_list     *args1,
75              va_list     *args2)
76 {
77 #ifdef  HAVE_VSNPRINTF
78   gint retval;
79   
80   retval = vsnprintf (str, n, fmt, *args1);
81   
82   return retval;
83 #else   /* !HAVE_VSNPRINTF */
84   gchar *printed;
85   
86   printed = g_vsprintf (fmt, args1, args2);
87   strncpy (str, printed, n);
88   str[n-1] = '\0';
89   
90   return strlen (str);
91 #endif /* !HAVE_VSNPRINTF */
92 }
93
94 guint        
95 g_parse_debug_string  (const gchar *string, 
96                        GDebugKey   *keys, 
97                        guint        nkeys)
98 {
99   guint i;
100   guint result = 0;
101   
102   g_return_val_if_fail (string != NULL, 0);
103   
104   if (!g_strcasecmp (string, "all"))
105     {
106       for (i=0; i<nkeys; i++)
107         result |= keys[i].value;
108     }
109   else
110     {
111       gchar *str = g_strdup (string);
112       gchar *p = str;
113       gchar *q;
114       gboolean done = FALSE;
115       
116       while (*p && !done)
117         {
118           q = strchr (p, ':');
119           if (!q)
120             {
121               q = p + strlen(p);
122               done = TRUE;
123             }
124           
125           *q = 0;
126           
127           for (i=0; i<nkeys; i++)
128             if (!g_strcasecmp(keys[i].key, p))
129               result |= keys[i].value;
130           
131           p = q+1;
132         }
133       
134       g_free (str);
135     }
136   
137   return result;
138 }
139
140 gchar*
141 g_basename (const gchar    *file_name)
142 {
143   register gchar *base;
144   
145   g_return_val_if_fail (file_name != NULL, NULL);
146   
147   base = strrchr (file_name, '/');
148   if (base)
149     return base + 1;
150   
151   return (gchar*) file_name;
152 }
153
154 gchar*
155 g_dirname (const gchar     *file_name)
156 {
157   register gchar *base;
158   register guint len;
159   
160   g_return_val_if_fail (file_name != NULL, NULL);
161   
162   base = strrchr (file_name, '/');
163   if (!base)
164     return g_strdup (".");
165   while (base > file_name && *base == '/')
166     base--;
167   len = (guint) 1 + base - file_name;
168   
169   base = g_new (gchar, len + 1);
170   g_memmove (base, file_name, len);
171   base[len] = 0;
172   
173   return base;
174 }
175
176 #ifdef  MAXPATHLEN
177 #define G_PATH_LENGTH   (MAXPATHLEN + 1)
178 #elif   defined (PATH_MAX)
179 #define G_PATH_LENGTH   (PATH_MAX + 1)
180 #else   /* !MAXPATHLEN */
181 #define G_PATH_LENGTH   (2048 + 1)
182 #endif  /* !MAXPATHLEN && !PATH_MAX */
183
184 gchar*
185 g_get_current_dir (void)
186 {
187   gchar *buffer;
188   gchar *dir;
189
190   buffer = g_new (gchar, G_PATH_LENGTH);
191   *buffer = 0;
192   
193   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
194    * and, if that wasn't bad enough, hangs in doing so.
195    */
196 #if     defined (sun) && !defined (__SVR4)
197   dir = getwd (buffer);
198 #else   /* !sun */
199   dir = getcwd (buffer, G_PATH_LENGTH - 1);
200 #endif  /* !sun */
201   
202   if (!dir || !*buffer)
203     {
204       /* hm, should we g_error() out here?
205        * this can happen if e.g. "./" has mode \0000
206        */
207       buffer[0] = '/';
208       buffer[1] = 0;
209     }
210
211   dir = g_strdup (buffer);
212   g_free (buffer);
213   
214   return dir;
215 }
216
217 static  gchar   *g_tmp_dir = NULL;
218 static  gchar   *g_user_name = NULL;
219 static  gchar   *g_real_name = NULL;
220 static  gchar   *g_home_dir = NULL;
221
222 static void
223 g_get_any_init (void)
224 {
225   if (!g_tmp_dir)
226     {
227       struct passwd *pw;
228       
229       g_tmp_dir = g_strdup (getenv ("TMPDIR"));
230       if (!g_tmp_dir)
231         g_tmp_dir = g_strdup (getenv ("TMP"));
232       if (!g_tmp_dir)
233         g_tmp_dir = g_strdup (getenv ("TEMP"));
234       if (!g_tmp_dir)
235         g_tmp_dir = g_strdup ("/tmp");
236       
237       g_home_dir = g_strdup (getenv ("HOME"));
238       
239       setpwent ();
240       pw = getpwuid (getuid ());
241       endpwent ();
242       
243       if (pw)
244         {
245           g_user_name = g_strdup (pw->pw_name);
246           g_real_name = g_strdup (pw->pw_gecos);
247           if (!g_home_dir)
248             g_home_dir = g_strdup (pw->pw_dir);
249         }
250     }
251 }
252
253 gchar*
254 g_get_user_name (void)
255 {
256   if (!g_tmp_dir)
257     g_get_any_init ();
258   
259   return g_user_name;
260 }
261
262 gchar*
263 g_get_real_name (void)
264 {
265   if (!g_tmp_dir)
266     g_get_any_init ();
267   
268   return g_real_name;
269 }
270
271 gchar*
272 g_get_home_dir (void)
273 {
274   if (!g_tmp_dir)
275     g_get_any_init ();
276   
277   return g_home_dir;
278 }
279
280 gchar*
281 g_get_tmp_dir (void)
282 {
283   if (!g_tmp_dir)
284     g_get_any_init ();
285   
286   return g_tmp_dir;
287 }
288
289 static gchar *g_prgname = NULL;
290
291 gchar*
292 g_get_prgname (void)
293 {
294   return g_prgname;
295 }
296
297 void
298 g_set_prgname (const gchar *prgname)
299 {
300   gchar *c = g_prgname;
301   
302   g_prgname = g_strdup (prgname);
303   g_free (c);
304 }
305
306 guint
307 g_direct_hash(gconstpointer v)
308 {
309   return GPOINTER_TO_UINT (v);
310 }
311
312 gint
313 g_direct_equal(gconstpointer v, gconstpointer v2)
314 {
315   return GPOINTER_TO_UINT (v) == GPOINTER_TO_UINT (v2);
316 }
317
318 gint
319 g_int_equal (gconstpointer v, gconstpointer v2)
320 {
321   return *((const gint*) v) == *((const gint*) v2);
322 }
323
324 guint
325 g_int_hash (gconstpointer v)
326 {
327   return *(const gint*) v;
328 }