bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Thomas Deutschmann
Subject: [PATCH] Fix \H: Use getaddrinfo to get full hostname
Date: Wed, 24 Jul 2019 15:58:35 +0200

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]