void _kprintf(char *msg, __gnuc_va_list ap) { char *aux = msg; int i; while(*aux != 0) { if (*aux == '%') { switch(*(++aux)) { case 's': aux++; char *str = va_arg(ap, char *); kprintf(str); break; case 'd': aux++; int val = va_arg(ap, int); static char str_val[20]; for (i=0; i<20; i++) str_val[i] = '\0'; int2str(val, str_val); kprintf(str_val); break; default: /* do nothing */ break; } } if (*aux == '\n') pr_putchar('\r'); pr_putchar(*aux); aux++; } }
static int pr_chooseprint(va_list ap, char c) { if (c == 'c') return (pr_printchar(ap)); else if (c == 's') return (pr_printstr(ap)); else if (c == 'd' || c == 'i') return (pr_printnbr(ap)); else if (c == 'u') return (pr_printunsigned(ap)); else if (c == 'o') return (pr_printoctal(ap)); else if (c == 'x') return (pr_printhexa(ap)); else if (c == 'p') return (pr_printvoid(ap)); else if (c == '%') return (pr_putchar(c)); return (0); }
int pr_printf(char const *s, ...) { int i; int len; va_list ap; i = 0; len = 0; va_start(ap, s); while (s[i] != '\0') { if (s[i] == '%') { len += pr_chooseprint(ap, s[i + 1]); i++; } else if (s[i] != '%') len += pr_putchar(s[i]); i++; } va_end(ap); return (len); }