gutils: Add functions for working with environment arrays
[platform/upstream/glib.git] / glib / gutils.h
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 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.
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  * Lesser General Public License for more details.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_UTILS_H__
32 #define __G_UTILS_H__
33
34 #include <glib/gtypes.h>
35 #include <stdarg.h>
36
37 G_BEGIN_DECLS
38
39 #ifdef G_OS_WIN32
40
41 /* On Win32, the canonical directory separator is the backslash, and
42  * the search path separator is the semicolon. Note that also the
43  * (forward) slash works as directory separator.
44  */
45 #define G_DIR_SEPARATOR '\\'
46 #define G_DIR_SEPARATOR_S "\\"
47 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
48 #define G_SEARCHPATH_SEPARATOR ';'
49 #define G_SEARCHPATH_SEPARATOR_S ";"
50
51 #else  /* !G_OS_WIN32 */
52
53 /* Unix */
54
55 #define G_DIR_SEPARATOR '/'
56 #define G_DIR_SEPARATOR_S "/"
57 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
58 #define G_SEARCHPATH_SEPARATOR ':'
59 #define G_SEARCHPATH_SEPARATOR_S ":"
60
61 #endif /* !G_OS_WIN32 */
62
63 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
64  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
65  */
66 #if !defined (G_VA_COPY)
67 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
68 #    define G_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
69 #  elif defined (G_VA_COPY_AS_ARRAY)
70 #    define G_VA_COPY(ap1, ap2)   g_memmove ((ap1), (ap2), sizeof (va_list))
71 #  else /* va_list is a pointer */
72 #    define G_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
73 #  endif /* va_list is a pointer */
74 #endif /* !G_VA_COPY */
75
76 /* inlining hassle. for compilers that don't allow the `inline' keyword,
77  * mostly because of strict ANSI C compliance or dumbness, we try to fall
78  * back to either `__inline__' or `__inline'.
79  * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be 
80  * actually *capable* to do function inlining, in which case inline 
81  * function bodies do make sense. we also define G_INLINE_FUNC to properly 
82  * export the function prototypes if no inlining can be performed.
83  * inline function bodies have to be special cased with G_CAN_INLINE and a
84  * .c file specific macro to allow one compiled instance with extern linkage
85  * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
86  */
87 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
88 #  undef inline
89 #  define inline __inline__
90 #elif !defined (G_HAVE_INLINE)
91 #  undef inline
92 #  if defined (G_HAVE___INLINE__)
93 #    define inline __inline__
94 #  elif defined (G_HAVE___INLINE)
95 #    define inline __inline
96 #  else /* !inline && !__inline__ && !__inline */
97 #    define inline  /* don't inline, then */
98 #  endif
99 #endif
100 #ifdef G_IMPLEMENT_INLINES
101 #  define G_INLINE_FUNC
102 #  undef  G_CAN_INLINE
103 #elif defined (__GNUC__) 
104 #  define G_INLINE_FUNC static __inline __attribute__ ((unused))
105 #elif defined (G_CAN_INLINE) 
106 #  define G_INLINE_FUNC static inline
107 #else /* can't inline */
108 #  define G_INLINE_FUNC
109 #endif /* !G_INLINE_FUNC */
110
111 #ifndef __GTK_DOC_IGNORE__
112 #ifdef G_OS_WIN32
113 #define g_get_user_name g_get_user_name_utf8
114 #define g_get_real_name g_get_real_name_utf8
115 #define g_get_home_dir g_get_home_dir_utf8
116 #define g_get_tmp_dir g_get_tmp_dir_utf8
117 #endif
118 #endif
119
120 const gchar *         g_get_user_name        (void);
121 const gchar *         g_get_real_name        (void);
122 const gchar *         g_get_home_dir         (void);
123 const gchar *         g_get_tmp_dir          (void);
124 const gchar *         g_get_host_name        (void);
125 gchar *               g_get_prgname          (void);
126 void                  g_set_prgname          (const gchar *prgname);
127 const gchar *         g_get_application_name (void);
128 void                  g_set_application_name (const gchar *application_name);
129
130 void      g_reload_user_special_dirs_cache     (void);
131 const gchar *         g_get_user_data_dir      (void);
132 const gchar *         g_get_user_config_dir    (void);
133 const gchar *         g_get_user_cache_dir     (void);
134 const gchar * const * g_get_system_data_dirs   (void);
135
136 #ifdef G_OS_WIN32
137 /* This functions is not part of the public GLib API */
138 const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
139 #endif
140
141 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
142 /* This function is not part of the public GLib API either. Just call
143  * g_get_system_data_dirs() in your code, never mind that that is
144  * actually a macro and you will in fact call this inline function.
145  */
146 static inline const gchar * const *
147 _g_win32_get_system_data_dirs (void)
148 {
149   return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
150 }
151 #define g_get_system_data_dirs _g_win32_get_system_data_dirs
152 #endif
153
154 const gchar * const * g_get_system_config_dirs (void);
155
156 const gchar * g_get_user_runtime_dir (void);
157
158 const gchar * const * g_get_language_names (void);
159
160 gchar **g_get_locale_variants (const gchar *locale);
161
162 /**
163  * GUserDirectory:
164  * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
165  * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
166  * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
167  * @G_USER_DIRECTORY_MUSIC: the user's Music directory
168  * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
169  * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
170  * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
171  * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
172  * @G_USER_N_DIRECTORIES: the number of enum values
173  *
174  * These are logical ids for special directories which are defined
175  * depending on the platform used. You should use g_get_user_special_dir()
176  * to retrieve the full path associated to the logical id.
177  *
178  * The #GUserDirectory enumeration can be extended at later date. Not
179  * every platform has a directory for every logical id in this
180  * enumeration.
181  *
182  * Since: 2.14
183  */
184 typedef enum {
185   G_USER_DIRECTORY_DESKTOP,
186   G_USER_DIRECTORY_DOCUMENTS,
187   G_USER_DIRECTORY_DOWNLOAD,
188   G_USER_DIRECTORY_MUSIC,
189   G_USER_DIRECTORY_PICTURES,
190   G_USER_DIRECTORY_PUBLIC_SHARE,
191   G_USER_DIRECTORY_TEMPLATES,
192   G_USER_DIRECTORY_VIDEOS,
193
194   G_USER_N_DIRECTORIES
195 } GUserDirectory;
196
197 const gchar * g_get_user_special_dir (GUserDirectory directory);
198
199 /**
200  * GDebugKey:
201  * @key: the string
202  * @value: the flag
203  *
204  * Associates a string with a bit flag.
205  * Used in g_parse_debug_string().
206  */
207 typedef struct _GDebugKey GDebugKey;
208 struct _GDebugKey
209 {
210   const gchar *key;
211   guint        value;
212 };
213
214 /* Miscellaneous utility functions
215  */
216 guint                 g_parse_debug_string (const gchar     *string,
217                                             const GDebugKey *keys,
218                                             guint            nkeys);
219
220 gint                  g_snprintf           (gchar       *string,
221                                             gulong       n,
222                                             gchar const *format,
223                                             ...) G_GNUC_PRINTF (3, 4);
224 gint                  g_vsnprintf          (gchar       *string,
225                                             gulong       n,
226                                             gchar const *format,
227                                             va_list      args);
228
229 /* Check if a file name is an absolute path */
230 gboolean              g_path_is_absolute   (const gchar *file_name);
231
232 /* In case of absolute paths, skip the root part */
233 const gchar *         g_path_skip_root     (const gchar *file_name);
234
235 #ifndef G_DISABLE_DEPRECATED
236
237 GLIB_DEPRECATED_FOR(g_path_get_basename)
238 const gchar *         g_basename           (const gchar *file_name);
239 #define g_dirname g_path_get_dirname
240
241 #endif /* G_DISABLE_DEPRECATED */
242
243 #ifndef __GTK_DOC_IGNORE__
244 #ifdef G_OS_WIN32
245 #define g_get_current_dir g_get_current_dir_utf8
246 #endif
247 #endif
248
249 /* The returned strings are newly allocated with g_malloc() */
250 gchar*                g_get_current_dir    (void);
251 gchar*                g_path_get_basename  (const gchar *file_name) G_GNUC_MALLOC;
252 gchar*                g_path_get_dirname   (const gchar *file_name) G_GNUC_MALLOC;
253
254 /* Set the pointer at the specified location to NULL */
255 void                  g_nullify_pointer    (gpointer    *nullify_location);
256
257 /* return the environment string for the variable. The returned memory
258  * must not be freed. */
259 #ifndef __GTK_DOC_IGNORE__
260 #ifdef G_OS_WIN32
261 #define g_getenv g_getenv_utf8
262 #define g_setenv g_setenv_utf8
263 #define g_unsetenv g_unsetenv_utf8
264 #define g_find_program_in_path g_find_program_in_path_utf8
265 #endif
266 #endif
267
268 const gchar *         g_getenv             (const gchar  *variable);
269 gboolean              g_setenv             (const gchar  *variable,
270                                             const gchar  *value,
271                                             gboolean      overwrite);
272 void                  g_unsetenv           (const gchar  *variable);
273 gchar **              g_listenv            (void);
274
275 gchar **              g_get_environ        (void);
276 const gchar *         g_environ_getenv     (gchar       **envp,
277                                             const gchar  *variable);
278 gchar **              g_environ_setenv     (gchar       **envp,
279                                             const gchar  *variable,
280                                             const gchar  *value,
281                                             gboolean      overwrite) G_GNUC_WARN_UNUSED_RESULT;
282 gchar **              g_environ_unsetenv   (gchar       **envp,
283                                             const gchar  *variable) G_GNUC_WARN_UNUSED_RESULT;
284
285 /* private */
286 const gchar*         _g_getenv_nomalloc    (const gchar  *variable,
287                                             gchar         buffer[1024]);
288
289 /**
290  * GVoidFunc:
291  *
292  * Declares a type of function which takes no arguments
293  * and has no return value. It is used to specify the type
294  * function passed to g_atexit().
295  */
296 typedef void (*GVoidFunc) (void);
297 #ifndef ATEXIT
298 # define ATEXIT(proc) g_ATEXIT(proc)
299 #else
300 # define G_NATIVE_ATEXIT
301 #endif /* ATEXIT */
302 /* we use a GLib function as a replacement for ATEXIT, so
303  * the programmer is not required to check the return value
304  * (if there is any in the implementation) and doesn't encounter
305  * missing include files.
306  */
307 void    g_atexit                (GVoidFunc    func);
308
309 #ifdef G_OS_WIN32
310 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
311  * atexit(), the function will be called when the GLib DLL is detached
312  * from the program, which is not what the caller wants. The caller
313  * wants the function to be called when it *itself* exits (or is
314  * detached, in case the caller, too, is a DLL).
315  */
316 #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
317 int atexit (void (*)(void));
318 #endif
319 #define g_atexit(func) atexit(func)
320 #endif
321
322 /* Look for an executable in PATH, following execvp() rules */
323 gchar*  g_find_program_in_path  (const gchar *program);
324
325 /* Bit tests
326  */
327 G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
328                                        gint    nth_bit) G_GNUC_CONST;
329 G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
330                                        gint    nth_bit) G_GNUC_CONST;
331 G_INLINE_FUNC guint     g_bit_storage (gulong  number) G_GNUC_CONST;
332
333 /* Trash Stacks
334  * elements need to be >= sizeof (gpointer)
335  */
336 typedef struct _GTrashStack     GTrashStack;
337 struct _GTrashStack
338 {
339   GTrashStack *next;
340 };
341
342 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
343                                                  gpointer      data_p);
344 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
345 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
346 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
347
348 /* inline function implementations
349  */
350 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
351 G_INLINE_FUNC gint
352 g_bit_nth_lsf (gulong mask,
353                gint   nth_bit)
354 {
355   if (G_UNLIKELY (nth_bit < -1))
356     nth_bit = -1;
357   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
358     {
359       nth_bit++;
360       if (mask & (1UL << nth_bit))
361         return nth_bit;
362     }
363   return -1;
364 }
365 G_INLINE_FUNC gint
366 g_bit_nth_msf (gulong mask,
367                gint   nth_bit)
368 {
369   if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
370     nth_bit = GLIB_SIZEOF_LONG * 8;
371   while (nth_bit > 0)
372     {
373       nth_bit--;
374       if (mask & (1UL << nth_bit))
375         return nth_bit;
376     }
377   return -1;
378 }
379 G_INLINE_FUNC guint
380 g_bit_storage (gulong number)
381 {
382 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
383   return G_LIKELY (number) ?
384            ((GLIB_SIZEOF_LONG * 8U - 1) ^ __builtin_clzl(number)) + 1 : 1;
385 #else
386   register guint n_bits = 0;
387   
388   do
389     {
390       n_bits++;
391       number >>= 1;
392     }
393   while (number);
394   return n_bits;
395 #endif
396 }
397 G_INLINE_FUNC void
398 g_trash_stack_push (GTrashStack **stack_p,
399                     gpointer      data_p)
400 {
401   GTrashStack *data = (GTrashStack *) data_p;
402
403   data->next = *stack_p;
404   *stack_p = data;
405 }
406 G_INLINE_FUNC gpointer
407 g_trash_stack_pop (GTrashStack **stack_p)
408 {
409   GTrashStack *data;
410
411   data = *stack_p;
412   if (data)
413     {
414       *stack_p = data->next;
415       /* NULLify private pointer here, most platforms store NULL as
416        * subsequent 0 bytes
417        */
418       data->next = NULL;
419     }
420
421   return data;
422 }
423 G_INLINE_FUNC gpointer
424 g_trash_stack_peek (GTrashStack **stack_p)
425 {
426   GTrashStack *data;
427
428   data = *stack_p;
429
430   return data;
431 }
432 G_INLINE_FUNC guint
433 g_trash_stack_height (GTrashStack **stack_p)
434 {
435   GTrashStack *data;
436   guint i = 0;
437
438   for (data = *stack_p; data; data = data->next)
439     i++;
440
441   return i;
442 }
443 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
444
445 /* Glib version.
446  * we prefix variable declarations so they can
447  * properly get exported in windows dlls.
448  */
449 GLIB_VAR const guint glib_major_version;
450 GLIB_VAR const guint glib_minor_version;
451 GLIB_VAR const guint glib_micro_version;
452 GLIB_VAR const guint glib_interface_age;
453 GLIB_VAR const guint glib_binary_age;
454
455 const gchar * glib_check_version (guint required_major,
456                                   guint required_minor,
457                                   guint required_micro);
458
459 /**
460  * GLIB_CHECK_VERSION:
461  * @major: the major version to check for
462  * @minor: the minor version to check for
463  * @micro: the micro version to check for
464  *
465  * Checks the version of the GLib library that is being compiled
466  * against.
467  *
468  * <example>
469  * <title>Checking the version of the GLib library</title>
470  * <programlisting>
471  *   if (!GLIB_CHECK_VERSION (1, 2, 0))
472  *     g_error ("GLib version 1.2.0 or above is needed");
473  * </programlisting>
474  * </example>
475  *
476  * See glib_check_version() for a runtime check.
477  *
478  * Returns: %TRUE if the version of the GLib header files
479  * is the same as or newer than the passed-in version.
480  */
481 #define GLIB_CHECK_VERSION(major,minor,micro)    \
482     (GLIB_MAJOR_VERSION > (major) || \
483      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
484      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
485       GLIB_MICRO_VERSION >= (micro)))
486
487 G_END_DECLS
488
489 #ifndef G_DISABLE_DEPRECATED
490
491 /*
492  * This macro is deprecated. This DllMain() is too complex. It is
493  * recommended to write an explicit minimal DLlMain() that just saves
494  * the handle to the DLL and then use that handle instead, for
495  * instance passing it to
496  * g_win32_get_package_installation_directory_of_module().
497  *
498  * On Windows, this macro defines a DllMain function that stores the
499  * actual DLL name that the code being compiled will be included in.
500  * STATIC should be empty or 'static'. DLL_NAME is the name of the
501  * (pointer to the) char array where the DLL name will be stored. If
502  * this is used, you must also include <windows.h>. If you need a more complex
503  * DLL entry point function, you cannot use this.
504  *
505  * On non-Windows platforms, expands to nothing.
506  */
507
508 #ifndef G_PLATFORM_WIN32
509 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
510 #else
511 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                 \
512 static char *dll_name;                                                  \
513                                                                         \
514 BOOL WINAPI                                                             \
515 DllMain (HINSTANCE hinstDLL,                                            \
516          DWORD     fdwReason,                                           \
517          LPVOID    lpvReserved)                                         \
518 {                                                                       \
519   wchar_t wcbfr[1000];                                                  \
520   char *tem;                                                            \
521   switch (fdwReason)                                                    \
522     {                                                                   \
523     case DLL_PROCESS_ATTACH:                                            \
524       GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
525       tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);              \
526       dll_name = g_path_get_basename (tem);                             \
527       g_free (tem);                                                     \
528       break;                                                            \
529     }                                                                   \
530                                                                         \
531   return TRUE;                                                          \
532 }
533
534 #endif  /* !G_DISABLE_DEPRECATED */
535
536 #endif /* G_PLATFORM_WIN32 */
537
538 #endif /* __G_UTILS_H__ */