+static void
+cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill,
+ char *epmax) {
+ int remaining; /* remaining output width available */
+ int c, ljust;
+ int end; /* number of input bytes remaining in str */
+#ifdef MULTIBYTE_SUPPORT
+ int char_len; /* bytes in current character */
+ int w;
+ wchar_t wide_char;
+ char *altstr = NULL;
+#endif
+ char *sp; /* current position in source string */
+ char *cp = *dest; /* current position in destination string */
+ int prevCtrl = 1;
+
+ /* get alignment */
+ ljust = 0;
+ if ((remaining = (wid)) < 0) {
+ remaining = -remaining;
+ ljust++;
+ }
+ if ((sp = (str))) {
+#ifdef MULTIBYTE_SUPPORT
+ mbtowc(NULL, NULL, 0); /* reset shift state */
+#endif
+ end = strlen(str);
+ while (*sp && remaining > 0 && end > 0) {
+#ifdef MULTIBYTE_SUPPORT
+ char_len = mbtowc(&wide_char, sp, end);
+
+ /*
+ * See the relevant comments in cpstripped() to explain what's
+ * going on here; we want to handle the case where we get
+ * characters that mbtowc() cannot handle
+ */