ifconfig(8): Render non-ASCII SSID names with UTF-8 locales
authorAaron LI <aly@aaronly.me>
Sun, 12 Apr 2020 12:56:39 +0000 (20:56 +0800)
committerAaron LI <aly@aaronly.me>
Fri, 26 Jun 2020 14:52:59 +0000 (22:52 +0800)
Currently ifconfig(8) only prints the hex representation of ssid names
with non-ASCII characters.  Many modern terminals are able to properly
render non-ASCII characters.  This change checks if the terminal charmap
is UTF-8, and if so, will render the characters, rather than the hex
value.

This behavior is circumvented by running ifconfig(8) in a non-UTF8
locale; e.g. C or POSIX.

Obtained from FreeBSD:
https://github.com/freebsd/freebsd/commit/137a4801848fde80544a987fb168ff56face2ffd
https://reviews.freebsd.org/D15922

sbin/ifconfig/ifieee80211.c

index 129784b..64b61e6 100644 (file)
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
+#include <langinfo.h>
+#include <locale.h>
+#include <stdarg.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <stdarg.h>
-#include <stddef.h>            /* NB: for offsetof */
 
 #include "ifconfig.h"
 #include "regdomain.h"
@@ -4971,16 +4973,21 @@ print_string(const u_int8_t *buf, int len)
 {
        int i;
        int hasspc;
+       int utf8;
 
        i = 0;
        hasspc = 0;
+
+       setlocale(LC_CTYPE, "");
+       utf8 = strncmp("UTF-8", nl_langinfo(CODESET), 5) == 0;
+
        for (; i < len; i++) {
-               if (!isprint(buf[i]) && buf[i] != '\0')
+               if (!isprint(buf[i]) && buf[i] != '\0' && !utf8)
                        break;
                if (isspace(buf[i]))
                        hasspc++;
        }
-       if (i == len) {
+       if (i == len || utf8) {
                if (hasspc || len == 0 || buf[0] == '\0')
                        printf("\"%.*s\"", len, buf);
                else