pidgin/pidgin

Replace gethostname by g_get_host_name

2021-03-04, Elliott Sales de Andrade
691bd780ab06
Parents afc646d2e7cb
Children 28d50eece92d
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 @@
jabber_session_init(js);
}
-static char *jabber_prep_resource(char *input) {
- char hostname[256], /* current hostname */
- *dot = NULL;
+static char *
+jabber_prep_resource(char *input)
+{
+ const gchar *hostname = NULL, *dot = NULL;
+ gchar *result = NULL;
/* Empty resource == don't send any */
if (input == NULL || *input == '\0')
@@ -176,25 +178,20 @@
return g_strdup(input);
/* 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
- instead
- */
- 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, '.')))
- *dot = '\0';
-
- 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);
+ } else {
+ result = purple_strreplace(input, "__HOSTNAME__", hostname);
+ }
+
+ return result;
}
static gboolean
--- 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 (!reenter) {
- if (gethostname(host, MAXHOSTNAMELEN) < 0)
- return (errno);
-
- hent = gethostbyname(host);
+ hent = gethostbyname(g_get_host_name());
if (hent) {
(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 */
struct hostent *hent;
- 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->ourhost);
+ zephyr->ourhostcanon = g_strdup(zephyr->ourhost);
return;
}
- 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));
- return;
- }
-
- 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);
zephyr->loctimer = 0;
zephyr->close(zephyr);
+
+ g_clear_pointer(&zephyr->ourhost, g_free);
+ g_clear_pointer(&zephyr->ourhostcanon, g_free);
}
static gboolean
--- 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 @@
GSList *subscrips;
int last_id;
unsigned short port;
- char ourhost[HOST_NAME_MAX + 1];
- char ourhostcanon[HOST_NAME_MAX + 1];
+ gchar *ourhost;
+ gchar *ourhostcanon;
zephyr_connection_type connection_type;
char *exposure;
GSubprocess *tzc_proc;
--- 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 @@
}
return hp;
}
-
-/* unistd.h */
-
-int wpurple_gethostname(char *name, size_t size) {
- if(gethostname(name, size) == SOCKET_ERROR) {
- errno = WSAGetLastError();
- return -1;
- }
- return 0;
-}
--- 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 )
-
/* stdio.h */
#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 @@
#define F_SETFL 4
#define O_NONBLOCK 04000
-/* unistd.h */
-int wpurple_gethostname(char *name, size_t size);
-
G_END_DECLS
#endif /* PURPLE_WIN32_LIBC_INTERNAL */