-#ifdef LOCALE
-#define PUTSF(cp, str, wid, fill) {\
- ljust = 0;\
- if ((i = (wid)) < 0) {\
- i = -i;\
- ljust++;\
- }\
- if ((sp = (str))) {\
- if (ljust) {\
- c = strlen(sp);\
- if (c > i)\
- sp += c - i;\
- else {\
- while( --i >= c && cp < ep)\
- *cp++ = fill;\
- i++;\
- }\
- } else {\
- while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
- sp++;\
- }\
- while ((c = (unsigned char) *sp++) && --i >= 0 && cp < ep)\
- if (!iscntrl(c) && !isspace(c)) \
- *cp++ = c;\
- else {\
- while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
- sp++;\
- *cp++ = ' ';\
- }\
- }\
- if (!ljust)\
- while( --i >= 0 && cp < ep)\
- *cp++ = fill;\
+/*
+ * 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;
+
+ end -= char_len;
+
+ if (iswcntrl(wide_char) || iswspace(wide_char)) {
+ sp += char_len;
+#else
+ int c;
+ end--;
+ /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
+ intentionally warns if they are passed a char. */
+ c = *sp;
+ if (iscntrl(c) || isspace(c)) {
+ sp++;
+#endif
+ if (!prevCtrl) {
+ *cp++ = ' ';
+ remaining--;
+ }
+
+ prevCtrl = 1;
+ continue;
+ }
+ prevCtrl = 0;
+
+#ifdef MULTIBYTE_SUPPORT
+ w = wcwidth(wide_char);
+ if (w >= 0 && remaining >= w) {
+ strncpy(cp, sp, char_len);
+ cp += char_len;
+ remaining -= w;
+ }
+ sp += char_len;
+#else
+ *cp++ = *sp++;
+ remaining--;
+#endif