- /* Append name/value pair, after first removing a final newline
- and (extraneous) semicolon. */
- if (*(cp = &body[strlen (body) - 1]) == '\n') *cp = '\0';
- if (*(cp = &body[strlen (body) - 1]) == ';') *cp = '\0';
- new_body = concat (body, "; ", name, "\"", value, "\"", NULL);
+ if ((cp = strstr (*str, old))) {
+ char *remainder = cp + strlen (old);
+ char *prefix, *new_str;
+
+ if (cp - *str) {
+ prefix = mh_xstrdup(*str);
+ *(prefix + (cp - *str)) = '\0';
+ new_str = concat (prefix, new, remainder, NULL);
+ free (prefix);
+ } else {
+ new_str = concat (new, remainder, NULL);
+ }
+
+ free (*str);
+
+ return *str = new_str;
+ }
+
+ return NULL;
+}
+
+
+/*
+ * Remove a name=value parameter, given just its name, from a header value.
+ */
+char *
+remove_parameter (char *str, const char *name) {
+ /* It looks to me, based on the BNF in RFC 2045, than there can't
+ be whitespace betwwen the parameter name and the "=", or
+ between the "=" and the parameter value. */
+ char *param_name = concat (name, "=", NULL);
+ char *cp;
+
+ if ((cp = strstr (str, param_name))) {
+ char *start, *end;
+ size_t count = 1;
+
+ /* Remove any leading spaces, before the parameter name. */
+ for (start = cp;
+ start > str && isspace ((unsigned char) *(start-1));
+ --start) {
+ continue;
+ }
+ /* Remove a leading semicolon. */
+ if (start > str && *(start-1) == ';') { --start; }
+
+ end = cp + strlen (name) + 1;
+ if (*end == '"') {
+ /* Skip past the quoted value, and then the final quote. */
+ for (++end ; *end && *end != '"'; ++end) { continue; }
+ ++end;
+ } else {
+ /* Skip past the value. */
+ for (++end ; *end && ! isspace ((unsigned char) *end); ++end) {}
+ }
+
+ /* Count how many characters need to be moved. Include
+ trailing null, which is accounted for by the
+ initialization of count to 1. */
+ for (cp = end; *cp; ++cp) { ++count; }
+ (void) memmove (start, end, count);