Path: csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Thomas Deutschmann Newsgroups: gnu.bash.bug Subject: [PATCH] Fix \H: Use getaddrinfo to get full hostname Date: Wed, 24 Jul 2019 15:58:35 +0200 Lines: 62 Approved: bug-bash@gnu.org Message-ID: References: <20190724135835.110473-1-whissi@gentoo.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Trace: usenet.stanford.edu 1563976753 31083 209.51.188.17 (24 Jul 2019 13:59:13 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org X-Mailer: git-send-email 2.22.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 140.211.166.183 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20190724135835.110473-1-whissi@gentoo.org> Xref: csiph.com gnu.bash.bug:15224 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 #include #include +#include #include "filecntl.h" #if defined (HAVE_PWD_H) # include @@ -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; =20 @@ -1864,10 +1867,17 @@ shell_initialize () if (current_host_name =3D=3D 0) { /* Initialize current_host_name. */ - if (gethostname (hostname, 255) < 0) - current_host_name =3D "??host??"; + memset(&addr_hints, 0, sizeof(struct addrinfo)); + addr_hints.ai_flags =3D AI_CANONNAME; + + if (gethostname (hostname, 255) >=3D 0) + { + if (getaddrinfo (hostname, NULL, &addr_hints, &addr_info) =3D=3D= 0) + strncpy (hostname, addr_info->ai_canonname, 255); + current_host_name =3D savestring (hostname); + } else - current_host_name =3D savestring (hostname); + current_host_name =3D "??host??"; } =20 /* Initialize the stuff in current_user that comes from the password --=20 2.22.0