- for (;*fmt;++fmt) {
- if (*fmt == '%') {
- switch (fmt[1]) {
- case 0:
- return;
- case 'd':
- snprintf(tmp,sizeof(tmp),"%02d",tm->tm_mday);
- safe_strncat(buf,len,tmp,2);
- break;
- case 'h':
- snprintf(tmp,sizeof(tmp),"%02d",tm->tm_hour);
- safe_strncat(buf,len,tmp,2);
- break;
- case 'm':
- snprintf(tmp,sizeof(tmp),"%02d",tm->tm_mon+1);
- safe_strncat(buf,len,tmp,2);
- break;
- case 'M':
- snprintf(tmp,sizeof(tmp),"%02d",tm->tm_min);
- safe_strncat(buf,len,tmp,2);
- break;
- case 'O':
- snprintf(tmp,sizeof(tmp),"%lo",(unsigned long)now);
- safe_strncat(buf,len,tmp,strlen(tmp));
- break;
- case 'p':
- snprintf(tmp,sizeof(tmp),"%u",(unsigned int)getpid());
- safe_strncat(buf,len,tmp,strlen(tmp));
- break;
- case 'P':
- snprintf(tmp,sizeof(tmp),"%c",MsgIdPfx);
- MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;
- safe_strncat(buf,len,tmp,1);
- break;
- case 'r':
- snprintf(tmp,sizeof(tmp),"%u",(unsigned int)rand());
- safe_strncat(buf,len,tmp,strlen(tmp));
- break;
- case 'R':
- snprintf(tmp,sizeof(tmp),"%x",(unsigned int)rand());
- safe_strncat(buf,len,tmp,strlen(tmp));
- break;
- case 's':
- snprintf(tmp,sizeof(tmp),"%02d",tm->tm_sec);
- safe_strncat(buf,len,tmp,2);
- break;
- case 'T':
- snprintf(tmp,sizeof(tmp),"%u",(unsigned int)now);
- safe_strncat(buf,len,tmp,strlen(tmp));
- break;
- case 'X':
- snprintf(tmp,sizeof(tmp),"%x",(unsigned int)now);
- safe_strncat(buf,len,tmp,strlen(tmp));
- break;
- case 'Y':
- snprintf(tmp,sizeof(tmp),"%04d",tm->tm_year+1900); /* this will break in the year 10000 ;-) */
- safe_strncat(buf,len,tmp,4);
- break;
- case '%':
- safe_strncat(buf,len,"%",1);
- break;
- default:
- safe_strncat(buf,len,".",1); /* invalid formats are replaced by '.' */
- } /* switch */
- ++fmt;
- } else {
- char c;
- c = mutt_normalized_char(*fmt); /* @todo: filter out invalid characters */
- safe_strncat(buf,len,&c,1);
+ now = time (NULL);
+ tm = gmtime (&now);
+
+ while (*fmt) {
+ int c = *fmt++;
+
+ if (c != '%') {
+ APPEND_BYTE(mutt_normalized_char(c));
+ continue;
+ }
+
+ 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);
+ }