diff options
author | Tim Janik <timj@gtk.org> | 1999-01-23 02:16:11 +0000 |
---|---|---|
committer | Tim Janik <timj@src.gnome.org> | 1999-01-23 02:16:11 +0000 |
commit | 63cc3f3cfb1ef2e4447ef962fd028ee58a8ec1c8 (patch) | |
tree | 2f57b3880059fff015ff9024ed164d88ad12b13c /glib | |
parent | 73fabcee297500a3d00cf2f8c3669c9fd41d0676 (diff) | |
download | glib-63cc3f3cfb1ef2e4447ef962fd028ee58a8ec1c8.tar.gz |
cleaned up the errno mess for GETPWUID. we especially don't want to
Sat Jan 23 02:14:28 1999 Tim Janik <timj@gtk.org>
* gutils.c (g_get_any_init): cleaned up the errno mess for
GETPWUID. we especially don't want to g_error() out here!
the warning for G_THREADS_ENABLED and !HAVE_GETPWUID_R isn't
gcc related.
if !HAVE_PWD_H and !NATIVE_WIN32, g_free the home dir before
resetting it to NULL, why are we doing this anyways?
reordered code a bit so we always provide defaults (except
for g_home_dir).
Diffstat (limited to 'glib')
-rw-r--r-- | glib/gutils.c | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/glib/gutils.c b/glib/gutils.c index 5ca27b7b5..1a5fad82e 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -371,6 +371,7 @@ g_getenv (const gchar *variable) #endif } + G_LOCK_DECLARE_STATIC (g_utils_global); static gchar *g_tmp_dir = NULL; @@ -400,6 +401,7 @@ g_get_any_init (void) g_tmp_dir[k-1] = '\0'; } #endif + if (!g_tmp_dir) { #ifndef NATIVE_WIN32 @@ -428,51 +430,55 @@ g_get_any_init (void) g_free (homedrive); g_free (homepath); } +#endif /* !NATIVE_WIN32 */ + if (!g_home_dir) g_home_dir = g_strdup (g_getenv ("HOME")); -#else - g_home_dir = g_strdup (g_getenv ("HOME")); -#endif + #ifdef HAVE_PWD_H { - struct passwd *pw = NULL, pwd; + struct passwd *pw = NULL; gpointer buffer = NULL; - guint bufsize = sizeof (struct passwd); -# ifdef HAVE_GETPWUID_R - while (TRUE) + +# ifdef HAVE_GETPWUID_R + struct passwd pwd; + guint bufsize = 1; // sizeof (struct passwd); + gint error; + + do { - int error = 0; - errno = 0; - buffer = g_realloc (buffer, bufsize); + g_free (buffer); + buffer = g_malloc (bufsize); + # ifdef HAVE_GETPWUID_R_POSIX error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw); - if (errno == 0) /* The errorcode is in error (might be 0, too) */ - errno = error; -# else /* HAVE_GETPWUID_R_POSIX */ + error = error ? errno : 0; +# else /* !HAVE_GETPWUID_R_POSIX */ pw = getpwuid_r (getuid (), &pwd, buffer, bufsize); -# endif /* HAVE_GETPWUID_R_POSIX */ - if (errno == 0) - { - g_assert (pw); - break; - } - - if (errno != ERANGE) - g_error ("Could not read account information: %s", - g_strerror (errno)); + error = errno; +# endif /* !HAVE_GETPWUID_R_POSIX */ + bufsize *= 2; } -# else /* HAVE_GETPWUID_R */ -# if defined(G_THREADS_ENABLED) && defined(__GNUC__) + while (error == ERANGE); + + if (error) + g_warning ("getpwuid_r(): failed due to: %s", g_strerror (error)); + +# else /* !HAVE_GETPWUID_R */ + +# ifdef G_THREADS_ENABLED # warning "the `g_get_(user_name|real_name|home_dir|tmp_dir)'" # warning "functions will not be MT-safe during their first call" # warning "because there is no `getpwuid_r' on your system." -# endif +# endif /* G_THREADS_ENABLED */ + setpwent (); pw = getpwuid (getuid ()); endpwent (); -# endif /* HAVE_GETPWUID_R */ + +# endif /* !HAVE_GETPWUID_R */ if (pw) { @@ -483,28 +489,32 @@ g_get_any_init (void) } g_free (buffer); } + #else /* !HAVE_PWD_H */ + # ifdef NATIVE_WIN32 { guint len = 17; + gchar buffer[17]; - g_user_name = g_new (gchar, len); - - if (!GetUserName (g_user_name, &len)) + if (GetUserName (buffer, &len)) { - g_free (g_user_name); - g_user_name = g_strdup ("somebody"); - g_real_name = g_strdup ("Unknown"); + g_user_name = g_strdup (buffer); + g_real_name = g_strdup (buffer); } - else - g_real_name = g_strdup (g_user_name); } # else /* !NATIVE_WIN32 */ - g_user_name = g_strdup ("somebody"); - g_real_name = g_strdup ("Unknown"); + /* why are we forcefully setting g_home_dir to NULL here? */ + g_free (g_home_dir); g_home_dir = NULL; # endif /* !NATIVE_WIN32 */ + #endif /* !HAVE_PWD_H */ + + if (!g_user_name) + g_user_name = g_strdup ("somebody"); + if (!g_real_name) + g_real_name = g_strdup ("Unknown"); } } |