char * wcstombsdup(wchar_t *w) { int n; char *mb; /* Fetch memory for worst case string length */ n = wslen(w) + 1; n *= MB_CUR_MAX; if ((mb = (char *)malloc(n)) == NULL) { return (NULL); } /* Convert the string */ if ((n = wcstombs(mb, w, n)) == -1) { int saverr = errno; free(mb); errno = saverr; return (0); } /* Shrink the string down */ if ((mb = (char *)realloc(mb, strlen(mb)+1)) == NULL) { return (NULL); } return (mb); }
static void tabulate(wchar_t line[]) { wchar_t *cp; int b, t; /* Toss trailing blanks in the output line */ cp = line + wslen(line) - 1; while (cp >= line && *cp == L' ') cp--; *++cp = L'\0'; /* Count the leading blank space and tabulate */ for (cp = line; *cp == L' '; cp++); b = cp - line; t = b >> 3; b &= 07; if (t > 0) do putc('\t', stdout); while (--t); if (b > 0) do putc(' ', stdout); while (--b); while (*cp) putwc(*cp++, stdout); putc('\n', stdout); }
wchar_t * tostring(wchar_t *s) { wchar_t *p; p = (wchar_t *) malloc((wslen(s)+1)*sizeof (wchar_t)); if (p == NULL) error(FATAL, "out of space in tostring on %ws", s); wscpy(p, s); return (p); }
/* * fill_hdrbuf - * Save given input line into next element of hdrbuf, * as a potential mail header line, to be processed later * once we decide whether or not the contents of hdrbuf is * really a mail header, via header_chk(). * * Does not allow hdrbuf to exceed MAXLINES lines. * Dynamically allocates space for each line. If we are unable * to allocate space for the current string, stop special mail * header preservation at this point and continue formatting * without it. */ static void fill_hdrbuf(wchar_t line[]) { wchar_t *cp; /* pointer to characters in input line */ int i; /* index into characters a hdrbuf line */ if (h_lines >= MAXLINES) { /* * if we run over MAXLINES potential mail header * lines, stop checking--this is most likely NOT a * mail header; flush out the hdrbuf, then process * the current 'line' normally. */ hdr_state = flush_hdr; process_hdrbuf(); prefix(line); return; } hdrbuf[h_lines] = (wchar_t *)malloc(sizeof (wchar_t) * (wslen(line) + 1)); if (hdrbuf[h_lines] == NULL) { perror("malloc"); fprintf(stderr, "fmt: unable to do mail header preservation\n"); errs++; /* * Can't process mail header; flush current contents * of mail header and continue with no more mail * header processing */ if (h_lines == 0) /* hdrbuf is empty; process this line normally */ prefix(line); else { hdr_state = flush_hdr; for (i = 0; i < h_lines; i++) { prefix(hdrbuf[i]); free(hdrbuf[i]); } h_lines = 0; } hdr_state = off; return; } /* save this line as a potential mail header line */ for (i = 0, cp = line; (hdrbuf[h_lines][i] = *cp) != L'\0'; i++, cp++); h_lines++; }