Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/usbdevs The structure returned by USB_DEVICEINFO ha...



details:   https://anonhg.NetBSD.org/src/rev/79f6dc8735bf
branches:  trunk
changeset: 751373:79f6dc8735bf
user:      drochner <drochner%NetBSD.org@localhost>
date:      Tue Feb 02 16:25:30 2010 +0000

description:
The structure returned by USB_DEVICEINFO has the vendor/device strings
UTF-8 encoded now. We can't simply print this to a terminal, so
convert it to the current codeset first.

diffstat:

 usr.sbin/usbdevs/usbdevs.c |  44 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 39 insertions(+), 5 deletions(-)

diffs (76 lines):

diff -r 24b8076892c7 -r 79f6dc8735bf usr.sbin/usbdevs/usbdevs.c
--- a/usr.sbin/usbdevs/usbdevs.c        Tue Feb 02 16:18:29 2010 +0000
+++ b/usr.sbin/usbdevs/usbdevs.c        Tue Feb 02 16:25:30 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usbdevs.c,v 1.25 2008/04/28 20:24:17 martin Exp $      */
+/*     $NetBSD: usbdevs.c,v 1.26 2010/02/02 16:25:30 drochner Exp $    */
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -37,6 +37,9 @@
 #include <unistd.h>
 #include <err.h>
 #include <errno.h>
+#include <locale.h>
+#include <langinfo.h>
+#include <iconv.h>
 #include <dev/usb/usb.h>
 
 #define USBDEV "/dev/usb"
@@ -61,6 +64,34 @@
 
 char done[USB_MAX_DEVICES];
 int indent;
+#define MAXLEN USB_MAX_ENCODED_STRING_LEN /* assume can't grow over UTF-8 */
+char vendor[MAXLEN], product[MAXLEN], serial[MAXLEN];
+
+static void
+u2t(const char *utf8str, char *termstr)
+{
+       static iconv_t ic;
+       static int iconv_inited = 0;
+       size_t insz, outsz, icres;
+
+       if (!iconv_inited) {
+               setlocale(LC_ALL, "");
+               ic = iconv_open(nl_langinfo(CODESET), "UTF-8");
+               if (ic == (iconv_t)-1)
+                       ic = iconv_open("ASCII", "UTF-8"); /* g.c.d. */
+               iconv_inited = 1;
+       }
+       if (ic != (iconv_t)-1) {
+               insz = strlen(utf8str);
+               outsz = MAXLEN - 1;
+               icres = iconv(ic, &utf8str, &insz, &termstr, &outsz);
+               if (icres != (size_t)-1) {
+                       *termstr = '\0';
+                       return;
+               }
+       }
+       strcpy(termstr, "(invalid)");
+}
 
 void
 usbdev(int f, int a, int rec)
@@ -93,14 +124,17 @@
                else
                        printf("unconfigured, ");
        }
+       u2t(di.udi_product, product);
+       u2t(di.udi_vendor, vendor);
+       u2t(di.udi_serial, serial);
        if (verbose) {
                printf("%s(0x%04x), %s(0x%04x), rev %s",
-                      di.udi_product, di.udi_productNo,
-                      di.udi_vendor, di.udi_vendorNo, di.udi_release);
+                      product, di.udi_productNo,
+                      vendor, di.udi_vendorNo, di.udi_release);
                if (di.udi_serial[0])
-                       printf(", serial %s", di.udi_serial);
+                       printf(", serial %s", serial);
        } else
-               printf("%s, %s", di.udi_product, di.udi_vendor);
+               printf("%s, %s", product, vendor);
        printf("\n");
        if (showdevs) {
                for (i = 0; i < USB_MAX_DEVNAMES; i++)



Home | Main Index | Thread Index | Old Index