7 /* Escape a display name, hopefully per RFC 5322.
8 The argument is assumed to be a pointer to a character array of
9 one-byte characters with enough space to handle the additional
12 escape_display_name (char *name) {
13 /* Quote and escape name that contains any specials, as necessary. */
14 if (strpbrk("\"(),.:;<>@[\\]", name)) {
15 size_t len = strlen(name);
17 size_t destpos, srcpos;
18 /* E.g., 2 characters, "", would require 7, "\"\""\0. */
19 char *tmp = malloc (2*len+3);
21 for (destp = tmp, srcp = name, destpos = 0, srcpos = 0;
23 ++destp, ++srcp, ++destpos, ++srcpos) {
25 /* Insert initial double quote, if needed. */
31 /* Escape embedded, unescaped ". */
32 if (*srcp == '"' && srcpos < len - 1 && *(srcp-1) != '\\') {
41 if (srcpos == len - 1) {
42 /* Insert final double quote, if needed. */
53 if (strcmp (tmp, "\"")) {
54 /* assert (strlen(tmp) + 1 == destpos); */
55 strncpy (name, tmp, destpos);
57 /* Handle just " as special case here instead of above. */
58 strcpy (name, "\"\\\"\"");