Replace gethostname by g_get_host_name
Replace `gethostname` by `g_get_host_name`.
Testing Done:
Compile only.
Reviewed at https://reviews.imfreedom.org/r/555/
--- a/libpurple/protocols/jabber/jabber.c Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/protocols/jabber/jabber.c Thu Mar 04 22:43:51 2021 -0600
@@ -164,9 +164,11 @@
-static char *jabber_prep_resource(char *input) {
- char hostname[256], /* current hostname */
+jabber_prep_resource(char *input) + const gchar *hostname = NULL, *dot = NULL; /* Empty resource == don't send any */
if (input == NULL || *input == '\0')
@@ -176,25 +178,20 @@
/* Replace __HOSTNAME__ with hostname */
- if (gethostname(hostname, sizeof(hostname) - 1)) {
- purple_debug_warning("jabber", "gethostname: %s\n", g_strerror(errno));
- /* according to glibc doc, the only time an error is returned
- is if the hostname is longer than the buffer, in which case
- glibc 2.2+ would still fill the buffer with partial
- hostname, so maybe we want to detect that and use it
- g_strlcpy(hostname, "localhost", sizeof(hostname));
- hostname[sizeof(hostname) - 1] = '\0';
+ hostname = g_get_host_name(); /* We want only the short hostname, not the FQDN - this will prevent the
* resource string from being unreasonably long on systems which stuff the
* whole FQDN in the hostname */
- if((dot = strchr(hostname, '.')))
- return purple_strreplace(input, "__HOSTNAME__", hostname);
+ if ((dot = strchr(hostname, '.')) != NULL) { + gchar *short_hostname = g_strndup(hostname, dot - hostname); + result = purple_strreplace(input, "__HOSTNAME__", short_hostname); + g_free(short_hostname); + result = purple_strreplace(input, "__HOSTNAME__", hostname); --- a/libpurple/protocols/zephyr/ZLocations.c Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/protocols/zephyr/ZLocations.c Thu Mar 04 22:43:51 2021 -0600
@@ -79,10 +79,7 @@
- if (gethostname(host, MAXHOSTNAMELEN) < 0)
- hent = gethostbyname(host);
+ hent = gethostbyname(g_get_host_name()); (void) strncpy(host, hent->h_name, sizeof(host));
host[sizeof(host) - 1] = '\0';
--- a/libpurple/protocols/zephyr/zephyr.c Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/protocols/zephyr/zephyr.c Thu Mar 04 22:43:51 2021 -0600
@@ -487,20 +487,18 @@
/* XXX This code may not be Win32 clean */
- if (gethostname(zephyr->ourhost, sizeof(zephyr->ourhost)) == -1) {
- purple_debug_error("zephyr", "unable to retrieve hostname, %%host%% and %%canon%% will be wrong in subscriptions and have been set to unknown\n");
- g_strlcpy(zephyr->ourhost, "unknown", sizeof(zephyr->ourhost));
- g_strlcpy(zephyr->ourhostcanon, "unknown", sizeof(zephyr->ourhostcanon));
+ zephyr->ourhost = g_strdup(g_get_host_name()); + if (!(hent = gethostbyname(zephyr->ourhost))) { + purple_debug_error("zephyr", + "unable to resolve hostname, %%canon%% will be " + "wrong in subscriptions and has been set to the " + "value of %%host%%, %s", + zephyr->ourhostcanon = g_strdup(zephyr->ourhost); - if (!(hent = gethostbyname(zephyr->ourhost))) {
- purple_debug_error("zephyr", "unable to resolve hostname, %%canon%% will be wrong in subscriptions.and has been set to the value of %%host%%, %s\n",zephyr->ourhost);
- g_strlcpy(zephyr->ourhostcanon, zephyr->ourhost, sizeof(zephyr->ourhostcanon));
- g_strlcpy(zephyr->ourhostcanon, hent->h_name, sizeof(zephyr->ourhostcanon));
+ zephyr->ourhostcanon = g_strdup(hent->h_name); static void process_zsubs(zephyr_account *zephyr)
@@ -860,6 +858,9 @@
g_source_remove(zephyr->loctimer);
+ g_clear_pointer(&zephyr->ourhost, g_free); + g_clear_pointer(&zephyr->ourhostcanon, g_free); --- a/libpurple/protocols/zephyr/zephyr_account.h Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/protocols/zephyr/zephyr_account.h Thu Mar 04 22:43:51 2021 -0600
@@ -63,8 +63,8 @@
- char ourhost[HOST_NAME_MAX + 1];
- char ourhostcanon[HOST_NAME_MAX + 1];
zephyr_connection_type connection_type;
--- a/libpurple/win32/libc_interface.c Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/win32/libc_interface.c Thu Mar 04 22:43:51 2021 -0600
@@ -108,13 +108,3 @@
-int wpurple_gethostname(char *name, size_t size) {
- if(gethostname(name, size) == SOCKET_ERROR) {
- errno = WSAGetLastError();
--- a/libpurple/win32/libc_interface.h Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/win32/libc_interface.h Thu Mar 04 22:43:51 2021 -0600
@@ -57,9 +57,6 @@
#define gethostbyname( name ) \
wpurple_gethostbyname( name )
-#define gethostname( name, size ) \
-wpurple_gethostname( name, size )
#if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 3 || \
!defined(IS_WIN32_CROSS_COMPILED)
--- a/libpurple/win32/libc_internal.h Thu Mar 04 20:32:36 2021 -0600
+++ b/libpurple/win32/libc_internal.h Thu Mar 04 22:43:51 2021 -0600
@@ -38,9 +38,6 @@
-int wpurple_gethostname(char *name, size_t size);
#endif /* PURPLE_WIN32_LIBC_INTERNAL */