indentation fixes.
[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
33 extern char* g_vsprintf (const gchar *fmt, va_list *args, va_list *args2);
34
35 gint
36 g_snprintf (gchar       *str,
37             gulong       n,
38             gchar const *fmt,
39             ...)
40 {
41 #ifdef HAVE_VSNPRINTF
42   va_list args;
43   gint retval;
44   
45   va_start (args, fmt);
46   retval = vsnprintf (str, n, fmt, args);
47   va_end (args);
48   
49   return retval;
50   
51 #else
52   gchar *printed;
53   va_list args, args2;
54   
55   va_start (args, fmt);
56   va_start (args2, fmt);
57   
58   printed = g_vsprintf (fmt, &args, &args2);
59   strncpy (str, printed, n);
60   str[n-1] = '\0';
61   
62   va_end (args2);
63   va_end (args);
64   
65   return strlen (str);
66   
67 #endif
68 }
69
70 guint        
71 g_parse_debug_string  (const gchar *string, 
72                        GDebugKey   *keys, 
73                        guint        nkeys)
74 {
75   guint i;
76   guint result = 0;
77   
78   g_return_val_if_fail (string != NULL, 0);
79   
80   if (!g_strcasecmp (string, "all"))
81     {
82       for (i=0; i<nkeys; i++)
83         result |= keys[i].value;
84     }
85   else
86     {
87       gchar *str = g_strdup (string);
88       gchar *p = str;
89       gchar *q;
90       gboolean done = FALSE;
91       
92       while (*p && !done)
93         {
94           q = strchr (p, ':');
95           if (!q)
96             {
97               q = p + strlen(p);
98               done = TRUE;
99             }
100           
101           *q = 0;
102           
103           for (i=0; i<nkeys; i++)
104             if (!g_strcasecmp(keys[i].key, p))
105               result |= keys[i].value;
106           
107           p = q+1;
108         }
109       
110       g_free (str);
111     }
112   
113   return result;
114 }
115
116 gchar*
117 g_basename (const gchar    *file_name)
118 {
119   register gchar *base;
120   
121   g_return_val_if_fail (file_name != NULL, NULL);
122   
123   base = strrchr (file_name, '/');
124   if (base)
125     return base + 1;
126   
127   return (gchar*) file_name;
128 }
129
130 gchar*
131 g_dirname (const gchar     *file_name)
132 {
133   register gchar *base;
134   register guint len;
135   
136   g_return_val_if_fail (file_name != NULL, NULL);
137   
138   base = strrchr (file_name, '/');
139   if (!base)
140     return g_strdup (".");
141   while (base > file_name && *base == '/')
142     base--;
143   len = (guint) 1 + base - file_name;
144   
145   base = g_new (gchar, len + 1);
146   g_memmove (base, file_name, len);
147   base[len] = 0;
148   
149   return base;
150 }
151
152 gchar*
153 g_getcwd (void)
154 {
155   static gchar g_getcwd_buf[MAXPATHLEN + 1] = { 0 };
156   register gchar *dir;
157   
158   g_getcwd_buf[0] = 0;
159   
160   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
161    * and, if that wasn't bad enough, hangs in doing so.
162    */
163 #if     defined (sun) && !defined (__SVR4)
164   dir = getwd (g_getcwd_buf);
165 #else   /* !sun */
166   dir = getcwd (g_getcwd_buf, MAXPATHLEN);
167 #endif  /* !sun */
168   
169   if (!dir || g_getcwd_buf[0] == 0)
170     {
171       /* hm, we should probably g_error() out here...
172        */
173       g_getcwd_buf[0] = '/';
174       g_getcwd_buf[1] = 0;
175     }
176   
177   return g_getcwd_buf;
178 }
179
180 static  gchar   *g_tmp_dir = NULL;
181 static  gchar   *g_user_name = NULL;
182 static  gchar   *g_real_name = NULL;
183 static  gchar   *g_home_dir = NULL;
184
185 static void
186 g_get_any_init (void)
187 {
188   if (!g_tmp_dir)
189     {
190       struct passwd *pw;
191       
192       g_tmp_dir = g_strdup (getenv ("TMPDIR"));
193       if (!g_tmp_dir)
194         g_tmp_dir = g_strdup (getenv ("TMP"));
195       if (!g_tmp_dir)
196         g_tmp_dir = g_strdup (getenv ("TEMP"));
197       if (!g_tmp_dir)
198         g_tmp_dir = g_strdup ("/tmp");
199       
200       g_home_dir = g_strdup (getenv ("HOME"));
201       
202       setpwent ();
203       pw = getpwuid (getuid ());
204       endpwent ();
205       
206       if (pw)
207         {
208           g_user_name = g_strdup (pw->pw_name);
209           g_real_name = g_strdup (pw->pw_gecos);
210           if (!g_home_dir)
211             g_home_dir = g_strdup (pw->pw_dir);
212         }
213     }
214 }
215
216 gchar*
217 g_get_user_name (void)
218 {
219   if (!g_tmp_dir)
220     g_get_any_init ();
221   
222   return g_user_name;
223 }
224
225 gchar*
226 g_get_real_name (void)
227 {
228   if (!g_tmp_dir)
229     g_get_any_init ();
230   
231   return g_real_name;
232 }
233
234 gchar*
235 g_get_home_dir (void)
236 {
237   if (!g_tmp_dir)
238     g_get_any_init ();
239   
240   return g_home_dir;
241 }
242
243 gchar*
244 g_get_tmp_dir (void)
245 {
246   if (!g_tmp_dir)
247     g_get_any_init ();
248   
249   return g_tmp_dir;
250 }
251
252 static gchar *g_prgname = NULL;
253
254 gchar*
255 g_get_prgname (void)
256 {
257   return g_prgname;
258 }
259
260 void
261 g_set_prgname (const gchar *prgname)
262 {
263   gchar *c = g_prgname;
264   
265   g_prgname = g_strdup (prgname);
266   g_free (c);
267 }
268
269 guint
270 g_direct_hash(gconstpointer v)
271 {
272   return GPOINTER_TO_UINT (v);
273 }
274
275 gint
276 g_direct_equal(gconstpointer v, gconstpointer v2)
277 {
278   return GPOINTER_TO_UINT (v) == GPOINTER_TO_UINT (v2);
279 }
280
281 gint
282 g_int_equal (gconstpointer v, gconstpointer v2)
283 {
284   return *((const gint*) v) == *((const gint*) v2);
285 }
286
287 guint
288 g_int_hash (gconstpointer v)
289 {
290   return *(const gint*) v;
291 }