]> Zhao Yanbai Git Server - kernel.git/commitdiff
add %u for vsprintf
authorAceVest <zhaoyanbai@126.com>
Sun, 6 Jul 2014 03:55:14 +0000 (11:55 +0800)
committerAceVest <zhaoyanbai@126.com>
Sun, 6 Jul 2014 03:55:14 +0000 (11:55 +0800)
kernel/pci.c
lib/vsprintf.c

index 87052f77b039542ba01a79fb4f1148af31b6030d..a42a6a92e972f3f2a3eb76849b17436d7b17efef 100644 (file)
@@ -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)
             {
index 35fb9949427ec51487cee602440e6bc57838ceb3..8e268e6189883283c4051b807204369c9a537cbe 100644 (file)
@@ -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;
 }