New function. Returns the machine's name, or one of its names. Document
authorTor Lillqvist <tml@novell.com>
Wed, 22 Jun 2005 08:54:28 +0000 (08:54 +0000)
committerTor Lillqvist <tml@src.gnome.org>
Wed, 22 Jun 2005 08:54:28 +0000 (08:54 +0000)
2005-06-22  Tor Lillqvist  <tml@novell.com>

* 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
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-8
glib/glib.symbols
glib/gutils.c
glib/gutils.h
tests/testglib.c

index cd102ae..0f7bb90 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2005-06-22  Tor Lillqvist  <tml@novell.com>
+
+       * 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  <mclasen@redhat.com>
 
        * glib/goption.h: 
index cd102ae..0f7bb90 100644 (file)
@@ -1,3 +1,16 @@
+2005-06-22  Tor Lillqvist  <tml@novell.com>
+
+       * 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  <mclasen@redhat.com>
 
        * glib/goption.h: 
index cd102ae..0f7bb90 100644 (file)
@@ -1,3 +1,16 @@
+2005-06-22  Tor Lillqvist  <tml@novell.com>
+
+       * 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  <mclasen@redhat.com>
 
        * glib/goption.h: 
index cd102ae..0f7bb90 100644 (file)
@@ -1,3 +1,16 @@
+2005-06-22  Tor Lillqvist  <tml@novell.com>
+
+       * 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  <mclasen@redhat.com>
 
        * glib/goption.h: 
index 411809b..b029230 100644 (file)
@@ -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
index b0b27c3..19274d2 100644 (file)
@@ -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;
 
index 4bc8528..079a86c 100644 (file)
@@ -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);
index 1f646b2..00f14cc 100644 (file)
@@ -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 ();