## Miscellaneous Utilities

Miscellaneous Utilities — Miscellaneous utility functions

## Synopsis

    #include <libxfce4util/libxfce4util.h>

    const gchar *       xfce_get_homedir                    (void);
    #define             xfce_get_homefile                   (...)
    gchar *             xfce_get_homefile_r                 (gchar *buffer,
                                                             size_t length,
                                                             const gchar *format,
                                                             ...);
    const gchar *       xfce_get_userdir                    (void);
    #define             xfce_get_userfile                   (...)
    gchar *             xfce_get_userfile_r                 (gchar *buffer,
                                                             size_t length,
                                                             const gchar *format,
                                                             ...);
    gchar *             xfce_strjoin                        (const gchar *separator,
                                                             gchar **strings,
                                                             gint count);
    gchar *             xfce_gethostname                    (void);
    gint                xfce_putenv                         (const gchar *string);
    gint                xfce_setenv                         (const gchar *name,
                                                             const gchar *value,
                                                             gboolean overwrite);
    void                xfce_unsetenv                       (const gchar *name);
    gchar *             xfce_expand_variables               (const gchar *command,
                                                             gchar **envp);

## Description

## Details

### xfce_get_homedir ()

    const gchar *       xfce_get_homedir                    (void);

Similar to `g_get_homedir()` in functionality but will never return NULL. While `g_get_homedir()` may return NULL under certain circumstances, this function is garantied to never ever return NULL, but always return a valid character pointer with the absolute path to the user's home directory.

The returned string is owned by libxfce4util and must not be freed by the caller.

Returns : | the path to the current user's home directory.

### xfce_get_homefile()

    #define             xfce_get_homefile(...)

Returns the path to a file in the user's home directory by taking a NULL terminated list of strings and appending them to the absolute path of the current user's home directory.

Returns : | the absolute path to the specified file in the current user's home directory. The caller is responsible for freeing the returned string using `g_free()`.

### xfce_get_homefile_r ()

    gchar *             xfce_get_homefile_r                 (gchar *buffer,
                                                             size_t length,
                                                             const gchar *format,
                                                             ...);

Similar in functionality to xfce_get_homefile, but uses a user defined `buffer` instead of allocating a new buffer.

xfce_get_homefile_r uses safe string operations, that says, it garanties that the resulting string is always zero terminated, as long as the `length` is greater than zero.

`buffer` : | pointer to a user provided destination buffer.
`length` : | size of `buffer` in bytes.
`format` : | printf style format string.
Returns : | pointer to `buffer`.

### xfce_get_userdir ()

    const gchar *       xfce_get_userdir                    (void);

Safe way to retrieve the path to the user's ".xfce4" directory. The path to the current user's ".xfce4" directory is either taken from the environment variable XFCE4HOME if defined, or if unset, is gained by concatenating the path to the user's home directory and the ".xfce4". That says, it will, by default, return the path "$HOME/.xfce4", where $HOME is replaced with the absolute path to the user's home directory.

The returned string is managed by libxfce4util and must not be freed by the caller.

Returns : | the path to the current user's ".xfce4" directory.

### xfce_get_userfile()

    #define             xfce_get_userfile(...)

Returns the absolute path to a file within the user's ".xfce4" directory, formed by a NULL terminated list of path components.

Returns : | the absolute path to the specified file in the user's ".xfce4" directory. The caller is responsible for freeing the returned string using `g_free()`.

### xfce_get_userfile_r ()

    gchar *             xfce_get_userfile_r                 (gchar *buffer,
                                                             size_t length,
                                                             const gchar *format,
                                                             ...);

`buffer` : | user provided destination buffer.
`length` : | size of `buffer` in bytes.
`format` : | printf style format string.
Returns : | pointer to `buffer`.

### xfce_strjoin ()

    gchar *             xfce_strjoin                        (const gchar *separator,
                                                             gchar **strings,
                                                             gint count);

Joins the `count` character strings pointed to by `strings` using `separator` to a single string.

Returns : | the joined string. The string has to be freed by the caller using `g_free()` when no longer needed.

### xfce_gethostname ()

    gchar *             xfce_gethostname                    (void);

Portable way to query the hostname of the node running the process. This function does not ever return `NULL`, but always returns a string containing the current node's hostname.

Returns : | the current node's hostname. The string has to be freed by the caller using `g_free()`.

### xfce_putenv ()

    gint                xfce_putenv                         (const gchar *string);

Portable replacement for the Unix `putenv()` library function. `string` has to have the form "name=value". Calling `xfce_putenv()` this way is equal to calling xfce_setenv("name", "value", `TRUE`).

`string` : | Character string in the form "name=value".
Returns : | 0 if the operation was successful; otherwise the global variable errno is set to indicate the error and a value of -1 is returned.

Since 4.2

### xfce_setenv ()

    gint                xfce_setenv                         (const gchar *name,
                                                             const gchar *value,
                                                             gboolean overwrite);

If the variable `name` does not exists in the list of environment variables, it is inserted with its value being set to `value`. If the variable does exist, then its value is only changed to `value` if `overwrite` is TRUE.

On plattforms that provide a working native `setenv()` library call, this functions is used, on all other plattforms `setenv()` is emulated using `xfce_putenv()`. That says, `xfce_setenv()` is not subject to the limitations that apply to some `setenv()` implementations and seem also to apply to `g_setenv()` in Glib 2.4.

`name` : | the name of the environment variable to set, must not contain '='.
`value` : | the value to set the variable to.
`overwrite` : | whether to change the variable if it already exists.
Returns : | 0 if the operation was successful; otherwise the global variable errno is set to indicate the error and a value of -1 is returned.

Since 4.2

### xfce_unsetenv ()

    void                xfce_unsetenv                       (const gchar *name);

Deletes all instances of the variables `name` from the list of environment variables in the current process.

Note that on some systems, the memory used for the variable and its value can't be reclaimed. Furthermore, this function can't be guaranteed to operate in a threadsafe way.

`name` : | the name of the environment variable to unset, must not contain '='.

Since 4.2

### xfce_expand_variables ()

    gchar *             xfce_expand_variables               (const gchar *command,
                                                             gchar **envp);

Expands shell like environment variables and tilde (~/ and ~user/ are both supported) in `command`.

`command` : | Input string or `NULL`.
`envp` : | Addition environment variables to take into account. These variables have higher priority than the ones in the process's environment.
Returns : | `NULL` on error, else the string, which should be freed using `g_free()` when no longer needed.

Since 4.2