chiark / gitweb /
hostnamectl: suppress outputting of pretty hostname field in status if empty
[elogind.git] / src / hostname / hostnamectl.c
index 1c9043534a316c5a5afff4172cfbada675f7aa3b..064581a31cfdb26b81829fadc3b1e6bf04345fab 100644 (file)
@@ -79,14 +79,18 @@ static void print_status_info(StatusInfo *i) {
         printf("   Static hostname: %s\n",
                strna(i->static_hostname));
 
-        if (!streq_ptr(i->hostname, i->static_hostname))
+        if (!isempty(i->pretty_hostname) &&
+            !streq_ptr(i->pretty_hostname, i->static_hostname))
+                printf("   Pretty hostname: %s\n",
+                       strna(i->pretty_hostname));
+
+        if (!isempty(i->hostname) &&
+            !streq_ptr(i->hostname, i->static_hostname))
                 printf("Transient hostname: %s\n",
                        strna(i->hostname));
 
-        printf("   Pretty hostname: %s\n"
-               "         Icon name: %s\n"
+        printf("         Icon name: %s\n"
                "           Chassis: %s\n",
-               strna(i->pretty_hostname),
                strna(i->icon_name),
                strna(i->chassis));
 
@@ -213,26 +217,6 @@ static int show_status(DBusConnection *bus, char **args, unsigned n) {
         return 0;
 }
 
-static char* hostname_simplify(char *s) {
-        char *p, *d;
-
-        for (p = s, d = s; *p; p++) {
-                if ((*p >= 'a' && *p <= 'z') ||
-                    (*p >= '0' && *p <= '9') ||
-                    *p == '-' || *p == '_')
-                        *(d++) = *p;
-                else if (*p >= 'A' && *p <= 'Z')
-                        *(d++) = *p - 'A' + 'a';
-                else if (*p == ' ')
-                        *(d++) = '-';
-        }
-
-        *d = 0;
-
-        strshorten(s, HOST_NAME_MAX);
-        return s;
-}
-
 static int set_hostname(DBusConnection *bus, char **args, unsigned n) {
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         dbus_bool_t interactive = true;
@@ -254,16 +238,17 @@ static int set_hostname(DBusConnection *bus, char **args, unsigned n) {
                  * just set the passed hostname as static/dynamic
                  * hostname. */
 
-                if (hostname_is_valid(hostname))
+                h = strdup(hostname);
+                if (!h)
+                        return log_oom();
+
+                hostname_cleanup(h, true);
+
+                if (arg_set_static && streq(h, hostname))
                         p = "";
                 else {
                         p = hostname;
-
-                        h = strdup(hostname);
-                        if (!h)
-                                return log_oom();
-
-                        hostname = hostname_simplify(h);
+                        hostname = h;
                 }
 
                 r = bus_method_call_with_reply(