[2942] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[PATCH] Use g_get_host_name instead of gethostname and MAXHOSTNAMELEN

daemon@ATHENA.MIT.EDU (David Benjamin)
Sat Feb 26 20:12:01 2011

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Sat, 26 Feb 2011 20:11:54 -0500

This lets us punt thishost from owl_global. g_get_host_name implements
the same localhost fallback, so we really don't need it.

Also, Solaris gets upset about MAXHOSTNAMELEN when you define
_XOPEN_SOURCE=500 without __EXTENSIONS__, if we ever care to get rid of
that. (That said, glib's answer to this is lame. They just hardcode 100
and even forget to NUL-terminate the buffer; POSIX is apparently
braindead and leaves truncation undefined. glibc is kind enough to give
you ENAMETOOLONG.)
---
 global.c   |   19 -------------------
 message.c  |   10 +---------
 owl.h      |    1 -
 variable.c |    2 +-
 4 files changed, 2 insertions(+), 30 deletions(-)

diff --git a/global.c b/global.c
index 2e13f3d..86d6299 100644
--- a/global.c
+++ b/global.c
@@ -8,28 +8,14 @@
 #include <time.h>
 #include "owl.h"
 
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 256
-#endif
-
 static void _owl_global_init_windows(owl_global *g);
 
 void owl_global_init(owl_global *g) {
-  struct hostent *hent;
-  char hostname[MAXHOSTNAMELEN];
   char *cd;
   const char *homedir;
 
   g_type_init();
 
-  gethostname(hostname, MAXHOSTNAMELEN);
-  hent=gethostbyname(hostname);
-  if (!hent) {
-    g->thishost=g_strdup("localhost");
-  } else {
-    g->thishost=g_strdup(hent->h_name);
-  }
-
   g->lines=LINES;
   g->cols=COLS;
   /* We shouldn't need this if we initialize lines and cols before the first
@@ -498,11 +484,6 @@ time_t owl_global_get_idletime(const owl_global *g) {
   return(time(NULL)-g->lastinputtime);
 }
 
-const char *owl_global_get_hostname(const owl_global *g) {
-  if (g->thishost) return(g->thishost);
-  return("");
-}
-
 /* viewwin */
 
 owl_viewwin *owl_global_get_viewwin(owl_global *g) {
diff --git a/message.c b/message.c
index a168216..e994587 100644
--- a/message.c
+++ b/message.c
@@ -959,8 +959,6 @@ void owl_message_create_pseudo_zlogin(owl_message *m, int direction, const char
 
 void owl_message_create_from_zwrite(owl_message *m, const owl_zwrite *z, const char *body)
 {
-  int ret;
-  char hostbuff[5000];
   char *replyline;
   
   owl_message_init(m);
@@ -990,13 +988,7 @@ void owl_message_create_from_zwrite(owl_message *m, const owl_zwrite *z, const c
   owl_message_set_zsig(m, owl_zwrite_get_zsig(z));
   
   /* save the hostname */
-  ret=gethostname(hostbuff, MAXHOSTNAMELEN);
-  hostbuff[MAXHOSTNAMELEN]='\0';
-  if (ret) {
-    owl_message_set_hostname(m, "localhost");
-  } else {
-    owl_message_set_hostname(m, hostbuff);
-  }
+  owl_message_set_hostname(m, g_get_host_name());
 
   /* set the "isprivate" attribute if it's a private zephyr. */
   if (owl_zwrite_is_personal(z)) {
diff --git a/owl.h b/owl.h
index aac5ff3..89405fb 100644
--- a/owl.h
+++ b/owl.h
@@ -593,7 +593,6 @@ typedef struct _owl_global {
   gulong typwin_erase_id;
   int rightshift;
   volatile sig_atomic_t resizepending;
-  char *thishost;
   char *homedir;
   char *confdir;
   char *startupfile;
diff --git a/variable.c b/variable.c
index b18ccc8..68307ea 100644
--- a/variable.c
+++ b/variable.c
@@ -465,7 +465,7 @@ int owl_variable_disable_ctrl_d_set(owl_variable *v, const void *newval)
 
 int owl_variable_tty_set(owl_variable *v, const void *newval)
 {
-  owl_zephyr_set_locationinfo(owl_global_get_hostname(&g), newval);
+  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
   return(owl_variable_string_set_default(v, newval));
 }
 
-- 
1.7.3.3.399.g06ddf


home help back first fref pref prev next nref lref last post