From 477989f555f13cd067c043f61c6941c5dc6409ad Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Wed, 22 Jun 2005 08:54:28 +0000 Subject: [PATCH] New function. Returns the machine's name, or one of its names. Document 2005-06-22 Tor Lillqvist * glib/gutils.c (g_get_host_name): New function. Returns the machine's name, or one of its names. Document that it is best-effort only, and not guaranteed to be unique or anything. (g_get_any_init): Get the host name here. On Unix use gethostname(), on Windows use GetComputerName(). (#5200) * glib/gutils.h * glib/glib.symbols: Add here, too. * tests/testglib.c: Test it. --- ChangeLog | 13 +++++++++++++ ChangeLog.pre-2-10 | 13 +++++++++++++ ChangeLog.pre-2-12 | 13 +++++++++++++ ChangeLog.pre-2-8 | 13 +++++++++++++ glib/glib.symbols | 1 + glib/gutils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ glib/gutils.h | 1 + tests/testglib.c | 1 + 8 files changed, 101 insertions(+) diff --git a/ChangeLog b/ChangeLog index cd102aeb..0f7bb905 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2005-06-22 Tor Lillqvist + + * glib/gutils.c (g_get_host_name): New function. Returns the + machine's name, or one of its names. Document that it is + best-effort only, and not guaranteed to be unique or anything. + (g_get_any_init): Get the host name here. On Unix use + gethostname(), on Windows use GetComputerName(). (#5200) + + * glib/gutils.h + * glib/glib.symbols: Add here, too. + + * tests/testglib.c: Test it. + 2005-06-18 Matthias Clasen * glib/goption.h: diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index cd102aeb..0f7bb905 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,16 @@ +2005-06-22 Tor Lillqvist + + * glib/gutils.c (g_get_host_name): New function. Returns the + machine's name, or one of its names. Document that it is + best-effort only, and not guaranteed to be unique or anything. + (g_get_any_init): Get the host name here. On Unix use + gethostname(), on Windows use GetComputerName(). (#5200) + + * glib/gutils.h + * glib/glib.symbols: Add here, too. + + * tests/testglib.c: Test it. + 2005-06-18 Matthias Clasen * glib/goption.h: diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index cd102aeb..0f7bb905 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,16 @@ +2005-06-22 Tor Lillqvist + + * glib/gutils.c (g_get_host_name): New function. Returns the + machine's name, or one of its names. Document that it is + best-effort only, and not guaranteed to be unique or anything. + (g_get_any_init): Get the host name here. On Unix use + gethostname(), on Windows use GetComputerName(). (#5200) + + * glib/gutils.h + * glib/glib.symbols: Add here, too. + + * tests/testglib.c: Test it. + 2005-06-18 Matthias Clasen * glib/goption.h: diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index cd102aeb..0f7bb905 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,16 @@ +2005-06-22 Tor Lillqvist + + * glib/gutils.c (g_get_host_name): New function. Returns the + machine's name, or one of its names. Document that it is + best-effort only, and not guaranteed to be unique or anything. + (g_get_any_init): Get the host name here. On Unix use + gethostname(), on Windows use GetComputerName(). (#5200) + + * glib/gutils.h + * glib/glib.symbols: Add here, too. + + * tests/testglib.c: Test it. + 2005-06-18 Matthias Clasen * glib/goption.h: diff --git a/glib/glib.symbols b/glib/glib.symbols index 411809b8..b0292309 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1164,6 +1164,7 @@ g_get_current_dir PRIVATE g_getenv PRIVATE g_unsetenv PRIVATE g_get_home_dir PRIVATE +g_get_host_name g_setenv PRIVATE g_listenv PRIVATE #ifdef G_OS_WIN32 diff --git a/glib/gutils.c b/glib/gutils.c index b0b27c3b..19274d23 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -1363,6 +1363,7 @@ static gchar *g_tmp_dir = NULL; static gchar *g_user_name = NULL; static gchar *g_real_name = NULL; static gchar *g_home_dir = NULL; +static gchar *g_host_name = NULL; #ifdef G_OS_WIN32 /* System codepage versions of the above, kept at file level so that they, @@ -1442,6 +1443,8 @@ g_get_any_init (void) { if (!g_tmp_dir) { + gchar hostname[100]; + g_tmp_dir = g_strdup (g_getenv ("TMPDIR")); if (!g_tmp_dir) g_tmp_dir = g_strdup (g_getenv ("TMP")); @@ -1650,6 +1653,22 @@ g_get_any_init (void) if (!g_real_name) g_real_name = g_strdup ("Unknown"); +#ifndef G_OS_WIN32 + if (gethostname (hostname, sizeof (hostname)) == -1) + g_host_name = g_strdup ("unknown"); + else + g_host_name = g_strdup (hostname); +#else + { + DWORD size = sizeof (hostname); + + if (!GetComputerName (hostname, &size)) + g_host_name = g_strdup ("unknown"); + else + g_host_name = g_strdup (hostname); + } +#endif + #ifdef G_OS_WIN32 g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL); g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL); @@ -1761,6 +1780,33 @@ g_get_tmp_dir (void) return g_tmp_dir; } +/** + * g_get_host_name: + * + * Return a name for the machine. + * + * The returned name is not necessarily a fully-qualified domain name, + * or even present in DNS or some other name service at all. It need + * not even be unique on your local network or site, but usually it + * is. Callers should not rely on the return value having any specific + * properties like uniqueness for security purposes. Even if the name + * of the machine is changed while an application is running, the + * return value from this function does not change. The returned + * string is owned by GLib and should not be modified or freed. If no + * name can be determined, a default fixed string "unknown" is + * returned. + */ +const gchar * +g_get_host_name (void) +{ + G_LOCK (g_utils_global); + if (!g_tmp_dir) + g_get_any_init (); + G_UNLOCK (g_utils_global); + + return g_host_name; +} + G_LOCK_DEFINE_STATIC (g_prgname); static gchar *g_prgname = NULL; diff --git a/glib/gutils.h b/glib/gutils.h index 4bc85282..079a86c5 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -117,6 +117,7 @@ G_CONST_RETURN gchar* g_get_user_name (void); G_CONST_RETURN gchar* g_get_real_name (void); G_CONST_RETURN gchar* g_get_home_dir (void); G_CONST_RETURN gchar* g_get_tmp_dir (void); +G_CONST_RETURN gchar* g_get_host_name (void); gchar* g_get_prgname (void); void g_set_prgname (const gchar *prgname); G_CONST_RETURN gchar* g_get_application_name (void); diff --git a/tests/testglib.c b/tests/testglib.c index 1f646b2d..00f14cce 100644 --- a/tests/testglib.c +++ b/tests/testglib.c @@ -438,6 +438,7 @@ main (int argc, g_free (string); g_print ("user: %s\n", g_get_user_name ()); g_print ("real: %s\n", g_get_real_name ()); + g_print ("host: %s\n", g_get_host_name ()); s = g_get_home_dir (); g_print ("home: %s\n", s ? s : "NULL!"); s = g_get_user_data_dir (); -- 2.34.1