At least the AOSP build doesn't try to _set_ the hostname...
---
 toys/lsb/hostname.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c
index 0a1f9b1..4b9347d 100644
--- a/toys/lsb/hostname.c
+++ b/toys/lsb/hostname.c
@@ -4,48 +4,60 @@
  *
  * 
http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/hostname.html

-USE_HOSTNAME(NEWTOY(hostname, "bF:", TOYFLAG_BIN))
+USE_HOSTNAME(NEWTOY(hostname, ">1bdsfF:[!bdsf]", TOYFLAG_BIN))

 config HOSTNAME
   bool "hostname"
   default y
   help
-    usage: hostname [-b] [-F FILENAME] [newname]
+    usage: hostname [-bsf] [-F FILENAME] [newname]

-    Get/Set the current hostname
+    Get/set the current hostname.

-    -b Set hostname to 'localhost' if otherwise unset
-    -F Set hostname to contents of FILENAME
+    -b set hostname to 'localhost' if otherwise unset
+    -d show DNS domain name (no host)
+    -f show fully-qualified name (host+domain, FQDN)
+    -F set hostname to contents of FILENAME
+    -s show short host name (no domain)
 */

 #define FOR_hostname
 #include "toys.h"

 GLOBALS(
-  char *fname;
+  char *F;
 )

 void hostname_main(void)
 {
-  char *hostname = *toys.optargs;
+  char *hostname = *toys.optargs, *dot;
+  struct hostent *h;

-  if (TT.fname && (hostname = xreadfile(TT.fname, 0, 0))) {
+  if (TT.F && (hostname = xreadfile(TT.F, 0, 0))) {
     if (!*chomp(hostname)) {
       if (CFG_TOYBOX_FREE) free(hostname);
-      if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.fname);
+      if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.F);
       hostname = 0;
     }
   }

+  // Implement -b.
   if (!hostname && (toys.optflags&FLAG_b))
     if (gethostname(toybuf, sizeof(toybuf)-1) || !*toybuf)
       hostname = "localhost";

+  // Setting?
   if (hostname) {
     if (sethostname(hostname, strlen(hostname)))
       perror_exit("set '%s'", hostname);
-  } else {
-    if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname");
-    xputs(toybuf);
+    return;
   }
+
+  // Get the hostname.
+  if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname");
+  if (!(h = gethostbyname(toybuf))) perror_exit("gethostbyname");
+  snprintf(toybuf, sizeof(toybuf), "%s", h->h_name);
+  dot = strchr(toybuf, '.');
+  if (toys.optflags&FLAG_s) *dot = '\0';
+  xputs(toys.optflags&FLAG_d ? dot+1 : toybuf);
 }
-- 
2.19.1.930.g4563a0d9d0-goog
From b7630ddde3c97e314f0079125ed233e76fd1d196 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Wed, 14 Nov 2018 15:16:29 -0800
Subject: [PATCH] hostname: add -s/-f and -d.

At least the AOSP build doesn't try to _set_ the hostname...
---
 toys/lsb/hostname.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c
index 0a1f9b1..4b9347d 100644
--- a/toys/lsb/hostname.c
+++ b/toys/lsb/hostname.c
@@ -4,48 +4,60 @@
  *
  * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/hostname.html
 
-USE_HOSTNAME(NEWTOY(hostname, "bF:", TOYFLAG_BIN))
+USE_HOSTNAME(NEWTOY(hostname, ">1bdsfF:[!bdsf]", TOYFLAG_BIN))
 
 config HOSTNAME
   bool "hostname"
   default y
   help
-    usage: hostname [-b] [-F FILENAME] [newname]
+    usage: hostname [-bsf] [-F FILENAME] [newname]
 
-    Get/Set the current hostname
+    Get/set the current hostname.
 
-    -b	Set hostname to 'localhost' if otherwise unset
-    -F	Set hostname to contents of FILENAME
+    -b	set hostname to 'localhost' if otherwise unset
+    -d	show DNS domain name (no host)
+    -f	show fully-qualified name (host+domain, FQDN)
+    -F	set hostname to contents of FILENAME
+    -s	show short host name (no domain)
 */
 
 #define FOR_hostname
 #include "toys.h"
 
 GLOBALS(
-  char *fname;
+  char *F;
 )
 
 void hostname_main(void)
 {
-  char *hostname = *toys.optargs;
+  char *hostname = *toys.optargs, *dot;
+  struct hostent *h;
 
-  if (TT.fname && (hostname = xreadfile(TT.fname, 0, 0))) {
+  if (TT.F && (hostname = xreadfile(TT.F, 0, 0))) {
     if (!*chomp(hostname)) {
       if (CFG_TOYBOX_FREE) free(hostname);
-      if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.fname);
+      if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.F);
       hostname = 0;
     }
   }
 
+  // Implement -b.
   if (!hostname && (toys.optflags&FLAG_b))
     if (gethostname(toybuf, sizeof(toybuf)-1) || !*toybuf)
       hostname = "localhost";
 
+  // Setting?
   if (hostname) {
     if (sethostname(hostname, strlen(hostname)))
       perror_exit("set '%s'", hostname);
-  } else {
-    if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname");
-    xputs(toybuf);
+    return;
   }
+
+  // Get the hostname.
+  if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname");
+  if (!(h = gethostbyname(toybuf))) perror_exit("gethostbyname");
+  snprintf(toybuf, sizeof(toybuf), "%s", h->h_name);
+  dot = strchr(toybuf, '.');
+  if (toys.optflags&FLAG_s) *dot = '\0';
+  xputs(toys.optflags&FLAG_d ? dot+1 : toybuf);
 }
-- 
2.19.1.930.g4563a0d9d0-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to