#include <stdarg.h>
#include <stdio.h>
#include <time.h>
int my_printf(const char *fmt, ...) {
va_list ap;
time_t tm;
struct tm tmt;
int iv;
va_start(ap, fmt);
while (*fmt) {
if (*fmt == '%') {
fmt++;
if (*fmt == 0) {
fmt--;
putchar('%');
} else {
switch (*fmt) {
default:
putchar(*fmt);
break;
case 'd':
iv = va_arg(ap, int);
printf("%d", iv);
break;
case 't':
tm = va_arg(ap, time_t);
gmtime_r(&tm, &tmt);
printf("[%04d-%02d-%02d %02d:%02d:%02d]",
tmt.tm_year + 1900, tmt.tm_mon + 1, tmt.tm_mday,
tmt.tm_hour, tmt.tm_min, tmt.tm_sec);
}
}
} else {
putchar(*fmt);
}
fmt++;
}
va_end(ap);
return 0;
}
int main(void) {
my_printf("test1: %d, %t, %d\n", 42, 0, 43);
my_printf("test2: %d, %t, %d\n", 44, (time_t)0, 45);
return 0;
}