From: AceVest Date: Sun, 6 Jul 2014 03:55:14 +0000 (+0800) Subject: add %u for vsprintf X-Git-Tag: 0.3.0~34 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=a55ae67263a51804604b750090cccd61fc642f40;p=kernel.git add %u for vsprintf --- diff --git a/kernel/pci.c b/kernel/pci.c index 87052f7..a42a6a9 100644 --- a/kernel/pci.c +++ b/kernel/pci.c @@ -73,22 +73,9 @@ void scan_pci_bus(int bus) { cmd = PCI_CMD(bus, dev, devfn, PCI_VENDORID); v = pci_read_config_word(cmd); - //v = pci_read_config_long(cmd); if(v == 0xFFFF) continue; -#if 0 - printk("dev %d ", dev); - unsigned int i; - for(i=0; i<16; ++i) - { - cmd = PCI_CMD(bus, dev, devfn, i*4); - printk("%08x ", pci_read_config_long(cmd)); - } - - printk("\n"); -#endif - pci_device_t *pci = kmalloc(sizeof(pci_device_t), 0); if(0 == pci) { diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 35fb994..8e268e6 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -5,8 +5,9 @@ // ======================================================================== #include "string.h" -char *itoa(char *s, int n); -char *itox(char *s, unsigned int n); +char *itoa(char *s, int n); +char *itou(char *s, unsigned int n); +char *itox(char *s, unsigned int n); enum { ALIGN_RIGHT, @@ -87,14 +88,18 @@ int vsprintf(char *buf, const char *fmt, char *args) break; case 'd': itoa(tmp, *((int*)args)); - p += write_buf(p, tmp, char_fill, char_cnt, align); + p += write_buf(p, tmp, char_fill, char_cnt, align); break; case 's': - p += write_buf(p, (const char *)*((unsigned int *) args), char_fill, char_cnt, align); + p += write_buf(p, (const char *)*((unsigned int *) args), char_fill, char_cnt, align); + break; + case 'u': + itou(tmp, *((unsigned int*)args)); + p += write_buf(p, tmp, char_fill, char_cnt, align); break; case 'x': itox(tmp, *((unsigned int *) args)); - p += write_buf(p, tmp, char_fill, char_cnt, align); + p += write_buf(p, tmp, char_fill, char_cnt, align); break; default: break; @@ -105,42 +110,91 @@ int vsprintf(char *buf, const char *fmt, char *args) *p = 0; } +void swap_char(char *a, char *b) +{ + char c; + c = *a; + *a = *b; + *b = c; +} + char *itoa(char *s, int n) { - int i; - char tmp[64]; + int i = 0; + char *p = 0; + if( n & 0x80000000 ) { - n = ~n + 1; - *s++ = '-'; + n = ~n + 1; + *s++ = '-'; } - i=0; + + p = s; + do { - tmp[i++] = (n % 10) + '0'; - n /= 10; + *p++ = (n % 10) + '0'; + n /= 10; }while(n); - while(i) *s++=tmp[--i]; - *s = 0; + + *p-- = 0; + + while(s < p) + { + swap_char(s, p); + s++; + p--; + } +} + + +char *itou(char *s, unsigned int n) +{ + char c; + char *p = s; + + do + { + *p++ = (n % 10) + '0'; + n /= 10; + }while(n); + + *p-- = 0; + + while(s < p) + { + swap_char(s, p); + s++; + p--; + } } char *itox(char *s, unsigned int n) { char *p = s; - char ch,i,flag = 0; - - //*p++ = '0'; *p++ = 'x'; + char ch; + int i; if(n==0){*p++='0';*p=0;return s;} + for(i=28; i>=0; i-=4) { - ch = (n>>i) & 0x0F; - if(ch >= 0) ch += '0'; - if(ch > '9') ch += 7; - if(ch != '0') flag = 1; - if(ch != '0' || flag == 1) - *p++ = ch; + ch = (n>>i) & 0x0F; + + if(ch>=0 && ch<=9) + { + ch += '0'; + } + else + { + ch -= 10; + ch += 'A'; + } + + *p++ = ch; } + *p = 0; + return s; }