-#define PUTDF(cp, num, wid, fill)\
- if (cp + wid < ep) {\
- if ((i = (num)) < 0)\
- i = -(num);\
- if ((c = (wid)) < 0)\
- c = -c;\
- sp = cp + c;\
- do {\
- *--sp = (i % 10) + '0';\
- i /= 10;\
- } while (i > 0 && sp > cp);\
- if (i > 0)\
- *sp = '?';\
- else if ((num) < 0 && sp > cp)\
- *--sp = '-';\
- while (sp > cp)\
- *--sp = fill;\
- cp += c;\
- }
+/*
+ * copy string from str to dest padding with the fill character to a size
+ * of wid characters. if wid is negative, the string is right aligned
+ * no more than n bytes are copied
+ */
+static void
+cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
+ 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;
+#endif
+ char *sp; /* current position in source string */
+ char *cp = *dest; /* current position in destination string */
+ char *ep = cp + n; /* end of destination buffer */
+ int prevCtrl = 1;
+
+ /* get alignment */
+ ljust = 0;
+ if ((remaining = (wid)) < 0) {
+ remaining = -remaining;
+ ljust++;
+ }
+ if ((sp = (str))) {
+ mbtowc(NULL, NULL, 0); /* reset shift state */
+ end = strlen(str);
+ while (*sp && remaining > 0 && end > 0) {
+#ifdef MULTIBYTE_SUPPORT
+ char_len = mbtowc(&wide_char, sp, end);
+ if (char_len <= 0 || (cp + char_len > ep))
+ break;