+ w = wcwidth(wide_char);
+
+ /*
+ * Account for multibyte characters, and increment the end pointer
+ * by the number of "extra" bytes in this character. That's the
+ * character length (char_len) minus the column width (w).
+ */
+ if (char_len > 1 && max - *end >= char_len - w) {
+ *end += char_len - w;
+ }
+
+ /*
+ * If mbrtowc() failed, then we have a character that isn't valid
+ * in the current encoding. Replace it with a '?'. We do that by
+ * setting the alstr variable to the value of the replacement string;
+ * altstr is used below when the bytes are copied into the output
+ * buffer.
+ */
+
+ if (char_len < 0) {
+ altstr = "?";
+ char_len = mbtowc(&wide_char, altstr, 1);
+ }