#include "h/mh.h" #include "h/mime.h" #include void fold(FILE *restrict stream, const char *restrict name, const char *restrict body) { const char *restrict body_next; const char *restrict wsp; const char *restrict wsp_next; const bool crlf = strchr(body, '\r'); size_t namelen = fprintf(stream, "%s:", name); if (namelen >= MAXTEXTPERLN) { if (crlf) { fputs("\r\n ", stream); } else { fputs("\n ", stream); } namelen = 0; } while (*body) { body_next = strchr(body, '\n'); if ((unsigned long) (body_next - body) <= MAXTEXTPERLN - namelen) { fwrite(body, 1, body_next - body + 1, stream); namelen = 0; body = body_next + 1; continue; } wsp = strpbrk(body, " \t"); /* if now whitespace is in the current line just print the curret line as is */ if (!wsp || wsp > body_next) { fwrite(body, 1, body_next - body + 1, stream); namelen = 0; body = body_next + 1; continue; } while ((unsigned long)(wsp - body) <= MAXTEXTPERLN - namelen) { wsp_next = strpbrk(wsp, " \t"); if (!wsp_next) { break; } if (wsp_next > body_next) { break; } wsp = wsp_next; } fwrite(body, 1, wsp - body, stream); if (crlf) { fputs("\r\n", stream); } else { fputs("\n", stream); } fputc(*wsp, stream); namelen = 1; body = wsp + 1; } }