Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15224 > unrolled thread

[PATCH] Fix \H: Use getaddrinfo to get full hostname

Started byThomas Deutschmann <whissi@gentoo.org>
First post2019-07-24 15:58 +0200
Last post2019-07-24 15:58 +0200
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH] Fix \H: Use getaddrinfo to get full hostname Thomas Deutschmann <whissi@gentoo.org> - 2019-07-24 15:58 +0200

#15224 — [PATCH] Fix \H: Use getaddrinfo to get full hostname

FromThomas Deutschmann <whissi@gentoo.org>
Date2019-07-24 15:58 +0200
Subject[PATCH] Fix \H: Use getaddrinfo to get full hostname
Message-ID<mailman.2133.1563976752.2688.bug-bash@gnu.org>
At the moment, \h and \H used in prompt PS1 or PS2 will actually return
the same value while manpage claims that \h should return hostname up to
the first '.' (like `hostname`) and \H should return full hostname (like
`hostname -f`).

This commit will make bash use the same API like hostname command to
return full hostname like intended.

This patch is based on the original work from Renato Silva [Link 1].

Link 1: https://lists.gnu.org/archive/html/bug-bash/2014-06/msg00021.html
---
 shell.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/shell.c b/shell.c
index a2b2a55e..28e89a2b 100644
--- a/shell.c
+++ b/shell.c
@@ -37,6 +37,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <errno.h>
+#include <netdb.h>
 #include "filecntl.h"
 #if defined (HAVE_PWD_H)
 #  include <pwd.h>
@@ -1839,6 +1840,8 @@ get_current_user_info ()
 static void
 shell_initialize ()
 {
+  struct addrinfo *addr_info;
+  struct addrinfo addr_hints;
   char hostname[256];
   int should_be_restricted;
 
@@ -1864,10 +1867,17 @@ shell_initialize ()
   if (current_host_name == 0)
     {
       /* Initialize current_host_name. */
-      if (gethostname (hostname, 255) < 0)
-	current_host_name = "??host??";
+      memset(&addr_hints, 0, sizeof(struct addrinfo));
+      addr_hints.ai_flags = AI_CANONNAME;
+
+      if (gethostname (hostname, 255) >= 0)
+        {
+          if (getaddrinfo (hostname, NULL, &addr_hints, &addr_info) == 0)
+            strncpy (hostname, addr_info->ai_canonname, 255);
+          current_host_name = savestring (hostname);
+        }
       else
-	current_host_name = savestring (hostname);
+	current_host_name = "??host??";
     }
 
   /* Initialize the stuff in current_user that comes from the password
-- 
2.22.0

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web