08-02-2013, 10:50 AM
The worst code I have in my current project:
Code:
void kprintf(char *format, ...)
{
char *p;
int i;
uint64_t i64;
unsigned u;
char *s;
va_list argp;
va_start(argp, format);
p = format;
for(p = format; *p != '\0'; p++)
{
if(*p != '%')
{
putch(*p);
continue;
}
p++;
switch(*p)
{
case 'c':
i = va_arg(argp, int);
putch(i);
break;
case 'd': case 'i':
i = va_arg(argp, int);
if(i < 0)
{
i = -i;
putch('-');
}
puts(convert(i, 10));
break;
case 'l':
i64 = va_arg(argp, uint64_t);
puts(convert_u(i64, 10));
break;
case 'o':
i = va_arg(argp, int);
puts(convert(i, 8));
break;
case 's':
s = va_arg(argp, char*);
puts(s);
break;
case 'u':
u = va_arg(argp, unsigned int);
puts(convert_u((uint64_t) u, 10));
break;
case 'x':
i64 = va_arg(argp, uint64_t);
puts(convert_u(i64, 16));
break;
case 'b':
u = va_arg(argp, unsigned int);
puts(convert(u, 2));
break;
case '%':
putch('%');
break;
}
}
va_end(argp);
}