static void _print_int( char **str, int *len, int num ) { char nstr[12]; int i=10; if( num == 0 ) { _print_char(str, len, '0'); } if( num < 0 ) { _print_char(str, len, '-'); _print_int(str, len, -1 * num ); return; } nstr[11]='\0'; while( (i>0) && num ) { nstr[i] = '0' + (num % 10); num /= 10; i--; } _print_str(str, len, nstr + i+1); }
static void _print_str( char **str, int *len, char * pstr ) { while( *pstr ) { _print_char( str, len, *pstr ); pstr++; } }
void print_char( char c ) { int i = -1; _print_char( NULL, &i, c ); }
static int __kvprintf(print_char_func print_func, void *data, const char *fmt, size_t fmtlen, va_list args) { static const int FALSE = 0; static const int TRUE = 1; char c; int count = 0; while((c = next_char(&fmt, &fmtlen)) != 0) { if(c != '%') { _print_char(print_func, data, &count, c); } else { c = next_char(&fmt, &fmtlen); if (c == 0) { // Error, incomplete format string return -1; } else if(c != '%') { int flags = 0; int width = 0; int precision = 0; int len = 0, num_len; uintptr_t arg; fmt--; fmtlen++; again: c = next_char(&fmt, &fmtlen); if (c == 0) { return -1; } switch(c) { case '-': flags |= PF_MINUS; goto again; case '*': /* dynamic field width. */ width = va_arg(args, int); goto again; case '+': flags |= PF_PLUS; goto again; case ' ': flags |= PF_SPC; goto again; case '#': flags |= PF_HASH; goto again; case '0': flags |= PF_ZERO; goto again; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': while(IS_DIGIT(c)) { width = width * 10 + (c - '0'); c = next_char(&fmt, &fmtlen); if (c == 0) { return -1; } } fmt--; fmtlen++; goto again; case 'h': flags |= PF_SHORT; goto again; case 'l': case 'z': flags |= PF_LONG; goto again; case '.': c = next_char(&fmt, &fmtlen); if (c == 0) { return -1; } if(c == '*') { precision = va_arg(args, int); } else { while(IS_DIGIT(c)) { precision = precision * 10 + (c - '0'); c = next_char(&fmt, &fmtlen); if (c == 0) { return -1; } } fmt--; fmtlen++; } goto again; case 'p': flags |= PF_LONG; break; } if(flags & PF_LONG) { arg = va_arg(args, unsigned long long); } else if(flags & PF_SHORT) {