From fc854762626c5e2dce8c5618860a882934ec81d2 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 29 Nov 2006 00:55:06 +0100 Subject: [PATCH] make some code yet more simple, using the VERY good semantics of snprintf Signed-off-by: Pierre Habouzit --- lib-mime/rfc822address.c | 41 +++--------- postpone.c | 9 +-- sendlib.c | 130 ++++++++++++++++++--------------------- 3 files changed, 71 insertions(+), 109 deletions(-) diff --git a/lib-mime/rfc822address.c b/lib-mime/rfc822address.c index 190588c..ad4e117 100644 --- a/lib-mime/rfc822address.c +++ b/lib-mime/rfc822address.c @@ -396,15 +396,9 @@ ssize_t rfc822_write_address_single(char *buf, ssize_t buflen, if (!addr) return 0; - buflen--; /* save room for the terminal nul */ - if (addr->personal) { pos = rfc822_strcpy(buf, buflen, addr->personal, RFC822Specials); - if (pos + 2 >= buflen) - goto done; - - buf[pos++] = ' '; - buf[pos++] = '<'; + pos += m_strcpy(buf + pos, buflen - pos, " <"); } if (addr->mailbox) { @@ -415,26 +409,16 @@ ssize_t rfc822_write_address_single(char *buf, ssize_t buflen, } if (addr->personal) { - if (pos + 1 >= buflen) - goto done; - buf[pos++] = '>'; + pos += m_strcpy(buf + pos, buflen - pos, ">"); } if (addr->group) { - if (pos + 1 >= buflen) - goto done; - buf[pos++] = ':'; + pos += m_strcpy(buf + pos, buflen - pos, ":"); } } else { - if (pos + 1 >= buflen) - goto done; - buf[pos++] = ';'; + pos += m_strcpy(buf + pos, buflen - pos, ";"); } - done: - /* no need to check for length here since we already save space at the - beginning of this routine */ - buf[pos] = 0; return pos; } @@ -444,34 +428,23 @@ rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display) { ssize_t pos; - buflen--; /* save room for the terminal nul */ pos = m_strnlen(buf, buflen); if (pos) { - if (pos + 2 >= buflen) - goto done; - - buf[pos++] = ','; - buf[pos++] = ' '; + pos += m_strcpy(buf + pos, buflen - pos, ", "); } for (; addr; addr = addr->next) { - pos += rfc822_write_address_single(buf + pos, buflen + 1 - pos, + pos += rfc822_write_address_single(buf + pos, buflen - pos, addr, display); if (!addr->group && addr->next && addr->next->mailbox) { /* if there is another address, and its not a group mailbox name or group terminator, add a comma to separate the addresses */ - if (pos + 2 >= buflen) - break; - - buf[pos++] = ','; - buf[pos++] = ' '; + pos += m_strcpy(buf + pos, buflen - pos, ", "); } } - done: - buf[pos] = '\0'; return pos; } diff --git a/postpone.c b/postpone.c index 62939a8..4a08b97 100644 --- a/postpone.c +++ b/postpone.c @@ -589,13 +589,10 @@ int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr, s.flags = 0; if (b->type == TYPETEXT) { - if (!ascii_strcasecmp - ("yes", parameter_getval(b->parameter, "x-mutt-noconv"))) - b->noconv = 1; - else { + b->noconv = !ascii_strcasecmp("yes", parameter_getval(b->parameter, + "x-mutt-noconv")); + if (b->noconv) s.flags |= M_CHARCONV; - b->noconv = 0; - } parameter_delval(&b->parameter, "x-mutt-noconv"); } diff --git a/sendlib.c b/sendlib.c index 8c3d68c..0f676b9 100644 --- a/sendlib.c +++ b/sendlib.c @@ -1212,16 +1212,17 @@ BODY *mutt_make_file_attach (const char *path) static int get_toplevel_encoding (BODY * a) { - int e = ENC7BIT; + int e = ENC7BIT; - for (; a; a = a->next) { - if (a->encoding == ENCBINARY) - return (ENCBINARY); - else if (a->encoding == ENC8BIT) - e = ENC8BIT; - } + for (; a; a = a->next) { + if (a->encoding == ENCBINARY) + return ENCBINARY; + + if (a->encoding == ENC8BIT) + e = ENC8BIT; + } - return (e); + return e; } BODY *mutt_make_multipart (BODY * b) @@ -1271,41 +1272,40 @@ char *mutt_make_date (char *s, ssize_t len) /* wrapper around mutt_write_address() so we can handle very large recipient lists without needing a huge temporary buffer in memory */ -void mutt_write_address_list (address_t * adr, FILE * fp, int linelen, - int display) +void +mutt_write_address_list(address_t *addr, FILE *fp, int linelen, int display) { - address_t *tmp; - char buf[LONG_STRING]; - int count = 0; - int len; + int first = 1; + + while (addr) { + char buf[LONG_STRING]; + int len; + + len = rfc822_write_address_single(buf, ssizeof(buf), addr, display); + + if (!first) { + if (linelen + len > 74) { + fputs("\n\t", fp); + linelen = 8; /* tab is usually about 8 spaces... */ + } else + if (addr->mailbox) { + fputc(' ', fp); + linelen++; + } + } + first = 0; - while (adr) { - tmp = adr->next; - adr->next = NULL; - buf[0] = 0; - rfc822_write_address (buf, sizeof (buf), adr, display); - len = m_strlen(buf); - if (count && linelen + len > 74) { - fputs ("\n\t", fp); - linelen = len + 8; /* tab is usually about 8 spaces... */ - } - else { - if (count && adr->mailbox) { - fputc (' ', fp); - linelen++; - } - linelen += len; - } - fputs (buf, fp); - adr->next = tmp; - if (!adr->group && adr->next && adr->next->mailbox) { - linelen++; - fputc (',', fp); + linelen += len + 1; + fputs(buf, fp); + + if (!addr->group && addr->next && addr->next->mailbox) { + fputc(',', fp); + linelen++; + } + + addr = addr->next; } - adr = adr->next; - count++; - } - fputc ('\n', fp); + fputc ('\n', fp); } /* need to write the list in reverse because they are stored in reverse order @@ -1359,7 +1359,6 @@ static int edit_header(int mode, const char *s) * anonymous remailer chains. * */ - int mutt_write_rfc822_header (FILE * fp, ENVELOPE * env, BODY * attach, int mode, int privacy) { @@ -1888,7 +1887,7 @@ static int mutt_invoke_sendmail (address_t * from, /* the sender */ args = add_option(args, &argslen, &argsmax, "-B8BITMIME"); if (option (OPTENVFROM)) { - address_t *f = NULL; + address_t *f = EnvFrom; if (EnvFrom) f = EnvFrom; else if (from && !from->next) @@ -1914,7 +1913,7 @@ static int mutt_invoke_sendmail (address_t * from, /* the sender */ } #endif - if (argslen == argsmax) + if (argslen >= argsmax) p_realloc(&args, ++argsmax); args[argslen++] = NULL; @@ -1926,14 +1925,14 @@ static int mutt_invoke_sendmail (address_t * from, /* the sender */ if (childout) { struct stat st; - if (stat (childout, &st) == 0 && st.st_size > 0) - mutt_do_pager (_("Output of the delivery process"), childout, 0, - NULL); + if (!stat(childout, &st) && st.st_size > 0) + mutt_do_pager(_("Output of the delivery process"), childout, 0, + NULL); } } - } - else + } else { unlink (childout); + } p_delete(&childout); p_delete(&path); @@ -1968,38 +1967,31 @@ int mutt_invoke_mta (address_t * from, /* the sender */ /* For postponing (!final) do the necessary encodings only */ void mutt_prepare_envelope (ENVELOPE * env, int final) { - char buffer[LONG_STRING]; - if (final) { if (env->bcc && !(env->to || env->cc)) { /* some MTA's will put an Apparently-To: header field showing the Bcc: * recipients if there is no To: or Cc: field, so attempt to suppress * it by using an empty To: field. */ - env->to = address_new (); + env->to = address_new(); env->to->group = 1; - env->to->next = address_new (); - - buffer[0] = 0; - rfc822_strcpy(buffer, sizeof(buffer), "undisclosed-recipients", - RFC822Specials); - - env->to->mailbox = m_strdup(buffer); + env->to->next = address_new(); + env->to->mailbox = m_strdup("undisclosed-recipients"); } - mutt_set_followup_to (env); + mutt_set_followup_to(env); - if (!env->message_id && MsgIdFormat && *MsgIdFormat) - env->message_id = mutt_gen_msgid (); + if (!env->message_id && !m_strisempty(MsgIdFormat)) + env->message_id = mutt_gen_msgid(); } /* Take care of 8-bit => 7-bit conversion. */ - rfc2047_encode_adrlist (env->to, "To"); - rfc2047_encode_adrlist (env->cc, "Cc"); - rfc2047_encode_adrlist (env->bcc, "Bcc"); - rfc2047_encode_adrlist (env->from, "From"); - rfc2047_encode_adrlist (env->mail_followup_to, "Mail-Followup-To"); - rfc2047_encode_adrlist (env->reply_to, "Reply-To"); + rfc2047_encode_adrlist(env->to, "To"); + rfc2047_encode_adrlist(env->cc, "Cc"); + rfc2047_encode_adrlist(env->bcc, "Bcc"); + rfc2047_encode_adrlist(env->from, "From"); + rfc2047_encode_adrlist(env->mail_followup_to, "Mail-Followup-To"); + rfc2047_encode_adrlist(env->reply_to, "Reply-To"); if (env->subject) #ifdef USE_NNTP @@ -2064,8 +2056,8 @@ static int _mutt_bounce_message (FILE * fp, HEADER * h, address_t * to, fseeko (fp, h->offset, 0); fprintf (f, "Resent-From: %s", resent_from); fprintf (f, "\nResent-%s", mutt_make_date (date, sizeof (date))); - if (MsgIdFormat && *MsgIdFormat) - fprintf (f, "Resent-Message-ID: %s\n", mutt_gen_msgid ()); + if (!m_strisempty(MsgIdFormat)) + fprintf (f, "Resent-Message-ID: %s\n", mutt_gen_msgid()); fputs ("Resent-To: ", f); mutt_write_address_list (to, f, 11, 0); mutt_copy_header (fp, h, f, ch_flags, NULL); -- 2.20.1