drop str_[n]cat.
authorPierre Habouzit <madcoder@debian.org>
Mon, 30 Oct 2006 16:04:16 +0000 (17:04 +0100)
committerPierre Habouzit <madcoder@debian.org>
Mon, 30 Oct 2006 16:04:16 +0000 (17:04 +0100)
write mutt_get_localpart more efficiently avoiding the use of a local
buffer.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
18 files changed:
browser.c
commands.c
edit.c
hcache.c
imap/auth_cram.c
imap/auth_gss.c
imap/command.c
imap/imap.c
imap/message.c
lib-lib/str.c
lib-lib/str.h
lib/str.c
lib/str.h
mutt_ssl.c
muttlib.c
recvcmd.c
sendlib.c
url.c

index 53bab1a..878b6bb 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -783,9 +783,9 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
           m_strcpy(LastDir, sizeof(LastDir), f);
         }
         else {
-          getcwd (LastDir, sizeof (LastDir));
-          str_cat (LastDir, sizeof (LastDir), "/");
-          str_ncat (LastDir, sizeof (LastDir), f, i);
+          getcwd(LastDir, sizeof(LastDir));
+          m_strcat(LastDir, sizeof(LastDir), "/");
+          m_strncat(LastDir, sizeof(LastDir), f, i);
         }
       }
       else {
index 2cfb5e8..aae6e61 100644 (file)
@@ -288,13 +288,12 @@ void ci_bounce_message (HEADER * h, int *redraw)
             buf);
 
   if (mutt_strwidth (prompt) > COLS - extra_space) {
-    mutt_format_string (prompt, sizeof (prompt),
-                        0, COLS - extra_space, 0, 0,
-                        prompt, sizeof (prompt), 0);
-    str_cat (prompt, sizeof (prompt), "...?");
+    mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
+                       prompt, sizeof(prompt), 0);
+    m_strcat(prompt, sizeof(prompt), "...?");
+  } else {
+    m_strcat(prompt, sizeof(prompt), "?");
   }
-  else
-    str_cat (prompt, sizeof (prompt), "?");
 
   if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
     rfc822_free_address (&adr);
diff --git a/edit.c b/edit.c
index 02d1858..37b46f2 100644 (file)
--- a/edit.c
+++ b/edit.c
@@ -440,7 +440,7 @@ int mutt_builtin_editor (const char *path, HEADER * msg, HEADER * cur)
     else if (m_strcmp(".", tmp) == 0)
       done = 1;
     else {
-      str_cat (tmp, sizeof (tmp), "\n");
+      m_strcat(tmp, sizeof(tmp), "\n");
       if (buflen == bufmax)
         p_realloc(&buf, bufmax += 25);
       buf[buflen++] = m_strdup(tmp[1] == '~' ? tmp + 1 : tmp);
index e513c76..877afd0 100644 (file)
--- a/hcache.c
+++ b/hcache.c
@@ -657,20 +657,20 @@ mutt_hcache_fetch(void *db, const char *filename,
 {
   struct header_cache *h = db;
   char path[_POSIX_PATH_MAX];
-  int ksize;
+  int ksize, len;
   char *data = NULL;
 
   if (!h)
     return NULL;
 
   m_strcpy(path, sizeof(path), h->folder);
-  str_cat(path, sizeof (path), filename);
+  m_strcat(path, sizeof(path), filename);
 
   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
 
   data = vlget(h->db, path, ksize, NULL);
 
-  if (! crc32_matches(data, h->crc))
+  if (!crc32_matches(data, h->crc))
   {
     p_delete(&data);
     return NULL;
@@ -694,7 +694,7 @@ mutt_hcache_store(void *db, const char *filename, HEADER * header,
     return -1;
 
   m_strcpy(path, sizeof(path), h->folder);
-  str_cat(path, sizeof (path), filename);
+  m_strcat(path, sizeof(path), filename);
 
   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
 
@@ -719,7 +719,7 @@ mutt_hcache_delete(void *db, const char *filename,
     return -1;
 
   m_strcpy(path, sizeof(path), h->folder);
-  str_cat(path, sizeof (path), filename);
+  m_strcat(path, sizeof(path), filename);
 
   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
 
index 402bd75..ba2f42e 100644 (file)
@@ -102,7 +102,7 @@ imap_auth_res_t imap_auth_cram_md5 (IMAP_DATA * idata, const char *method)
 
   mutt_to_base64 ((unsigned char *) ibuf, (unsigned char *) obuf,
                   m_strlen(obuf), sizeof (ibuf) - 2);
-  str_cat (ibuf, sizeof (ibuf), "\r\n");
+  m_strcat(ibuf, sizeof(ibuf), "\r\n");
   mutt_socket_write (idata->conn, ibuf);
 
   do
index e5f1bce..ff51d89 100644 (file)
@@ -115,7 +115,7 @@ imap_auth_res_t imap_auth_gss (IMAP_DATA * idata, const char *method)
   mutt_to_base64 ((unsigned char *) buf1, send_token.value, send_token.length,
                   sizeof (buf1) - 2);
   gss_release_buffer (&min_stat, &send_token);
-  str_cat (buf1, sizeof (buf1), "\r\n");
+  m_strcat(buf1, sizeof(buf1), "\r\n");
   mutt_socket_write (idata->conn, buf1);
 
   while (maj_stat == GSS_S_CONTINUE_NEEDED) {
@@ -150,7 +150,7 @@ imap_auth_res_t imap_auth_gss (IMAP_DATA * idata, const char *method)
     mutt_to_base64 ((unsigned char *) buf1, send_token.value,
                     send_token.length, sizeof (buf1) - 2);
     gss_release_buffer (&min_stat, &send_token);
-    str_cat (buf1, sizeof (buf1), "\r\n");
+    m_strcat(buf1, sizeof(buf1), "\r\n");
     mutt_socket_write (idata->conn, buf1);
   }
 
@@ -213,7 +213,7 @@ imap_auth_res_t imap_auth_gss (IMAP_DATA * idata, const char *method)
   mutt_to_base64 ((unsigned char *) buf1, send_token.value, send_token.length,
                   sizeof (buf1) - 2);
   debug_print (2, ("Requesting authorisation as %s\n", idata->conn->account.user));
-  str_cat (buf1, sizeof (buf1), "\r\n");
+  m_strcat(buf1, sizeof(buf1), "\r\n");
   mutt_socket_write (idata->conn, buf1);
 
   /* Joy of victory or agony of defeat? */
index 6c470e8..915e3f1 100644 (file)
@@ -575,7 +575,7 @@ static void cmd_parse_lsub (IMAP_DATA* idata, char* s) {
     if (!m_strcmp(url.user, ImapUser))
       url.user = NULL;
     url_ciss_tostring (&url, buf + 11, sizeof (buf) - 10, 0);
-    str_cat (buf, sizeof (buf), "\"");
+    m_strcat(buf, sizeof(buf), "\"");
     p_clear(&token, 1);
     err.data = errstr;
     err.dsize = sizeof (errstr);
index 6f50317..2a4feca 100644 (file)
@@ -785,12 +785,13 @@ int imap_close_connection (CONTEXT *ctx)
 
 /* imap_set_flag: append str to flags if we currently have permission
  *   according to aclbit */
-static void imap_set_flag (IMAP_DATA * idata, int aclbit, int flag,
-                           const char *str, char *flags, size_t flsize)
+static void imap_set_flag(IMAP_DATA *idata, int aclbit, int flag,
+                          const char *str, char *flags, size_t flsize)
 {
-  if (mutt_bit_isset (idata->rights, aclbit))
-    if (flag)
-      str_cat (flags, flsize, str);
+    if (mutt_bit_isset(idata->rights, aclbit)) {
+        if (flag)
+            m_strcat(flags, flsize, str);
+    }
 }
 
 /* imap_make_msg_set: make an IMAP4rev1 UID message set out of a set of
index 1dddd52..c6db226 100644 (file)
@@ -771,8 +771,8 @@ void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
 
   while (keywords) {
     if (msg_has_flag (mailbox_flags, keywords->data)) {
-      str_cat (s, slen, keywords->data);
-      str_cat (s, slen, " ");
+      m_strcat(s, slen, keywords->data);
+      m_strcat(s, slen, " ");
     }
     keywords = keywords->next;
   }
index fd5fbce..8b0c393 100644 (file)
@@ -33,3 +33,15 @@ ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
     return len;
 }
 
+ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
+{
+    ssize_t len = MIN(m_strlen(src), l);
+
+    if (dst && n > 0) {
+        ssize_t dlen = MIN(n - 1, len);
+        memcpy(dst, src, dlen);
+        dst[dlen] = '\0';
+    }
+
+    return len;
+}
index 3694371..4ef9d48 100644 (file)
@@ -46,10 +46,17 @@ static inline int m_strcmp(const char *a, const char *b) {
 
 
 ssize_t m_strcpy(char *dst, ssize_t n, const char *src);
+ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l);
 
 static inline ssize_t m_strcat(char *dst, ssize_t n, const char *src) {
-    ssize_t dlen = m_strnlen(dst, n);
+    ssize_t dlen = m_strnlen(dst, n - 1);
     return dlen + m_strcpy(dst + dlen, n - dlen, src);
 }
 
+static inline ssize_t
+m_strncat(char *dst, ssize_t n, const char *src, ssize_t l) {
+    ssize_t dlen = m_strnlen(dst, n - 1);
+    return dlen + m_strncpy(dst + dlen, n - dlen, src, l);
+}
+
 #endif /* MUTT_LIB_LIB_STR_H */
index f90e861..016376e 100644 (file)
--- a/lib/str.c
+++ b/lib/str.c
 
 #include "str.h"
 
-char *str_cat (char *d, size_t l, const char *s)
-{
-  char *p = d;
-
-  if (!l)
-    return d;
-
-  l--;                          /* Space for the trailing '\0'. */
-
-  for (; *d && l; l--)
-    d++;
-  for (; *s && l; l--)
-    *d++ = *s++;
-
-  *d = '\0';
-
-  return p;
-}
-
-char *str_ncat (char *d, size_t l, const char *s, size_t sl)
-{
-  char *p = d;
-
-  if (!l)
-    return d;
-
-  l--;                          /* Space for the trailing '\0'. */
-
-  for (; *d && l; l--)
-    d++;
-  for (; *s && l && sl; l--, sl--)
-    *d++ = *s++;
-
-  *d = '\0';
-
-  return p;
-}
-
 int str_casecmp (const char *a, const char *b)
 {
   return strcasecmp (NONULL (a), NONULL (b));
index bc3cb82..2f4e33c 100644 (file)
--- a/lib/str.h
+++ b/lib/str.h
@@ -39,8 +39,6 @@
  * safety wrappers/replacements
  * (mostly only difference: safely handle NULL strings)
  */
-char *str_cat (char*, size_t, const char*);
-char *str_ncat (char*, size_t, const char*, size_t);
 int str_casecmp (const char*, const char*);
 int str_ncmp (const char*, const char*, size_t);
 int str_ncasecmp (const char*, const char*, size_t);
index 804183c..aeed464 100644 (file)
@@ -435,14 +435,14 @@ static void x509_fingerprint (char *s, int l, X509 * cert)
   int j;
 
   if (!X509_digest (cert, EVP_md5 (), md, &n)) {
-    snprintf (s, l, "%s", _("[unable to calculate]"));
+    m_strcpy(s, l, _("[unable to calculate]"));
   }
   else {
     for (j = 0; j < (int) n; j++) {
       char ch[8];
 
-      snprintf (ch, 8, "%02X%s", md[j], (j % 2 ? " " : ""));
-      str_cat (s, l, ch);
+      snprintf(ch, 8, "%02X%s", md[j], (j % 2 ? " " : ""));
+      m_strcat(s, l, ch);
     }
   }
 }
@@ -636,9 +636,9 @@ static int ssl_check_certificate (sslsockdata * data)
 
   helpstr[0] = '\0';
   mutt_make_help (buf, sizeof (buf), _("Exit  "), MENU_GENERIC, OP_EXIT);
-  str_cat (helpstr, sizeof (helpstr), buf);
+  m_strcat(helpstr, sizeof(helpstr), buf);
   mutt_make_help (buf, sizeof (buf), _("Help"), MENU_GENERIC, OP_HELP);
-  str_cat (helpstr, sizeof (helpstr), buf);
+  m_strcat(helpstr, sizeof(helpstr), buf);
   menu->help = helpstr;
 
   done = 0;
index a168093..45dd76d 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -803,8 +803,8 @@ void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt,
   *d = '\0';
 
   if (!found && destlen > 0) {
-    str_cat (dest, destlen, " ");
-    str_cat (dest, destlen, src);
+    m_strcat(dest, destlen, " ");
+    m_strcat(dest, destlen, src);
   }
 
 }
index 17ced5a..fec63fe 100644 (file)
--- a/recvcmd.c
+++ b/recvcmd.c
@@ -175,10 +175,10 @@ void mutt_attach_bounce (FILE * fp, HEADER * hdr,
     mutt_format_string (prompt, sizeof (prompt) - 4,
                         0, COLS - extra_space, 0, 0,
                         prompt, sizeof (prompt), 0);
-    str_cat (prompt, sizeof (prompt), "...?");
+    m_strcat(prompt, sizeof(prompt), "...?");
+  } else {
+    m_strcat(prompt, sizeof(prompt), "?");
   }
-  else
-    str_cat (prompt, sizeof (prompt), "?");
 
   if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
     rfc822_free_address (&adr);
index a07b87d..0f17bf8 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
@@ -1690,90 +1690,91 @@ static char mutt_normalized_char (char c)
   return '.';                   /* normalized character (we're stricter than RFC2822, 3.6.4) */
 }
 
-static void mutt_gen_localpart (char *buf, unsigned int len, char *fmt)
+static void mutt_gen_localpart(char *buf, unsigned int len, char *fmt)
 {
-  time_t now;
-  struct tm *tm;
-  char tmp[SHORT_STRING];
+    time_t now;
+    struct tm *tm;
+    int snlen;
 
-  *buf = '\0';
+    *buf = '\0';
 
-  now = time (NULL);
-  tm = gmtime (&now);
+    now = time (NULL);
+    tm = gmtime (&now);
 
-  for (; *fmt; ++fmt) {
-    if (*fmt == '%') {
-      switch (fmt[1]) {
-      case 0:
-        return;
-      case 'd':
-        snprintf (tmp, sizeof (tmp), "%02d", tm->tm_mday);
-        str_ncat (buf, len, tmp, 2);
-        break;
-      case 'h':
-        snprintf (tmp, sizeof (tmp), "%02d", tm->tm_hour);
-        str_ncat (buf, len, tmp, 2);
-        break;
-      case 'm':
-        snprintf (tmp, sizeof (tmp), "%02d", tm->tm_mon + 1);
-        str_ncat (buf, len, tmp, 2);
-        break;
-      case 'M':
-        snprintf (tmp, sizeof (tmp), "%02d", tm->tm_min);
-        str_ncat (buf, len, tmp, 2);
-        break;
-      case 'O':
-        snprintf (tmp, sizeof (tmp), "%lo", (unsigned long) now);
-        str_ncat (buf, len, tmp, m_strlen(tmp));
-        break;
-      case 'p':
-        snprintf (tmp, sizeof (tmp), "%u", (unsigned int) getpid ());
-        str_ncat (buf, len, tmp, m_strlen(tmp));
-        break;
-      case 'P':
-        snprintf (tmp, sizeof (tmp), "%c", MsgIdPfx);
-        MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;
-        str_ncat (buf, len, tmp, 1);
-        break;
-      case 'r':
-        snprintf (tmp, sizeof (tmp), "%u", (unsigned int) rand ());
-        str_ncat (buf, len, tmp, m_strlen(tmp));
-        break;
-      case 'R':
-        snprintf (tmp, sizeof (tmp), "%x", (unsigned int) rand ());
-        str_ncat (buf, len, tmp, m_strlen(tmp));
-        break;
-      case 's':
-        snprintf (tmp, sizeof (tmp), "%02d", tm->tm_sec);
-        str_ncat (buf, len, tmp, 2);
-        break;
-      case 'T':
-        snprintf (tmp, sizeof (tmp), "%u", (unsigned int) now);
-        str_ncat (buf, len, tmp, m_strlen(tmp));
-        break;
-      case 'X':
-        snprintf (tmp, sizeof (tmp), "%x", (unsigned int) now);
-        str_ncat (buf, len, tmp, m_strlen(tmp));
-        break;
-      case 'Y':
-        snprintf (tmp, sizeof (tmp), "%04d", tm->tm_year + 1900);       /* this will break in the year 10000 ;-) */
-        str_ncat (buf, len, tmp, 4);
-        break;
-      case '%':
-        str_ncat (buf, len, "%", 1);
-        break;
-      default:
-        str_ncat (buf, len, ".", 1);        /* invalid formats are replaced by '.' */
-      }                         /* switch */
-      ++fmt;
-    }
-    else {
-      char c;
+    for (; *fmt; ++fmt) {
+#define APPEND_FMT(fmt, ...) \
+        if (len > 0) {                                             \
+            snlen = snprintf(buf, len, fmt, ##__VA_ARGS__);        \
+            buf += snlen;                                          \
+            len -= snlen;                                          \
+        }
 
-      c = mutt_normalized_char (*fmt);  /* @todo: filter out invalid characters */
-      str_ncat (buf, len, &c, 1);
+#define APPEND_BYTE(c) \
+        if (len > 1) {                                             \
+            *buf++ = c;                                            \
+            *buf   = '\0';                                         \
+            len--;                                                 \
+        }
+
+        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));
+        }
+
+#undef APPEND_BYTE
+#undef APPEND_FMT
     }
-  }
 }
 
 char *mutt_gen_msgid (void)
diff --git a/url.c b/url.c
index 8c49fd0..8a00cdd 100644 (file)
--- a/url.c
+++ b/url.c
@@ -173,7 +173,7 @@ int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
   snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
 
   if (ciss->host) {
-    str_cat (dest, len, "//");
+    m_strcat(dest, len, "//");
     len -= (l = m_strlen(dest));
     dest += l;
 
@@ -194,7 +194,7 @@ int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
   }
 
   if (ciss->path)
-    str_cat (dest, len, ciss->path);
+    m_strcat(dest, len, ciss->path);
 
   return 0;
 }