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 {
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);
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);
{
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;
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));
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));
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
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) {
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);
}
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? */
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);
/* 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
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;
}
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;
+}
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 */
#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));
* 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);
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);
}
}
}
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;
*d = '\0';
if (!found && destlen > 0) {
- str_cat (dest, destlen, " ");
- str_cat (dest, destlen, src);
+ m_strcat(dest, destlen, " ");
+ m_strcat(dest, destlen, src);
}
}
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);
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)
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;
}
if (ciss->path)
- str_cat (dest, len, ciss->path);
+ m_strcat(dest, len, ciss->path);
return 0;
}