+static void
+getwidth(const char *string)
+{
+ wchar_t c;
+ int charlen, charleft = strlen(string);
+ int length = 0;
+
+ /*
+ * In theory we should be able to use wcswidth(), but since we're
+ * testing out how the format libraries behave we'll do it a character
+ * at a time.
+ */
+
+ mbtowc(NULL, NULL, 0);
+
+ while (charleft > 0) {
+ int clen;
+
+ charlen = mbtowc(&c, string, charleft);
+
+ if (charlen == 0)
+ break;
+
+ if (charlen < 0) {
+ fprintf(stderr, "Unable to convert string \"%s\"\n",
+ string);
+ return;
+ }
+
+ if ((clen = wcwidth(c)) < 0) {
+ fprintf(stderr, "U+%04lX non-printable\n",
+ (unsigned long int) c);
+ return;
+ }
+
+ length += clen;
+ string += charlen;
+ charleft -= charlen;
+ }
+
+ printf("%d\n", length);
+}
+