From d9960a434f5c00a534a0dabe02ae5ab8d4881569 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Mon, 30 Oct 2006 17:19:45 +0100 Subject: [PATCH] make code more readable. fix a bug with uninitialized values. Signed-off-by: Pierre Habouzit --- sendlib.c | 154 +++++++++++++++++++++++++++--------------------------- 1 file changed, 76 insertions(+), 78 deletions(-) diff --git a/sendlib.c b/sendlib.c index 0f17bf8..a461354 100644 --- a/sendlib.c +++ b/sendlib.c @@ -808,7 +808,7 @@ CONTENT *mutt_get_content_info (const char *fname, BODY * b) CONTENT_STATE state; FILE *fp = NULL; char *fromcode = NULL; - char *tocode; + char *tocode = NULL; char buffer[100]; char chsbuf[STRING]; size_t r; @@ -1681,100 +1681,98 @@ const char *mutt_fqdn (short may_hide_host) return p; } -static char mutt_normalized_char (char c) +/* normalized character (we're stricter than RFC2822, 3.6.4) */ +static char mutt_normalized_char(char c) { - if (isalnum (c)) - return c; - if (strchr (".!#$%&'*+-/=?^_`{|}~", c)) - return c; - return '.'; /* normalized character (we're stricter than RFC2822, 3.6.4) */ + return (isalnum(c) || strchr(".!#$%&'*+-/=?^_`{|}~", c)) ? c : '.'; } -static void mutt_gen_localpart(char *buf, unsigned int len, char *fmt) +static void mutt_gen_localpart(char *buf, unsigned int len, const char *fmt) { +#define APPEND_FMT(fmt, arg) \ + if (len > 1) { \ + int snlen = snprintf(buf, len, fmt, arg); \ + buf += snlen; \ + len -= snlen; \ + } + +#define APPEND_BYTE(c) \ + if (len > 1) { \ + *buf++ = c; \ + *buf = '\0'; \ + len--; \ + } + time_t now; struct tm *tm; - int snlen; - - *buf = '\0'; now = time (NULL); tm = gmtime (&now); - for (; *fmt; ++fmt) { -#define APPEND_FMT(fmt, ...) \ - if (len > 0) { \ - snlen = snprintf(buf, len, fmt, ##__VA_ARGS__); \ - buf += snlen; \ - len -= snlen; \ - } + while (*fmt) { + int c = *fmt++; -#define APPEND_BYTE(c) \ - if (len > 1) { \ - *buf++ = c; \ - *buf = '\0'; \ - len--; \ + if (c != '%') { + APPEND_BYTE(mutt_normalized_char(c)); + continue; } - if (*fmt == '%') { - switch (fmt[1]) { - case 0: - return; - case 'd': - APPEND_FMT("%02d", tm->tm_mday); - break; - case 'h': - APPEND_FMT("%02d", tm->tm_hour); - break; - case 'm': - APPEND_FMT("%02d", tm->tm_mon + 1); - break; - case 'M': - APPEND_FMT("%02d", tm->tm_min); - break; - case 'O': - APPEND_FMT("%lo", (unsigned long)now); - break; - case 'p': - APPEND_FMT("%u", (unsigned int)getpid()); - break; - case 'P': - APPEND_FMT("%c", MsgIdPfx); - MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1; - break; - case 'r': - APPEND_FMT("%u", (unsigned int)rand()); - break; - case 'R': - APPEND_FMT("%x", (unsigned int)rand()); - break; - case 's': - APPEND_FMT("%02d", tm->tm_sec); - break; - case 'T': - APPEND_FMT("%u", (unsigned int) now); - break; - case 'X': - APPEND_FMT("%x", (unsigned int) now); - break; - case 'Y': /* this will break in the year 10000 ;-) */ - APPEND_FMT("%04d", tm->tm_year + 1900); - break; - case '%': - APPEND_BYTE('%'); - break; - default: /* invalid formats are replaced by '.' */ - APPEND_BYTE('.'); - m_strncat(buf, len, ".", 1); - } - ++fmt; - } else { - APPEND_BYTE(mutt_normalized_char(*fmt)); + switch (*fmt++) { + case 0: + return; + case 'd': + APPEND_FMT("%02d", tm->tm_mday); + break; + case 'h': + APPEND_FMT("%02d", tm->tm_hour); + break; + case 'm': + APPEND_FMT("%02d", tm->tm_mon + 1); + break; + case 'M': + APPEND_FMT("%02d", tm->tm_min); + break; + case 'O': + APPEND_FMT("%lo", (unsigned long)now); + break; + case 'p': + APPEND_FMT("%u", (unsigned int)getpid()); + break; + case 'P': + APPEND_FMT("%c", MsgIdPfx); + MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1; + break; + case 'r': + APPEND_FMT("%u", (unsigned int)rand()); + break; + case 'R': + APPEND_FMT("%x", (unsigned int)rand()); + break; + case 's': + APPEND_FMT("%02d", tm->tm_sec); + break; + case 'T': + APPEND_FMT("%u", (unsigned int) now); + break; + case 'X': + APPEND_FMT("%x", (unsigned int) now); + break; + case 'Y': /* this will break in the year 10000 ;-) */ + APPEND_FMT("%04d", tm->tm_year + 1900); + break; + case '%': + APPEND_BYTE('%'); + break; + default: /* invalid formats are replaced by '.' */ + APPEND_BYTE('.'); + m_strncat(buf, len, ".", 1); } + } + + *buf = '\0'; #undef APPEND_BYTE #undef APPEND_FMT - } } char *mutt_gen_msgid (void) -- 2.20.1