printf.c (1252B)
1 #include "extern.h" 2 3 static char * 4 itoa(int n, char *buf, int base) 5 { 6 (void)n; 7 (void)base; 8 9 return buf; 10 } 11 12 int 13 vsprintf(char *buf, const char *fmt, va_list args) 14 { 15 char *str, *s; 16 int base, i, n; 17 18 for (str = buf; *fmt != '\0'; fmt++) { 19 base = 10; 20 if (*fmt != '%') { 21 *str++ = *fmt; 22 continue; 23 } 24 if (*fmt == '%') { 25 switch (*(++fmt)) { 26 case 'c': 27 *str++ = (unsigned char)va_arg(args, int); 28 continue; 29 case 's': 30 s = va_arg(args, char *); 31 n = strlen(s); 32 for (i = 0; i < n; i++) 33 *str++ = *s++; 34 continue; 35 case 'p': 36 continue; 37 case 'd': /* FALLTHROUGH */ 38 case 'i': 39 n = va_arg(args, int); 40 break; 41 case 'u': 42 n = va_arg(args, unsigned int); 43 break; 44 case 'o': 45 base = 8; 46 break; 47 case 'X': 48 base = 16; 49 break; 50 case '%': 51 *str++ = '%'; 52 continue; 53 } 54 str = itoa(n, str, base); 55 } 56 } 57 *str = '\0'; 58 59 return str - buf; 60 } 61 62 int 63 sprintf(char *buf, const char *fmt, ...) 64 { 65 va_list args; 66 int n; 67 68 va_start(args, fmt); 69 n = vsprintf(buf, fmt, args); 70 va_end(args); 71 72 return n; 73 } 74 75 int 76 printf(const char *fmt, ...) 77 { 78 char buf[BUFSIZ]; 79 va_list args; 80 int n; 81 82 va_start(args, fmt); 83 n = vsprintf(buf, fmt, args); 84 va_end(args); 85 tty_write(buf); 86 87 return n; 88 }