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).
This commit is contained in:
Tim Janik 1999-01-23 02:16:11 +00:00 committed by Tim Janik
parent 73fabcee29
commit 63cc3f3cfb
12 changed files with 188 additions and 72 deletions

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -1,3 +1,14 @@
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).
Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com> Thu Jan 21 12:40:11 EST 1999 Jeff Garzik <jgarzik@pobox.com>
* tests/{Makefile.am, string-test.c, strfunc-test.c}: * tests/{Makefile.am, string-test.c, strfunc-test.c}:

View File

@ -371,6 +371,7 @@ g_getenv (const gchar *variable)
#endif #endif
} }
G_LOCK_DECLARE_STATIC (g_utils_global); G_LOCK_DECLARE_STATIC (g_utils_global);
static gchar *g_tmp_dir = NULL; static gchar *g_tmp_dir = NULL;
@ -400,6 +401,7 @@ g_get_any_init (void)
g_tmp_dir[k-1] = '\0'; g_tmp_dir[k-1] = '\0';
} }
#endif #endif
if (!g_tmp_dir) if (!g_tmp_dir)
{ {
#ifndef NATIVE_WIN32 #ifndef NATIVE_WIN32
@ -428,51 +430,55 @@ g_get_any_init (void)
g_free (homedrive); g_free (homedrive);
g_free (homepath); g_free (homepath);
} }
#endif /* !NATIVE_WIN32 */
if (!g_home_dir) if (!g_home_dir)
g_home_dir = g_strdup (g_getenv ("HOME")); g_home_dir = g_strdup (g_getenv ("HOME"));
#else
g_home_dir = g_strdup (g_getenv ("HOME"));
#endif
#ifdef HAVE_PWD_H #ifdef HAVE_PWD_H
{ {
struct passwd *pw = NULL, pwd; struct passwd *pw = NULL;
gpointer buffer = NULL; gpointer buffer = NULL;
guint bufsize = sizeof (struct passwd);
# ifdef HAVE_GETPWUID_R # ifdef HAVE_GETPWUID_R
while (TRUE) struct passwd pwd;
guint bufsize = 1; // sizeof (struct passwd);
gint error;
do
{ {
int error = 0; g_free (buffer);
errno = 0; buffer = g_malloc (bufsize);
buffer = g_realloc (buffer, bufsize);
# ifdef HAVE_GETPWUID_R_POSIX # ifdef HAVE_GETPWUID_R_POSIX
error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw); error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
if (errno == 0) /* The errorcode is in error (might be 0, too) */ error = error ? errno : 0;
errno = error; # else /* !HAVE_GETPWUID_R_POSIX */
# else /* HAVE_GETPWUID_R_POSIX */
pw = getpwuid_r (getuid (), &pwd, buffer, bufsize); pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
# endif /* HAVE_GETPWUID_R_POSIX */ error = errno;
if (errno == 0) # endif /* !HAVE_GETPWUID_R_POSIX */
{
g_assert (pw);
break;
}
if (errno != ERANGE)
g_error ("Could not read account information: %s",
g_strerror (errno));
bufsize *= 2; bufsize *= 2;
} }
# else /* HAVE_GETPWUID_R */ while (error == ERANGE);
# if defined(G_THREADS_ENABLED) && defined(__GNUC__)
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 "the `g_get_(user_name|real_name|home_dir|tmp_dir)'"
# warning "functions will not be MT-safe during their first call" # warning "functions will not be MT-safe during their first call"
# warning "because there is no `getpwuid_r' on your system." # warning "because there is no `getpwuid_r' on your system."
# endif # endif /* G_THREADS_ENABLED */
setpwent (); setpwent ();
pw = getpwuid (getuid ()); pw = getpwuid (getuid ());
endpwent (); endpwent ();
# endif /* HAVE_GETPWUID_R */
# endif /* !HAVE_GETPWUID_R */
if (pw) if (pw)
{ {
@ -483,28 +489,32 @@ g_get_any_init (void)
} }
g_free (buffer); g_free (buffer);
} }
#else /* !HAVE_PWD_H */ #else /* !HAVE_PWD_H */
# ifdef NATIVE_WIN32 # ifdef NATIVE_WIN32
{ {
guint len = 17; guint len = 17;
gchar buffer[17];
g_user_name = g_new (gchar, len); if (GetUserName (buffer, &len))
if (!GetUserName (g_user_name, &len))
{ {
g_free (g_user_name); g_user_name = g_strdup (buffer);
g_user_name = g_strdup ("somebody"); g_real_name = g_strdup (buffer);
g_real_name = g_strdup ("Unknown");
} }
else
g_real_name = g_strdup (g_user_name);
} }
# else /* !NATIVE_WIN32 */ # else /* !NATIVE_WIN32 */
g_user_name = g_strdup ("somebody"); /* why are we forcefully setting g_home_dir to NULL here? */
g_real_name = g_strdup ("Unknown"); g_free (g_home_dir);
g_home_dir = NULL; g_home_dir = NULL;
# endif /* !NATIVE_WIN32 */ # endif /* !NATIVE_WIN32 */
#endif /* !HAVE_PWD_H */ #endif /* !HAVE_PWD_H */
if (!g_user_name)
g_user_name = g_strdup ("somebody");
if (!g_real_name)
g_real_name = g_strdup ("Unknown");
} }
} }

View File

@ -371,6 +371,7 @@ g_getenv (const gchar *variable)
#endif #endif
} }
G_LOCK_DECLARE_STATIC (g_utils_global); G_LOCK_DECLARE_STATIC (g_utils_global);
static gchar *g_tmp_dir = NULL; static gchar *g_tmp_dir = NULL;
@ -400,6 +401,7 @@ g_get_any_init (void)
g_tmp_dir[k-1] = '\0'; g_tmp_dir[k-1] = '\0';
} }
#endif #endif
if (!g_tmp_dir) if (!g_tmp_dir)
{ {
#ifndef NATIVE_WIN32 #ifndef NATIVE_WIN32
@ -428,51 +430,55 @@ g_get_any_init (void)
g_free (homedrive); g_free (homedrive);
g_free (homepath); g_free (homepath);
} }
#endif /* !NATIVE_WIN32 */
if (!g_home_dir) if (!g_home_dir)
g_home_dir = g_strdup (g_getenv ("HOME")); g_home_dir = g_strdup (g_getenv ("HOME"));
#else
g_home_dir = g_strdup (g_getenv ("HOME"));
#endif
#ifdef HAVE_PWD_H #ifdef HAVE_PWD_H
{ {
struct passwd *pw = NULL, pwd; struct passwd *pw = NULL;
gpointer buffer = NULL; gpointer buffer = NULL;
guint bufsize = sizeof (struct passwd);
# ifdef HAVE_GETPWUID_R # ifdef HAVE_GETPWUID_R
while (TRUE) struct passwd pwd;
guint bufsize = 1; // sizeof (struct passwd);
gint error;
do
{ {
int error = 0; g_free (buffer);
errno = 0; buffer = g_malloc (bufsize);
buffer = g_realloc (buffer, bufsize);
# ifdef HAVE_GETPWUID_R_POSIX # ifdef HAVE_GETPWUID_R_POSIX
error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw); error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
if (errno == 0) /* The errorcode is in error (might be 0, too) */ error = error ? errno : 0;
errno = error; # else /* !HAVE_GETPWUID_R_POSIX */
# else /* HAVE_GETPWUID_R_POSIX */
pw = getpwuid_r (getuid (), &pwd, buffer, bufsize); pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
# endif /* HAVE_GETPWUID_R_POSIX */ error = errno;
if (errno == 0) # endif /* !HAVE_GETPWUID_R_POSIX */
{
g_assert (pw);
break;
}
if (errno != ERANGE)
g_error ("Could not read account information: %s",
g_strerror (errno));
bufsize *= 2; bufsize *= 2;
} }
# else /* HAVE_GETPWUID_R */ while (error == ERANGE);
# if defined(G_THREADS_ENABLED) && defined(__GNUC__)
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 "the `g_get_(user_name|real_name|home_dir|tmp_dir)'"
# warning "functions will not be MT-safe during their first call" # warning "functions will not be MT-safe during their first call"
# warning "because there is no `getpwuid_r' on your system." # warning "because there is no `getpwuid_r' on your system."
# endif # endif /* G_THREADS_ENABLED */
setpwent (); setpwent ();
pw = getpwuid (getuid ()); pw = getpwuid (getuid ());
endpwent (); endpwent ();
# endif /* HAVE_GETPWUID_R */
# endif /* !HAVE_GETPWUID_R */
if (pw) if (pw)
{ {
@ -483,28 +489,32 @@ g_get_any_init (void)
} }
g_free (buffer); g_free (buffer);
} }
#else /* !HAVE_PWD_H */ #else /* !HAVE_PWD_H */
# ifdef NATIVE_WIN32 # ifdef NATIVE_WIN32
{ {
guint len = 17; guint len = 17;
gchar buffer[17];
g_user_name = g_new (gchar, len); if (GetUserName (buffer, &len))
if (!GetUserName (g_user_name, &len))
{ {
g_free (g_user_name); g_user_name = g_strdup (buffer);
g_user_name = g_strdup ("somebody"); g_real_name = g_strdup (buffer);
g_real_name = g_strdup ("Unknown");
} }
else
g_real_name = g_strdup (g_user_name);
} }
# else /* !NATIVE_WIN32 */ # else /* !NATIVE_WIN32 */
g_user_name = g_strdup ("somebody"); /* why are we forcefully setting g_home_dir to NULL here? */
g_real_name = g_strdup ("Unknown"); g_free (g_home_dir);
g_home_dir = NULL; g_home_dir = NULL;
# endif /* !NATIVE_WIN32 */ # endif /* !NATIVE_WIN32 */
#endif /* !HAVE_PWD_H */ #endif /* !HAVE_PWD_H */
if (!g_user_name)
g_user_name = g_strdup ("somebody");
if (!g_real_name)
g_real_name = g_strdup ("Unknown");
} }
} }

View File

@ -341,6 +341,10 @@ main (int argc,
string = g_get_current_dir (); string = g_get_current_dir ();
g_print ("cwd: %s\n", string); g_print ("cwd: %s\n", string);
g_free (string); g_free (string);
g_print ("user: %s\n", g_get_user_name ());
g_print ("real: %s\n", g_get_real_name ());
g_print ("home: %s\n", g_get_home_dir ());
g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
/* type sizes */ /* type sizes */
g_print ("checking size of gint8: %d", (int)sizeof (gint8)); g_print ("checking size of gint8: %d", (int)sizeof (gint8));

View File

@ -341,6 +341,10 @@ main (int argc,
string = g_get_current_dir (); string = g_get_current_dir ();
g_print ("cwd: %s\n", string); g_print ("cwd: %s\n", string);
g_free (string); g_free (string);
g_print ("user: %s\n", g_get_user_name ());
g_print ("real: %s\n", g_get_real_name ());
g_print ("home: %s\n", g_get_home_dir ());
g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
/* type sizes */ /* type sizes */
g_print ("checking size of gint8: %d", (int)sizeof (gint8)); g_print ("checking size of gint8: %d", (int)sizeof (gint8));