]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
agetty: replace gethostbyname() with getaddrinfo()
authorSami Kerola <kerolasa@iki.fi>
Fri, 12 Oct 2012 21:11:16 +0000 (22:11 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 22 Oct 2012 08:14:36 +0000 (10:14 +0200)
The gethostbyname() is legacy function which may be withdrawn in a
future.

Reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyname.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
term-utils/agetty.8
term-utils/agetty.c

index e400ec855d586bad347159a916137ca97c7b1310..e51017d0d5e4525690c7ec31a243c77d9ea041a3 100644 (file)
@@ -215,7 +215,7 @@ no hostname at all will be shown.
 \-\-long\-hostname
 By default the hostname is only printed until the first dot.  With
 this option enabled, the full qualified hostname by gethostname()
-or if not found by gethostbyname() is shown.
+or if not found by getaddrinfo() is shown.
 .TP
 \-\-version
 Output version information and exit.
index 9bb0fd753ff08b2f60dfe5a8dba9b4e64dd1b66a..e53a70188eb89a1fc967be145995cb56dde6eaa3 100644 (file)
@@ -1331,18 +1331,30 @@ static void do_prompt(struct options *op, struct termios *tp)
                char *hn = xgethostname();
 
                if (hn) {
-                       struct hostent *ht;
                        char *dot = strchr(hn, '.');
+                       char *cn = hn;
+                       struct addrinfo *res = NULL;
 
                        if ((op->flags & F_LONGHNAME) == 0) {
                                if (dot)
                                        *dot = '\0';
-                               write_all(STDOUT_FILENO, hn, strlen(hn));
-                       } else if (dot == NULL && (ht = gethostbyname(hn)))
-                               write_all(STDOUT_FILENO, ht->h_name, strlen(ht->h_name));
-                       else
-                               write_all(STDOUT_FILENO, hn, strlen(hn));
+
+                       } else if (dot == NULL) {
+                               struct addrinfo hints;
+
+                               memset(&hints, 0, sizeof(hints));
+                               hints.ai_flags = AI_CANONNAME;
+
+                               if (!getaddrinfo(hn, NULL, &hints, &res)
+                                   && res && res->ai_canonname)
+                                       cn = res->ai_canonname;
+                       }
+
+                       write_all(STDOUT_FILENO, cn, strlen(cn));
                        write_all(STDOUT_FILENO, " ", 1);
+
+                       if (res)
+                               freeaddrinfo(res);
                        free(hn);
                }
        }