g_get_home_dir



In the course of setting up a GTK based application we
needed to redirect the user's preferences files
through an environment variable.  External business
reasons require us to be able to support several
instances of the program running under one username
and its easy enough to create the necessary
configuration information in a directory.   However it
appears that many applications built using GTK do not
provide a comand line option to select profile
information.  Instead they place it inthe home
directory, relying on changes to $HOME to allow
redirection.  

This would be fine in or case except that testing
revealed that the code used g_get_home_dir and that
routine always returns the /etc/passwd entry if
possible,regardless of the value of HOME. 

g_get_home_dir is documented in the head comment as
finding the user's home directory through an
environmetn variable (such as HOME) first and if that
fails to fall back to the underlying OS features (such
as /etc/passwd). 

Looking deeper in the source this appears to be due to
a choice in g_get_any_init.   

I quote:

/* Return the home directory of the user. If there is
a HOME
 * environment variable, its value is returned,
otherwise use some
 * system-dependent way of finding it out. If no home
directory can be
 * deduced, return NULL.
 */

G_CONST_RETURN gchar*
g_get_home_dir (void)
{
  G_LOCK (g_utils_global);
  if (!g_tmp_dir)
    g_get_any_init ();
  G_UNLOCK (g_utils_global);
  
  return g_home_dir;
}

 

However perusing the code it calls in g_get_any_init
in the glib source I have to hand,  glib 2.2.3, I find
a comment in the windows if def's  reading:

#ifdef G_OS_WIN32
      /* We check $HOME first for Win32, though it is
a last resort for Unix
       * where we prefer the results of getpwuid().
       */

and indeed the UNIX code calls get_pw_ent and only
respects $HOME if there is nothing else found:

      if (!g_home_dir)
        g_home_dir = g_strdup (g_getenv ("HOME"));

While ignoring environment variables is an appropriate
security measure in some cases it is counter intuitive
- as the code comments clearly supports - and makes
life much harder for those working with applications
built on gtk. 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]